@@ -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})
4445Expect (resp .Allowed ).Should (BeTrue ())
45- Expect (resp .Patches ).To (HaveLen (3 ))
46+ Expect (resp .Patches ).To (HaveLen (4 ))
4647Expect (resp .Patches ).To (ContainElements (
48+ jsonpatch.JsonPatchOperation {
49+ Operation : "add" ,
50+ Path : "/labels" ,
51+ Value : map [string ]any {"foo" : "bar" },
52+ },
4753jsonpatch.JsonPatchOperation {
4854Operation : "add" ,
4955Path : "/replica" ,
@@ -74,8 +80,13 @@ var _ = Describe("Defaulter Handler", func() {
7480},
7581})
7682Expect (resp .Allowed ).Should (BeTrue ())
77- Expect (resp .Patches ).To (HaveLen (2 ))
83+ Expect (resp .Patches ).To (HaveLen (3 ))
7884Expect (resp .Patches ).To (ContainElements (
85+ jsonpatch.JsonPatchOperation {
86+ Operation : "add" ,
87+ Path : "/labels" ,
88+ Value : map [string ]any {"foo" : "bar" },
89+ },
7990jsonpatch.JsonPatchOperation {
8091Operation : "add" ,
8192Path : "/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+
112125Replica int `json:"replica,omitempty"`
113126TotalReplicas 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 {
120133return & TestDefaulter {
134+ Labels : maps .Clone (d .Labels ),
121135Replica : d .Replica ,
122136TotalReplicas : d .TotalReplicas ,
123137}
@@ -141,6 +155,12 @@ type TestCustomDefaulter struct{}
141155
142156func (d * TestCustomDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
143157o := obj .(* TestDefaulter )
158+
159+ if o .Labels == nil {
160+ o .Labels = map [string ]string {}
161+ }
162+ o .Labels ["foo" ] = "bar"
163+
144164if o .Replica < 2 {
145165o .Replica = 2
146166}
0 commit comments