blob: 00184710760b673c72af13b7b5f6b3948c8c7622 [file] [log] [blame]
sslobodrd046be82019-01-16 10:02:22 -05001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package versioning
18
19import (
20 "io"
21 "reflect"
22
23 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24 "k8s.io/apimachinery/pkg/runtime"
25 "k8s.io/apimachinery/pkg/runtime/schema"
26)
27
28// NewDefaultingCodecForScheme is a convenience method for callers that are using a scheme.
29func 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,
36) runtime.Codec {
37 return NewCodec(encoder, decoder, runtime.UnsafeObjectConvertor(scheme), scheme, scheme, scheme, encodeVersion, decodeVersion, scheme.Name())
38}
39
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.
43func NewCodec(
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,
53) runtime.Codec {
54 internal := &codec{
55 encoder: encoder,
56 decoder: decoder,
57 convertor: convertor,
58 creater: creater,
59 typer: typer,
60 defaulter: defaulter,
61
62 encodeVersion: encodeVersion,
63 decodeVersion: decodeVersion,
64
65 originalSchemeName: originalSchemeName,
66 }
67 return internal
68}
69
70type codec struct {
71 encoder runtime.Encoder
72 decoder runtime.Decoder
73 convertor runtime.ObjectConvertor
74 creater runtime.ObjectCreater
75 typer runtime.ObjectTyper
76 defaulter runtime.ObjectDefaulter
77
78 encodeVersion runtime.GroupVersioner
79 decodeVersion runtime.GroupVersioner
80
81 // originalSchemeName is optional, but when filled in it holds the name of the scheme from which this codec originates
82 originalSchemeName string
83}
84
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.
88func (c *codec) Decode(data []byte, defaultGVK *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
89 versioned, isVersioned := into.(*runtime.VersionedObjects)
90 if isVersioned {
91 into = versioned.Last()
92 }
93
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`)
96 decodeInto := into
97 if into != nil {
98 if _, ok := into.(runtime.Unstructured); ok && !into.GetObjectKind().GroupVersionKind().GroupVersion().Empty() {
99 decodeInto = reflect.New(reflect.TypeOf(into).Elem()).Interface().(runtime.Object)
100 }
101 }
102
103 obj, gvk, err := c.decoder.Decode(data, defaultGVK, decodeInto)
104 if err != nil {
105 return nil, gvk, err
106 }
107
108 if d, ok := obj.(runtime.NestedObjectDecoder); ok {
109 if err := d.DecodeNestedObjects(DirectDecoder{c.decoder}); err != nil {
110 return nil, gvk, err
111 }
112 }
113
114 // if we specify a target, use generic conversion.
115 if into != nil {
116 if into == obj {
117 if isVersioned {
118 return versioned, gvk, nil
119 }
120 return into, gvk, nil
121 }
122
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
126 if isVersioned {
127 versioned.Objects = []runtime.Object{obj.DeepCopyObject()}
128 }
129 c.defaulter.Default(obj)
130 } else {
131 if isVersioned {
132 versioned.Objects = []runtime.Object{obj}
133 }
134 }
135
136 if err := c.convertor.Convert(obj, into, c.decodeVersion); err != nil {
137 return nil, gvk, err
138 }
139
140 if isVersioned {
141 versioned.Objects = append(versioned.Objects, into)
142 return versioned, gvk, nil
143 }
144 return into, gvk, nil
145 }
146
147 // Convert if needed.
148 if isVersioned {
149 // create a copy, because ConvertToVersion does not guarantee non-mutation of objects
150 versioned.Objects = []runtime.Object{obj.DeepCopyObject()}
151 }
152
153 // perform defaulting if requested
154 if c.defaulter != nil {
155 c.defaulter.Default(obj)
156 }
157
158 out, err := c.convertor.ConvertToVersion(obj, c.decodeVersion)
159 if err != nil {
160 return nil, gvk, err
161 }
162 if isVersioned {
163 if versioned.Last() != out {
164 versioned.Objects = append(versioned.Objects, out)
165 }
166 return versioned, gvk, nil
167 }
168 return out, gvk, nil
169}
170
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.
173func (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)
186 }
187 targetGVK, ok := c.encodeVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{objGVK})
188 if !ok {
189 return runtime.NewNotRegisteredGVKErrForTarget(c.originalSchemeName, objGVK, c.encodeVersion)
190 }
191 if targetGVK == objGVK {
192 return c.encoder.Encode(obj, w)
193 }
194 }
195 }
196
197 gvks, isUnversioned, err := c.typer.ObjectKinds(obj)
198 if err != nil {
199 return err
200 }
201
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 {
205 return err
206 }
207 }
208 objectKind := obj.GetObjectKind()
209 old := objectKind.GroupVersionKind()
210 objectKind.SetGroupVersionKind(gvks[0])
211 err = c.encoder.Encode(obj, w)
212 objectKind.SetGroupVersionKind(old)
213 return err
214 }
215
216 // Perform a conversion if necessary
217 objectKind := obj.GetObjectKind()
218 old := objectKind.GroupVersionKind()
219 out, err := c.convertor.ConvertToVersion(obj, c.encodeVersion)
220 if err != nil {
221 return err
222 }
223
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 {
226 return err
227 }
228 }
229
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)
234 return err
235}
236
237// DirectEncoder serializes an object and ensures the GVK is set.
238type DirectEncoder struct {
239 Version runtime.GroupVersioner
240 runtime.Encoder
241 runtime.ObjectTyper
242}
243
244// Encode does not do conversion. It sets the gvk during serialization.
245func (e DirectEncoder) Encode(obj runtime.Object, stream io.Writer) error {
246 gvks, _, err := e.ObjectTyper.ObjectKinds(obj)
247 if err != nil {
248 if runtime.IsNotRegisteredError(err) {
249 return e.Encoder.Encode(obj, stream)
250 }
251 return err
252 }
253 kind := obj.GetObjectKind()
254 oldGVK := kind.GroupVersionKind()
255 gvk := gvks[0]
256 if e.Version != nil {
257 preferredGVK, ok := e.Version.KindForGroupVersionKinds(gvks)
258 if ok {
259 gvk = preferredGVK
260 }
261 }
262 kind.SetGroupVersionKind(gvk)
263 err = e.Encoder.Encode(obj, stream)
264 kind.SetGroupVersionKind(oldGVK)
265 return err
266}
267
268// DirectDecoder clears the group version kind of a deserialized object.
269type DirectDecoder struct {
270 runtime.Decoder
271}
272
273// Decode does not do conversion. It removes the gvk during deserialization.
274func (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)
276 if obj != nil {
277 kind := obj.GetObjectKind()
278 // clearing the gvk is just a convention of a codec
279 kind.SetGroupVersionKind(schema.GroupVersionKind{})
280 }
281 return obj, gvk, err
282}