@@ -17,6 +17,7 @@ limitations under the License.
1717package v1beta2
1818
1919import (
20+ "context"
2021 "fmt"
2122
2223 apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -32,21 +33,31 @@ import (
3233var _ = ctrl .Log .WithName ("awsclusterstaticidentity-resource" )
3334
3435func (r * AWSClusterStaticIdentity ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
36+ w := new (awsClusterRoleIdentityWebhook )
3537 return ctrl .NewWebhookManagedBy (mgr ).
3638 For (r ).
39+ WithValidator (w ).
40+ WithDefaulter (w ).
3741 Complete ()
3842}
3943
4044// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-awsclusterstaticidentity,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusterstaticidentities,versions=v1beta2,name=validation.awsclusterstaticidentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
4145// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-awsclusterstaticidentity,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusterstaticidentities,versions=v1beta2,name=default.awsclusterstaticidentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
4246
47+ type awsClusterStaticIdentityWebhook struct {}
48+
4349var (
44- _ webhook.Validator = & AWSClusterStaticIdentity {}
45- _ webhook.Defaulter = & AWSClusterStaticIdentity {}
50+ _ webhook.CustomValidator = & awsClusterStaticIdentityWebhook {}
51+ _ webhook.CustomDefaulter = & awsClusterStaticIdentityWebhook {}
4652)
4753
48- // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
49- func (r * AWSClusterStaticIdentity ) ValidateCreate () (admission.Warnings , error ) {
54+ // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
55+ func (_ * awsClusterStaticIdentityWebhook ) ValidateCreate (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
56+ r , ok := obj .(* AWSClusterStaticIdentity )
57+ if ! ok {
58+ return nil , fmt .Errorf ("expected an AWSClusterStaticIdentity object but got %T" , r )
59+ }
60+
5061 // Validate selector parses as Selector
5162 if r .Spec .AllowedNamespaces != nil {
5263 _ , err := metav1 .LabelSelectorAsSelector (& r .Spec .AllowedNamespaces .Selector )
@@ -58,16 +69,21 @@ func (r *AWSClusterStaticIdentity) ValidateCreate() (admission.Warnings, error)
5869 return nil , nil
5970}
6071
61- // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
62- func (r * AWSClusterStaticIdentity ) ValidateDelete () (admission.Warnings , error ) {
72+ // ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
73+ func (_ * awsClusterStaticIdentityWebhook ) ValidateDelete (_ context. Context , obj runtime. Object ) (admission.Warnings , error ) {
6374 return nil , nil
6475}
6576
66- // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
67- func (r * AWSClusterStaticIdentity ) ValidateUpdate (old runtime.Object ) (admission.Warnings , error ) {
68- oldP , ok := old .(* AWSClusterStaticIdentity )
77+ // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
78+ func (_ * awsClusterStaticIdentityWebhook ) ValidateUpdate (_ context. Context , oldObj , newObj runtime.Object ) (admission.Warnings , error ) {
79+ r , ok := newObj .(* AWSClusterStaticIdentity )
6980 if ! ok {
70- return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected an AWSClusterStaticIdentity but got a %T" , old ))
81+ return nil , fmt .Errorf ("expected an AWSClusterStaticIdentity object but got %T" , r )
82+ }
83+
84+ oldP , ok := oldObj .(* AWSClusterStaticIdentity )
85+ if ! ok {
86+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected an AWSClusterStaticIdentity but got a %T" , oldObj ))
7187 }
7288
7389 if oldP .Spec .SecretRef != r .Spec .SecretRef {
@@ -87,6 +103,11 @@ func (r *AWSClusterStaticIdentity) ValidateUpdate(old runtime.Object) (admission
87103}
88104
89105// Default should return the default AWSClusterStaticIdentity.
90- func (r * AWSClusterStaticIdentity ) Default () {
106+ func (_ * awsClusterStaticIdentityWebhook ) Default (_ context.Context , obj runtime.Object ) error {
107+ r , ok := obj .(* AWSClusterStaticIdentity )
108+ if ! ok {
109+ return fmt .Errorf ("expected an AWSClusterStaticIdentity object but got %T" , r )
110+ }
91111 SetDefaults_Labels (& r .ObjectMeta )
112+ return nil
92113}
0 commit comments