sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1beta1/awsclusterstaticidentity_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("awsclusterstaticidentity-resource") 33 34 func (r *AWSClusterStaticIdentity) 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-awsclusterstaticidentity,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusterstaticidentities,versions=v1beta1,name=validation.awsclusterstaticidentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 41 // +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-awsclusterstaticidentity,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusterstaticidentities,versions=v1beta1,name=default.awsclusterstaticidentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 42 43 var ( 44 _ webhook.Validator = &AWSClusterStaticIdentity{} 45 _ webhook.Defaulter = &AWSClusterStaticIdentity{} 46 ) 47 48 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. 49 func (r *AWSClusterStaticIdentity) ValidateCreate() error { 50 // Validate selector parses as Selector 51 if r.Spec.AllowedNamespaces != nil { 52 _, err := metav1.LabelSelectorAsSelector(&r.Spec.AllowedNamespaces.Selector) 53 if err != nil { 54 return field.Invalid(field.NewPath("spec", "allowedNamespaces", "selector"), r.Spec.AllowedNamespaces.Selector, err.Error()) 55 } 56 } 57 58 return nil 59 } 60 61 // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. 62 func (r *AWSClusterStaticIdentity) ValidateDelete() error { 63 return nil 64 } 65 66 // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. 67 func (r *AWSClusterStaticIdentity) ValidateUpdate(old runtime.Object) error { 68 oldP, ok := old.(*AWSClusterStaticIdentity) 69 if !ok { 70 return apierrors.NewBadRequest(fmt.Sprintf("expected an AWSClusterStaticIdentity but got a %T", old)) 71 } 72 73 if oldP.Spec.SecretRef != r.Spec.SecretRef { 74 return field.Invalid(field.NewPath("spec", "secretRef"), 75 r.Spec.SecretRef, "field cannot be updated") 76 } 77 78 // Validate selector parses as Selector 79 if r.Spec.AllowedNamespaces != nil { 80 _, err := metav1.LabelSelectorAsSelector(&r.Spec.AllowedNamespaces.Selector) 81 if err != nil { 82 return field.Invalid(field.NewPath("spec", "allowedNamespaces", "selector"), r.Spec.AllowedNamespaces.Selector, err.Error()) 83 } 84 } 85 86 return nil 87 } 88 89 // Default should return the default AWSClusterStaticIdentity. 90 func (r *AWSClusterStaticIdentity) Default() { 91 SetDefaults_Labels(&r.ObjectMeta) 92 }