sigs.k8s.io/cluster-api-provider-azure@v1.14.3/api/v1beta1/azureclustertemplate_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 v1beta1 18 19 import ( 20 "reflect" 21 22 apierrors "k8s.io/apimachinery/pkg/api/errors" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/apimachinery/pkg/util/validation/field" 25 ctrl "sigs.k8s.io/controller-runtime" 26 "sigs.k8s.io/controller-runtime/pkg/webhook" 27 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 28 ) 29 30 // AzureClusterTemplateImmutableMsg is the message used for errors on fields that are immutable. 31 const AzureClusterTemplateImmutableMsg = "AzureClusterTemplate spec.template.spec field is immutable. Please create new resource instead. ref doc: https://cluster-api.sigs.k8s.io/tasks/experimental-features/cluster-class/change-clusterclass.html" 32 33 // SetupWebhookWithManager will set up the webhook to be managed by the specified manager. 34 func (c *AzureClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error { 35 return ctrl.NewWebhookManagedBy(mgr). 36 For(c). 37 Complete() 38 } 39 40 // +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azureclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azureclustertemplates,versions=v1beta1,name=validation.azureclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 41 // +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-azureclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azureclustertemplates,versions=v1beta1,name=default.azureclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 42 43 var _ webhook.Defaulter = &AzureClusterTemplate{} 44 45 // Default implements webhook.Defaulter so a webhook will be registered for the type. 46 func (c *AzureClusterTemplate) Default() { 47 c.setDefaults() 48 } 49 50 var _ webhook.Validator = &AzureClusterTemplate{} 51 52 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. 53 func (c *AzureClusterTemplate) ValidateCreate() (admission.Warnings, error) { 54 return c.validateClusterTemplate() 55 } 56 57 // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. 58 func (c *AzureClusterTemplate) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) { 59 var allErrs field.ErrorList 60 old := oldRaw.(*AzureClusterTemplate) 61 if !reflect.DeepEqual(c.Spec.Template.Spec, old.Spec.Template.Spec) { 62 allErrs = append(allErrs, 63 field.Invalid(field.NewPath("AzureClusterTemplate", "spec", "template", "spec"), c, AzureClusterTemplateImmutableMsg), 64 ) 65 } 66 67 if len(allErrs) == 0 { 68 return nil, nil 69 } 70 return nil, apierrors.NewInvalid(GroupVersion.WithKind(AzureClusterTemplateKind).GroupKind(), c.Name, allErrs) 71 } 72 73 // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. 74 func (c *AzureClusterTemplate) ValidateDelete() (admission.Warnings, error) { 75 return nil, nil 76 }