sigs.k8s.io/cluster-api-provider-azure@v1.17.0/api/v1alpha1/azureasomanagedcontrolplane_webhook.go (about) 1 /* 2 Copyright 2024 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 v1alpha1 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 "sigs.k8s.io/cluster-api-provider-azure/feature" 27 ctrl "sigs.k8s.io/controller-runtime" 28 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 29 ) 30 31 // SetupAzureASOManagedControlPlaneWebhookWithManager sets up and registers the webhook with the manager. 32 func SetupAzureASOManagedControlPlaneWebhookWithManager(mgr ctrl.Manager) error { 33 azureASOManagedControlPlaneWebhook := &azureASOManagedControlPlaneWebhook{} 34 return ctrl.NewWebhookManagedBy(mgr). 35 For(&AzureASOManagedControlPlane{}). 36 WithValidator(azureASOManagedControlPlaneWebhook). 37 Complete() 38 } 39 40 // azureASOManagedControlPlaneWebhook implements a validating webhook for AzureASOManagedControlPlane. 41 type azureASOManagedControlPlaneWebhook struct { 42 } 43 44 // +kubebuilder:webhook:verbs=create,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha1-azureasomanagedcontrolplane,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azureasomanagedcontrolplanes,versions=v1alpha1,name=validation.azureasomanagedcontrolplane.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 45 46 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. 47 func (ampw *azureASOManagedControlPlaneWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { 48 _, ok := obj.(*AzureASOManagedControlPlane) 49 if !ok { 50 return nil, apierrors.NewBadRequest("expected an AzureASOManagedControlPlane") 51 } 52 if !feature.Gates.Enabled(feature.ASOAPI) { 53 return nil, field.Forbidden( 54 field.NewPath("spec"), 55 fmt.Sprintf("can be set only if the %s feature flag is enabled", feature.ASOAPI), 56 ) 57 } 58 return nil, nil 59 } 60 61 // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. 62 func (ampw *azureASOManagedControlPlaneWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { 63 return nil, nil 64 } 65 66 // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. 67 func (ampw *azureASOManagedControlPlaneWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { 68 return nil, nil 69 }