sigs.k8s.io/cluster-api-provider-azure@v1.14.3/api/v1beta1/azuremanagedcluster_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 *AzureManagedCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
    31  	return ctrl.NewWebhookManagedBy(mgr).
    32  		For(r).
    33  		Complete()
    34  }
    35  
    36  // +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedcluster,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedclusters,versions=v1beta1,name=validation.azuremanagedclusters.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
    37  
    38  var _ webhook.Validator = &AzureManagedCluster{}
    39  
    40  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
    41  func (r *AzureManagedCluster) ValidateCreate() (admission.Warnings, error) {
    42  	// NOTE: AzureManagedCluster 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  			"can be set only if the Cluster API 'MachinePool' feature flag is 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 *AzureManagedCluster) ValidateUpdate(oldRaw 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 *AzureManagedCluster) ValidateDelete() (admission.Warnings, error) {
    60  	return nil, nil
    61  }