@@ -26,11 +26,13 @@ var _ = Describe("Anchor", func() {
2626 var (
2727 fooName string
2828 barName string
29+ bazName string
2930 )
3031
3132 BeforeEach (func () {
3233 fooName = CreateNS (ctx , "foo" )
3334 barName = CreateNSName ("bar" )
35+ bazName = CreateNSName ("baz" )
3436 config .SetNamespaces ("" )
3537 })
3638
@@ -111,6 +113,122 @@ var _ = Describe("Anchor", func() {
111113 return barHier .Spec .Parent
112114 }).Should (Equal (fooName ))
113115 })
116+
117+ It ("should propagate managed labels and not unmanaged labels" , func () {
118+ Expect (config .SetManagedMeta ([]string {"legal-.*" }, nil )).Should (Succeed ())
119+
120+ // Set the managed label and confirm that it only exists on one namespace
121+ foo_anchor_bar := newAnchor (barName , fooName )
122+ foo_anchor_bar .Spec .Labels = []api.MetaKVP {
123+ {Key : "legal-label" , Value : "val-1" },
124+ {Key : "unpropagated-label" , Value : "not-allowed" },
125+ }
126+ updateAnchor (ctx , foo_anchor_bar )
127+ Eventually (GetLabel (ctx , barName , "legal-label" )).Should (Equal ("val-1" ))
128+
129+ // Add 'baz' as a child and verify the right labels are propagated
130+ bar_anchor_baz := newAnchor (bazName , barName )
131+ updateAnchor (ctx , bar_anchor_baz )
132+ Eventually (HasChild (ctx , barName , bazName )).Should (Equal (true ))
133+ Eventually (GetLabel (ctx , bazName , "legal-label" )).Should (Equal ("val-1" ))
134+
135+ // Verify that the bad label isn't propagated and that a condition is set
136+ Eventually (GetLabel (ctx , barName , "unpropagated-label" )).Should (Equal ("" ))
137+ Eventually (GetLabel (ctx , bazName , "unpropagated-label" )).Should (Equal ("" ))
138+ Eventually (HasCondition (ctx , barName , api .ConditionBadConfiguration , api .ReasonIllegalManagedLabel )).Should (Equal (true ))
139+
140+ // Remove the bad config and verify that the condition is removed
141+ foo_anchor_bar = getAnchor (ctx , fooName , barName )
142+ foo_anchor_bar .Spec .Labels = foo_anchor_bar .Spec .Labels [0 :1 ]
143+ updateAnchor (ctx , foo_anchor_bar )
144+ Eventually (HasCondition (ctx , barName , api .ConditionBadConfiguration , api .ReasonIllegalManagedLabel )).Should (Equal (false ))
145+
146+ // Change the value of the label and verify that it's propagated
147+ foo_anchor_bar = getAnchor (ctx , fooName , barName )
148+ foo_anchor_bar .Spec .Labels [0 ].Value = "second-value"
149+ updateAnchor (ctx , foo_anchor_bar )
150+ Eventually (GetLabel (ctx , barName , "legal-label" )).Should (Equal ("second-value" ))
151+
152+ //Remove label from hierarchyconfiguration and verify that the label is NOT removed
153+ barHier := GetHierarchy (ctx , barName )
154+ barHier .Spec .Labels = []api.MetaKVP {}
155+ UpdateHierarchy (ctx , barHier )
156+ Consistently (GetLabel (ctx , barName , "legal-label" )).Should (Equal ("second-value" ))
157+
158+ //Remove subnamespace-of annotation from child namespace and verify anchor is in conflict
159+ barNs := GetNamespace (ctx , barName )
160+ delete (barNs .GetAnnotations (), api .SubnamespaceOf )
161+ UpdateNamespace (ctx , barNs )
162+ Eventually (getAnchorState (ctx , fooName , barName )).Should (Equal (api .Conflict ))
163+
164+ //Delete parent anchor with labels and verify that label is not removed
165+ DeleteObject (ctx , "subnamespaceanchors" , fooName , barName )
166+ Consistently (GetLabel (ctx , barName , "legal-label" )).Should (Equal ("second-value" ))
167+
168+ //Remove label from hierarchyconfiguration and verify that label is removed
169+ barHier = GetHierarchy (ctx , barName )
170+ barHier .Spec .Labels = []api.MetaKVP {}
171+ UpdateHierarchy (ctx , barHier )
172+ Eventually (GetLabel (ctx , barName , "legal-label" )).Should (Equal ("" ))
173+ })
174+
175+ It ("should propagate managed annotations and not unmanaged annotations" , func () {
176+ Expect (config .SetManagedMeta (nil , []string {"legal-.*" })).Should (Succeed ())
177+
178+ // Set the managed annotation and confirm that it only exists on one namespace
179+ foo_anchor_bar := newAnchor (barName , fooName )
180+ foo_anchor_bar .Spec .Annotations = []api.MetaKVP {
181+ {Key : "legal-annotation" , Value : "val-1" },
182+ {Key : "unpropagated-annotation" , Value : "not-allowed" },
183+ }
184+ updateAnchor (ctx , foo_anchor_bar )
185+ Eventually (GetAnnotation (ctx , barName , "legal-annotation" )).Should (Equal ("val-1" ))
186+
187+ // Add 'baz' as a child and verify the right annotations are propagated
188+ bar_anchor_baz := newAnchor (bazName , barName )
189+ updateAnchor (ctx , bar_anchor_baz )
190+ Eventually (HasChild (ctx , barName , bazName )).Should (Equal (true ))
191+ Eventually (GetAnnotation (ctx , bazName , "legal-annotation" )).Should (Equal ("val-1" ))
192+
193+ // Verify that the bad annotation isn't propagated and that a condition is set
194+ Eventually (GetAnnotation (ctx , barName , "unpropagated-annotation" )).Should (Equal ("" ))
195+ Eventually (GetAnnotation (ctx , bazName , "unpropagated-annotation" )).Should (Equal ("" ))
196+ Eventually (HasCondition (ctx , barName , api .ConditionBadConfiguration , api .ReasonIllegalManagedAnnotation )).Should (Equal (true ))
197+
198+ // Remove the bad config and verify that the condition is removed
199+ foo_anchor_bar = getAnchor (ctx , fooName , barName )
200+ foo_anchor_bar .Spec .Annotations = foo_anchor_bar .Spec .Annotations [0 :1 ]
201+ updateAnchor (ctx , foo_anchor_bar )
202+ Eventually (HasCondition (ctx , barName , api .ConditionBadConfiguration , api .ReasonIllegalManagedAnnotation )).Should (Equal (false ))
203+
204+ // Change the value of the annotation and verify that it's propagated
205+ foo_anchor_bar = getAnchor (ctx , fooName , barName )
206+ foo_anchor_bar .Spec .Annotations [0 ].Value = "second-value"
207+ updateAnchor (ctx , foo_anchor_bar )
208+ Eventually (GetAnnotation (ctx , barName , "legal-annotation" )).Should (Equal ("second-value" ))
209+
210+ //Remove annotation from hierarchyconfiguration and verify that the annotation is NOT removed
211+ barHier := GetHierarchy (ctx , barName )
212+ barHier .Spec .Annotations = []api.MetaKVP {}
213+ UpdateHierarchy (ctx , barHier )
214+ Consistently (GetAnnotation (ctx , barName , "legal-annotation" )).Should (Equal ("second-value" ))
215+
216+ //Remove subnamespace-of annotation from child namespace and verify anchor is in conflict
217+ barNs := GetNamespace (ctx , barName )
218+ delete (barNs .GetAnnotations (), api .SubnamespaceOf )
219+ UpdateNamespace (ctx , barNs )
220+ Eventually (getAnchorState (ctx , fooName , barName )).Should (Equal (api .Conflict ))
221+
222+ //Delete parent anchor with annotations and verify that annotation is not removed
223+ DeleteObject (ctx , "subnamespaceanchors" , fooName , barName )
224+ Consistently (GetAnnotation (ctx , barName , "legal-annotation" )).Should (Equal ("second-value" ))
225+
226+ //Remove label from hierarchyconfiguration and verify that annotation is removed
227+ barHier = GetHierarchy (ctx , barName )
228+ barHier .Spec .Annotations = []api.MetaKVP {}
229+ UpdateHierarchy (ctx , barHier )
230+ Eventually (GetAnnotation (ctx , barName , "legal-annotation" )).Should (Equal ("" ))
231+ })
114232})
115233
116234func getAnchorState (ctx context.Context , pnm , nm string ) func () api.SubnamespaceAnchorState {
0 commit comments