1 /* 2 Copyright 2021 The Kubernetes Authors. 3 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 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 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. 15 */ 16 17 package conversion 18 19 import ( 20 "k8s.io/apimachinery/pkg/runtime" 21 "k8s.io/apimachinery/pkg/runtime/schema" 22 "k8s.io/apimachinery/pkg/runtime/serializer" 23 ) 24 25 // Decoder knows how to decode the contents of a CRD version conversion 26 // request into a concrete object. 27 // TODO(droot): consider reusing decoder from admission pkg for this. 28 type Decoder struct { 29 codecs serializer.CodecFactory 30 } 31 32 // NewDecoder creates a Decoder given the runtime.Scheme 33 func NewDecoder(scheme *runtime.Scheme) *Decoder { 34 if scheme == nil { 35 panic("scheme should never be nil") 36 } 37 return &Decoder{codecs: serializer.NewCodecFactory(scheme)} 38 } 39 40 // Decode decodes the inlined object. 41 func (d *Decoder) Decode(content []byte) (runtime.Object, *schema.GroupVersionKind, error) { 42 deserializer := d.codecs.UniversalDeserializer() 43 return deserializer.Decode(content, nil, nil) 44 } 45 46 // DecodeInto decodes the inlined object in the into the passed-in runtime.Object. 47 func (d *Decoder) DecodeInto(content []byte, into runtime.Object) error { 48 deserializer := d.codecs.UniversalDeserializer() 49 return runtime.DecodeInto(deserializer, content, into) 50 } 51