@@ -80,3 +80,135 @@ var _ = Describe("stateSkel", func() {
8080 })
8181 })
8282})
83+
84+ var _ = Describe ("SetConfigHashAnnotation" , func () {
85+ Context ("when configHash is empty" , func () {
86+ It ("should not modify any objects" , func () {
87+ ds := createTestDaemonSet ("test-ds" )
88+ objs := []* unstructured.Unstructured {ds }
89+ err := SetConfigHashAnnotation (objs , "" )
90+ Expect (err ).NotTo (HaveOccurred ())
91+
92+ annotations , found , err := unstructured .NestedStringMap (ds .Object ,
93+ "spec" , "template" , "metadata" , "annotations" )
94+ Expect (err ).NotTo (HaveOccurred ())
95+ Expect (found ).To (BeFalse ())
96+ Expect (annotations ).To (BeNil ())
97+ })
98+ })
99+
100+ Context ("when configHash is provided" , func () {
101+ It ("should add annotation to DaemonSet pod template" , func () {
102+ ds := createTestDaemonSet ("test-ds" )
103+ objs := []* unstructured.Unstructured {ds }
104+ configHash := "abc123hash"
105+
106+ err := SetConfigHashAnnotation (objs , configHash )
107+ Expect (err ).NotTo (HaveOccurred ())
108+
109+ annotations , found , err := unstructured .NestedStringMap (ds .Object ,
110+ "spec" , "template" , "metadata" , "annotations" )
111+ Expect (err ).NotTo (HaveOccurred ())
112+ Expect (found ).To (BeTrue ())
113+ Expect (annotations [consts .ConfigHashAnnotation ]).To (Equal (configHash ))
114+ })
115+
116+ It ("should preserve existing annotations" , func () {
117+ ds := createTestDaemonSet ("test-ds" )
118+ // Add existing annotation
119+ err := unstructured .SetNestedStringMap (ds .Object ,
120+ map [string ]string {"existing" : "annotation" },
121+ "spec" , "template" , "metadata" , "annotations" )
122+ Expect (err ).NotTo (HaveOccurred ())
123+
124+ objs := []* unstructured.Unstructured {ds }
125+ configHash := "abc123hash"
126+
127+ err = SetConfigHashAnnotation (objs , configHash )
128+ Expect (err ).NotTo (HaveOccurred ())
129+
130+ annotations , found , err := unstructured .NestedStringMap (ds .Object ,
131+ "spec" , "template" , "metadata" , "annotations" )
132+ Expect (err ).NotTo (HaveOccurred ())
133+ Expect (found ).To (BeTrue ())
134+ Expect (annotations ["existing" ]).To (Equal ("annotation" ))
135+ Expect (annotations [consts .ConfigHashAnnotation ]).To (Equal (configHash ))
136+ })
137+
138+ It ("should not modify non-DaemonSet objects" , func () {
139+ configMap := & unstructured.Unstructured {
140+ Object : map [string ]interface {}{
141+ "apiVersion" : "v1" ,
142+ "kind" : "ConfigMap" ,
143+ "metadata" : map [string ]interface {}{
144+ "name" : "test-cm" ,
145+ "namespace" : "test-ns" ,
146+ },
147+ },
148+ }
149+ objs := []* unstructured.Unstructured {configMap }
150+ configHash := "abc123hash"
151+
152+ err := SetConfigHashAnnotation (objs , configHash )
153+ Expect (err ).NotTo (HaveOccurred ())
154+
155+ // ConfigMap should not have the annotation path
156+ _ , found , _ := unstructured .NestedStringMap (configMap .Object ,
157+ "spec" , "template" , "metadata" , "annotations" )
158+ Expect (found ).To (BeFalse ())
159+ })
160+
161+ It ("should handle multiple DaemonSets" , func () {
162+ ds1 := createTestDaemonSet ("test-ds-1" )
163+ ds2 := createTestDaemonSet ("test-ds-2" )
164+ objs := []* unstructured.Unstructured {ds1 , ds2 }
165+ configHash := "abc123hash"
166+
167+ err := SetConfigHashAnnotation (objs , configHash )
168+ Expect (err ).NotTo (HaveOccurred ())
169+
170+ for _ , ds := range objs {
171+ annotations , found , err := unstructured .NestedStringMap (ds .Object ,
172+ "spec" , "template" , "metadata" , "annotations" )
173+ Expect (err ).NotTo (HaveOccurred ())
174+ Expect (found ).To (BeTrue ())
175+ Expect (annotations [consts .ConfigHashAnnotation ]).To (Equal (configHash ))
176+ }
177+ })
178+ })
179+ })
180+
181+ func createTestDaemonSet (name string ) * unstructured.Unstructured {
182+ return & unstructured.Unstructured {
183+ Object : map [string ]interface {}{
184+ "apiVersion" : "apps/v1" ,
185+ "kind" : "DaemonSet" ,
186+ "metadata" : map [string ]interface {}{
187+ "name" : name ,
188+ "namespace" : "test-ns" ,
189+ },
190+ "spec" : map [string ]interface {}{
191+ "selector" : map [string ]interface {}{
192+ "matchLabels" : map [string ]interface {}{
193+ "app" : name ,
194+ },
195+ },
196+ "template" : map [string ]interface {}{
197+ "metadata" : map [string ]interface {}{
198+ "labels" : map [string ]interface {}{
199+ "app" : name ,
200+ },
201+ },
202+ "spec" : map [string ]interface {}{
203+ "containers" : []interface {}{
204+ map [string ]interface {}{
205+ "name" : "test-container" ,
206+ "image" : "test-image" ,
207+ },
208+ },
209+ },
210+ },
211+ },
212+ },
213+ }
214+ }
0 commit comments