versioning.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 identifiersMap sync.Map
  77. type codecIdentifier struct {
  78. EncodeGV string `json:"encodeGV,omitempty"`
  79. Encoder string `json:"encoder,omitempty"`
  80. Name string `json:"name,omitempty"`
  81. }
  82. // identifier computes Identifier of Encoder based on codec parameters.
  83. func identifier(encodeGV runtime.GroupVersioner, encoder runtime.Encoder) runtime.Identifier {
  84. result := codecIdentifier{
  85. Name: "versioning",
  86. }
  87. if encodeGV != nil {
  88. result.EncodeGV = encodeGV.Identifier()
  89. }
  90. if encoder != nil {
  91. result.Encoder = string(encoder.Identifier())
  92. }
  93. if id, ok := identifiersMap.Load(result); ok {
  94. return id.(runtime.Identifier)
  95. }
  96. identifier, err := json.Marshal(result)
  97. if err != nil {
  98. klog.Fatalf("Failed marshaling identifier for codec: %v", err)
  99. }
  100. identifiersMap.Store(result, runtime.Identifier(identifier))
  101. return runtime.Identifier(identifier)
  102. }
  103. // Decode attempts a decode of the object, then tries to convert it to the internal version. If into is provided and the decoding is
  104. // successful, the returned runtime.Object will be the value passed as into. Note that this may bypass conversion if you pass an
  105. // into that matches the serialized version.
  106. func (c *codec) Decode(data []byte, defaultGVK *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
  107. // If the into object is unstructured and expresses an opinion about its group/version,
  108. // create a new instance of the type so we always exercise the conversion path (skips short-circuiting on `into == obj`)
  109. decodeInto := into
  110. if into != nil {
  111. if _, ok := into.(runtime.Unstructured); ok && !into.GetObjectKind().GroupVersionKind().GroupVersion().Empty() {
  112. decodeInto = reflect.New(reflect.TypeOf(into).Elem()).Interface().(runtime.Object)
  113. }
  114. }
  115. obj, gvk, err := c.decoder.Decode(data, defaultGVK, decodeInto)
  116. if err != nil {
  117. return nil, gvk, err
  118. }
  119. if d, ok := obj.(runtime.NestedObjectDecoder); ok {
  120. if err := d.DecodeNestedObjects(runtime.WithoutVersionDecoder{c.decoder}); err != nil {
  121. return nil, gvk, err
  122. }
  123. }
  124. // if we specify a target, use generic conversion.
  125. if into != nil {
  126. // perform defaulting if requested
  127. if c.defaulter != nil {
  128. c.defaulter.Default(obj)
  129. }
  130. // Short-circuit conversion if the into object is same object
  131. if into == obj {
  132. return into, gvk, nil
  133. }
  134. if err := c.convertor.Convert(obj, into, c.decodeVersion); err != nil {
  135. return nil, gvk, err
  136. }
  137. return into, gvk, nil
  138. }
  139. // perform defaulting if requested
  140. if c.defaulter != nil {
  141. c.defaulter.Default(obj)
  142. }
  143. out, err := c.convertor.ConvertToVersion(obj, c.decodeVersion)
  144. if err != nil {
  145. return nil, gvk, err
  146. }
  147. return out, gvk, nil
  148. }
  149. // Encode ensures the provided object is output in the appropriate group and version, invoking
  150. // conversion if necessary. Unversioned objects (according to the ObjectTyper) are output as is.
  151. func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
  152. if co, ok := obj.(runtime.CacheableObject); ok {
  153. return co.CacheEncode(c.Identifier(), c.doEncode, w)
  154. }
  155. return c.doEncode(obj, w)
  156. }
  157. func (c *codec) doEncode(obj runtime.Object, w io.Writer) error {
  158. switch obj := obj.(type) {
  159. case *runtime.Unknown:
  160. return c.encoder.Encode(obj, w)
  161. case runtime.Unstructured:
  162. // An unstructured list can contain objects of multiple group version kinds. don't short-circuit just
  163. // because the top-level type matches our desired destination type. actually send the object to the converter
  164. // to give it a chance to convert the list items if needed.
  165. if _, ok := obj.(*unstructured.UnstructuredList); !ok {
  166. // 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)
  167. objGVK := obj.GetObjectKind().GroupVersionKind()
  168. if len(objGVK.Version) == 0 {
  169. return c.encoder.Encode(obj, w)
  170. }
  171. targetGVK, ok := c.encodeVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{objGVK})
  172. if !ok {
  173. return runtime.NewNotRegisteredGVKErrForTarget(c.originalSchemeName, objGVK, c.encodeVersion)
  174. }
  175. if targetGVK == objGVK {
  176. return c.encoder.Encode(obj, w)
  177. }
  178. }
  179. }
  180. gvks, isUnversioned, err := c.typer.ObjectKinds(obj)
  181. if err != nil {
  182. return err
  183. }
  184. objectKind := obj.GetObjectKind()
  185. old := objectKind.GroupVersionKind()
  186. // restore the old GVK after encoding
  187. defer objectKind.SetGroupVersionKind(old)
  188. if c.encodeVersion == nil || isUnversioned {
  189. if e, ok := obj.(runtime.NestedObjectEncoder); ok {
  190. if err := e.EncodeNestedObjects(runtime.WithVersionEncoder{Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
  191. return err
  192. }
  193. }
  194. objectKind.SetGroupVersionKind(gvks[0])
  195. return c.encoder.Encode(obj, w)
  196. }
  197. // Perform a conversion if necessary
  198. out, err := c.convertor.ConvertToVersion(obj, c.encodeVersion)
  199. if err != nil {
  200. return err
  201. }
  202. if e, ok := out.(runtime.NestedObjectEncoder); ok {
  203. if err := e.EncodeNestedObjects(runtime.WithVersionEncoder{Version: c.encodeVersion, Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
  204. return err
  205. }
  206. }
  207. // Conversion is responsible for setting the proper group, version, and kind onto the outgoing object
  208. return c.encoder.Encode(out, w)
  209. }
  210. // Identifier implements runtime.Encoder interface.
  211. func (c *codec) Identifier() runtime.Identifier {
  212. return c.identifier
  213. }