Skip to content

Commit

Permalink
refactor: introduce new type as Warnings
Browse files Browse the repository at this point in the history
Signed-off-by: STRRL <im@strrl.dev>
  • Loading branch information
STRRL committed Apr 13, 2023
1 parent 1e1dd76 commit 8770b4d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
9 changes: 5 additions & 4 deletions examples/builtins/validatingwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}
7 changes: 4 additions & 3 deletions examples/crd/pkg/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()}) {
Expand All @@ -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()}) {
Expand All @@ -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()}) {
Expand Down
18 changes: 9 additions & 9 deletions pkg/builder/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/webhook/admission/fake_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/webhook/admission/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions pkg/webhook/admission/validator_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8770b4d

Please sign in to comment.