interfaces.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/kubernetes/pkg/api/unversioned"
  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 []unversioned.GroupVersionKind) (target unversioned.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 *unversioned.GroupVersionKind, into Object) (Object, *unversioned.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 unversioned.GroupVersion, into Object) error
  67. // EncodeParameters encodes the provided object as query parameters or returns an error.
  68. EncodeParameters(obj Object, to unversioned.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. Serializer
  78. // EncodesAsText indicates this serializer can be encoded to UTF-8 safely.
  79. EncodesAsText bool
  80. // MediaType is the value that represents this serializer over the wire.
  81. MediaType string
  82. }
  83. // StreamSerializerInfo contains information about a specific stream serialization format
  84. type StreamSerializerInfo struct {
  85. SerializerInfo
  86. // Framer is the factory for retrieving streams that separate objects on the wire
  87. Framer
  88. // Embedded is the type of the nested serialization that should be used.
  89. Embedded SerializerInfo
  90. }
  91. // NegotiatedSerializer is an interface used for obtaining encoders, decoders, and serializers
  92. // for multiple supported media types. This would commonly be accepted by a server component
  93. // that performs HTTP content negotiation to accept multiple formats.
  94. type NegotiatedSerializer interface {
  95. // SupportedMediaTypes is the media types supported for reading and writing single objects.
  96. SupportedMediaTypes() []string
  97. // SerializerForMediaType returns a serializer for the provided media type. params is the set of
  98. // parameters applied to the media type that may modify the resulting output. ok will be false
  99. // if no serializer matched the media type.
  100. SerializerForMediaType(mediaType string, params map[string]string) (s SerializerInfo, ok bool)
  101. // SupportedStreamingMediaTypes returns the media types of the supported streaming serializers.
  102. // Streaming serializers control how multiple objects are written to a stream output.
  103. SupportedStreamingMediaTypes() []string
  104. // StreamingSerializerForMediaType returns a serializer for the provided media type that supports
  105. // reading and writing multiple objects to a stream. It returns a framer and serializer, or an
  106. // error if no such serializer can be created. Params is the set of parameters applied to the
  107. // media type that may modify the resulting output. ok will be false if no serializer matched
  108. // the media type.
  109. StreamingSerializerForMediaType(mediaType string, params map[string]string) (s StreamSerializerInfo, ok bool)
  110. // EncoderForVersion returns an encoder that ensures objects being written to the provided
  111. // serializer are in the provided group version.
  112. EncoderForVersion(serializer Encoder, gv GroupVersioner) Encoder
  113. // DecoderForVersion returns a decoder that ensures objects being read by the provided
  114. // serializer are in the provided group version by default.
  115. DecoderToVersion(serializer Decoder, gv GroupVersioner) Decoder
  116. }
  117. // StorageSerializer is an interface used for obtaining encoders, decoders, and serializers
  118. // that can read and write data at rest. This would commonly be used by client tools that must
  119. // read files, or server side storage interfaces that persist restful objects.
  120. type StorageSerializer interface {
  121. // SerializerForMediaType returns a serializer for the provided media type. Options is a set of
  122. // parameters applied to the media type that may modify the resulting output.
  123. SerializerForMediaType(mediaType string, options map[string]string) (SerializerInfo, bool)
  124. // UniversalDeserializer returns a Serializer that can read objects in multiple supported formats
  125. // by introspecting the data at rest.
  126. UniversalDeserializer() Decoder
  127. // EncoderForVersion returns an encoder that ensures objects being written to the provided
  128. // serializer are in the provided group version.
  129. EncoderForVersion(serializer Encoder, gv GroupVersioner) Encoder
  130. // DecoderForVersion returns a decoder that ensures objects being read by the provided
  131. // serializer are in the provided group version by default.
  132. DecoderToVersion(serializer Decoder, gv GroupVersioner) Decoder
  133. }
  134. // NestedObjectEncoder is an optional interface that objects may implement to be given
  135. // an opportunity to encode any nested Objects / RawExtensions during serialization.
  136. type NestedObjectEncoder interface {
  137. EncodeNestedObjects(e Encoder) error
  138. }
  139. // NestedObjectDecoder is an optional interface that objects may implement to be given
  140. // an opportunity to decode any nested Objects / RawExtensions during serialization.
  141. type NestedObjectDecoder interface {
  142. DecodeNestedObjects(d Decoder) error
  143. }
  144. ///////////////////////////////////////////////////////////////////////////////
  145. // Non-codec interfaces
  146. type ObjectVersioner interface {
  147. ConvertToVersion(in Object, gv GroupVersioner) (out Object, err error)
  148. }
  149. // ObjectConvertor converts an object to a different version.
  150. type ObjectConvertor interface {
  151. // Convert attempts to convert one object into another, or returns an error. This method does
  152. // not guarantee the in object is not mutated. The context argument will be passed to
  153. // 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 guarantee that the in object is not mutated. This method is similar to
  157. // Convert() but handles specific details of choosing the correct output version.
  158. ConvertToVersion(in Object, gv GroupVersioner) (out Object, err error)
  159. ConvertFieldLabel(version, kind, label, value string) (string, string, error)
  160. }
  161. // ObjectTyper contains methods for extracting the APIVersion and Kind
  162. // of objects.
  163. type ObjectTyper interface {
  164. // ObjectKinds returns the all possible group,version,kind of the provided object, true if
  165. // the object is unversioned, or an error if the object is not recognized
  166. // (IsNotRegisteredError will return true).
  167. ObjectKinds(Object) ([]unversioned.GroupVersionKind, bool, error)
  168. // Recognizes returns true if the scheme is able to handle the provided version and kind,
  169. // or more precisely that the provided version is a possible conversion or decoding
  170. // target.
  171. Recognizes(gvk unversioned.GroupVersionKind) bool
  172. }
  173. // ObjectCreater contains methods for instantiating an object by kind and version.
  174. type ObjectCreater interface {
  175. New(kind unversioned.GroupVersionKind) (out Object, err error)
  176. }
  177. // ObjectCopier duplicates an object.
  178. type ObjectCopier interface {
  179. // Copy returns an exact copy of the provided Object, or an error if the
  180. // copy could not be completed.
  181. Copy(Object) (Object, error)
  182. }
  183. // ResourceVersioner provides methods for setting and retrieving
  184. // the resource version from an API object.
  185. type ResourceVersioner interface {
  186. SetResourceVersion(obj Object, version string) error
  187. ResourceVersion(obj Object) (string, error)
  188. }
  189. // SelfLinker provides methods for setting and retrieving the SelfLink field of an API object.
  190. type SelfLinker interface {
  191. SetSelfLink(obj Object, selfLink string) error
  192. SelfLink(obj Object) (string, error)
  193. // Knowing Name is sometimes necessary to use a SelfLinker.
  194. Name(obj Object) (string, error)
  195. // Knowing Namespace is sometimes necessary to use a SelfLinker
  196. Namespace(obj Object) (string, error)
  197. }
  198. // All API types registered with Scheme must support the Object interface. Since objects in a scheme are
  199. // expected to be serialized to the wire, the interface an Object must provide to the Scheme allows
  200. // serializers to set the kind, version, and group the object is represented as. An Object may choose
  201. // to return a no-op ObjectKindAccessor in cases where it is not expected to be serialized.
  202. type Object interface {
  203. GetObjectKind() unversioned.ObjectKind
  204. }