@@ -539,11 +539,13 @@ func runTests(admissionReviewVersion string) {
539539// TestDefaulter.
540540var _ runtime.Object = & TestDefaulter {}
541541
542+ const testDefaulterKind = "TestDefaulter"
543+
542544type TestDefaulter struct {
543545 Replica int `json:"replica,omitempty"`
544546}
545547
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 }
547549
548550func (d * TestDefaulter ) GetObjectKind () schema.ObjectKind { return d }
549551func (d * TestDefaulter ) DeepCopyObject () runtime.Object {
@@ -574,11 +576,13 @@ func (d *TestDefaulter) Default() {
574576// TestValidator.
575577var _ runtime.Object = & TestValidator {}
576578
579+ const testValidatorKind = "TestValidator"
580+
577581type TestValidator struct {
578582 Replica int `json:"replica,omitempty"`
579583}
580584
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 }
582586
583587func (v * TestValidator ) GetObjectKind () schema.ObjectKind { return v }
584588func (v * TestValidator ) DeepCopyObject () runtime.Object {
@@ -694,6 +698,14 @@ func (dv *TestDefaultValidator) ValidateDelete() error {
694698type TestCustomDefaulter struct {}
695699
696700func (* 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+
697709 d := obj .(* TestDefaulter ) //nolint:ifshort
698710 if d .Replica < 2 {
699711 d .Replica = 2
@@ -708,6 +720,14 @@ var _ admission.CustomDefaulter = &TestCustomDefaulter{}
708720type TestCustomValidator struct {}
709721
710722func (* 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+
711731 v := obj .(* TestValidator ) //nolint:ifshort
712732 if v .Replica < 0 {
713733 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
716736}
717737
718738func (* 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+
719747 v := newObj .(* TestValidator )
720748 old := oldObj .(* TestValidator ) //nolint:ifshort
721749 if v .Replica < 0 {
@@ -728,6 +756,14 @@ func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj r
728756}
729757
730758func (* 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+
731767 v := obj .(* TestValidator ) //nolint:ifshort
732768 if v .Replica > 0 {
733769 return errors .New ("number of replica should be less than or equal to 0 to delete" )
0 commit comments