Skip to content

Commit a2769fe

Browse files
Fix usage of deprecated webhook.Validator interface
Replace usage of deprecated `webhook.Validator` interface with `admission.CustomValidator`. Signed-off-by: Saeid Askari <[email protected]>
1 parent 4be7024 commit a2769fe

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

api/v1/policy_webhook.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package v1
55

66
import (
7+
"context"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -29,46 +30,61 @@ var (
2930
func (r *Policy) SetupWebhookWithManager(mgr ctrl.Manager) error {
3031
return ctrl.NewWebhookManagedBy(mgr).
3132
For(r).
33+
WithValidator(&PolicyCustomValidator{}).
3234
Complete()
3335
}
3436

35-
//+kubebuilder:webhook:path=/validate-policy-open-cluster-management-io-v1-policy,mutating=false,failurePolicy=Ignore,sideEffects=None,groups=policy.open-cluster-management.io,resources=policies,verbs=create,versions=v1,name=policy.open-cluster-management.io.webhook,admissionReviewVersions=v1
37+
// +kubebuilder:webhook:path=/validate-policy-open-cluster-management-io-v1-policy,mutating=false,failurePolicy=Ignore,sideEffects=None,groups=policy.open-cluster-management.io,resources=policies,verbs=create,versions=v1,name=policy.open-cluster-management.io.webhook,admissionReviewVersions=v1
38+
// +kubebuilder:object:generate=false
39+
type PolicyCustomValidator struct{}
3640

37-
var _ webhook.Validator = &Policy{}
41+
var _ webhook.CustomValidator = &PolicyCustomValidator{}
3842

39-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
40-
func (r *Policy) ValidateCreate() (admission.Warnings, error) {
41-
log := policylog.WithValues("policyName", r.Name, "policyNamespace", r.Namespace)
43+
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
44+
func (r *PolicyCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
45+
policy, ok := obj.(*Policy)
46+
if !ok {
47+
return nil, fmt.Errorf("expected a Policy object but got %T", obj)
48+
}
49+
50+
log := policylog.WithValues("policyName", policy.Name, "policyNamespace", policy.Namespace)
4251
log.V(1).Info("Validate policy creation request")
4352

44-
err := r.validateName()
53+
err := policy.validateName()
4554
if err != nil {
4655
return nil, err
4756
}
4857

49-
err = r.validateRemediationAction()
58+
err = policy.validateRemediationAction()
5059
if err != nil {
5160
return nil, err
5261
}
5362

5463
return nil, nil
5564
}
5665

57-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
58-
func (r *Policy) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
59-
log := policylog.WithValues("policyName", r.Name, "policyNamespace", r.Namespace)
66+
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
67+
func (r *PolicyCustomValidator) ValidateUpdate(
68+
_ context.Context, _, newObj runtime.Object,
69+
) (admission.Warnings, error) {
70+
policy, ok := newObj.(*Policy)
71+
if !ok {
72+
return nil, fmt.Errorf("expected a Policy object but got %T", newObj)
73+
}
74+
75+
log := policylog.WithValues("policyName", policy.Name, "policyNamespace", policy.Namespace)
6076
log.V(1).Info("Validate policy update request")
6177

62-
err := r.validateRemediationAction()
78+
err := policy.validateRemediationAction()
6379
if err != nil {
6480
return nil, err
6581
}
6682

6783
return nil, nil
6884
}
6985

70-
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
71-
func (r *Policy) ValidateDelete() (admission.Warnings, error) {
86+
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
87+
func (r *PolicyCustomValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
7288
return nil, nil
7389
}
7490

0 commit comments

Comments
 (0)