blob: a04a2e98bff13ef47521709915c6dcb8fdf00b23 [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"
David Bainbridge86971522019-09-26 22:09:39 +000021 "reflect"
Zack Williamse940c7a2019-08-21 14:25:39 -070022
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
David Bainbridge86971522019-09-26 22:09:39 +000094 // 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)
Zack Williamse940c7a2019-08-21 14:25:39 -0700104 if err != nil {
105 return nil, gvk, err
106 }
107
108 if d, ok := obj.(runtime.NestedObjectDecoder); ok {
David Bainbridge86971522019-09-26 22:09:39 +0000109 if err := d.DecodeNestedObjects(runtime.WithoutVersionDecoder{c.decoder}); err != nil {
Zack Williamse940c7a2019-08-21 14:25:39 -0700110 return nil, gvk, err
111 }
112 }
113
114 // if we specify a target, use generic conversion.
115 if into != nil {
Zack Williamse940c7a2019-08-21 14:25:39 -0700116 // perform defaulting if requested
117 if c.defaulter != nil {
118 // create a copy to ensure defaulting is not applied to the original versioned objects
119 if isVersioned {
120 versioned.Objects = []runtime.Object{obj.DeepCopyObject()}
121 }
122 c.defaulter.Default(obj)
123 } else {
124 if isVersioned {
125 versioned.Objects = []runtime.Object{obj}
126 }
127 }
128
David Bainbridge86971522019-09-26 22:09:39 +0000129 // Short-circuit conversion if the into object is same object
130 if into == obj {
131 if isVersioned {
132 return versioned, gvk, nil
133 }
134 return into, gvk, nil
135 }
136
Zack Williamse940c7a2019-08-21 14:25:39 -0700137 if err := c.convertor.Convert(obj, into, c.decodeVersion); err != nil {
138 return nil, gvk, err
139 }
140
141 if isVersioned {
142 versioned.Objects = append(versioned.Objects, into)
143 return versioned, gvk, nil
144 }
145 return into, gvk, nil
146 }
147
148 // Convert if needed.
149 if isVersioned {
150 // create a copy, because ConvertToVersion does not guarantee non-mutation of objects
151 versioned.Objects = []runtime.Object{obj.DeepCopyObject()}
152 }
153
154 // perform defaulting if requested
155 if c.defaulter != nil {
156 c.defaulter.Default(obj)
157 }
158
159 out, err := c.convertor.ConvertToVersion(obj, c.decodeVersion)
160 if err != nil {
161 return nil, gvk, err
162 }
163 if isVersioned {
164 if versioned.Last() != out {
165 versioned.Objects = append(versioned.Objects, out)
166 }
167 return versioned, gvk, nil
168 }
169 return out, gvk, nil
170}
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.
174func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
175 switch obj := obj.(type) {
176 case *runtime.Unknown:
177 return c.encoder.Encode(obj, w)
178 case runtime.Unstructured:
179 // An unstructured list can contain objects of multiple group version kinds. don't short-circuit just
180 // because the top-level type matches our desired destination type. actually send the object to the converter
181 // to give it a chance to convert the list items if needed.
182 if _, ok := obj.(*unstructured.UnstructuredList); !ok {
183 // 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)
184 objGVK := obj.GetObjectKind().GroupVersionKind()
185 if len(objGVK.Version) == 0 {
186 return c.encoder.Encode(obj, w)
187 }
188 targetGVK, ok := c.encodeVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{objGVK})
189 if !ok {
190 return runtime.NewNotRegisteredGVKErrForTarget(c.originalSchemeName, objGVK, c.encodeVersion)
191 }
192 if targetGVK == objGVK {
193 return c.encoder.Encode(obj, w)
194 }
195 }
196 }
197
198 gvks, isUnversioned, err := c.typer.ObjectKinds(obj)
199 if err != nil {
200 return err
201 }
202
David Bainbridge86971522019-09-26 22:09:39 +0000203 objectKind := obj.GetObjectKind()
204 old := objectKind.GroupVersionKind()
205 // restore the old GVK after encoding
206 defer objectKind.SetGroupVersionKind(old)
207
Zack Williamse940c7a2019-08-21 14:25:39 -0700208 if c.encodeVersion == nil || isUnversioned {
209 if e, ok := obj.(runtime.NestedObjectEncoder); ok {
David Bainbridge86971522019-09-26 22:09:39 +0000210 if err := e.EncodeNestedObjects(runtime.WithVersionEncoder{Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
Zack Williamse940c7a2019-08-21 14:25:39 -0700211 return err
212 }
213 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700214 objectKind.SetGroupVersionKind(gvks[0])
David Bainbridge86971522019-09-26 22:09:39 +0000215 return c.encoder.Encode(obj, w)
Zack Williamse940c7a2019-08-21 14:25:39 -0700216 }
217
218 // Perform a conversion if necessary
Zack Williamse940c7a2019-08-21 14:25:39 -0700219 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 {
David Bainbridge86971522019-09-26 22:09:39 +0000225 if err := e.EncodeNestedObjects(runtime.WithVersionEncoder{Version: c.encodeVersion, Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
Zack Williamse940c7a2019-08-21 14:25:39 -0700226 return err
227 }
228 }
229
230 // Conversion is responsible for setting the proper group, version, and kind onto the outgoing object
David Bainbridge86971522019-09-26 22:09:39 +0000231 return c.encoder.Encode(out, w)
Zack Williamse940c7a2019-08-21 14:25:39 -0700232}
233
David Bainbridge86971522019-09-26 22:09:39 +0000234// DirectEncoder was moved and renamed to runtime.WithVersionEncoder in 1.15.
235// TODO: remove in 1.16.
236type DirectEncoder = runtime.WithVersionEncoder
Zack Williamse940c7a2019-08-21 14:25:39 -0700237
David Bainbridge86971522019-09-26 22:09:39 +0000238// DirectDecoder was moved and renamed to runtime.WithoutVersionDecoder in 1.15.
239// TODO: remove in 1.16.
240type DirectDecoder = runtime.WithoutVersionDecoder