From 8770b4d3b5425b156f48c971ee140ea25f9c1bfa Mon Sep 17 00:00:00 2001 From: STRRL Date: Thu, 13 Apr 2023 22:18:44 +0800 Subject: [PATCH] refactor: introduce new type as Warnings Signed-off-by: STRRL --- examples/builtins/validatingwebhook.go | 9 +++++---- examples/crd/pkg/resource.go | 7 ++++--- pkg/builder/webhook_test.go | 18 +++++++++--------- pkg/webhook/admission/fake_validator_test.go | 6 +++--- pkg/webhook/admission/validator.go | 9 ++++++--- pkg/webhook/admission/validator_custom.go | 6 +++--- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/examples/builtins/validatingwebhook.go b/examples/builtins/validatingwebhook.go index 6e6f642bdd..1fe40f80c4 100644 --- a/examples/builtins/validatingwebhook.go +++ b/examples/builtins/validatingwebhook.go @@ -24,6 +24,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) // +kubebuilder:webhook:path=/validate-v1-pod,mutating=false,failurePolicy=fail,groups="",resources=pods,verbs=create;update,versions=v1,name=vpod.kb.io @@ -32,7 +33,7 @@ import ( type podValidator struct{} // validate admits a pod if a specific annotation exists. -func (v *podValidator) validate(ctx context.Context, obj runtime.Object) ([]string, error) { +func (v *podValidator) validate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { log := logf.FromContext(ctx) pod, ok := obj.(*corev1.Pod) if !ok { @@ -52,14 +53,14 @@ func (v *podValidator) validate(ctx context.Context, obj runtime.Object) ([]stri return nil, nil } -func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) ([]string, error) { +func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { return v.validate(ctx, obj) } -func (v *podValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) ([]string, error) { +func (v *podValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { return v.validate(ctx, newObj) } -func (v *podValidator) ValidateDelete(ctx context.Context, obj runtime.Object) ([]string, error) { +func (v *podValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { return v.validate(ctx, obj) } diff --git a/examples/crd/pkg/resource.go b/examples/crd/pkg/resource.go index 0b6581ea70..555029f5de 100644 --- a/examples/crd/pkg/resource.go +++ b/examples/crd/pkg/resource.go @@ -24,6 +24,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/webhook" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) // ChaosPodSpec defines the desired state of ChaosPod @@ -66,7 +67,7 @@ type ChaosPodList struct { var _ webhook.Validator = &ChaosPod{} // ValidateCreate implements webhookutil.validator so a webhook will be registered for the type -func (c *ChaosPod) ValidateCreate() ([]string, error) { +func (c *ChaosPod) ValidateCreate() (admission.Warnings, error) { log.Info("validate create", "name", c.Name) if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) { @@ -76,7 +77,7 @@ func (c *ChaosPod) ValidateCreate() ([]string, error) { } // ValidateUpdate implements webhookutil.validator so a webhook will be registered for the type -func (c *ChaosPod) ValidateUpdate(old runtime.Object) ([]string, error) { +func (c *ChaosPod) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { log.Info("validate update", "name", c.Name) if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) { @@ -94,7 +95,7 @@ func (c *ChaosPod) ValidateUpdate(old runtime.Object) ([]string, error) { } // ValidateDelete implements webhookutil.validator so a webhook will be registered for the type -func (c *ChaosPod) ValidateDelete() ([]string, error) { +func (c *ChaosPod) ValidateDelete() (admission.Warnings, error) { log.Info("validate delete", "name", c.Name) if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) { diff --git a/pkg/builder/webhook_test.go b/pkg/builder/webhook_test.go index bedb6b0206..fee86562bc 100644 --- a/pkg/builder/webhook_test.go +++ b/pkg/builder/webhook_test.go @@ -749,7 +749,7 @@ func (*TestValidatorList) DeepCopyObject() runtime.Object { return nil } var _ admission.Validator = &TestValidator{} -func (v *TestValidator) ValidateCreate() ([]string, error) { +func (v *TestValidator) ValidateCreate() (admission.Warnings, error) { if v.Panic { panic("fake panic test") } @@ -759,7 +759,7 @@ func (v *TestValidator) ValidateCreate() ([]string, error) { return nil, nil } -func (v *TestValidator) ValidateUpdate(old runtime.Object) ([]string, error) { +func (v *TestValidator) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { if v.Panic { panic("fake panic test") } @@ -774,7 +774,7 @@ func (v *TestValidator) ValidateUpdate(old runtime.Object) ([]string, error) { return nil, nil } -func (v *TestValidator) ValidateDelete() ([]string, error) { +func (v *TestValidator) ValidateDelete() (admission.Warnings, error) { if v.Panic { panic("fake panic test") } @@ -824,21 +824,21 @@ func (dv *TestDefaultValidator) Default() { var _ admission.Validator = &TestDefaultValidator{} -func (dv *TestDefaultValidator) ValidateCreate() ([]string, error) { +func (dv *TestDefaultValidator) ValidateCreate() (admission.Warnings, error) { if dv.Replica < 0 { return nil, errors.New("number of replica should be greater than or equal to 0") } return nil, nil } -func (dv *TestDefaultValidator) ValidateUpdate(old runtime.Object) ([]string, error) { +func (dv *TestDefaultValidator) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { if dv.Replica < 0 { return nil, errors.New("number of replica should be greater than or equal to 0") } return nil, nil } -func (dv *TestDefaultValidator) ValidateDelete() ([]string, error) { +func (dv *TestDefaultValidator) ValidateDelete() (admission.Warnings, error) { if dv.Replica > 0 { return nil, errors.New("number of replica should be less than or equal to 0 to delete") } @@ -872,7 +872,7 @@ var _ admission.CustomDefaulter = &TestCustomDefaulter{} type TestCustomValidator struct{} -func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) ([]string, error) { +func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { logf.FromContext(ctx).Info("Validating object") req, err := admission.RequestFromContext(ctx) if err != nil { @@ -889,7 +889,7 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje return nil, nil } -func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) ([]string, error) { +func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { logf.FromContext(ctx).Info("Validating object") req, err := admission.RequestFromContext(ctx) if err != nil { @@ -910,7 +910,7 @@ func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj r return nil, nil } -func (*TestCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) ([]string, error) { +func (*TestCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { logf.FromContext(ctx).Info("Validating object") req, err := admission.RequestFromContext(ctx) if err != nil { diff --git a/pkg/webhook/admission/fake_validator_test.go b/pkg/webhook/admission/fake_validator_test.go index f5bb999ace..e4acf63731 100644 --- a/pkg/webhook/admission/fake_validator_test.go +++ b/pkg/webhook/admission/fake_validator_test.go @@ -35,15 +35,15 @@ type fakeValidator struct { WarningsToReturn []string } -func (v *fakeValidator) ValidateCreate() (warnings []string, err error) { +func (v *fakeValidator) ValidateCreate() (warnings Warnings, err error) { return v.WarningsToReturn, v.ErrorToReturn } -func (v *fakeValidator) ValidateUpdate(old runtime.Object) (warnings []string, err error) { +func (v *fakeValidator) ValidateUpdate(old runtime.Object) (warnings Warnings, err error) { return v.WarningsToReturn, v.ErrorToReturn } -func (v *fakeValidator) ValidateDelete() (warnings []string, err error) { +func (v *fakeValidator) ValidateDelete() (warnings Warnings, err error) { return v.WarningsToReturn, v.ErrorToReturn } diff --git a/pkg/webhook/admission/validator.go b/pkg/webhook/admission/validator.go index ce5061d0eb..fae2c94991 100644 --- a/pkg/webhook/admission/validator.go +++ b/pkg/webhook/admission/validator.go @@ -27,6 +27,9 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) +// Warnings represents warning messages. +type Warnings []string + // Validator defines functions for validating an operation. // The custom resource kind which implements this interface can validate itself. // To validate the custom resource with another specific struct, use CustomValidator instead. @@ -36,17 +39,17 @@ type Validator interface { // ValidateCreate validates the object on creation. // The optional warnings will be added to the response as warning messages. // Return an error if the object is invalid. - ValidateCreate() (warnings []string, err error) + ValidateCreate() (warnings Warnings, err error) // ValidateUpdate validates the object on update. The oldObj is the object before the update. // The optional warnings will be added to the response as warning messages. // Return an error if the object is invalid. - ValidateUpdate(old runtime.Object) (warnings []string, err error) + ValidateUpdate(old runtime.Object) (warnings Warnings, err error) // ValidateDelete validates the object on deletion. // The optional warnings will be added to the response as warning messages. // Return an error if the object is invalid. - ValidateDelete() (warnings []string, err error) + ValidateDelete() (warnings Warnings, err error) } // ValidatingWebhookFor creates a new Webhook for validating the provided type. diff --git a/pkg/webhook/admission/validator_custom.go b/pkg/webhook/admission/validator_custom.go index 835f5dae09..72f76256ea 100644 --- a/pkg/webhook/admission/validator_custom.go +++ b/pkg/webhook/admission/validator_custom.go @@ -34,17 +34,17 @@ type CustomValidator interface { // ValidateCreate validates the object on creation. // The optional warnings will be added to the response as warning messages. // Return an error if the object is invalid. - ValidateCreate(ctx context.Context, obj runtime.Object) (warnings []string, err error) + ValidateCreate(ctx context.Context, obj runtime.Object) (warnings Warnings, err error) // ValidateUpdate validates the object on update. // The optional warnings will be added to the response as warning messages. // Return an error if the object is invalid. - ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings []string, err error) + ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings Warnings, err error) // ValidateDelete validates the object on deletion. // The optional warnings will be added to the response as warning messages. // Return an error if the object is invalid. - ValidateDelete(ctx context.Context, obj runtime.Object) (warnings []string, err error) + ValidateDelete(ctx context.Context, obj runtime.Object) (warnings Warnings, err error) } // WithCustomValidator creates a new Webhook for validating the provided type.