Skip to content

Commit 1c2a7c9

Browse files
authored
Merge pull request #2014 from STRRL/webhook-warning
⚠️ feat: new features about support warning with webhook
2 parents 3d915df + b2a9552 commit 1c2a7c9

File tree

8 files changed

+396
-202
lines changed

8 files changed

+396
-202
lines changed

examples/builtins/validatingwebhook.go

Lines changed: 9 additions & 8 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,34 +33,34 @@ 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) 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 {
39-
return fmt.Errorf("expected a Pod but got a %T", obj)
40+
return nil, fmt.Errorf("expected a Pod but got a %T", obj)
4041
}
4142

4243
log.Info("Validating Pod")
4344
key := "example-mutating-admission-webhook"
4445
anno, found := pod.Annotations[key]
4546
if !found {
46-
return fmt.Errorf("missing annotation %s", key)
47+
return nil, fmt.Errorf("missing annotation %s", key)
4748
}
4849
if anno != "foo" {
49-
return fmt.Errorf("annotation %s did not have value %q", key, "foo")
50+
return nil, fmt.Errorf("annotation %s did not have value %q", key, "foo")
5051
}
5152

52-
return nil
53+
return nil, nil
5354
}
5455

55-
func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) 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) 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) 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: 12 additions & 11 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,41 +67,41 @@ 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() 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()}) {
73-
return fmt.Errorf(".spec.nextStop must be later than current time")
74+
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
7475
}
75-
return nil
76+
return nil, nil
7677
}
7778

7879
// ValidateUpdate implements webhookutil.validator so a webhook will be registered for the type
79-
func (c *ChaosPod) ValidateUpdate(old runtime.Object) 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()}) {
83-
return fmt.Errorf(".spec.nextStop must be later than current time")
84+
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
8485
}
8586

8687
oldC, ok := old.(*ChaosPod)
8788
if !ok {
88-
return fmt.Errorf("expect old object to be a %T instead of %T", oldC, old)
89+
return nil, fmt.Errorf("expect old object to be a %T instead of %T", oldC, old)
8990
}
9091
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")
92+
return nil, fmt.Errorf("it is not allowed to delay.spec.nextStop for more than 1 hour")
9293
}
93-
return nil
94+
return nil, nil
9495
}
9596

9697
// ValidateDelete implements webhookutil.validator so a webhook will be registered for the type
97-
func (c *ChaosPod) ValidateDelete() 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()}) {
101-
return fmt.Errorf(".spec.nextStop must be later than current time")
102+
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
102103
}
103-
return nil
104+
return nil, nil
104105
}
105106

106107
// +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() (admission.Warnings, 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) (admission.Warnings, 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() (admission.Warnings, 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() (admission.Warnings, 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) (admission.Warnings, 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() (admission.Warnings, 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) (admission.Warnings, 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) (admission.Warnings, 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) (admission.Warnings, 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/admissiontest/doc.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pkg/webhook/admission/admissiontest/util.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)