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