...

Source file src/sigs.k8s.io/controller-runtime/pkg/webhook/admission/defaulter.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  	"context"
    21  	"encoding/json"
    22  	"net/http"
    23  
    24  	admissionv1 "k8s.io/api/admission/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  )
    28  
    29  // Defaulter defines functions for setting defaults on resources.
    30  // Deprecated: Ue CustomDefaulter instead.
    31  type Defaulter interface {
    32  	runtime.Object
    33  	Default()
    34  }
    35  
    36  // DefaultingWebhookFor creates a new Webhook for Defaulting the provided type.
    37  // Deprecated: Use WithCustomDefaulter instead.
    38  func DefaultingWebhookFor(scheme *runtime.Scheme, defaulter Defaulter) *Webhook {
    39  	return &Webhook{
    40  		Handler: &mutatingHandler{defaulter: defaulter, decoder: NewDecoder(scheme)},
    41  	}
    42  }
    43  
    44  type mutatingHandler struct {
    45  	defaulter Defaulter
    46  	decoder   Decoder
    47  }
    48  
    49  // Handle handles admission requests.
    50  func (h *mutatingHandler) 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  
    58  	// always skip when a DELETE operation received in mutation handler
    59  	// describe in https://github.com/kubernetes-sigs/controller-runtime/issues/1762
    60  	if req.Operation == admissionv1.Delete {
    61  		return Response{AdmissionResponse: admissionv1.AdmissionResponse{
    62  			Allowed: true,
    63  			Result: &metav1.Status{
    64  				Code: http.StatusOK,
    65  			},
    66  		}}
    67  	}
    68  
    69  	// Get the object in the request
    70  	obj := h.defaulter.DeepCopyObject().(Defaulter)
    71  	if err := h.decoder.Decode(req, obj); err != nil {
    72  		return Errored(http.StatusBadRequest, err)
    73  	}
    74  
    75  	// Default the object
    76  	obj.Default()
    77  	marshalled, err := json.Marshal(obj)
    78  	if err != nil {
    79  		return Errored(http.StatusInternalServerError, err)
    80  	}
    81  
    82  	// Create the patch
    83  	return PatchResponseFromRaw(req.Object.Raw, marshalled)
    84  }
    85  

View as plain text