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