...

Source file src/sigs.k8s.io/controller-runtime/pkg/webhook/admission/decode.go

Documentation: sigs.k8s.io/controller-runtime/pkg/webhook/admission

     1  /*
     2  Copyright 2018 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 admission
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  	"k8s.io/apimachinery/pkg/runtime/serializer"
    24  	"k8s.io/apimachinery/pkg/util/json"
    25  )
    26  
    27  // Decoder knows how to decode the contents of an admission
    28  // request into a concrete object.
    29  type Decoder interface {
    30  	// Decode decodes the inlined object in the AdmissionRequest into the passed-in runtime.Object.
    31  	// If you want decode the OldObject in the AdmissionRequest, use DecodeRaw.
    32  	// It errors out if req.Object.Raw is empty i.e. containing 0 raw bytes.
    33  	Decode(req Request, into runtime.Object) error
    34  
    35  	// DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.
    36  	// It errors out if rawObj is empty i.e. containing 0 raw bytes.
    37  	DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error
    38  }
    39  
    40  // decoder knows how to decode the contents of an admission
    41  // request into a concrete object.
    42  type decoder struct {
    43  	codecs serializer.CodecFactory
    44  }
    45  
    46  // NewDecoder creates a decoder given the runtime.Scheme.
    47  func NewDecoder(scheme *runtime.Scheme) Decoder {
    48  	if scheme == nil {
    49  		panic("scheme should never be nil")
    50  	}
    51  	return &decoder{codecs: serializer.NewCodecFactory(scheme)}
    52  }
    53  
    54  // Decode decodes the inlined object in the AdmissionRequest into the passed-in runtime.Object.
    55  // If you want decode the OldObject in the AdmissionRequest, use DecodeRaw.
    56  // It errors out if req.Object.Raw is empty i.e. containing 0 raw bytes.
    57  func (d *decoder) Decode(req Request, into runtime.Object) error {
    58  	// we error out if rawObj is an empty object.
    59  	if len(req.Object.Raw) == 0 {
    60  		return fmt.Errorf("there is no content to decode")
    61  	}
    62  	return d.DecodeRaw(req.Object, into)
    63  }
    64  
    65  // DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.
    66  // It errors out if rawObj is empty i.e. containing 0 raw bytes.
    67  func (d *decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error {
    68  	// NB(directxman12): there's a bug/weird interaction between decoders and
    69  	// the API server where the API server doesn't send a GVK on the embedded
    70  	// objects, which means the unstructured decoder refuses to decode.  It
    71  	// also means we can't pass the unstructured directly in, since it'll try
    72  	// and call unstructured's special Unmarshal implementation, which calls
    73  	// back into that same decoder :-/
    74  	// See kubernetes/kubernetes#74373.
    75  
    76  	// we error out if rawObj is an empty object.
    77  	if len(rawObj.Raw) == 0 {
    78  		return fmt.Errorf("there is no content to decode")
    79  	}
    80  	if unstructuredInto, isUnstructured := into.(runtime.Unstructured); isUnstructured {
    81  		// unmarshal into unstructured's underlying object to avoid calling the decoder
    82  		var object map[string]interface{}
    83  		if err := json.Unmarshal(rawObj.Raw, &object); err != nil {
    84  			return err
    85  		}
    86  		unstructuredInto.SetUnstructuredContent(object)
    87  		return nil
    88  	}
    89  
    90  	deserializer := d.codecs.UniversalDeserializer()
    91  	return runtime.DecodeInto(deserializer, rawObj.Raw, into)
    92  }
    93  

View as plain text