@@ -539,11 +539,13 @@ func runTests(admissionReviewVersion string) {
539
539
// TestDefaulter.
540
540
var _ runtime.Object = & TestDefaulter {}
541
541
542
+ const testDefaulterKind = "TestDefaulter"
543
+
542
544
type TestDefaulter struct {
543
545
Replica int `json:"replica,omitempty"`
544
546
}
545
547
546
- var testDefaulterGVK = schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : "TestDefaulter" }
548
+ var testDefaulterGVK = schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : testDefaulterKind }
547
549
548
550
func (d * TestDefaulter ) GetObjectKind () schema.ObjectKind { return d }
549
551
func (d * TestDefaulter ) DeepCopyObject () runtime.Object {
@@ -574,11 +576,13 @@ func (d *TestDefaulter) Default() {
574
576
// TestValidator.
575
577
var _ runtime.Object = & TestValidator {}
576
578
579
+ const testValidatorKind = "TestValidator"
580
+
577
581
type TestValidator struct {
578
582
Replica int `json:"replica,omitempty"`
579
583
}
580
584
581
- var testValidatorGVK = schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : "TestValidator" }
585
+ var testValidatorGVK = schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : testValidatorKind }
582
586
583
587
func (v * TestValidator ) GetObjectKind () schema.ObjectKind { return v }
584
588
func (v * TestValidator ) DeepCopyObject () runtime.Object {
@@ -694,6 +698,14 @@ func (dv *TestDefaultValidator) ValidateDelete() error {
694
698
type TestCustomDefaulter struct {}
695
699
696
700
func (* TestCustomDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
701
+ req , err := admission .RequestFromContext (ctx )
702
+ if err != nil {
703
+ return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
704
+ }
705
+ if req .Kind .Kind != testDefaulterKind {
706
+ return fmt .Errorf ("expected Kind TestDefaulter got %q" , req .Kind .Kind )
707
+ }
708
+
697
709
d := obj .(* TestDefaulter ) //nolint:ifshort
698
710
if d .Replica < 2 {
699
711
d .Replica = 2
@@ -708,6 +720,14 @@ var _ admission.CustomDefaulter = &TestCustomDefaulter{}
708
720
type TestCustomValidator struct {}
709
721
710
722
func (* TestCustomValidator ) ValidateCreate (ctx context.Context , obj runtime.Object ) error {
723
+ req , err := admission .RequestFromContext (ctx )
724
+ if err != nil {
725
+ return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
726
+ }
727
+ if req .Kind .Kind != testValidatorKind {
728
+ return fmt .Errorf ("expected Kind TestValidator got %q" , req .Kind .Kind )
729
+ }
730
+
711
731
v := obj .(* TestValidator ) //nolint:ifshort
712
732
if v .Replica < 0 {
713
733
return errors .New ("number of replica should be greater than or equal to 0" )
@@ -716,6 +736,14 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
716
736
}
717
737
718
738
func (* TestCustomValidator ) ValidateUpdate (ctx context.Context , oldObj , newObj runtime.Object ) error {
739
+ req , err := admission .RequestFromContext (ctx )
740
+ if err != nil {
741
+ return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
742
+ }
743
+ if req .Kind .Kind != testValidatorKind {
744
+ return fmt .Errorf ("expected Kind TestValidator got %q" , req .Kind .Kind )
745
+ }
746
+
719
747
v := newObj .(* TestValidator )
720
748
old := oldObj .(* TestValidator ) //nolint:ifshort
721
749
if v .Replica < 0 {
@@ -728,6 +756,14 @@ func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj r
728
756
}
729
757
730
758
func (* TestCustomValidator ) ValidateDelete (ctx context.Context , obj runtime.Object ) error {
759
+ req , err := admission .RequestFromContext (ctx )
760
+ if err != nil {
761
+ return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
762
+ }
763
+ if req .Kind .Kind != testValidatorKind {
764
+ return fmt .Errorf ("expected Kind TestValidator got %q" , req .Kind .Kind )
765
+ }
766
+
731
767
v := obj .(* TestValidator ) //nolint:ifshort
732
768
if v .Replica > 0 {
733
769
return errors .New ("number of replica should be less than or equal to 0 to delete" )
0 commit comments