Skip to content

Commit 1ac370e

Browse files
authored
Merge pull request #3056 from mbobrovskyi/fix/panic-on-compare-patches
🐛 Fix custom defaulter: compare only remove patches
2 parents a9b7c2d + 46319bb commit 1ac370e

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

pkg/webhook/admission/defaulter_custom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (h *defaulterForType) dropSchemeRemovals(r Response, original runtime.Objec
155155
removedByScheme := sets.New(slices.DeleteFunc(patchOriginal, func(p jsonpatch.JsonPatchOperation) bool { return p.Operation != opRemove })...)
156156

157157
r.Patches = slices.DeleteFunc(r.Patches, func(p jsonpatch.JsonPatchOperation) bool {
158-
return removedByScheme.Has(p)
158+
return p.Operation == opRemove && removedByScheme.Has(p)
159159
})
160160

161161
if len(r.Patches) == 0 {

pkg/webhook/admission/defaulter_custom_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package admission
1616

1717
import (
1818
"context"
19+
"maps"
1920
"net/http"
2021

2122
. "github.com/onsi/ginkgo/v2"
@@ -42,8 +43,13 @@ var _ = Describe("Defaulter Handler", func() {
4243
},
4344
})
4445
Expect(resp.Allowed).Should(BeTrue())
45-
Expect(resp.Patches).To(HaveLen(3))
46+
Expect(resp.Patches).To(HaveLen(4))
4647
Expect(resp.Patches).To(ContainElements(
48+
jsonpatch.JsonPatchOperation{
49+
Operation: "add",
50+
Path: "/labels",
51+
Value: map[string]any{"foo": "bar"},
52+
},
4753
jsonpatch.JsonPatchOperation{
4854
Operation: "add",
4955
Path: "/replica",
@@ -74,8 +80,13 @@ var _ = Describe("Defaulter Handler", func() {
7480
},
7581
})
7682
Expect(resp.Allowed).Should(BeTrue())
77-
Expect(resp.Patches).To(HaveLen(2))
83+
Expect(resp.Patches).To(HaveLen(3))
7884
Expect(resp.Patches).To(ContainElements(
85+
jsonpatch.JsonPatchOperation{
86+
Operation: "add",
87+
Path: "/labels",
88+
Value: map[string]any{"foo": "bar"},
89+
},
7990
jsonpatch.JsonPatchOperation{
8091
Operation: "add",
8192
Path: "/replica",
@@ -109,6 +120,8 @@ var _ = Describe("Defaulter Handler", func() {
109120
var _ runtime.Object = &TestDefaulter{}
110121

111122
type TestDefaulter struct {
123+
Labels map[string]string `json:"labels,omitempty"`
124+
112125
Replica int `json:"replica,omitempty"`
113126
TotalReplicas int `json:"totalReplicas,omitempty"`
114127
}
@@ -118,6 +131,7 @@ var testDefaulterGVK = schema.GroupVersionKind{Group: "foo.test.org", Version: "
118131
func (d *TestDefaulter) GetObjectKind() schema.ObjectKind { return d }
119132
func (d *TestDefaulter) DeepCopyObject() runtime.Object {
120133
return &TestDefaulter{
134+
Labels: maps.Clone(d.Labels),
121135
Replica: d.Replica,
122136
TotalReplicas: d.TotalReplicas,
123137
}
@@ -141,6 +155,12 @@ type TestCustomDefaulter struct{}
141155

142156
func (d *TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) error {
143157
o := obj.(*TestDefaulter)
158+
159+
if o.Labels == nil {
160+
o.Labels = map[string]string{}
161+
}
162+
o.Labels["foo"] = "bar"
163+
144164
if o.Replica < 2 {
145165
o.Replica = 2
146166
}

0 commit comments

Comments
 (0)