@@ -163,6 +163,68 @@ var _ = Describe("HRQ reconciler tests", func() {
163163 Eventually (getRQSpec (ctx , barName )).Should (equalRL ("secrets" , "6" , "pods" , "3" ))
164164 Eventually (getRQSpec (ctx , bazName )).Should (equalRL ("secrets" , "6" , "pods" , "3" ))
165165 })
166+
167+ It ("should update usages in status correctly after moving namespace out of subtree" , func () {
168+ setHRQ (ctx , fooHRQName , fooName , "secrets" , "6" , "pods" , "3" )
169+ setHRQ (ctx , barHRQName , barName , "secrets" , "100" , "cpu" , "50" )
170+ setHRQ (ctx , bazHRQName , bazName , "pods" , "1" )
171+ // Simulate the K8s ResourceQuota controller to update usages.
172+ updateRQUsage (ctx , fooName , "secrets" , "0" , "pods" , "0" )
173+ updateRQUsage (ctx , barName , "secrets" , "0" , "cpu" , "0" , "pods" , "0" )
174+ updateRQUsage (ctx , bazName , "secrets" , "0" , "pods" , "0" )
175+
176+ // Increase pods count from 0 to 1 in baz and verify that the usage is
177+ // increased in foo's HRQ but not bar's (not an ancestor of baz)
178+ updateRQUsage (ctx , bazName , "pods" , "1" )
179+ Eventually (getHRQUsed (ctx , fooName , fooHRQName )).Should (equalRL ("secrets" , "0" , "pods" , "1" ))
180+ Eventually (getHRQUsed (ctx , barName , barHRQName )).Should (equalRL ("secrets" , "0" , "cpu" , "0" ))
181+ Eventually (getHRQUsed (ctx , bazName , bazHRQName )).Should (equalRL ("pods" , "1" ))
182+
183+ // Make baz a full namespace by changing its parent to nil
184+ bazHier := GetHierarchy (ctx , bazName )
185+ bazHier .Spec .Parent = ""
186+ UpdateHierarchy (ctx , bazHier )
187+
188+ Eventually (HasChild (ctx , fooName , bazName )).ShouldNot (Equal (true ))
189+
190+ // Ensure pods usage is decreased on foo after the change in hierarchy
191+ Eventually (getHRQUsed (ctx , fooName , fooHRQName )).Should (equalRL ("secrets" , "0" , "pods" , "0" ))
192+ Eventually (getHRQUsed (ctx , barName , barHRQName )).Should (equalRL ("secrets" , "0" , "cpu" , "0" ))
193+ Eventually (getHRQUsed (ctx , bazName , bazHRQName )).Should (equalRL ("pods" , "1" ))
194+ })
195+
196+ It ("should update usages in status correctly after moving full namespace with limits into hierarchy" , func () {
197+ // Make bar a full namespace by changing its parent to nil
198+ barHier := GetHierarchy (ctx , barName )
199+ barHier .Spec .Parent = ""
200+ UpdateHierarchy (ctx , barHier )
201+
202+ Eventually (HasChild (ctx , fooName , barName )).ShouldNot (Equal (true ))
203+
204+ setHRQ (ctx , fooHRQName , fooName , "secrets" , "6" , "pods" , "3" )
205+ setHRQ (ctx , barHRQName , barName , "secrets" , "100" , "cpu" , "50" )
206+
207+ // Simulate the K8s ResourceQuota controller to update usages.
208+ updateRQUsage (ctx , fooName , "secrets" , "0" , "pods" , "0" )
209+ updateRQUsage (ctx , barName , "secrets" , "0" , "cpu" , "0" , "pods" , "0" )
210+
211+ // Increase secrets count from 0 to 1 in bar and verify that the usage is
212+ // increased in bar's HRQ but not foo's (not an ancestor of baz)
213+ updateRQUsage (ctx , barName , "secrets" , "1" )
214+ Eventually (getHRQUsed (ctx , fooName , fooHRQName )).Should (equalRL ("secrets" , "0" , "pods" , "0" ))
215+ Eventually (getHRQUsed (ctx , barName , barHRQName )).Should (equalRL ("secrets" , "1" , "cpu" , "0" ))
216+
217+ // Make bar a full namespace by changing its parent to nil
218+ barHier = GetHierarchy (ctx , barName )
219+ barHier .Spec .Parent = fooName
220+ UpdateHierarchy (ctx , barHier )
221+
222+ Eventually (HasChild (ctx , fooName , barName )).Should (Equal (true ))
223+
224+ // Ensure secrets usage is decreased on foo after the change in hierarchy
225+ Eventually (getHRQUsed (ctx , fooName , fooHRQName )).Should (equalRL ("secrets" , "1" , "pods" , "0" ))
226+ Eventually (getHRQUsed (ctx , barName , barHRQName )).Should (equalRL ("secrets" , "1" , "cpu" , "0" ))
227+ })
166228})
167229
168230func forestSetSubtreeUsages (ns string , args ... string ) {
0 commit comments