sigs.k8s.io/cluster-api-provider-azure@v1.14.3/exp/api/v1beta1/azuremachinepoolmachine_webhook.go (about) 1 /* 2 Copyright 2021 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 "github.com/pkg/errors" 21 "k8s.io/apimachinery/pkg/runtime" 22 "k8s.io/apimachinery/pkg/util/validation/field" 23 "sigs.k8s.io/cluster-api-provider-azure/feature" 24 capifeature "sigs.k8s.io/cluster-api/feature" 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 // SetupWebhookWithManager sets up and registers the webhook with the manager. 31 func (ampm *AzureMachinePoolMachine) SetupWebhookWithManager(mgr ctrl.Manager) error { 32 return ctrl.NewWebhookManagedBy(mgr). 33 For(ampm). 34 Complete() 35 } 36 37 // +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremachinepoolmachine,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azuremachinepoolmachines,versions=v1beta1,name=azuremachinepoolmachine.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 38 39 var _ webhook.Validator = &AzureMachinePoolMachine{} 40 41 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. 42 func (ampm *AzureMachinePoolMachine) ValidateCreate() (admission.Warnings, error) { 43 return nil, nil 44 } 45 46 // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. 47 func (ampm *AzureMachinePoolMachine) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { 48 // NOTE: AzureMachinePoolMachine is behind MachinePool feature gate flag; the webhook 49 // must prevent creating new objects new case the feature flag is disabled. 50 if !feature.Gates.Enabled(capifeature.MachinePool) { 51 return nil, field.Forbidden( 52 field.NewPath("spec"), 53 "can be set only if the MachinePool feature flag is enabled", 54 ) 55 } 56 57 oldMachine, ok := old.(*AzureMachinePoolMachine) 58 if !ok { 59 return nil, errors.New("expected and AzureMachinePoolMachine") 60 } 61 62 if oldMachine.Spec.ProviderID != "" && ampm.Spec.ProviderID != oldMachine.Spec.ProviderID { 63 return nil, errors.New("providerID is immutable") 64 } 65 66 return nil, nil 67 } 68 69 // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. 70 func (ampm *AzureMachinePoolMachine) ValidateDelete() (admission.Warnings, error) { 71 return nil, nil 72 }