2 Copyright 2014 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
23 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24 "k8s.io/apimachinery/pkg/runtime"
25 "k8s.io/apimachinery/pkg/runtime/schema"
28 // NewDefaultingCodecForScheme is a convenience method for callers that are using a scheme.
29 func NewDefaultingCodecForScheme(
30 // TODO: I should be a scheme interface?
31 scheme *runtime.Scheme,
32 encoder runtime.Encoder,
33 decoder runtime.Decoder,
34 encodeVersion runtime.GroupVersioner,
35 decodeVersion runtime.GroupVersioner,
37 return NewCodec(encoder, decoder, runtime.UnsafeObjectConvertor(scheme), scheme, scheme, scheme, encodeVersion, decodeVersion, scheme.Name())
40 // NewCodec takes objects in their internal versions and converts them to external versions before
41 // serializing them. It assumes the serializer provided to it only deals with external versions.
42 // This class is also a serializer, but is generally used with a specific version.
44 encoder runtime.Encoder,
45 decoder runtime.Decoder,
46 convertor runtime.ObjectConvertor,
47 creater runtime.ObjectCreater,
48 typer runtime.ObjectTyper,
49 defaulter runtime.ObjectDefaulter,
50 encodeVersion runtime.GroupVersioner,
51 decodeVersion runtime.GroupVersioner,
52 originalSchemeName string,
62 encodeVersion: encodeVersion,
63 decodeVersion: decodeVersion,
65 originalSchemeName: originalSchemeName,
71 encoder runtime.Encoder
72 decoder runtime.Decoder
73 convertor runtime.ObjectConvertor
74 creater runtime.ObjectCreater
75 typer runtime.ObjectTyper
76 defaulter runtime.ObjectDefaulter
78 encodeVersion runtime.GroupVersioner
79 decodeVersion runtime.GroupVersioner
81 // originalSchemeName is optional, but when filled in it holds the name of the scheme from which this codec originates
82 originalSchemeName string
85 // Decode attempts a decode of the object, then tries to convert it to the internal version. If into is provided and the decoding is
86 // successful, the returned runtime.Object will be the value passed as into. Note that this may bypass conversion if you pass an
87 // into that matches the serialized version.
88 func (c *codec) Decode(data []byte, defaultGVK *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
89 versioned, isVersioned := into.(*runtime.VersionedObjects)
91 into = versioned.Last()
94 // If the into object is unstructured and expresses an opinion about its group/version,
95 // create a new instance of the type so we always exercise the conversion path (skips short-circuiting on `into == obj`)
98 if _, ok := into.(runtime.Unstructured); ok && !into.GetObjectKind().GroupVersionKind().GroupVersion().Empty() {
99 decodeInto = reflect.New(reflect.TypeOf(into).Elem()).Interface().(runtime.Object)
103 obj, gvk, err := c.decoder.Decode(data, defaultGVK, decodeInto)
108 if d, ok := obj.(runtime.NestedObjectDecoder); ok {
109 if err := d.DecodeNestedObjects(DirectDecoder{c.decoder}); err != nil {
114 // if we specify a target, use generic conversion.
118 return versioned, gvk, nil
120 return into, gvk, nil
123 // perform defaulting if requested
124 if c.defaulter != nil {
125 // create a copy to ensure defaulting is not applied to the original versioned objects
127 versioned.Objects = []runtime.Object{obj.DeepCopyObject()}
129 c.defaulter.Default(obj)
132 versioned.Objects = []runtime.Object{obj}
136 if err := c.convertor.Convert(obj, into, c.decodeVersion); err != nil {
141 versioned.Objects = append(versioned.Objects, into)
142 return versioned, gvk, nil
144 return into, gvk, nil
147 // Convert if needed.
149 // create a copy, because ConvertToVersion does not guarantee non-mutation of objects
150 versioned.Objects = []runtime.Object{obj.DeepCopyObject()}
153 // perform defaulting if requested
154 if c.defaulter != nil {
155 c.defaulter.Default(obj)
158 out, err := c.convertor.ConvertToVersion(obj, c.decodeVersion)
163 if versioned.Last() != out {
164 versioned.Objects = append(versioned.Objects, out)
166 return versioned, gvk, nil
171 // Encode ensures the provided object is output in the appropriate group and version, invoking
172 // conversion if necessary. Unversioned objects (according to the ObjectTyper) are output as is.
173 func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
174 switch obj := obj.(type) {
175 case *runtime.Unknown:
176 return c.encoder.Encode(obj, w)
177 case runtime.Unstructured:
178 // An unstructured list can contain objects of multiple group version kinds. don't short-circuit just
179 // because the top-level type matches our desired destination type. actually send the object to the converter
180 // to give it a chance to convert the list items if needed.
181 if _, ok := obj.(*unstructured.UnstructuredList); !ok {
182 // 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)
183 objGVK := obj.GetObjectKind().GroupVersionKind()
184 if len(objGVK.Version) == 0 {
185 return c.encoder.Encode(obj, w)
187 targetGVK, ok := c.encodeVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{objGVK})
189 return runtime.NewNotRegisteredGVKErrForTarget(c.originalSchemeName, objGVK, c.encodeVersion)
191 if targetGVK == objGVK {
192 return c.encoder.Encode(obj, w)
197 gvks, isUnversioned, err := c.typer.ObjectKinds(obj)
202 if c.encodeVersion == nil || isUnversioned {
203 if e, ok := obj.(runtime.NestedObjectEncoder); ok {
204 if err := e.EncodeNestedObjects(DirectEncoder{Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
208 objectKind := obj.GetObjectKind()
209 old := objectKind.GroupVersionKind()
210 objectKind.SetGroupVersionKind(gvks[0])
211 err = c.encoder.Encode(obj, w)
212 objectKind.SetGroupVersionKind(old)
216 // Perform a conversion if necessary
217 objectKind := obj.GetObjectKind()
218 old := objectKind.GroupVersionKind()
219 out, err := c.convertor.ConvertToVersion(obj, c.encodeVersion)
224 if e, ok := out.(runtime.NestedObjectEncoder); ok {
225 if err := e.EncodeNestedObjects(DirectEncoder{Version: c.encodeVersion, Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
230 // Conversion is responsible for setting the proper group, version, and kind onto the outgoing object
231 err = c.encoder.Encode(out, w)
232 // restore the old GVK, in case conversion returned the same object
233 objectKind.SetGroupVersionKind(old)
237 // DirectEncoder serializes an object and ensures the GVK is set.
238 type DirectEncoder struct {
239 Version runtime.GroupVersioner
244 // Encode does not do conversion. It sets the gvk during serialization.
245 func (e DirectEncoder) Encode(obj runtime.Object, stream io.Writer) error {
246 gvks, _, err := e.ObjectTyper.ObjectKinds(obj)
248 if runtime.IsNotRegisteredError(err) {
249 return e.Encoder.Encode(obj, stream)
253 kind := obj.GetObjectKind()
254 oldGVK := kind.GroupVersionKind()
256 if e.Version != nil {
257 preferredGVK, ok := e.Version.KindForGroupVersionKinds(gvks)
262 kind.SetGroupVersionKind(gvk)
263 err = e.Encoder.Encode(obj, stream)
264 kind.SetGroupVersionKind(oldGVK)
268 // DirectDecoder clears the group version kind of a deserialized object.
269 type DirectDecoder struct {
273 // Decode does not do conversion. It removes the gvk during deserialization.
274 func (d DirectDecoder) Decode(data []byte, defaults *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
275 obj, gvk, err := d.Decoder.Decode(data, defaults, into)
277 kind := obj.GetObjectKind()
278 // clearing the gvk is just a convention of a codec
279 kind.SetGroupVersionKind(schema.GroupVersionKind{})