Skip to content

Commit 6e2a023

Browse files
committed
chore: rename validator_warn_test.go to validator_test.go
and fix compilation errors Signed-off-by: STRRL <[email protected]>
1 parent 9205bab commit 6e2a023

File tree

6 files changed

+58
-61
lines changed

6 files changed

+58
-61
lines changed

examples/builtins/validatingwebhook.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,34 @@ import (
3232
type podValidator struct{}
3333

3434
// validate admits a pod if a specific annotation exists.
35-
func (v *podValidator) validate(ctx context.Context, obj runtime.Object) error {
35+
func (v *podValidator) validate(ctx context.Context, obj runtime.Object) ([]string, error) {
3636
log := logf.FromContext(ctx)
3737
pod, ok := obj.(*corev1.Pod)
3838
if !ok {
39-
return fmt.Errorf("expected a Pod but got a %T", obj)
39+
return nil, fmt.Errorf("expected a Pod but got a %T", obj)
4040
}
4141

4242
log.Info("Validating Pod")
4343
key := "example-mutating-admission-webhook"
4444
anno, found := pod.Annotations[key]
4545
if !found {
46-
return fmt.Errorf("missing annotation %s", key)
46+
return nil, fmt.Errorf("missing annotation %s", key)
4747
}
4848
if anno != "foo" {
49-
return fmt.Errorf("annotation %s did not have value %q", key, "foo")
49+
return nil, fmt.Errorf("annotation %s did not have value %q", key, "foo")
5050
}
5151

52-
return nil
52+
return nil, nil
5353
}
5454

55-
func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) error {
55+
func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) ([]string, error) {
5656
return v.validate(ctx, obj)
5757
}
5858

59-
func (v *podValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) error {
59+
func (v *podValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) ([]string, error) {
6060
return v.validate(ctx, newObj)
6161
}
6262

63-
func (v *podValidator) ValidateDelete(ctx context.Context, obj runtime.Object) error {
63+
func (v *podValidator) ValidateDelete(ctx context.Context, obj runtime.Object) ([]string, error) {
6464
return v.validate(ctx, obj)
6565
}

examples/crd/pkg/resource.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,41 +66,41 @@ type ChaosPodList struct {
6666
var _ webhook.Validator = &ChaosPod{}
6767

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

7272
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
73-
return fmt.Errorf(".spec.nextStop must be later than current time")
73+
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
7474
}
75-
return nil
75+
return nil, nil
7676
}
7777

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

8282
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
83-
return fmt.Errorf(".spec.nextStop must be later than current time")
83+
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
8484
}
8585

8686
oldC, ok := old.(*ChaosPod)
8787
if !ok {
88-
return fmt.Errorf("expect old object to be a %T instead of %T", oldC, old)
88+
return nil, fmt.Errorf("expect old object to be a %T instead of %T", oldC, old)
8989
}
9090
if c.Spec.NextStop.After(oldC.Spec.NextStop.Add(time.Hour)) {
91-
return fmt.Errorf("it is not allowed to delay.spec.nextStop for more than 1 hour")
91+
return nil, fmt.Errorf("it is not allowed to delay.spec.nextStop for more than 1 hour")
9292
}
93-
return nil
93+
return nil, nil
9494
}
9595

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

100100
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
101-
return fmt.Errorf(".spec.nextStop must be later than current time")
101+
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
102102
}
103-
return nil
103+
return nil, nil
104104
}
105105

106106
// +kubebuilder:webhook:path=/mutate-chaosapps-metamagical-io-v1-chaospod,mutating=true,failurePolicy=fail,groups=chaosapps.metamagical.io,resources=chaospods,verbs=create;update,versions=v1,name=mchaospod.kb.io

pkg/builder/webhook_test.go

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

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

752-
func (v *TestValidator) ValidateCreate() error {
752+
func (v *TestValidator) ValidateCreate() ([]string, error) {
753753
if v.Panic {
754754
panic("fake panic test")
755755
}
756756
if v.Replica < 0 {
757-
return errors.New("number of replica should be greater than or equal to 0")
757+
return nil, errors.New("number of replica should be greater than or equal to 0")
758758
}
759-
return nil
759+
return nil, nil
760760
}
761761

762-
func (v *TestValidator) ValidateUpdate(old runtime.Object) error {
762+
func (v *TestValidator) ValidateUpdate(old runtime.Object) ([]string, error) {
763763
if v.Panic {
764764
panic("fake panic test")
765765
}
766766
if v.Replica < 0 {
767-
return errors.New("number of replica should be greater than or equal to 0")
767+
return nil, errors.New("number of replica should be greater than or equal to 0")
768768
}
769769
if oldObj, ok := old.(*TestValidator); !ok {
770-
return fmt.Errorf("the old object is expected to be %T", oldObj)
770+
return nil, fmt.Errorf("the old object is expected to be %T", oldObj)
771771
} else if v.Replica < oldObj.Replica {
772-
return fmt.Errorf("new replica %v should not be fewer than old replica %v", v.Replica, oldObj.Replica)
772+
return nil, fmt.Errorf("new replica %v should not be fewer than old replica %v", v.Replica, oldObj.Replica)
773773
}
774-
return nil
774+
return nil, nil
775775
}
776776

777-
func (v *TestValidator) ValidateDelete() error {
777+
func (v *TestValidator) ValidateDelete() ([]string, error) {
778778
if v.Panic {
779779
panic("fake panic test")
780780
}
781781
if v.Replica > 0 {
782-
return errors.New("number of replica should be less than or equal to 0 to delete")
782+
return nil, errors.New("number of replica should be less than or equal to 0 to delete")
783783
}
784-
return nil
784+
return nil, nil
785785
}
786786

787787
// TestDefaultValidator.
@@ -824,25 +824,25 @@ func (dv *TestDefaultValidator) Default() {
824824

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

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

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

841-
func (dv *TestDefaultValidator) ValidateDelete() error {
841+
func (dv *TestDefaultValidator) ValidateDelete() ([]string, error) {
842842
if dv.Replica > 0 {
843-
return errors.New("number of replica should be less than or equal to 0 to delete")
843+
return nil, errors.New("number of replica should be less than or equal to 0 to delete")
844844
}
845-
return nil
845+
return nil, nil
846846
}
847847

848848
// TestCustomDefaulter.
@@ -872,59 +872,59 @@ var _ admission.CustomDefaulter = &TestCustomDefaulter{}
872872

873873
type TestCustomValidator struct{}
874874

875-
func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) error {
875+
func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) ([]string, error) {
876876
logf.FromContext(ctx).Info("Validating object")
877877
req, err := admission.RequestFromContext(ctx)
878878
if err != nil {
879-
return fmt.Errorf("expected admission.Request in ctx: %w", err)
879+
return nil, fmt.Errorf("expected admission.Request in ctx: %w", err)
880880
}
881881
if req.Kind.Kind != testValidatorKind {
882-
return fmt.Errorf("expected Kind TestValidator got %q", req.Kind.Kind)
882+
return nil, fmt.Errorf("expected Kind TestValidator got %q", req.Kind.Kind)
883883
}
884884

885885
v := obj.(*TestValidator) //nolint:ifshort
886886
if v.Replica < 0 {
887-
return errors.New("number of replica should be greater than or equal to 0")
887+
return nil, errors.New("number of replica should be greater than or equal to 0")
888888
}
889-
return nil
889+
return nil, nil
890890
}
891891

892-
func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) error {
892+
func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) ([]string, error) {
893893
logf.FromContext(ctx).Info("Validating object")
894894
req, err := admission.RequestFromContext(ctx)
895895
if err != nil {
896-
return fmt.Errorf("expected admission.Request in ctx: %w", err)
896+
return nil, fmt.Errorf("expected admission.Request in ctx: %w", err)
897897
}
898898
if req.Kind.Kind != testValidatorKind {
899-
return fmt.Errorf("expected Kind TestValidator got %q", req.Kind.Kind)
899+
return nil, fmt.Errorf("expected Kind TestValidator got %q", req.Kind.Kind)
900900
}
901901

902902
v := newObj.(*TestValidator)
903903
old := oldObj.(*TestValidator)
904904
if v.Replica < 0 {
905-
return errors.New("number of replica should be greater than or equal to 0")
905+
return nil, errors.New("number of replica should be greater than or equal to 0")
906906
}
907907
if v.Replica < old.Replica {
908-
return fmt.Errorf("new replica %v should not be fewer than old replica %v", v.Replica, old.Replica)
908+
return nil, fmt.Errorf("new replica %v should not be fewer than old replica %v", v.Replica, old.Replica)
909909
}
910-
return nil
910+
return nil, nil
911911
}
912912

913-
func (*TestCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) error {
913+
func (*TestCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) ([]string, error) {
914914
logf.FromContext(ctx).Info("Validating object")
915915
req, err := admission.RequestFromContext(ctx)
916916
if err != nil {
917-
return fmt.Errorf("expected admission.Request in ctx: %w", err)
917+
return nil, fmt.Errorf("expected admission.Request in ctx: %w", err)
918918
}
919919
if req.Kind.Kind != testValidatorKind {
920-
return fmt.Errorf("expected Kind TestValidator got %q", req.Kind.Kind)
920+
return nil, fmt.Errorf("expected Kind TestValidator got %q", req.Kind.Kind)
921921
}
922922

923923
v := obj.(*TestValidator) //nolint:ifshort
924924
if v.Replica > 0 {
925-
return errors.New("number of replica should be less than or equal to 0 to delete")
925+
return nil, errors.New("number of replica should be less than or equal to 0 to delete")
926926
}
927-
return nil
927+
return nil, nil
928928
}
929929

930930
var _ admission.CustomValidator = &TestCustomValidator{}

pkg/webhook/admission/validator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ func (h *validatingHandler) Handle(ctx context.Context, req Request) Response {
5555
if h.validator == nil {
5656
panic("validator should never be nil")
5757
}
58-
59-
ctx = NewContextWithRequest(ctx, req)
60-
6158
// Get the object in the request
6259
obj := h.validator.DeepCopyObject().(Validator)
6360

6461
var err error
6562
var warnings []string
6663

6764
switch req.Operation {
65+
case v1.Connect:
66+
// No validation for connect requests.
67+
// TODO(vincepri): Should we validate CONNECT requests? In what cases?
6868
case v1.Create:
6969
err = h.decoder.Decode(req, obj)
7070
if err != nil {

pkg/webhook/alias.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ type Defaulter = admission.Defaulter
2929
// Validator defines functions for validating an operation.
3030
type Validator = admission.Validator
3131

32-
// ValidatorWarn like Validator, but could return warnings.
33-
type ValidatorWarn = admission.ValidatorWarn
34-
3532
// CustomDefaulter defines functions for setting defaults on resources.
3633
type CustomDefaulter = admission.CustomDefaulter
3734

0 commit comments

Comments
 (0)