Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/webhook/admission/defaulter_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (h *defaulterForType) dropSchemeRemovals(r Response, original runtime.Objec
removedByScheme := sets.New(slices.DeleteFunc(patchOriginal, func(p jsonpatch.JsonPatchOperation) bool { return p.Operation != opRemove })...)

r.Patches = slices.DeleteFunc(r.Patches, func(p jsonpatch.JsonPatchOperation) bool {
return removedByScheme.Has(p)
return p.Operation == opRemove && removedByScheme.Has(p)
})

if len(r.Patches) == 0 {
Expand Down
24 changes: 22 additions & 2 deletions pkg/webhook/admission/defaulter_custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package admission

import (
"context"
"maps"
"net/http"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -42,8 +43,13 @@ var _ = Describe("Defaulter Handler", func() {
},
})
Expect(resp.Allowed).Should(BeTrue())
Expect(resp.Patches).To(HaveLen(3))
Expect(resp.Patches).To(HaveLen(4))
Expect(resp.Patches).To(ContainElements(
jsonpatch.JsonPatchOperation{
Operation: "add",
Path: "/labels",
Value: map[string]any{"foo": "bar"},
},
jsonpatch.JsonPatchOperation{
Operation: "add",
Path: "/replica",
Expand Down Expand Up @@ -74,8 +80,13 @@ var _ = Describe("Defaulter Handler", func() {
},
})
Expect(resp.Allowed).Should(BeTrue())
Expect(resp.Patches).To(HaveLen(2))
Expect(resp.Patches).To(HaveLen(3))
Expect(resp.Patches).To(ContainElements(
jsonpatch.JsonPatchOperation{
Operation: "add",
Path: "/labels",
Value: map[string]any{"foo": "bar"},
},
jsonpatch.JsonPatchOperation{
Operation: "add",
Path: "/replica",
Expand Down Expand Up @@ -109,6 +120,8 @@ var _ = Describe("Defaulter Handler", func() {
var _ runtime.Object = &TestDefaulter{}

type TestDefaulter struct {
Labels map[string]string `json:"labels,omitempty"`

Replica int `json:"replica,omitempty"`
TotalReplicas int `json:"totalReplicas,omitempty"`
}
Expand All @@ -118,6 +131,7 @@ var testDefaulterGVK = schema.GroupVersionKind{Group: "foo.test.org", Version: "
func (d *TestDefaulter) GetObjectKind() schema.ObjectKind { return d }
func (d *TestDefaulter) DeepCopyObject() runtime.Object {
return &TestDefaulter{
Labels: maps.Clone(d.Labels),
Replica: d.Replica,
TotalReplicas: d.TotalReplicas,
}
Expand All @@ -141,6 +155,12 @@ type TestCustomDefaulter struct{}

func (d *TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) error {
o := obj.(*TestDefaulter)

if o.Labels == nil {
o.Labels = map[string]string{}
}
o.Labels["foo"] = "bar"

if o.Replica < 2 {
o.Replica = 2
}
Expand Down
Loading