interfaces.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 runtime
  14. import (
  15. "io"
  16. "net/url"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. )
  19. const (
  20. // APIVersionInternal may be used if you are registering a type that should not
  21. // be considered stable or serialized - it is a convention only and has no
  22. // special behavior in this package.
  23. APIVersionInternal = "__internal"
  24. )
  25. // GroupVersioner refines a set of possible conversion targets into a single option.
  26. type GroupVersioner interface {
  27. // KindForGroupVersionKinds returns a desired target group version kind for the given input, or returns ok false if no
  28. // target is known. In general, if the return target is not in the input list, the caller is expected to invoke
  29. // Scheme.New(target) and then perform a conversion between the current Go type and the destination Go type.
  30. // Sophisticated implementations may use additional information about the input kinds to pick a destination kind.
  31. KindForGroupVersionKinds(kinds []schema.GroupVersionKind) (target schema.GroupVersionKind, ok bool)
  32. }
  33. // Encoders write objects to a serialized form
  34. type Encoder interface {
  35. // Encode writes an object to a stream. Implementations may return errors if the versions are
  36. // incompatible, or if no conversion is defined.
  37. Encode(obj Object, w io.Writer) error
  38. }
  39. // Decoders attempt to load an object from data.
  40. type Decoder interface {
  41. // Decode attempts to deserialize the provided data using either the innate typing of the scheme or the
  42. // default kind, group, and version provided. It returns a decoded object as well as the kind, group, and
  43. // version from the serialized data, or an error. If into is non-nil, it will be used as the target type
  44. // and implementations may choose to use it rather than reallocating an object. However, the object is not
  45. // guaranteed to be populated. The returned object is not guaranteed to match into. If defaults are
  46. // provided, they are applied to the data by default. If no defaults or partial defaults are provided, the
  47. // type of the into may be used to guide conversion decisions.
  48. Decode(data []byte, defaults *schema.GroupVersionKind, into Object) (Object, *schema.GroupVersionKind, error)
  49. }
  50. // Serializer is the core interface for transforming objects into a serialized format and back.
  51. // Implementations may choose to perform conversion of the object, but no assumptions should be made.
  52. type Serializer interface {
  53. Encoder
  54. Decoder
  55. }
  56. // Codec is a Serializer that deals with the details of versioning objects. It offers the same
  57. // interface as Serializer, so this is a marker to consumers that care about the version of the objects
  58. // they receive.
  59. type Codec Serializer
  60. // ParameterCodec defines methods for serializing and deserializing API objects to url.Values and
  61. // performing any necessary conversion. Unlike the normal Codec, query parameters are not self describing
  62. // and the desired version must be specified.
  63. type ParameterCodec interface {
  64. // DecodeParameters takes the given url.Values in the specified group version and decodes them
  65. // into the provided object, or returns an error.
  66. DecodeParameters(parameters url.Values, from schema.GroupVersion, into Object) error
  67. // EncodeParameters encodes the provided object as query parameters or returns an error.
  68. EncodeParameters(obj Object, to schema.GroupVersion) (url.Values, error)
  69. }
  70. // Framer is a factory for creating readers and writers that obey a particular framing pattern.
  71. type Framer interface {
  72. NewFrameReader(r io.ReadCloser) io.ReadCloser
  73. NewFrameWriter(w io.Writer) io.Writer
  74. }
  75. // SerializerInfo contains information about a specific serialization format
  76. type SerializerInfo struct {
  77. // MediaType is the value that represents this serializer over the wire.
  78. MediaType string
  79. // EncodesAsText indicates this serializer can be encoded to UTF-8 safely.
  80. EncodesAsText bool
  81. // Serializer is the individual object serializer for this media type.
  82. Serializer Serializer
  83. // PrettySerializer, if set, can serialize this object in a form biased towards
  84. // readability.
  85. PrettySerializer Serializer
  86. // StreamSerializer, if set, describes the streaming serialization format
  87. // for this media type.
  88. StreamSerializer *StreamSerializerInfo
  89. }
  90. // StreamSerializerInfo contains information about a specific stream serialization format
  91. type StreamSerializerInfo struct {
  92. // EncodesAsText indicates this serializer can be encoded to UTF-8 safely.
  93. EncodesAsText bool
  94. // Serializer is the top level object serializer for this type when streaming
  95. Serializer
  96. // Framer is the factory for retrieving streams that separate objects on the wire
  97. Framer
  98. }
  99. // NegotiatedSerializer is an interface used for obtaining encoders, decoders, and serializers
  100. // for multiple supported media types. This would commonly be accepted by a server component
  101. // that performs HTTP content negotiation to accept multiple formats.
  102. type NegotiatedSerializer interface {
  103. // SupportedMediaTypes is the media types supported for reading and writing single objects.
  104. SupportedMediaTypes() []SerializerInfo
  105. // EncoderForVersion returns an encoder that ensures objects being written to the provided
  106. // serializer are in the provided group version.
  107. EncoderForVersion(serializer Encoder, gv GroupVersioner) Encoder
  108. // DecoderForVersion returns a decoder that ensures objects being read by the provided
  109. // serializer are in the provided group version by default.
  110. DecoderToVersion(serializer Decoder, gv GroupVersioner) Decoder
  111. }
  112. // StorageSerializer is an interface used for obtaining encoders, decoders, and serializers
  113. // that can read and write data at rest. This would commonly be used by client tools that must
  114. // read files, or server side storage interfaces that persist restful objects.
  115. type StorageSerializer interface {
  116. // SupportedMediaTypes are the media types supported for reading and writing objects.
  117. SupportedMediaTypes() []SerializerInfo
  118. // UniversalDeserializer returns a Serializer that can read objects in multiple supported formats
  119. // by introspecting the data at rest.
  120. UniversalDeserializer() Decoder
  121. // EncoderForVersion returns an encoder that ensures objects being written to the provided
  122. // serializer are in the provided group version.
  123. EncoderForVersion(serializer Encoder, gv GroupVersioner) Encoder
  124. // DecoderForVersion returns a decoder that ensures objects being read by the provided
  125. // serializer are in the provided group version by default.
  126. DecoderToVersion(serializer Decoder, gv GroupVersioner) Decoder
  127. }
  128. // NestedObjectEncoder is an optional interface that objects may implement to be given
  129. // an opportunity to encode any nested Objects / RawExtensions during serialization.
  130. type NestedObjectEncoder interface {
  131. EncodeNestedObjects(e Encoder) error
  132. }
  133. // NestedObjectDecoder is an optional interface that objects may implement to be given
  134. // an opportunity to decode any nested Objects / RawExtensions during serialization.
  135. type NestedObjectDecoder interface {
  136. DecodeNestedObjects(d Decoder) error
  137. }
  138. ///////////////////////////////////////////////////////////////////////////////
  139. // Non-codec interfaces
  140. type ObjectDefaulter interface {
  141. // Default takes an object (must be a pointer) and applies any default values.
  142. // Defaulters may not error.
  143. Default(in Object)
  144. }
  145. type ObjectVersioner interface {
  146. ConvertToVersion(in Object, gv GroupVersioner) (out Object, err error)
  147. }
  148. // ObjectConvertor converts an object to a different version.
  149. type ObjectConvertor interface {
  150. // Convert attempts to convert one object into another, or returns an error. This
  151. // method does not mutate the in object, but the in and out object might share data structures,
  152. // i.e. the out object cannot be mutated without mutating the in object as well.
  153. // The context argument will be passed to all nested conversions.
  154. Convert(in, out, context interface{}) error
  155. // ConvertToVersion takes the provided object and converts it the provided version. This
  156. // method does not mutate the in object, but the in and out object might share data structures,
  157. // i.e. the out object cannot be mutated without mutating the in object as well.
  158. // This method is similar to Convert() but handles specific details of choosing the correct
  159. // output version.
  160. ConvertToVersion(in Object, gv GroupVersioner) (out Object, err error)
  161. ConvertFieldLabel(version, kind, label, value string) (string, string, error)
  162. }
  163. // ObjectTyper contains methods for extracting the APIVersion and Kind
  164. // of objects.
  165. type ObjectTyper interface {
  166. // ObjectKinds returns the all possible group,version,kind of the provided object, true if
  167. // the object is unversioned, or an error if the object is not recognized
  168. // (IsNotRegisteredError will return true).
  169. ObjectKinds(Object) ([]schema.GroupVersionKind, bool, error)
  170. // Recognizes returns true if the scheme is able to handle the provided version and kind,
  171. // or more precisely that the provided version is a possible conversion or decoding
  172. // target.
  173. Recognizes(gvk schema.GroupVersionKind) bool
  174. }
  175. // ObjectCreater contains methods for instantiating an object by kind and version.
  176. type ObjectCreater interface {
  177. New(kind schema.GroupVersionKind) (out Object, err error)
  178. }
  179. // ResourceVersioner provides methods for setting and retrieving
  180. // the resource version from an API object.
  181. type ResourceVersioner interface {
  182. SetResourceVersion(obj Object, version string) error
  183. ResourceVersion(obj Object) (string, error)
  184. }
  185. // SelfLinker provides methods for setting and retrieving the SelfLink field of an API object.
  186. type SelfLinker interface {
  187. SetSelfLink(obj Object, selfLink string) error
  188. SelfLink(obj Object) (string, error)
  189. // Knowing Name is sometimes necessary to use a SelfLinker.
  190. Name(obj Object) (string, error)
  191. // Knowing Namespace is sometimes necessary to use a SelfLinker
  192. Namespace(obj Object) (string, error)
  193. }
  194. // All API types registered with Scheme must support the Object interface. Since objects in a scheme are
  195. // expected to be serialized to the wire, the interface an Object must provide to the Scheme allows
  196. // serializers to set the kind, version, and group the object is represented as. An Object may choose
  197. // to return a no-op ObjectKindAccessor in cases where it is not expected to be serialized.
  198. type Object interface {
  199. GetObjectKind() schema.ObjectKind
  200. DeepCopyObject() Object
  201. }
  202. // Unstructured objects store values as map[string]interface{}, with only values that can be serialized
  203. // to JSON allowed.
  204. type Unstructured interface {
  205. Object
  206. // UnstructuredContent returns a non-nil map with this object's contents. Values may be
  207. // []interface{}, map[string]interface{}, or any primitive type. Contents are typically serialized to
  208. // and from JSON. SetUnstructuredContent should be used to mutate the contents.
  209. UnstructuredContent() map[string]interface{}
  210. // SetUnstructuredContent updates the object content to match the provided map.
  211. SetUnstructuredContent(map[string]interface{})
  212. // IsList returns true if this type is a list or matches the list convention - has an array called "items".
  213. IsList() bool
  214. // EachListItem should pass a single item out of the list as an Object to the provided function. Any
  215. // error should terminate the iteration. If IsList() returns false, this method should return an error
  216. // instead of calling the provided function.
  217. EachListItem(func(Object) error) error
  218. }