rest.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. Copyright 2014 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 rest
  14. import (
  15. "io"
  16. "net/http"
  17. "net/url"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. "k8s.io/kubernetes/pkg/runtime"
  21. "k8s.io/kubernetes/pkg/watch"
  22. )
  23. //TODO:
  24. // Storage interfaces need to be separated into two groups; those that operate
  25. // on collections and those that operate on individually named items.
  26. // Collection interfaces:
  27. // (Method: Current -> Proposed)
  28. // GET: Lister -> CollectionGetter
  29. // WATCH: Watcher -> CollectionWatcher
  30. // CREATE: Creater -> CollectionCreater
  31. // DELETE: (n/a) -> CollectionDeleter
  32. // UPDATE: (n/a) -> CollectionUpdater
  33. //
  34. // Single item interfaces:
  35. // (Method: Current -> Proposed)
  36. // GET: Getter -> NamedGetter
  37. // WATCH: (n/a) -> NamedWatcher
  38. // CREATE: (n/a) -> NamedCreater
  39. // DELETE: Deleter -> NamedDeleter
  40. // UPDATE: Update -> NamedUpdater
  41. // Storage is a generic interface for RESTful storage services.
  42. // Resources which are exported to the RESTful API of apiserver need to implement this interface. It is expected
  43. // that objects may implement any of the below interfaces.
  44. type Storage interface {
  45. // New returns an empty object that can be used with Create and Update after request data has been put into it.
  46. // This object must be a pointer type for use with Codec.DecodeInto([]byte, runtime.Object)
  47. New() runtime.Object
  48. }
  49. // KindProvider specifies a different kind for its API than for its internal storage. This is necessary for external
  50. // objects that are not compiled into the api server. For such objects, there is no in-memory representation for
  51. // the object, so they must be represented as generic objects (e.g. runtime.Unknown), but when we present the object as part of
  52. // API discovery we want to present the specific kind, not the generic internal representation.
  53. type KindProvider interface {
  54. Kind() string
  55. }
  56. // Lister is an object that can retrieve resources that match the provided field and label criteria.
  57. type Lister interface {
  58. // NewList returns an empty object that can be used with the List call.
  59. // This object must be a pointer type for use with Codec.DecodeInto([]byte, runtime.Object)
  60. NewList() runtime.Object
  61. // List selects resources in the storage which match to the selector. 'options' can be nil.
  62. List(ctx api.Context, options *api.ListOptions) (runtime.Object, error)
  63. }
  64. // Exporter is an object that knows how to strip a RESTful resource for export
  65. type Exporter interface {
  66. // Export an object. Fields that are not user specified (e.g. Status, ObjectMeta.ResourceVersion) are stripped out
  67. // Returns the stripped object. If 'exact' is true, fields that are specific to the cluster (e.g. namespace) are
  68. // retained, otherwise they are stripped also.
  69. Export(ctx api.Context, name string, opts unversioned.ExportOptions) (runtime.Object, error)
  70. }
  71. // Getter is an object that can retrieve a named RESTful resource.
  72. type Getter interface {
  73. // Get finds a resource in the storage by name and returns it.
  74. // Although it can return an arbitrary error value, IsNotFound(err) is true for the
  75. // returned error value err when the specified resource is not found.
  76. Get(ctx api.Context, name string) (runtime.Object, error)
  77. }
  78. // GetterWithOptions is an object that retrieve a named RESTful resource and takes
  79. // additional options on the get request. It allows a caller to also receive the
  80. // subpath of the GET request.
  81. type GetterWithOptions interface {
  82. // Get finds a resource in the storage by name and returns it.
  83. // Although it can return an arbitrary error value, IsNotFound(err) is true for the
  84. // returned error value err when the specified resource is not found.
  85. // The options object passed to it is of the same type returned by the NewGetOptions
  86. // method.
  87. Get(ctx api.Context, name string, options runtime.Object) (runtime.Object, error)
  88. // NewGetOptions returns an empty options object that will be used to pass
  89. // options to the Get method. It may return a bool and a string, if true, the
  90. // value of the request path below the object will be included as the named
  91. // string in the serialization of the runtime object. E.g., returning "path"
  92. // will convert the trailing request scheme value to "path" in the map[string][]string
  93. // passed to the converter.
  94. NewGetOptions() (runtime.Object, bool, string)
  95. }
  96. // Deleter is an object that can delete a named RESTful resource.
  97. type Deleter interface {
  98. // Delete finds a resource in the storage and deletes it.
  99. // Although it can return an arbitrary error value, IsNotFound(err) is true for the
  100. // returned error value err when the specified resource is not found.
  101. // Delete *may* return the object that was deleted, or a status object indicating additional
  102. // information about deletion.
  103. Delete(ctx api.Context, name string) (runtime.Object, error)
  104. }
  105. // GracefulDeleter knows how to pass deletion options to allow delayed deletion of a
  106. // RESTful object.
  107. type GracefulDeleter interface {
  108. // Delete finds a resource in the storage and deletes it.
  109. // If options are provided, the resource will attempt to honor them or return an invalid
  110. // request error.
  111. // Although it can return an arbitrary error value, IsNotFound(err) is true for the
  112. // returned error value err when the specified resource is not found.
  113. // Delete *may* return the object that was deleted, or a status object indicating additional
  114. // information about deletion.
  115. Delete(ctx api.Context, name string, options *api.DeleteOptions) (runtime.Object, error)
  116. }
  117. // GracefulDeleteAdapter adapts the Deleter interface to GracefulDeleter
  118. type GracefulDeleteAdapter struct {
  119. Deleter
  120. }
  121. // Delete implements RESTGracefulDeleter in terms of Deleter
  122. func (w GracefulDeleteAdapter) Delete(ctx api.Context, name string, options *api.DeleteOptions) (runtime.Object, error) {
  123. return w.Deleter.Delete(ctx, name)
  124. }
  125. // CollectionDeleter is an object that can delete a collection
  126. // of RESTful resources.
  127. type CollectionDeleter interface {
  128. // DeleteCollection selects all resources in the storage matching given 'listOptions'
  129. // and deletes them. If 'options' are provided, the resource will attempt to honor
  130. // them or return an invalid request error.
  131. // DeleteCollection may not be atomic - i.e. it may delete some objects and still
  132. // return an error after it. On success, returns a list of deleted objects.
  133. DeleteCollection(ctx api.Context, options *api.DeleteOptions, listOptions *api.ListOptions) (runtime.Object, error)
  134. }
  135. // Creater is an object that can create an instance of a RESTful object.
  136. type Creater interface {
  137. // New returns an empty object that can be used with Create after request data has been put into it.
  138. // This object must be a pointer type for use with Codec.DecodeInto([]byte, runtime.Object)
  139. New() runtime.Object
  140. // Create creates a new version of a resource.
  141. Create(ctx api.Context, obj runtime.Object) (runtime.Object, error)
  142. }
  143. // NamedCreater is an object that can create an instance of a RESTful object using a name parameter.
  144. type NamedCreater interface {
  145. // New returns an empty object that can be used with Create after request data has been put into it.
  146. // This object must be a pointer type for use with Codec.DecodeInto([]byte, runtime.Object)
  147. New() runtime.Object
  148. // Create creates a new version of a resource. It expects a name parameter from the path.
  149. // This is needed for create operations on subresources which include the name of the parent
  150. // resource in the path.
  151. Create(ctx api.Context, name string, obj runtime.Object) (runtime.Object, error)
  152. }
  153. // UpdatedObjectInfo provides information about an updated object to an Updater.
  154. // It requires access to the old object in order to return the newly updated object.
  155. type UpdatedObjectInfo interface {
  156. // Returns preconditions built from the updated object, if applicable.
  157. // May return nil, or a preconditions object containing nil fields,
  158. // if no preconditions can be determined from the updated object.
  159. Preconditions() *api.Preconditions
  160. // UpdatedObject returns the updated object, given a context and old object.
  161. // The only time an empty oldObj should be passed in is if a "create on update" is occurring (there is no oldObj).
  162. UpdatedObject(ctx api.Context, oldObj runtime.Object) (newObj runtime.Object, err error)
  163. }
  164. // Updater is an object that can update an instance of a RESTful object.
  165. type Updater interface {
  166. // New returns an empty object that can be used with Update after request data has been put into it.
  167. // This object must be a pointer type for use with Codec.DecodeInto([]byte, runtime.Object)
  168. New() runtime.Object
  169. // Update finds a resource in the storage and updates it. Some implementations
  170. // may allow updates creates the object - they should set the created boolean
  171. // to true.
  172. Update(ctx api.Context, name string, objInfo UpdatedObjectInfo) (runtime.Object, bool, error)
  173. }
  174. // CreaterUpdater is a storage object that must support both create and update.
  175. // Go prevents embedded interfaces that implement the same method.
  176. type CreaterUpdater interface {
  177. Creater
  178. Update(ctx api.Context, name string, objInfo UpdatedObjectInfo) (runtime.Object, bool, error)
  179. }
  180. // CreaterUpdater must satisfy the Updater interface.
  181. var _ Updater = CreaterUpdater(nil)
  182. // Patcher is a storage object that supports both get and update.
  183. type Patcher interface {
  184. Getter
  185. Updater
  186. }
  187. // Watcher should be implemented by all Storage objects that
  188. // want to offer the ability to watch for changes through the watch api.
  189. type Watcher interface {
  190. // 'label' selects on labels; 'field' selects on the object's fields. Not all fields
  191. // are supported; an error should be returned if 'field' tries to select on a field that
  192. // isn't supported. 'resourceVersion' allows for continuing/starting a watch at a
  193. // particular version.
  194. Watch(ctx api.Context, options *api.ListOptions) (watch.Interface, error)
  195. }
  196. // StandardStorage is an interface covering the common verbs. Provided for testing whether a
  197. // resource satisfies the normal storage methods. Use Storage when passing opaque storage objects.
  198. type StandardStorage interface {
  199. Getter
  200. Lister
  201. CreaterUpdater
  202. GracefulDeleter
  203. CollectionDeleter
  204. Watcher
  205. }
  206. // Redirector know how to return a remote resource's location.
  207. type Redirector interface {
  208. // ResourceLocation should return the remote location of the given resource, and an optional transport to use to request it, or an error.
  209. ResourceLocation(ctx api.Context, id string) (remoteLocation *url.URL, transport http.RoundTripper, err error)
  210. }
  211. // Responder abstracts the normal response behavior for a REST method and is passed to callers that
  212. // may wish to handle the response directly in some cases, but delegate to the normal error or object
  213. // behavior in other cases.
  214. type Responder interface {
  215. // Object writes the provided object to the response. Invoking this method multiple times is undefined.
  216. Object(statusCode int, obj runtime.Object)
  217. // Error writes the provided error to the response. This method may only be invoked once.
  218. Error(err error)
  219. }
  220. // Connecter is a storage object that responds to a connection request.
  221. type Connecter interface {
  222. // Connect returns an http.Handler that will handle the request/response for a given API invocation.
  223. // The provided responder may be used for common API responses. The responder will write both status
  224. // code and body, so the ServeHTTP method should exit after invoking the responder. The Handler will
  225. // be used for a single API request and then discarded. The Responder is guaranteed to write to the
  226. // same http.ResponseWriter passed to ServeHTTP.
  227. Connect(ctx api.Context, id string, options runtime.Object, r Responder) (http.Handler, error)
  228. // NewConnectOptions returns an empty options object that will be used to pass
  229. // options to the Connect method. If nil, then a nil options object is passed to
  230. // Connect. It may return a bool and a string. If true, the value of the request
  231. // path below the object will be included as the named string in the serialization
  232. // of the runtime object.
  233. NewConnectOptions() (runtime.Object, bool, string)
  234. // ConnectMethods returns the list of HTTP methods handled by Connect
  235. ConnectMethods() []string
  236. }
  237. // ResourceStreamer is an interface implemented by objects that prefer to be streamed from the server
  238. // instead of decoded directly.
  239. type ResourceStreamer interface {
  240. // InputStream should return an io.ReadCloser if the provided object supports streaming. The desired
  241. // api version and an accept header (may be empty) are passed to the call. If no error occurs,
  242. // the caller may return a flag indicating whether the result should be flushed as writes occur
  243. // and a content type string that indicates the type of the stream.
  244. // If a null stream is returned, a StatusNoContent response wil be generated.
  245. InputStream(apiVersion, acceptHeader string) (stream io.ReadCloser, flush bool, mimeType string, err error)
  246. }
  247. // StorageMetadata is an optional interface that callers can implement to provide additional
  248. // information about their Storage objects.
  249. type StorageMetadata interface {
  250. // ProducesMIMETypes returns a list of the MIME types the specified HTTP verb (GET, POST, DELETE,
  251. // PATCH) can respond with.
  252. ProducesMIMETypes(verb string) []string
  253. }
  254. // ConnectRequest is an object passed to admission control for Connect operations
  255. type ConnectRequest struct {
  256. // Name is the name of the object on which the connect request was made
  257. Name string
  258. // Options is the options object passed to the connect request. See the NewConnectOptions method on Connecter
  259. Options runtime.Object
  260. // ResourcePath is the path for the resource in the REST server (ie. "pods/proxy")
  261. ResourcePath string
  262. }
  263. func (obj *ConnectRequest) GetObjectKind() unversioned.ObjectKind { return unversioned.EmptyObjectKind }