interfaces.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package storage
  14. import (
  15. "golang.org/x/net/context"
  16. "k8s.io/kubernetes/pkg/runtime"
  17. "k8s.io/kubernetes/pkg/types"
  18. "k8s.io/kubernetes/pkg/watch"
  19. )
  20. // Versioner abstracts setting and retrieving metadata fields from database response
  21. // onto the object ot list.
  22. type Versioner interface {
  23. // UpdateObject sets storage metadata into an API object. Returns an error if the object
  24. // cannot be updated correctly. May return nil if the requested object does not need metadata
  25. // from database.
  26. UpdateObject(obj runtime.Object, resourceVersion uint64) error
  27. // UpdateList sets the resource version into an API list object. Returns an error if the object
  28. // cannot be updated correctly. May return nil if the requested object does not need metadata
  29. // from database.
  30. UpdateList(obj runtime.Object, resourceVersion uint64) error
  31. // ObjectResourceVersion returns the resource version (for persistence) of the specified object.
  32. // Should return an error if the specified object does not have a persistable version.
  33. ObjectResourceVersion(obj runtime.Object) (uint64, error)
  34. }
  35. // ResponseMeta contains information about the database metadata that is associated with
  36. // an object. It abstracts the actual underlying objects to prevent coupling with concrete
  37. // database and to improve testability.
  38. type ResponseMeta struct {
  39. // TTL is the time to live of the node that contained the returned object. It may be
  40. // zero or negative in some cases (objects may be expired after the requested
  41. // expiration time due to server lag).
  42. TTL int64
  43. // The resource version of the node that contained the returned object.
  44. ResourceVersion uint64
  45. }
  46. // MatchValue defines a pair (<index name>, <value for that index>).
  47. type MatchValue struct {
  48. IndexName string
  49. Value string
  50. }
  51. // TriggerPublisherFunc is a function that takes an object, and returns a list of pairs
  52. // (<index name>, <index value for the given object>) for all indexes known
  53. // to that function.
  54. type TriggerPublisherFunc func(obj runtime.Object) []MatchValue
  55. // Filter is interface that is used to pass filtering mechanism.
  56. type Filter interface {
  57. // Filter is a predicate which takes an API object and returns true
  58. // if and only if the object should remain in the set.
  59. Filter(obj runtime.Object) bool
  60. // For any triggers known to the Filter, if Filter() can return only
  61. // (a subset of) objects for which indexing function returns <value>,
  62. // (<index name>, <value> pair would be returned.
  63. //
  64. // This is optimization to avoid computing Filter() function (which are
  65. // usually relatively expensive) in case we are sure they will return
  66. // false anyway.
  67. Trigger() []MatchValue
  68. }
  69. // Everything is a Filter which accepts all objects.
  70. var Everything Filter = everything{}
  71. // everything is implementation of Everything.
  72. type everything struct {
  73. }
  74. func (e everything) Filter(runtime.Object) bool {
  75. return true
  76. }
  77. func (e everything) Trigger() []MatchValue {
  78. return nil
  79. }
  80. // Pass an UpdateFunc to Interface.GuaranteedUpdate to make an update
  81. // that is guaranteed to succeed.
  82. // See the comment for GuaranteedUpdate for more details.
  83. type UpdateFunc func(input runtime.Object, res ResponseMeta) (output runtime.Object, ttl *uint64, err error)
  84. // Preconditions must be fulfilled before an operation (update, delete, etc.) is carried out.
  85. type Preconditions struct {
  86. // Specifies the target UID.
  87. UID *types.UID `json:"uid,omitempty"`
  88. }
  89. // NewUIDPreconditions returns a Preconditions with UID set.
  90. func NewUIDPreconditions(uid string) *Preconditions {
  91. u := types.UID(uid)
  92. return &Preconditions{UID: &u}
  93. }
  94. // Interface offers a common interface for object marshaling/unmarshaling operations and
  95. // hides all the storage-related operations behind it.
  96. type Interface interface {
  97. // Returns Versioner associated with this interface.
  98. Versioner() Versioner
  99. // Create adds a new object at a key unless it already exists. 'ttl' is time-to-live
  100. // in seconds (0 means forever). If no error is returned and out is not nil, out will be
  101. // set to the read value from database.
  102. Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error
  103. // Delete removes the specified key and returns the value that existed at that spot.
  104. // If key didn't exist, it will return NotFound storage error.
  105. Delete(ctx context.Context, key string, out runtime.Object, preconditions *Preconditions) error
  106. // Watch begins watching the specified key. Events are decoded into API objects,
  107. // and any items passing 'filter' are sent down to returned watch.Interface.
  108. // resourceVersion may be used to specify what version to begin watching,
  109. // which should be the current resourceVersion, and no longer rv+1
  110. // (e.g. reconnecting without missing any updates).
  111. Watch(ctx context.Context, key string, resourceVersion string, filter Filter) (watch.Interface, error)
  112. // WatchList begins watching the specified key's items. Items are decoded into API
  113. // objects and any item passing 'filter' are sent down to returned watch.Interface.
  114. // resourceVersion may be used to specify what version to begin watching,
  115. // which should be the current resourceVersion, and no longer rv+1
  116. // (e.g. reconnecting without missing any updates).
  117. WatchList(ctx context.Context, key string, resourceVersion string, filter Filter) (watch.Interface, error)
  118. // Get unmarshals json found at key into objPtr. On a not found error, will either
  119. // return a zero object of the requested type, or an error, depending on ignoreNotFound.
  120. // Treats empty responses and nil response nodes exactly like a not found error.
  121. Get(ctx context.Context, key string, objPtr runtime.Object, ignoreNotFound bool) error
  122. // GetToList unmarshals json found at key and opaque it into *List api object
  123. // (an object that satisfies the runtime.IsList definition).
  124. GetToList(ctx context.Context, key string, filter Filter, listObj runtime.Object) error
  125. // List unmarshalls jsons found at directory defined by key and opaque them
  126. // into *List api object (an object that satisfies runtime.IsList definition).
  127. // The returned contents may be delayed, but it is guaranteed that they will
  128. // be have at least 'resourceVersion'.
  129. List(ctx context.Context, key string, resourceVersion string, filter Filter, listObj runtime.Object) error
  130. // GuaranteedUpdate keeps calling 'tryUpdate()' to update key 'key' (of type 'ptrToType')
  131. // retrying the update until success if there is index conflict.
  132. // Note that object passed to tryUpdate may change across invocations of tryUpdate() if
  133. // other writers are simultaneously updating it, so tryUpdate() needs to take into account
  134. // the current contents of the object when deciding how the update object should look.
  135. // If the key doesn't exist, it will return NotFound storage error if ignoreNotFound=false
  136. // or zero value in 'ptrToType' parameter otherwise.
  137. // If the object to update has the same value as previous, it won't do any update
  138. // but will return the object in 'ptrToType' parameter.
  139. //
  140. // Example:
  141. //
  142. // s := /* implementation of Interface */
  143. // err := s.GuaranteedUpdate(
  144. // "myKey", &MyType{}, true,
  145. // func(input runtime.Object, res ResponseMeta) (runtime.Object, *uint64, error) {
  146. // // Before each incovation of the user defined function, "input" is reset to
  147. // // current contents for "myKey" in database.
  148. // curr := input.(*MyType) // Guaranteed to succeed.
  149. //
  150. // // Make the modification
  151. // curr.Counter++
  152. //
  153. // // Return the modified object - return an error to stop iterating. Return
  154. // // a uint64 to alter the TTL on the object, or nil to keep it the same value.
  155. // return cur, nil, nil
  156. // }
  157. // })
  158. GuaranteedUpdate(ctx context.Context, key string, ptrToType runtime.Object, ignoreNotFound bool, precondtions *Preconditions, tryUpdate UpdateFunc) error
  159. }