@@ -23,6 +23,9 @@ var resourceQuota []byte
2323//go:embed testdata/serviceaccountWithDummySecrets.yaml
2424var serviceAccountWithDummySecretsYAML []byte
2525
26+ //go:embed testdata/conflicting-subnamespace.yaml
27+ var conflictingSubnamespaceYAML []byte
28+
2629var (
2730 sealedJSON []byte
2831 k8sMinorVersion int
@@ -292,6 +295,51 @@ var _ = Describe("kubectl accurate", func() {
292295 Expect (err ).To (HaveOccurred ())
293296 })
294297
298+ It ("should (re)create sub-namespace when conflicting namespace deleted" , func () {
299+ By ("preparing namespaces" )
300+ kubectlSafe (nil , "create" , "ns" , "conflict-root1" )
301+ kubectlSafe (nil , "accurate" , "ns" , "set-type" , "conflict-root1" , "root" )
302+ kubectlSafe (nil , "create" , "ns" , "conflict-sub1" )
303+
304+ By ("creating conflicting subnamespace" )
305+ // Cannot use "kubectl accurate" here, since conflict is validated client-side.
306+ kubectlSafe (conflictingSubnamespaceYAML , "apply" , "-f" , "-" )
307+ var conditions []metav1.Condition
308+ Eventually (func () ([]metav1.Condition , error ) {
309+ out , err := kubectl (nil , "get" , "-n" , "conflict-root1" , "subnamespaces" , "conflict-sub1" , "-o" , "json" )
310+ if err != nil {
311+ return nil , err
312+ }
313+ sn := & accuratev2.SubNamespace {}
314+ if err := json .Unmarshal (out , sn ); err != nil {
315+ return nil , err
316+ }
317+ conditions = sn .Status .Conditions
318+ return conditions , nil
319+ }).Should (HaveLen (1 ))
320+ Expect (conditions [0 ].Reason ).To (Equal ("Conflict" ))
321+
322+ By ("deleting conflicting namespace" )
323+ kubectlSafe (nil , "delete" , "ns" , "conflict-sub1" )
324+ Eventually (func () ([]metav1.Condition , error ) {
325+ out , err := kubectl (nil , "get" , "-n" , "conflict-root1" , "subnamespaces" , "conflict-sub1" , "-o" , "json" )
326+ if err != nil {
327+ return nil , err
328+ }
329+ sn := & accuratev2.SubNamespace {}
330+ if err := json .Unmarshal (out , sn ); err != nil {
331+ return nil , err
332+ }
333+ return sn .Status .Conditions , nil
334+ }).Should (BeEmpty ())
335+ out , err := kubectl (nil , "get" , "ns" , "conflict-sub1" , "-o" , "json" )
336+ Expect (err ).NotTo (HaveOccurred ())
337+ sn := & corev1.Namespace {}
338+ err = json .Unmarshal (out , sn )
339+ Expect (err ).NotTo (HaveOccurred ())
340+ Expect (sn .Labels ).To (HaveKeyWithValue (constants .LabelParent , "conflict-root1" ))
341+ })
342+
295343 It ("should propagate ServiceAccount w/o secrets field" , func () {
296344 // From Kubernetes 1.24, the auto-generation of secret-based service account tokens has been disabled by default.
297345 // So the secrets field in the ServiceAccount is not updated. But when upgrading Kubernetes from 1.23 or lower,
0 commit comments