sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1beta1/awsclustertemplate_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/google/go-cmp/cmp" 21 apierrors "k8s.io/apimachinery/pkg/api/errors" 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/apimachinery/pkg/util/validation/field" 24 ctrl "sigs.k8s.io/controller-runtime" 25 "sigs.k8s.io/controller-runtime/pkg/webhook" 26 ) 27 28 func (r *AWSClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error { 29 return ctrl.NewWebhookManagedBy(mgr). 30 For(r). 31 Complete() 32 } 33 34 // +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-awsclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustertemplates,versions=v1beta1,name=validation.awsclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 35 // +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-awsclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustertemplates,versions=v1beta1,name=default.awsclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 36 37 var _ webhook.Defaulter = &AWSClusterTemplate{} 38 var _ webhook.Validator = &AWSClusterTemplate{} 39 40 // Default implements webhook.Defaulter so a webhook will be registered for the type. 41 func (r *AWSClusterTemplate) Default() { 42 SetObjectDefaults_AWSClusterTemplate(r) 43 } 44 45 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. 46 func (r *AWSClusterTemplate) ValidateCreate() error { 47 var allErrs field.ErrorList 48 49 allErrs = append(allErrs, r.Spec.Template.Spec.Bastion.Validate()...) 50 allErrs = append(allErrs, validateSSHKeyName(r.Spec.Template.Spec.SSHKeyName)...) 51 52 return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs) 53 } 54 55 // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. 56 func (r *AWSClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error { 57 old := oldRaw.(*AWSClusterTemplate) 58 59 if !cmp.Equal(r.Spec, old.Spec) { 60 return apierrors.NewBadRequest("AWSClusterTemplate.Spec is immutable") 61 } 62 return nil 63 } 64 65 // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. 66 func (r *AWSClusterTemplate) ValidateDelete() error { 67 return nil 68 }