sigs.k8s.io/cluster-api-provider-azure@v1.14.3/api/v1beta1/azuremanagedclustertemplate_webhook.go (about) 1 /* 2 Copyright 2023 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 "k8s.io/apimachinery/pkg/runtime" 21 "k8s.io/apimachinery/pkg/util/validation/field" 22 "sigs.k8s.io/cluster-api-provider-azure/feature" 23 capifeature "sigs.k8s.io/cluster-api/feature" 24 ctrl "sigs.k8s.io/controller-runtime" 25 "sigs.k8s.io/controller-runtime/pkg/webhook" 26 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 27 ) 28 29 // SetupWebhookWithManager sets up and registers the webhook with the manager. 30 func (r *AzureManagedClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error { 31 return ctrl.NewWebhookManagedBy(mgr). 32 For(r). 33 Complete() 34 } 35 36 // +kubebuilder:webhook:verbs=update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedclustertemplates,versions=v1beta1,name=validation.azuremanagedclustertemplates.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 37 38 var _ webhook.Validator = &AzureManagedClusterTemplate{} 39 40 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. 41 func (r *AzureManagedClusterTemplate) ValidateCreate() (admission.Warnings, error) { 42 // NOTE: AzureManagedClusterTemplate relies upon MachinePools, which is behind a feature gate flag. 43 // The webhook must prevent creating new objects in case the feature flag is disabled. 44 if !feature.Gates.Enabled(capifeature.MachinePool) { 45 return nil, field.Forbidden( 46 field.NewPath("spec"), 47 "cannot be set if the Cluster API 'MachinePool' feature flag is not enabled", 48 ) 49 } 50 return nil, nil 51 } 52 53 // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. 54 func (r *AzureManagedClusterTemplate) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) { 55 return nil, nil 56 } 57 58 // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. 59 func (r *AzureManagedClusterTemplate) ValidateDelete() (admission.Warnings, error) { 60 return nil, nil 61 }