versioning.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 versioning
  14. import (
  15. "io"
  16. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. )
  20. // NewCodecForScheme is a convenience method for callers that are using a scheme.
  21. func NewCodecForScheme(
  22. // TODO: I should be a scheme interface?
  23. scheme *runtime.Scheme,
  24. encoder runtime.Encoder,
  25. decoder runtime.Decoder,
  26. encodeVersion runtime.GroupVersioner,
  27. decodeVersion runtime.GroupVersioner,
  28. ) runtime.Codec {
  29. return NewCodec(encoder, decoder, runtime.UnsafeObjectConvertor(scheme), scheme, scheme, nil, encodeVersion, decodeVersion)
  30. }
  31. // NewDefaultingCodecForScheme is a convenience method for callers that are using a scheme.
  32. func NewDefaultingCodecForScheme(
  33. // TODO: I should be a scheme interface?
  34. scheme *runtime.Scheme,
  35. encoder runtime.Encoder,
  36. decoder runtime.Decoder,
  37. encodeVersion runtime.GroupVersioner,
  38. decodeVersion runtime.GroupVersioner,
  39. ) runtime.Codec {
  40. return NewCodec(encoder, decoder, runtime.UnsafeObjectConvertor(scheme), scheme, scheme, scheme, encodeVersion, decodeVersion)
  41. }
  42. // NewCodec takes objects in their internal versions and converts them to external versions before
  43. // serializing them. It assumes the serializer provided to it only deals with external versions.
  44. // This class is also a serializer, but is generally used with a specific version.
  45. func NewCodec(
  46. encoder runtime.Encoder,
  47. decoder runtime.Decoder,
  48. convertor runtime.ObjectConvertor,
  49. creater runtime.ObjectCreater,
  50. typer runtime.ObjectTyper,
  51. defaulter runtime.ObjectDefaulter,
  52. encodeVersion runtime.GroupVersioner,
  53. decodeVersion runtime.GroupVersioner,
  54. ) runtime.Codec {
  55. internal := &codec{
  56. encoder: encoder,
  57. decoder: decoder,
  58. convertor: convertor,
  59. creater: creater,
  60. typer: typer,
  61. defaulter: defaulter,
  62. encodeVersion: encodeVersion,
  63. decodeVersion: decodeVersion,
  64. }
  65. return internal
  66. }
  67. type codec struct {
  68. encoder runtime.Encoder
  69. decoder runtime.Decoder
  70. convertor runtime.ObjectConvertor
  71. creater runtime.ObjectCreater
  72. typer runtime.ObjectTyper
  73. defaulter runtime.ObjectDefaulter
  74. encodeVersion runtime.GroupVersioner
  75. decodeVersion runtime.GroupVersioner
  76. }
  77. // Decode attempts a decode of the object, then tries to convert it to the internal version. If into is provided and the decoding is
  78. // successful, the returned runtime.Object will be the value passed as into. Note that this may bypass conversion if you pass an
  79. // into that matches the serialized version.
  80. func (c *codec) Decode(data []byte, defaultGVK *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
  81. versioned, isVersioned := into.(*runtime.VersionedObjects)
  82. if isVersioned {
  83. into = versioned.Last()
  84. }
  85. obj, gvk, err := c.decoder.Decode(data, defaultGVK, into)
  86. if err != nil {
  87. return nil, gvk, err
  88. }
  89. if d, ok := obj.(runtime.NestedObjectDecoder); ok {
  90. if err := d.DecodeNestedObjects(DirectDecoder{c.decoder}); err != nil {
  91. return nil, gvk, err
  92. }
  93. }
  94. // if we specify a target, use generic conversion.
  95. if into != nil {
  96. if into == obj {
  97. if isVersioned {
  98. return versioned, gvk, nil
  99. }
  100. return into, gvk, nil
  101. }
  102. // perform defaulting if requested
  103. if c.defaulter != nil {
  104. // create a copy to ensure defaulting is not applied to the original versioned objects
  105. if isVersioned {
  106. versioned.Objects = []runtime.Object{obj.DeepCopyObject()}
  107. }
  108. c.defaulter.Default(obj)
  109. } else {
  110. if isVersioned {
  111. versioned.Objects = []runtime.Object{obj}
  112. }
  113. }
  114. if err := c.convertor.Convert(obj, into, c.decodeVersion); err != nil {
  115. return nil, gvk, err
  116. }
  117. if isVersioned {
  118. versioned.Objects = append(versioned.Objects, into)
  119. return versioned, gvk, nil
  120. }
  121. return into, gvk, nil
  122. }
  123. // Convert if needed.
  124. if isVersioned {
  125. // create a copy, because ConvertToVersion does not guarantee non-mutation of objects
  126. versioned.Objects = []runtime.Object{obj.DeepCopyObject()}
  127. }
  128. // perform defaulting if requested
  129. if c.defaulter != nil {
  130. c.defaulter.Default(obj)
  131. }
  132. out, err := c.convertor.ConvertToVersion(obj, c.decodeVersion)
  133. if err != nil {
  134. return nil, gvk, err
  135. }
  136. if isVersioned {
  137. if versioned.Last() != out {
  138. versioned.Objects = append(versioned.Objects, out)
  139. }
  140. return versioned, gvk, nil
  141. }
  142. return out, gvk, nil
  143. }
  144. // Encode ensures the provided object is output in the appropriate group and version, invoking
  145. // conversion if necessary. Unversioned objects (according to the ObjectTyper) are output as is.
  146. func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
  147. switch obj := obj.(type) {
  148. case *runtime.Unknown:
  149. return c.encoder.Encode(obj, w)
  150. case runtime.Unstructured:
  151. // An unstructured list can contain objects of multiple group version kinds. don't short-circuit just
  152. // because the top-level type matches our desired destination type. actually send the object to the converter
  153. // to give it a chance to convert the list items if needed.
  154. if _, ok := obj.(*unstructured.UnstructuredList); !ok {
  155. // avoid conversion roundtrip if GVK is the right one already or is empty (yes, this is a hack, but the old behaviour we rely on in kubectl)
  156. objGVK := obj.GetObjectKind().GroupVersionKind()
  157. if len(objGVK.Version) == 0 {
  158. return c.encoder.Encode(obj, w)
  159. }
  160. targetGVK, ok := c.encodeVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{objGVK})
  161. if !ok {
  162. return runtime.NewNotRegisteredGVKErrForTarget(objGVK, c.encodeVersion)
  163. }
  164. if targetGVK == objGVK {
  165. return c.encoder.Encode(obj, w)
  166. }
  167. }
  168. }
  169. gvks, isUnversioned, err := c.typer.ObjectKinds(obj)
  170. if err != nil {
  171. return err
  172. }
  173. if c.encodeVersion == nil || isUnversioned {
  174. if e, ok := obj.(runtime.NestedObjectEncoder); ok {
  175. if err := e.EncodeNestedObjects(DirectEncoder{Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
  176. return err
  177. }
  178. }
  179. objectKind := obj.GetObjectKind()
  180. old := objectKind.GroupVersionKind()
  181. objectKind.SetGroupVersionKind(gvks[0])
  182. err = c.encoder.Encode(obj, w)
  183. objectKind.SetGroupVersionKind(old)
  184. return err
  185. }
  186. // Perform a conversion if necessary
  187. objectKind := obj.GetObjectKind()
  188. old := objectKind.GroupVersionKind()
  189. out, err := c.convertor.ConvertToVersion(obj, c.encodeVersion)
  190. if err != nil {
  191. return err
  192. }
  193. if e, ok := out.(runtime.NestedObjectEncoder); ok {
  194. if err := e.EncodeNestedObjects(DirectEncoder{Version: c.encodeVersion, Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
  195. return err
  196. }
  197. }
  198. // Conversion is responsible for setting the proper group, version, and kind onto the outgoing object
  199. err = c.encoder.Encode(out, w)
  200. // restore the old GVK, in case conversion returned the same object
  201. objectKind.SetGroupVersionKind(old)
  202. return err
  203. }
  204. // DirectEncoder serializes an object and ensures the GVK is set.
  205. type DirectEncoder struct {
  206. Version runtime.GroupVersioner
  207. runtime.Encoder
  208. runtime.ObjectTyper
  209. }
  210. // Encode does not do conversion. It sets the gvk during serialization.
  211. func (e DirectEncoder) Encode(obj runtime.Object, stream io.Writer) error {
  212. gvks, _, err := e.ObjectTyper.ObjectKinds(obj)
  213. if err != nil {
  214. if runtime.IsNotRegisteredError(err) {
  215. return e.Encoder.Encode(obj, stream)
  216. }
  217. return err
  218. }
  219. kind := obj.GetObjectKind()
  220. oldGVK := kind.GroupVersionKind()
  221. gvk := gvks[0]
  222. if e.Version != nil {
  223. preferredGVK, ok := e.Version.KindForGroupVersionKinds(gvks)
  224. if ok {
  225. gvk = preferredGVK
  226. }
  227. }
  228. kind.SetGroupVersionKind(gvk)
  229. err = e.Encoder.Encode(obj, stream)
  230. kind.SetGroupVersionKind(oldGVK)
  231. return err
  232. }
  233. // DirectDecoder clears the group version kind of a deserialized object.
  234. type DirectDecoder struct {
  235. runtime.Decoder
  236. }
  237. // Decode does not do conversion. It removes the gvk during deserialization.
  238. func (d DirectDecoder) Decode(data []byte, defaults *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
  239. obj, gvk, err := d.Decoder.Decode(data, defaults, into)
  240. if obj != nil {
  241. kind := obj.GetObjectKind()
  242. // clearing the gvk is just a convention of a codec
  243. kind.SetGroupVersionKind(schema.GroupVersionKind{})
  244. }
  245. return obj, gvk, err
  246. }