versioning.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. "encoding/json"
  16. "io"
  17. "reflect"
  18. "sync"
  19. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apimachinery/pkg/runtime/schema"
  22. "k8s.io/klog/v2"
  23. )
  24. // NewDefaultingCodecForScheme is a convenience method for callers that are using a scheme.
  25. func NewDefaultingCodecForScheme(
  26. // TODO: I should be a scheme interface?
  27. scheme *runtime.Scheme,
  28. encoder runtime.Encoder,
  29. decoder runtime.Decoder,
  30. encodeVersion runtime.GroupVersioner,
  31. decodeVersion runtime.GroupVersioner,
  32. ) runtime.Codec {
  33. return NewCodec(encoder, decoder, runtime.UnsafeObjectConvertor(scheme), scheme, scheme, scheme, encodeVersion, decodeVersion, scheme.Name())
  34. }
  35. // NewCodec takes objects in their internal versions and converts them to external versions before
  36. // serializing them. It assumes the serializer provided to it only deals with external versions.
  37. // This class is also a serializer, but is generally used with a specific version.
  38. func NewCodec(
  39. encoder runtime.Encoder,
  40. decoder runtime.Decoder,
  41. convertor runtime.ObjectConvertor,
  42. creater runtime.ObjectCreater,
  43. typer runtime.ObjectTyper,
  44. defaulter runtime.ObjectDefaulter,
  45. encodeVersion runtime.GroupVersioner,
  46. decodeVersion runtime.GroupVersioner,
  47. originalSchemeName string,
  48. ) runtime.Codec {
  49. internal := &codec{
  50. encoder: encoder,
  51. decoder: decoder,
  52. convertor: convertor,
  53. creater: creater,
  54. typer: typer,
  55. defaulter: defaulter,
  56. encodeVersion: encodeVersion,
  57. decodeVersion: decodeVersion,
  58. identifier: identifier(encodeVersion, encoder),
  59. originalSchemeName: originalSchemeName,
  60. }
  61. return internal
  62. }
  63. type codec struct {
  64. encoder runtime.Encoder
  65. decoder runtime.Decoder
  66. convertor runtime.ObjectConvertor
  67. creater runtime.ObjectCreater
  68. typer runtime.ObjectTyper
  69. defaulter runtime.ObjectDefaulter
  70. encodeVersion runtime.GroupVersioner
  71. decodeVersion runtime.GroupVersioner
  72. identifier runtime.Identifier
  73. // originalSchemeName is optional, but when filled in it holds the name of the scheme from which this codec originates
  74. originalSchemeName string
  75. }
  76. var _ runtime.EncoderWithAllocator = &codec{}
  77. var identifiersMap sync.Map
  78. type codecIdentifier struct {
  79. EncodeGV string `json:"encodeGV,omitempty"`
  80. Encoder string `json:"encoder,omitempty"`
  81. Name string `json:"name,omitempty"`
  82. }
  83. // identifier computes Identifier of Encoder based on codec parameters.
  84. func identifier(encodeGV runtime.GroupVersioner, encoder runtime.Encoder) runtime.Identifier {
  85. result := codecIdentifier{
  86. Name: "versioning",
  87. }
  88. if encodeGV != nil {
  89. result.EncodeGV = encodeGV.Identifier()
  90. }
  91. if encoder != nil {
  92. result.Encoder = string(encoder.Identifier())
  93. }
  94. if id, ok := identifiersMap.Load(result); ok {
  95. return id.(runtime.Identifier)
  96. }
  97. identifier, err := json.Marshal(result)
  98. if err != nil {
  99. klog.Fatalf("Failed marshaling identifier for codec: %v", err)
  100. }
  101. identifiersMap.Store(result, runtime.Identifier(identifier))
  102. return runtime.Identifier(identifier)
  103. }
  104. // Decode attempts a decode of the object, then tries to convert it to the internal version. If into is provided and the decoding is
  105. // successful, the returned runtime.Object will be the value passed as into. Note that this may bypass conversion if you pass an
  106. // into that matches the serialized version.
  107. func (c *codec) Decode(data []byte, defaultGVK *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
  108. // If the into object is unstructured and expresses an opinion about its group/version,
  109. // create a new instance of the type so we always exercise the conversion path (skips short-circuiting on `into == obj`)
  110. decodeInto := into
  111. if into != nil {
  112. if _, ok := into.(runtime.Unstructured); ok && !into.GetObjectKind().GroupVersionKind().GroupVersion().Empty() {
  113. decodeInto = reflect.New(reflect.TypeOf(into).Elem()).Interface().(runtime.Object)
  114. }
  115. }
  116. var strictDecodingErrs []error
  117. obj, gvk, err := c.decoder.Decode(data, defaultGVK, decodeInto)
  118. if err != nil {
  119. if strictErr, ok := runtime.AsStrictDecodingError(err); obj != nil && ok {
  120. // save the strictDecodingError and let the caller decide what to do with it
  121. strictDecodingErrs = append(strictDecodingErrs, strictErr.Errors()...)
  122. } else {
  123. return nil, gvk, err
  124. }
  125. }
  126. if d, ok := obj.(runtime.NestedObjectDecoder); ok {
  127. if err := d.DecodeNestedObjects(runtime.WithoutVersionDecoder{Decoder: c.decoder}); err != nil {
  128. if strictErr, ok := runtime.AsStrictDecodingError(err); ok {
  129. // save the strictDecodingError let and the caller decide what to do with it
  130. strictDecodingErrs = append(strictDecodingErrs, strictErr.Errors()...)
  131. } else {
  132. return nil, gvk, err
  133. }
  134. }
  135. }
  136. // aggregate the strict decoding errors into one
  137. var strictDecodingErr error
  138. if len(strictDecodingErrs) > 0 {
  139. strictDecodingErr = runtime.NewStrictDecodingError(strictDecodingErrs)
  140. }
  141. // if we specify a target, use generic conversion.
  142. if into != nil {
  143. // perform defaulting if requested
  144. if c.defaulter != nil {
  145. c.defaulter.Default(obj)
  146. }
  147. // Short-circuit conversion if the into object is same object
  148. if into == obj {
  149. return into, gvk, strictDecodingErr
  150. }
  151. if err := c.convertor.Convert(obj, into, c.decodeVersion); err != nil {
  152. return nil, gvk, err
  153. }
  154. return into, gvk, strictDecodingErr
  155. }
  156. // perform defaulting if requested
  157. if c.defaulter != nil {
  158. c.defaulter.Default(obj)
  159. }
  160. out, err := c.convertor.ConvertToVersion(obj, c.decodeVersion)
  161. if err != nil {
  162. return nil, gvk, err
  163. }
  164. return out, gvk, strictDecodingErr
  165. }
  166. // EncodeWithAllocator ensures the provided object is output in the appropriate group and version, invoking
  167. // conversion if necessary. Unversioned objects (according to the ObjectTyper) are output as is.
  168. // In addition, it allows for providing a memory allocator for efficient memory usage during object serialization.
  169. func (c *codec) EncodeWithAllocator(obj runtime.Object, w io.Writer, memAlloc runtime.MemoryAllocator) error {
  170. return c.encode(obj, w, memAlloc)
  171. }
  172. // Encode ensures the provided object is output in the appropriate group and version, invoking
  173. // conversion if necessary. Unversioned objects (according to the ObjectTyper) are output as is.
  174. func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
  175. return c.encode(obj, w, nil)
  176. }
  177. func (c *codec) encode(obj runtime.Object, w io.Writer, memAlloc runtime.MemoryAllocator) error {
  178. if co, ok := obj.(runtime.CacheableObject); ok {
  179. return co.CacheEncode(c.Identifier(), func(obj runtime.Object, w io.Writer) error { return c.doEncode(obj, w, memAlloc) }, w)
  180. }
  181. return c.doEncode(obj, w, memAlloc)
  182. }
  183. func (c *codec) doEncode(obj runtime.Object, w io.Writer, memAlloc runtime.MemoryAllocator) error {
  184. encodeFn := c.encoder.Encode
  185. if memAlloc != nil {
  186. if encoder, supportsAllocator := c.encoder.(runtime.EncoderWithAllocator); supportsAllocator {
  187. encodeFn = func(obj runtime.Object, w io.Writer) error {
  188. return encoder.EncodeWithAllocator(obj, w, memAlloc)
  189. }
  190. } else {
  191. klog.V(6).Infof("a memory allocator was provided but the encoder %s doesn't implement the runtime.EncoderWithAllocator, using regular encoder.Encode method", c.encoder.Identifier())
  192. }
  193. }
  194. switch obj := obj.(type) {
  195. case *runtime.Unknown:
  196. return encodeFn(obj, w)
  197. case runtime.Unstructured:
  198. // An unstructured list can contain objects of multiple group version kinds. don't short-circuit just
  199. // because the top-level type matches our desired destination type. actually send the object to the converter
  200. // to give it a chance to convert the list items if needed.
  201. if _, ok := obj.(*unstructured.UnstructuredList); !ok {
  202. // 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)
  203. objGVK := obj.GetObjectKind().GroupVersionKind()
  204. if len(objGVK.Version) == 0 {
  205. return encodeFn(obj, w)
  206. }
  207. targetGVK, ok := c.encodeVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{objGVK})
  208. if !ok {
  209. return runtime.NewNotRegisteredGVKErrForTarget(c.originalSchemeName, objGVK, c.encodeVersion)
  210. }
  211. if targetGVK == objGVK {
  212. return encodeFn(obj, w)
  213. }
  214. }
  215. }
  216. gvks, isUnversioned, err := c.typer.ObjectKinds(obj)
  217. if err != nil {
  218. return err
  219. }
  220. objectKind := obj.GetObjectKind()
  221. old := objectKind.GroupVersionKind()
  222. // restore the old GVK after encoding
  223. defer objectKind.SetGroupVersionKind(old)
  224. if c.encodeVersion == nil || isUnversioned {
  225. if e, ok := obj.(runtime.NestedObjectEncoder); ok {
  226. if err := e.EncodeNestedObjects(runtime.WithVersionEncoder{Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
  227. return err
  228. }
  229. }
  230. objectKind.SetGroupVersionKind(gvks[0])
  231. return encodeFn(obj, w)
  232. }
  233. // Perform a conversion if necessary
  234. out, err := c.convertor.ConvertToVersion(obj, c.encodeVersion)
  235. if err != nil {
  236. return err
  237. }
  238. if e, ok := out.(runtime.NestedObjectEncoder); ok {
  239. if err := e.EncodeNestedObjects(runtime.WithVersionEncoder{Version: c.encodeVersion, Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
  240. return err
  241. }
  242. }
  243. // Conversion is responsible for setting the proper group, version, and kind onto the outgoing object
  244. return encodeFn(out, w)
  245. }
  246. // Identifier implements runtime.Encoder interface.
  247. func (c *codec) Identifier() runtime.Identifier {
  248. return c.identifier
  249. }