...

Source file src/sigs.k8s.io/controller-runtime/pkg/webhook/admission/defaulter_test.go

Documentation: sigs.k8s.io/controller-runtime/pkg/webhook/admission

     1  package admission
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  
    10  	admissionv1 "k8s.io/api/admission/v1"
    11  	"k8s.io/apimachinery/pkg/runtime"
    12  	"k8s.io/apimachinery/pkg/runtime/schema"
    13  )
    14  
    15  var _ = Describe("Defaulter Handler", func() {
    16  
    17  	It("should return ok if received delete verb in defaulter handler", func() {
    18  		obj := &TestDefaulter{}
    19  		handler := DefaultingWebhookFor(admissionScheme, obj)
    20  
    21  		resp := handler.Handle(context.TODO(), Request{
    22  			AdmissionRequest: admissionv1.AdmissionRequest{
    23  				Operation: admissionv1.Delete,
    24  				OldObject: runtime.RawExtension{
    25  					Raw: []byte("{}"),
    26  				},
    27  			},
    28  		})
    29  		Expect(resp.Allowed).Should(BeTrue())
    30  		Expect(resp.Result.Code).Should(Equal(int32(http.StatusOK)))
    31  	})
    32  
    33  })
    34  
    35  // TestDefaulter.
    36  var _ runtime.Object = &TestDefaulter{}
    37  
    38  type TestDefaulter struct {
    39  	Replica int `json:"replica,omitempty"`
    40  }
    41  
    42  var testDefaulterGVK = schema.GroupVersionKind{Group: "foo.test.org", Version: "v1", Kind: "TestDefaulter"}
    43  
    44  func (d *TestDefaulter) GetObjectKind() schema.ObjectKind { return d }
    45  func (d *TestDefaulter) DeepCopyObject() runtime.Object {
    46  	return &TestDefaulter{
    47  		Replica: d.Replica,
    48  	}
    49  }
    50  
    51  func (d *TestDefaulter) GroupVersionKind() schema.GroupVersionKind {
    52  	return testDefaulterGVK
    53  }
    54  
    55  func (d *TestDefaulter) SetGroupVersionKind(gvk schema.GroupVersionKind) {}
    56  
    57  var _ runtime.Object = &TestDefaulterList{}
    58  
    59  type TestDefaulterList struct{}
    60  
    61  func (*TestDefaulterList) GetObjectKind() schema.ObjectKind { return nil }
    62  func (*TestDefaulterList) DeepCopyObject() runtime.Object   { return nil }
    63  
    64  func (d *TestDefaulter) Default() {
    65  	if d.Replica < 2 {
    66  		d.Replica = 2
    67  	}
    68  }
    69  

View as plain text