4
4
package v1
5
5
6
6
import (
7
+ "context"
7
8
"encoding/json"
8
9
"errors"
9
10
"fmt"
@@ -29,46 +30,60 @@ var (
29
30
func (r * Policy ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
30
31
return ctrl .NewWebhookManagedBy (mgr ).
31
32
For (r ).
33
+ WithValidator (& PolicyCustomValidator {}).
32
34
Complete ()
33
35
}
34
36
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
+ type PolicyCustomValidator struct {}
36
38
37
- var _ webhook.Validator = & Policy {}
39
+ // +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
40
+ var _ webhook.CustomValidator = & PolicyCustomValidator {}
38
41
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 )
42
+ // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
43
+ func (r * PolicyCustomValidator ) ValidateCreate (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
44
+ policy , ok := obj .(* Policy )
45
+ log := policylog .WithValues ("policyName" , policy .Name , "policyNamespace" , policy .Namespace )
42
46
log .V (1 ).Info ("Validate policy creation request" )
43
47
44
- err := r .validateName ()
48
+ if ! ok {
49
+ return nil , fmt .Errorf ("expected a Policy object but got %T" , obj )
50
+ }
51
+
52
+ err := policy .validateName ()
45
53
if err != nil {
46
54
return nil , err
47
55
}
48
56
49
- err = r .validateRemediationAction ()
57
+ err = policy .validateRemediationAction ()
50
58
if err != nil {
51
59
return nil , err
52
60
}
53
61
54
62
return nil , nil
55
63
}
56
64
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 )
65
+ // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
66
+ func (r * PolicyCustomValidator ) ValidateUpdate (
67
+ _ context.Context , _ , newObj runtime.Object ,
68
+ ) (admission.Warnings , error ) {
69
+ policy , ok := newObj .(* Policy )
70
+ log := policylog .WithValues ("policyName" , policy .Name , "policyNamespace" , policy .Namespace )
60
71
log .V (1 ).Info ("Validate policy update request" )
61
72
62
- err := r .validateRemediationAction ()
73
+ if ! ok {
74
+ return nil , fmt .Errorf ("expected a Policy object but got %T" , newObj )
75
+ }
76
+
77
+ err := policy .validateRemediationAction ()
63
78
if err != nil {
64
79
return nil , err
65
80
}
66
81
67
82
return nil , nil
68
83
}
69
84
70
- // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
71
- func (r * Policy ) ValidateDelete () (admission.Warnings , error ) {
85
+ // ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
86
+ func (r * PolicyCustomValidator ) ValidateDelete (_ context. Context , _ runtime. Object ) (admission.Warnings , error ) {
72
87
return nil , nil
73
88
}
74
89
0 commit comments