@@ -16,6 +16,7 @@ package admission
1616
1717import (
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() {
109120var _ runtime.Object = & TestDefaulter {}
110121
111122type 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: "
118131func (d * TestDefaulter ) GetObjectKind () schema.ObjectKind { return d }
119132func (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
142156func (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