sigs.k8s.io/cluster-api@v1.7.1/exp/addons/internal/webhooks/clusterresourcesetbinding_webhook.go (about) 1 /* 2 Copyright 2022 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 webhooks 18 19 import ( 20 "context" 21 "fmt" 22 23 apierrors "k8s.io/apimachinery/pkg/api/errors" 24 "k8s.io/apimachinery/pkg/runtime" 25 "k8s.io/apimachinery/pkg/util/validation/field" 26 ctrl "sigs.k8s.io/controller-runtime" 27 "sigs.k8s.io/controller-runtime/pkg/webhook" 28 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 29 30 addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" 31 "sigs.k8s.io/cluster-api/feature" 32 ) 33 34 func (webhook *ClusterResourceSetBinding) SetupWebhookWithManager(mgr ctrl.Manager) error { 35 return ctrl.NewWebhookManagedBy(mgr). 36 For(&addonsv1.ClusterResourceSetBinding{}). 37 WithValidator(webhook). 38 Complete() 39 } 40 41 // +kubebuilder:webhook:verbs=create;update,path=/validate-addons-cluster-x-k8s-io-v1beta1-clusterresourcesetbinding,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=addons.cluster.x-k8s.io,resources=clusterresourcesetbindings,versions=v1beta1,name=validation.clusterresourcesetbinding.addons.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 42 43 // ClusterResourceSetBinding implements a validation webhook for ClusterResourceSetBinding. 44 type ClusterResourceSetBinding struct{} 45 46 var _ webhook.CustomValidator = &ClusterResourceSetBinding{} 47 48 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. 49 func (webhook *ClusterResourceSetBinding) ValidateCreate(_ context.Context, newObj runtime.Object) (admission.Warnings, error) { 50 newBinding, ok := newObj.(*addonsv1.ClusterResourceSetBinding) 51 if !ok { 52 return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a ClusterResourceSetBinding but got a %T", newObj)) 53 } 54 return nil, webhook.validate(nil, newBinding) 55 } 56 57 // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. 58 func (webhook *ClusterResourceSetBinding) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { 59 oldBinding, ok := oldObj.(*addonsv1.ClusterResourceSetBinding) 60 if !ok { 61 return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a ClusterResourceSetBinding but got a %T", oldObj)) 62 } 63 newBinding, ok := newObj.(*addonsv1.ClusterResourceSetBinding) 64 if !ok { 65 return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a ClusterResourceSetBinding but got a %T", newObj)) 66 } 67 return nil, webhook.validate(oldBinding, newBinding) 68 } 69 70 // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. 71 func (webhook *ClusterResourceSetBinding) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { 72 return nil, nil 73 } 74 75 func (webhook *ClusterResourceSetBinding) validate(oldCRSB, newCRSB *addonsv1.ClusterResourceSetBinding) error { 76 // NOTE: ClusterResourceSet is behind ClusterResourceSet feature gate flag; the web hook 77 // must prevent creating new objects in case the feature flag is disabled. 78 if !feature.Gates.Enabled(feature.ClusterResourceSet) { 79 return field.Forbidden( 80 field.NewPath("spec"), 81 "can be set only if the ClusterResourceSet feature flag is enabled", 82 ) 83 } 84 var allErrs field.ErrorList 85 if oldCRSB != nil && oldCRSB.Spec.ClusterName != "" && oldCRSB.Spec.ClusterName != newCRSB.Spec.ClusterName { 86 allErrs = append(allErrs, 87 field.Invalid(field.NewPath("spec", "clusterName"), newCRSB.Spec.ClusterName, "field is immutable")) 88 } 89 if len(allErrs) == 0 { 90 return nil 91 } 92 93 return apierrors.NewInvalid(addonsv1.GroupVersion.WithKind("ClusterResourceSetBinding").GroupKind(), newCRSB.Name, allErrs) 94 }