Skip to content

Commit 8770b4d

Browse files
committed
refactor: introduce new type as Warnings
Signed-off-by: STRRL <[email protected]>
1 parent 1e1dd76 commit 8770b4d

File tree

6 files changed

+30
-25
lines changed

6 files changed

+30
-25
lines changed

examples/builtins/validatingwebhook.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"k8s.io/apimachinery/pkg/runtime"
2525

2626
logf "sigs.k8s.io/controller-runtime/pkg/log"
27+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2728
)
2829

2930
// +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 (
3233
type podValidator struct{}
3334

3435
// validate admits a pod if a specific annotation exists.
35-
func (v *podValidator) validate(ctx context.Context, obj runtime.Object) ([]string, error) {
36+
func (v *podValidator) validate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
3637
log := logf.FromContext(ctx)
3738
pod, ok := obj.(*corev1.Pod)
3839
if !ok {
@@ -52,14 +53,14 @@ func (v *podValidator) validate(ctx context.Context, obj runtime.Object) ([]stri
5253
return nil, nil
5354
}
5455

55-
func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) ([]string, error) {
56+
func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
5657
return v.validate(ctx, obj)
5758
}
5859

59-
func (v *podValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) ([]string, error) {
60+
func (v *podValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
6061
return v.validate(ctx, newObj)
6162
}
6263

63-
func (v *podValidator) ValidateDelete(ctx context.Context, obj runtime.Object) ([]string, error) {
64+
func (v *podValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
6465
return v.validate(ctx, obj)
6566
}

examples/crd/pkg/resource.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/runtime"
2626
"sigs.k8s.io/controller-runtime/pkg/webhook"
27+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2728
)
2829

2930
// ChaosPodSpec defines the desired state of ChaosPod
@@ -66,7 +67,7 @@ type ChaosPodList struct {
6667
var _ webhook.Validator = &ChaosPod{}
6768

6869
// ValidateCreate implements webhookutil.validator so a webhook will be registered for the type
69-
func (c *ChaosPod) ValidateCreate() ([]string, error) {
70+
func (c *ChaosPod) ValidateCreate() (admission.Warnings, error) {
7071
log.Info("validate create", "name", c.Name)
7172

7273
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
@@ -76,7 +77,7 @@ func (c *ChaosPod) ValidateCreate() ([]string, error) {
7677
}
7778

7879
// ValidateUpdate implements webhookutil.validator so a webhook will be registered for the type
79-
func (c *ChaosPod) ValidateUpdate(old runtime.Object) ([]string, error) {
80+
func (c *ChaosPod) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
8081
log.Info("validate update", "name", c.Name)
8182

8283
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
@@ -94,7 +95,7 @@ func (c *ChaosPod) ValidateUpdate(old runtime.Object) ([]string, error) {
9495
}
9596

9697
// ValidateDelete implements webhookutil.validator so a webhook will be registered for the type
97-
func (c *ChaosPod) ValidateDelete() ([]string, error) {
98+
func (c *ChaosPod) ValidateDelete() (admission.Warnings, error) {
9899
log.Info("validate delete", "name", c.Name)
99100

100101
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {

pkg/builder/webhook_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ func (*TestValidatorList) DeepCopyObject() runtime.Object { return nil }
749749

750750
var _ admission.Validator = &TestValidator{}
751751

752-
func (v *TestValidator) ValidateCreate() ([]string, error) {
752+
func (v *TestValidator) ValidateCreate() (admission.Warnings, error) {
753753
if v.Panic {
754754
panic("fake panic test")
755755
}
@@ -759,7 +759,7 @@ func (v *TestValidator) ValidateCreate() ([]string, error) {
759759
return nil, nil
760760
}
761761

762-
func (v *TestValidator) ValidateUpdate(old runtime.Object) ([]string, error) {
762+
func (v *TestValidator) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
763763
if v.Panic {
764764
panic("fake panic test")
765765
}
@@ -774,7 +774,7 @@ func (v *TestValidator) ValidateUpdate(old runtime.Object) ([]string, error) {
774774
return nil, nil
775775
}
776776

777-
func (v *TestValidator) ValidateDelete() ([]string, error) {
777+
func (v *TestValidator) ValidateDelete() (admission.Warnings, error) {
778778
if v.Panic {
779779
panic("fake panic test")
780780
}
@@ -824,21 +824,21 @@ func (dv *TestDefaultValidator) Default() {
824824

825825
var _ admission.Validator = &TestDefaultValidator{}
826826

827-
func (dv *TestDefaultValidator) ValidateCreate() ([]string, error) {
827+
func (dv *TestDefaultValidator) ValidateCreate() (admission.Warnings, error) {
828828
if dv.Replica < 0 {
829829
return nil, errors.New("number of replica should be greater than or equal to 0")
830830
}
831831
return nil, nil
832832
}
833833

834-
func (dv *TestDefaultValidator) ValidateUpdate(old runtime.Object) ([]string, error) {
834+
func (dv *TestDefaultValidator) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
835835
if dv.Replica < 0 {
836836
return nil, errors.New("number of replica should be greater than or equal to 0")
837837
}
838838
return nil, nil
839839
}
840840

841-
func (dv *TestDefaultValidator) ValidateDelete() ([]string, error) {
841+
func (dv *TestDefaultValidator) ValidateDelete() (admission.Warnings, error) {
842842
if dv.Replica > 0 {
843843
return nil, errors.New("number of replica should be less than or equal to 0 to delete")
844844
}
@@ -872,7 +872,7 @@ var _ admission.CustomDefaulter = &TestCustomDefaulter{}
872872

873873
type TestCustomValidator struct{}
874874

875-
func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) ([]string, error) {
875+
func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
876876
logf.FromContext(ctx).Info("Validating object")
877877
req, err := admission.RequestFromContext(ctx)
878878
if err != nil {
@@ -889,7 +889,7 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
889889
return nil, nil
890890
}
891891

892-
func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) ([]string, error) {
892+
func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
893893
logf.FromContext(ctx).Info("Validating object")
894894
req, err := admission.RequestFromContext(ctx)
895895
if err != nil {
@@ -910,7 +910,7 @@ func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj r
910910
return nil, nil
911911
}
912912

913-
func (*TestCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) ([]string, error) {
913+
func (*TestCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
914914
logf.FromContext(ctx).Info("Validating object")
915915
req, err := admission.RequestFromContext(ctx)
916916
if err != nil {

pkg/webhook/admission/fake_validator_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ type fakeValidator struct {
3535
WarningsToReturn []string
3636
}
3737

38-
func (v *fakeValidator) ValidateCreate() (warnings []string, err error) {
38+
func (v *fakeValidator) ValidateCreate() (warnings Warnings, err error) {
3939
return v.WarningsToReturn, v.ErrorToReturn
4040
}
4141

42-
func (v *fakeValidator) ValidateUpdate(old runtime.Object) (warnings []string, err error) {
42+
func (v *fakeValidator) ValidateUpdate(old runtime.Object) (warnings Warnings, err error) {
4343
return v.WarningsToReturn, v.ErrorToReturn
4444
}
4545

46-
func (v *fakeValidator) ValidateDelete() (warnings []string, err error) {
46+
func (v *fakeValidator) ValidateDelete() (warnings Warnings, err error) {
4747
return v.WarningsToReturn, v.ErrorToReturn
4848
}
4949

pkg/webhook/admission/validator.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime"
2828
)
2929

30+
// Warnings represents warning messages.
31+
type Warnings []string
32+
3033
// Validator defines functions for validating an operation.
3134
// The custom resource kind which implements this interface can validate itself.
3235
// To validate the custom resource with another specific struct, use CustomValidator instead.
@@ -36,17 +39,17 @@ type Validator interface {
3639
// ValidateCreate validates the object on creation.
3740
// The optional warnings will be added to the response as warning messages.
3841
// Return an error if the object is invalid.
39-
ValidateCreate() (warnings []string, err error)
42+
ValidateCreate() (warnings Warnings, err error)
4043

4144
// ValidateUpdate validates the object on update. The oldObj is the object before the update.
4245
// The optional warnings will be added to the response as warning messages.
4346
// Return an error if the object is invalid.
44-
ValidateUpdate(old runtime.Object) (warnings []string, err error)
47+
ValidateUpdate(old runtime.Object) (warnings Warnings, err error)
4548

4649
// ValidateDelete validates the object on deletion.
4750
// The optional warnings will be added to the response as warning messages.
4851
// Return an error if the object is invalid.
49-
ValidateDelete() (warnings []string, err error)
52+
ValidateDelete() (warnings Warnings, err error)
5053
}
5154

5255
// ValidatingWebhookFor creates a new Webhook for validating the provided type.

pkg/webhook/admission/validator_custom.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ type CustomValidator interface {
3434
// ValidateCreate validates the object on creation.
3535
// The optional warnings will be added to the response as warning messages.
3636
// Return an error if the object is invalid.
37-
ValidateCreate(ctx context.Context, obj runtime.Object) (warnings []string, err error)
37+
ValidateCreate(ctx context.Context, obj runtime.Object) (warnings Warnings, err error)
3838

3939
// ValidateUpdate validates the object on update.
4040
// The optional warnings will be added to the response as warning messages.
4141
// Return an error if the object is invalid.
42-
ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings []string, err error)
42+
ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings Warnings, err error)
4343

4444
// ValidateDelete validates the object on deletion.
4545
// The optional warnings will be added to the response as warning messages.
4646
// Return an error if the object is invalid.
47-
ValidateDelete(ctx context.Context, obj runtime.Object) (warnings []string, err error)
47+
ValidateDelete(ctx context.Context, obj runtime.Object) (warnings Warnings, err error)
4848
}
4949

5050
// WithCustomValidator creates a new Webhook for validating the provided type.

0 commit comments

Comments
 (0)