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