Skip to content

Commit

Permalink
Merge pull request #3056 from mbobrovskyi/fix/panic-on-compare-patches
Browse files Browse the repository at this point in the history
🐛 Fix custom defaulter: compare only remove patches
  • Loading branch information
k8s-ci-robot authored Jan 3, 2025
2 parents a9b7c2d + 46319bb commit 1ac370e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit 1ac370e

Please sign in to comment.