Skip to content

Commit

Permalink
🐛 skip mutation handler when received deletion verb
Browse files Browse the repository at this point in the history
Signed-off-by: AnyISalIn <anyisalin@gmail.com>
  • Loading branch information
AnyISalIn committed May 16, 2022
1 parent 3f265c3 commit 1ead647
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/webhook/admission/defaulter.go
Expand Up @@ -21,6 +21,8 @@ import (
"encoding/json"
"net/http"

admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

Expand Down Expand Up @@ -56,6 +58,17 @@ func (h *mutatingHandler) Handle(ctx context.Context, req Request) Response {
panic("defaulter should never be nil")
}

// always skip when a DELETE operation received in mutation handler
// describe in https://github.com/kubernetes-sigs/controller-runtime/issues/1762
if req.Operation == admissionv1.Delete {
return Response{AdmissionResponse: admissionv1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
},
}}
}

// Get the object in the request
obj := h.defaulter.DeepCopyObject().(Defaulter)
if err := h.decoder.Decode(req, obj); err != nil {
Expand Down
68 changes: 68 additions & 0 deletions pkg/webhook/admission/defaulter_test.go
@@ -0,0 +1,68 @@
package admission

import (
"context"
"net/http"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

var _ = Describe("Defaulter Handler", func() {

It("should return ok if received delete verb in defaulter handler", func() {
obj := &TestDefaulter{}
handler := DefaultingWebhookFor(obj)

resp := handler.Handle(context.TODO(), Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: admissionv1.Delete,
OldObject: runtime.RawExtension{
Raw: []byte("{}"),
},
},
})
Expect(resp.Allowed).Should(BeTrue())
Expect(resp.Result.Code).Should(Equal(int32(http.StatusOK)))
})

})

// TestDefaulter.
var _ runtime.Object = &TestDefaulter{}

type TestDefaulter struct {
Replica int `json:"replica,omitempty"`
}

var testDefaulterGVK = schema.GroupVersionKind{Group: "foo.test.org", Version: "v1", Kind: "TestDefaulter"}

func (d *TestDefaulter) GetObjectKind() schema.ObjectKind { return d }
func (d *TestDefaulter) DeepCopyObject() runtime.Object {
return &TestDefaulter{
Replica: d.Replica,
}
}

func (d *TestDefaulter) GroupVersionKind() schema.GroupVersionKind {
return testDefaulterGVK
}

func (d *TestDefaulter) SetGroupVersionKind(gvk schema.GroupVersionKind) {}

var _ runtime.Object = &TestDefaulterList{}

type TestDefaulterList struct{}

func (*TestDefaulterList) GetObjectKind() schema.ObjectKind { return nil }
func (*TestDefaulterList) DeepCopyObject() runtime.Object { return nil }

func (d *TestDefaulter) Default() {
if d.Replica < 2 {
d.Replica = 2
}
}

0 comments on commit 1ead647

Please sign in to comment.