@@ -17,6 +17,7 @@ limitations under the License.
1717package v1beta2
1818
1919import (
20+ "context"
2021 "fmt"
2122 "reflect"
2223
@@ -32,15 +33,22 @@ import (
3233
3334var managedclusterlogger = ctrl .Log .WithName ("ocimanagedcluster-resource" )
3435
36+ type OCIManagedClusterWebhook struct {}
37+
3538var (
36- _ webhook.Defaulter = & OCIManagedCluster {}
37- _ webhook.Validator = & OCIManagedCluster {}
39+ _ webhook.CustomDefaulter = & OCIManagedClusterWebhook {}
40+ _ webhook.CustomValidator = & OCIManagedClusterWebhook {}
3841)
3942
4043// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ocimanagedcluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=ocimanagedclusters,versions=v1beta2,name=validation.ocimanagedcluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
4144// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ocimanagedcluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=ocimanagedclusters,versions=v1beta2,name=default.ocimanagedcluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
4245
43- func (c * OCIManagedCluster ) Default () {
46+ func (* OCIManagedClusterWebhook ) Default (_ context.Context , obj runtime.Object ) error {
47+ c , ok := obj .(* OCIManagedCluster )
48+ if ! ok {
49+ return fmt .Errorf ("expected an OCIManagedCluster object but got %T" , c )
50+ }
51+
4452 if c .Spec .OCIResourceIdentifier == "" {
4553 c .Spec .OCIResourceIdentifier = string (uuid .NewUUID ())
4654 }
@@ -106,16 +114,25 @@ func (c *OCIManagedCluster) Default() {
106114 c .Spec .NetworkSpec .Vcn .CIDR = VcnDefaultCidr
107115 }
108116 }
117+
118+ return nil
109119}
110120
111121func (c * OCIManagedCluster ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
122+ w := new (OCIManagedClusterWebhook )
112123 return ctrl .NewWebhookManagedBy (mgr ).
113124 For (c ).
125+ WithDefaulter (w ).
126+ WithValidator (w ).
114127 Complete ()
115128}
116129
117130// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
118- func (c * OCIManagedCluster ) ValidateCreate () (admission.Warnings , error ) {
131+ func (* OCIManagedClusterWebhook ) ValidateCreate (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
132+ c , ok := obj .(* OCIManagedCluster )
133+ if ! ok {
134+ return nil , fmt .Errorf ("expected an OCIManagedCluster object but got %T" , c )
135+ }
119136 managedclusterlogger .Info ("validate create cluster" , "name" , c .Name )
120137
121138 var allErrs field.ErrorList
@@ -130,21 +147,31 @@ func (c *OCIManagedCluster) ValidateCreate() (admission.Warnings, error) {
130147}
131148
132149// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
133- func (c * OCIManagedCluster ) ValidateDelete () (admission.Warnings , error ) {
150+ func (* OCIManagedClusterWebhook ) ValidateDelete (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
151+ c , ok := obj .(* OCIManagedCluster )
152+ if ! ok {
153+ return nil , fmt .Errorf ("expected an OCIManagedCluster object but got %T" , c )
154+ }
155+
134156 managedclusterlogger .Info ("validate delete cluster" , "name" , c .Name )
135157
136158 return nil , nil
137159}
138160
139161// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
140- func (c * OCIManagedCluster ) ValidateUpdate (old runtime.Object ) (admission.Warnings , error ) {
162+ func (* OCIManagedClusterWebhook ) ValidateUpdate (_ context.Context , oldRaw , newObj runtime.Object ) (admission.Warnings , error ) {
163+ c , ok := newObj .(* OCIManagedCluster )
164+ if ! ok {
165+ return nil , fmt .Errorf ("expected an OCIManagedCluster object but got %T" , c )
166+ }
167+
141168 managedclusterlogger .Info ("validate update cluster" , "name" , c .Name )
142169
143170 var allErrs field.ErrorList
144171
145- oldCluster , ok := old .(* OCIManagedCluster )
172+ oldCluster , ok := oldRaw .(* OCIManagedCluster )
146173 if ! ok {
147- return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected an OCIManagedCluster but got a %T" , old ))
174+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected an OCIManagedCluster but got a %T" , oldRaw ))
148175 }
149176
150177 if c .Spec .Region != oldCluster .Spec .Region {
0 commit comments