k8s.io/kubernetes@v1.29.3/pkg/registry/admissionregistration/mutatingwebhookconfiguration/strategy.go (about)

     1  /*
     2  Copyright 2014 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 mutatingwebhookconfiguration
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  	"k8s.io/apiserver/pkg/storage/names"
    26  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    27  	"k8s.io/kubernetes/pkg/apis/admissionregistration"
    28  	"k8s.io/kubernetes/pkg/apis/admissionregistration/validation"
    29  )
    30  
    31  // mutatingWebhookConfigurationStrategy implements verification logic for mutatingWebhookConfiguration.
    32  type mutatingWebhookConfigurationStrategy struct {
    33  	runtime.ObjectTyper
    34  	names.NameGenerator
    35  }
    36  
    37  // Strategy is the default logic that applies when creating and updating mutatingWebhookConfiguration objects.
    38  var Strategy = mutatingWebhookConfigurationStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    39  
    40  // NamespaceScoped returns false because MutatingWebhookConfiguration is cluster-scoped resource.
    41  func (mutatingWebhookConfigurationStrategy) NamespaceScoped() bool {
    42  	return false
    43  }
    44  
    45  // PrepareForCreate clears the status of an mutatingWebhookConfiguration before creation.
    46  func (mutatingWebhookConfigurationStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    47  	ic := obj.(*admissionregistration.MutatingWebhookConfiguration)
    48  	ic.Generation = 1
    49  
    50  	admissionregistration.DropDisabledMutatingWebhookConfigurationFields(ic, nil)
    51  }
    52  
    53  // WarningsOnCreate returns warnings for the creation of the given object.
    54  func (mutatingWebhookConfigurationStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
    55  	return nil
    56  }
    57  
    58  // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
    59  func (mutatingWebhookConfigurationStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    60  	newIC := obj.(*admissionregistration.MutatingWebhookConfiguration)
    61  	oldIC := old.(*admissionregistration.MutatingWebhookConfiguration)
    62  
    63  	admissionregistration.DropDisabledMutatingWebhookConfigurationFields(newIC, oldIC)
    64  	// Any changes to the spec increment the generation number, any changes to the
    65  	// status should reflect the generation number of the corresponding object.
    66  	// See metav1.ObjectMeta description for more information on Generation.
    67  	if !reflect.DeepEqual(oldIC.Webhooks, newIC.Webhooks) {
    68  		newIC.Generation = oldIC.Generation + 1
    69  	}
    70  }
    71  
    72  // Validate validates a new mutatingWebhookConfiguration.
    73  func (mutatingWebhookConfigurationStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    74  	ic := obj.(*admissionregistration.MutatingWebhookConfiguration)
    75  	return validation.ValidateMutatingWebhookConfiguration(ic)
    76  }
    77  
    78  // Canonicalize normalizes the object after validation.
    79  func (mutatingWebhookConfigurationStrategy) Canonicalize(obj runtime.Object) {
    80  }
    81  
    82  // AllowCreateOnUpdate is true for mutatingWebhookConfiguration; this means you may create one with a PUT request.
    83  func (mutatingWebhookConfigurationStrategy) AllowCreateOnUpdate() bool {
    84  	return false
    85  }
    86  
    87  // ValidateUpdate is the default update validation for an end user.
    88  func (mutatingWebhookConfigurationStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
    89  	return validation.ValidateMutatingWebhookConfigurationUpdate(obj.(*admissionregistration.MutatingWebhookConfiguration), old.(*admissionregistration.MutatingWebhookConfiguration))
    90  }
    91  
    92  // WarningsOnUpdate returns warnings for the given update.
    93  func (mutatingWebhookConfigurationStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
    94  	return nil
    95  }
    96  
    97  // AllowUnconditionalUpdate is the default update policy for mutatingWebhookConfiguration objects. Status update should
    98  // only be allowed if version match.
    99  func (mutatingWebhookConfigurationStrategy) AllowUnconditionalUpdate() bool {
   100  	return false
   101  }