sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1beta1/awsclustercontrolleridentity_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 "fmt" 21 22 "github.com/google/go-cmp/cmp" 23 "github.com/pkg/errors" 24 apierrors "k8s.io/apimachinery/pkg/api/errors" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/apimachinery/pkg/util/validation/field" 28 ctrl "sigs.k8s.io/controller-runtime" 29 logf "sigs.k8s.io/controller-runtime/pkg/log" 30 "sigs.k8s.io/controller-runtime/pkg/webhook" 31 ) 32 33 // log is for logging in this package. 34 var _ = logf.Log.WithName("awsclustercontrolleridentity-resource") 35 36 func (r *AWSClusterControllerIdentity) SetupWebhookWithManager(mgr ctrl.Manager) error { 37 return ctrl.NewWebhookManagedBy(mgr). 38 For(r). 39 Complete() 40 } 41 42 // +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-awsclustercontrolleridentity,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustercontrolleridentities,versions=v1beta1,name=validation.awsclustercontrolleridentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 43 // +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-awsclustercontrolleridentity,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustercontrolleridentities,versions=v1beta1,name=default.awsclustercontrolleridentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 44 45 var ( 46 _ webhook.Validator = &AWSClusterControllerIdentity{} 47 _ webhook.Defaulter = &AWSClusterControllerIdentity{} 48 ) 49 50 // ValidateCreate will do any extra validation when creating an AWSClusterControllerIdentity. 51 func (r *AWSClusterControllerIdentity) ValidateCreate() error { 52 // Ensures AWSClusterControllerIdentity being singleton by only allowing "default" as name 53 if r.Name != AWSClusterControllerIdentityName { 54 return field.Invalid(field.NewPath("name"), 55 r.Name, "AWSClusterControllerIdentity is a singleton and only acceptable name is default") 56 } 57 58 // Validate selector parses as Selector if AllowedNameSpaces is populated 59 if r.Spec.AllowedNamespaces != nil { 60 _, err := metav1.LabelSelectorAsSelector(&r.Spec.AllowedNamespaces.Selector) 61 if err != nil { 62 return field.Invalid(field.NewPath("spec", "allowedNamespaces", "selector"), r.Spec.AllowedNamespaces.Selector, err.Error()) 63 } 64 } 65 66 return nil 67 } 68 69 // ValidateDelete allows you to add any extra validation when deleting an AWSClusterControllerIdentity. 70 func (r *AWSClusterControllerIdentity) ValidateDelete() error { 71 return nil 72 } 73 74 // ValidateUpdate will do any extra validation when updating an AWSClusterControllerIdentity. 75 func (r *AWSClusterControllerIdentity) ValidateUpdate(old runtime.Object) error { 76 oldP, ok := old.(*AWSClusterControllerIdentity) 77 if !ok { 78 return apierrors.NewBadRequest(fmt.Sprintf("expected an AWSClusterControllerIdentity but got a %T", old)) 79 } 80 81 if !cmp.Equal(r.Spec, oldP.Spec) { 82 return errors.New("AWSClusterControllerIdentity is immutable") 83 } 84 85 if r.Name != oldP.Name { 86 return field.Invalid(field.NewPath("name"), 87 r.Name, "AWSClusterControllerIdentity is a singleton and only acceptable name is default") 88 } 89 90 // Validate selector parses as Selector if AllowedNameSpaces is not nil 91 if r.Spec.AllowedNamespaces != nil { 92 _, err := metav1.LabelSelectorAsSelector(&r.Spec.AllowedNamespaces.Selector) 93 if err != nil { 94 return field.Invalid(field.NewPath("spec", "allowedNamespaces", "selectors"), r.Spec.AllowedNamespaces.Selector, err.Error()) 95 } 96 } 97 98 return nil 99 } 100 101 // Default will set default values for the AWSClusterControllerIdentity. 102 func (r *AWSClusterControllerIdentity) Default() { 103 SetDefaults_Labels(&r.ObjectMeta) 104 }