@@ -23,6 +23,7 @@ import (
2323 . "github.com/onsi/ginkgo/v2"
2424 . "github.com/onsi/gomega"
2525 corev1 "k8s.io/api/core/v1"
26+ "k8s.io/apimachinery/pkg/api/errors"
2627 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728 "k8s.io/apimachinery/pkg/types"
2829 "k8s.io/apimachinery/pkg/util/intstr"
@@ -77,11 +78,21 @@ var _ = Describe("validating CRP when resources exists", Ordered, func() {
7778 })
7879
7980 AfterAll (func () {
81+ By ("deleting created work resources on member cluster" )
82+ cleanWorkResourcesOnCluster (allMemberClusters [0 ])
83+
8084 By (fmt .Sprintf ("deleting placement %s" , crpName ))
8185 cleanupCRP (crpName )
8286
83- By ("deleting created work resources on member cluster" )
84- cleanWorkResourcesOnCluster (allMemberClusters [0 ])
87+ // Check if work is deleted. Needed to ensure that the Work resource is cleaned up before the next CRP is created.
88+ // This is because the Work resource is created with a finalizer that blocks deletion until the all applied work
89+ // and applied work itself is successfully deleted. If the Work resource is not deleted, it can cause resource overlap
90+ // and flakiness in subsequent tests.
91+ By ("Check if work is deleted" )
92+ Eventually (func () bool {
93+ work := & placementv1beta1.Work {}
94+ return errors .IsNotFound (hubClient .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("%s-work" , crpName ), Namespace : fmt .Sprintf ("fleet-member-%s" , allMemberClusterNames [0 ])}, work ))
95+ }, 6 * eventuallyDuration , eventuallyInterval ).Should (BeTrue (), "Work resource should be deleted from hub" )
8596 })
8697
8798 It ("should update CRP status as expected" , func () {
@@ -119,7 +130,7 @@ var _ = Describe("validating CRP when resources exists", Ordered, func() {
119130 It ("namespace should be kept on member cluster" , func () {
120131 Consistently (func () error {
121132 ns := & corev1.Namespace {}
122- return allMemberClusters [ 0 ]. KubeClient .Get (ctx , types.NamespacedName {Name : workNamespaceName }, ns )
133+ return hubClient .Get (ctx , types.NamespacedName {Name : workNamespaceName }, ns )
123134 }, consistentlyDuration , consistentlyInterval ).Should (Succeed (), "Namespace which is not owned by the CRP should not be deleted" )
124135 })
125136 })
@@ -139,6 +150,16 @@ var _ = Describe("validating CRP when resources exists", Ordered, func() {
139150 })
140151
141152 AfterAll (func () {
153+ // Check if work is deleted. Needed to ensure that the Work resource is cleaned up before the next CRP is created.
154+ // This is because the Work resource is created with a finalizer that blocks deletion until the all applied work
155+ // and applied work itself is successfully deleted. If the Work resource is not deleted, it can cause resource overlap
156+ // and flakiness in subsequent tests.
157+ By ("Check if work is deleted" )
158+ Eventually (func () bool {
159+ work := & placementv1beta1.Work {}
160+ return errors .IsNotFound (hubClient .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("%s-work" , crpName ), Namespace : fmt .Sprintf ("fleet-member-%s" , allMemberClusterNames [0 ])}, work ))
161+ }, 6 * eventuallyDuration , eventuallyInterval ).Should (BeTrue (), "Work resource should be deleted from hub" )
162+
142163 By (fmt .Sprintf ("deleting placement %s" , crpName ))
143164 cleanupCRP (crpName )
144165 })
@@ -194,6 +215,7 @@ var _ = Describe("validating CRP when resources exists", Ordered, func() {
194215 AfterAll (func () {
195216 By (fmt .Sprintf ("deleting placement %s" , crpName ))
196217 cleanupCRP (crpName )
218+
197219 })
198220
199221 It ("should update CRP status as expected" , func () {
@@ -250,11 +272,21 @@ var _ = Describe("validating CRP when resources exists", Ordered, func() {
250272 })
251273
252274 AfterAll (func () {
275+ By ("deleting created work resources on member cluster" )
276+ cleanWorkResourcesOnCluster (allMemberClusters [0 ])
277+
253278 By (fmt .Sprintf ("deleting placement %s" , crpName ))
254279 cleanupCRP (crpName )
255280
256- By ("deleting created work resources on member cluster" )
257- cleanWorkResourcesOnCluster (allMemberClusters [0 ])
281+ // Check if work is deleted. Needed to ensure that the Work resource is cleaned up before the next CRP is created.
282+ // This is because the Work resource is created with a finalizer that blocks deletion until the all applied work
283+ // and applied work itself is successfully deleted. If the Work resource is not deleted, it can cause resource overlap
284+ // and flakiness in subsequent tests.
285+ By ("Check if work is deleted" )
286+ Eventually (func () bool {
287+ work := & placementv1beta1.Work {}
288+ return errors .IsNotFound (hubClient .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("%s-work" , crpName ), Namespace : fmt .Sprintf ("fleet-member-%s" , allMemberClusterNames [0 ])}, work ))
289+ }, 6 * eventuallyDuration , eventuallyInterval ).Should (BeTrue (), "Work resource should be deleted from hub" )
258290 })
259291
260292 It ("should update CRP status as expected" , func () {
@@ -385,11 +417,21 @@ var _ = Describe("validating CRP when resources exists", Ordered, func() {
385417 })
386418
387419 AfterAll (func () {
420+ By ("deleting created work resources on member cluster" )
421+ cleanWorkResourcesOnCluster (allMemberClusters [0 ])
422+
388423 By (fmt .Sprintf ("deleting placement %s" , crpName ))
389424 cleanupCRP (crpName )
390425
391- By ("deleting created work resources on member cluster" )
392- cleanWorkResourcesOnCluster (allMemberClusters [0 ])
426+ // Check if work is deleted. Needed to ensure that the Work resource is cleaned up before the next CRP is created.
427+ // This is because the Work resource is created with a finalizer that blocks deletion until the all applied work
428+ // and applied work itself is successfully deleted. If the Work resource is not deleted, it can cause resource overlap
429+ // and flakiness in subsequent tests.
430+ By ("Check if work is deleted" )
431+ Eventually (func () bool {
432+ work := & placementv1beta1.Work {}
433+ return errors .IsNotFound (hubClient .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("%s-work" , crpName ), Namespace : fmt .Sprintf ("fleet-member-%s" , allMemberClusterNames [0 ])}, work ))
434+ }, 7 * eventuallyDuration , eventuallyInterval ).Should (BeTrue (), "Work resource should be deleted from hub" )
393435 })
394436
395437 It ("should update CRP status as expected" , func () {
0 commit comments