...

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

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

     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 admission
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"errors"
    23  	"net/http"
    24  
    25  	admissionv1 "k8s.io/api/admission/v1"
    26  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  )
    30  
    31  // CustomDefaulter defines functions for setting defaults on resources.
    32  type CustomDefaulter interface {
    33  	Default(ctx context.Context, obj runtime.Object) error
    34  }
    35  
    36  // WithCustomDefaulter creates a new Webhook for a CustomDefaulter interface.
    37  func WithCustomDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter CustomDefaulter) *Webhook {
    38  	return &Webhook{
    39  		Handler: &defaulterForType{object: obj, defaulter: defaulter, decoder: NewDecoder(scheme)},
    40  	}
    41  }
    42  
    43  type defaulterForType struct {
    44  	defaulter CustomDefaulter
    45  	object    runtime.Object
    46  	decoder   Decoder
    47  }
    48  
    49  // Handle handles admission requests.
    50  func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
    51  	if h.decoder == nil {
    52  		panic("decoder should never be nil")
    53  	}
    54  	if h.defaulter == nil {
    55  		panic("defaulter should never be nil")
    56  	}
    57  	if h.object == nil {
    58  		panic("object should never be nil")
    59  	}
    60  
    61  	// Always skip when a DELETE operation received in custom mutation handler.
    62  	if req.Operation == admissionv1.Delete {
    63  		return Response{AdmissionResponse: admissionv1.AdmissionResponse{
    64  			Allowed: true,
    65  			Result: &metav1.Status{
    66  				Code: http.StatusOK,
    67  			},
    68  		}}
    69  	}
    70  
    71  	ctx = NewContextWithRequest(ctx, req)
    72  
    73  	// Get the object in the request
    74  	obj := h.object.DeepCopyObject()
    75  	if err := h.decoder.Decode(req, obj); err != nil {
    76  		return Errored(http.StatusBadRequest, err)
    77  	}
    78  
    79  	// Default the object
    80  	if err := h.defaulter.Default(ctx, obj); err != nil {
    81  		var apiStatus apierrors.APIStatus
    82  		if errors.As(err, &apiStatus) {
    83  			return validationResponseFromStatus(false, apiStatus.Status())
    84  		}
    85  		return Denied(err.Error())
    86  	}
    87  
    88  	// Create the patch
    89  	marshalled, err := json.Marshal(obj)
    90  	if err != nil {
    91  		return Errored(http.StatusInternalServerError, err)
    92  	}
    93  	return PatchResponseFromRaw(req.Object.Raw, marshalled)
    94  }
    95  

View as plain text