@@ -50,6 +50,7 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
5050 var kubeClient client.Client
5151 var ako operator.Operator
5252 var testNamespace * corev1.Namespace
53+ var sharedGroupNamespace * corev1.Namespace
5354 var testGroup * nextapiv1.Group
5455 var groupID string
5556 var orgID string
@@ -79,12 +80,28 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
7980 },
8081 )).To (Succeed ())
8182
83+ By ("Create namespace and credentials for shared test Group" , func () {
84+ sharedGroupNamespace = & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {
85+ Name : utils .RandomName ("flex-shared-grp-ns" ),
86+ }}
87+ Expect (kubeClient .Create (ctx , sharedGroupNamespace )).To (Succeed ())
88+
89+ globalCredsKey := client.ObjectKey {
90+ Name : DefaultGlobalCredentials ,
91+ Namespace : control .MustEnvVar ("OPERATOR_NAMESPACE" ),
92+ }
93+ credentialsSecret , err := copySecretToNamespace (ctx , kubeClient , globalCredsKey , sharedGroupNamespace .Name )
94+ Expect (err ).NotTo (HaveOccurred ())
95+ Expect (
96+ kubeClient .Patch (ctx , credentialsSecret , client .Apply , client .ForceOwnership , GinkGoFieldOwner ),
97+ ).To (Succeed ())
98+ })
99+
82100 By ("Create test Group" , func () {
83- operatorNamespace := control .MustEnvVar ("OPERATOR_NAMESPACE" )
84101 groupName := utils .RandomName ("flexcluster-test-group" )
85102 // Replace placeholders in the Group YAML template
86103 groupYAML := strings .ReplaceAll (string (flexsamples .TestGroup ), "__GROUP_NAME__" , groupName )
87- groupYAML = strings .ReplaceAll (groupYAML , "__OPERATOR_NAMESPACE__" , operatorNamespace )
104+ groupYAML = strings .ReplaceAll (groupYAML , "__OPERATOR_NAMESPACE__" , sharedGroupNamespace . Name )
88105 groupYAML = strings .ReplaceAll (groupYAML , "__CREDENTIALS_SECRET_NAME__" , DefaultGlobalCredentials )
89106 groupYAML = strings .ReplaceAll (groupYAML , "__ORG_ID__" , orgID )
90107 objs := yml .MustParseObjects (strings .NewReader (groupYAML ))
@@ -122,6 +139,14 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
122139 }).WithTimeout (5 * time .Minute ).WithPolling (5 * time .Second ).NotTo (Succeed ())
123140 })
124141 }
142+ if kubeClient != nil && sharedGroupNamespace != nil {
143+ By ("Clean up shared group namespace" , func () {
144+ Expect (kubeClient .Delete (ctx , sharedGroupNamespace )).To (Succeed ())
145+ Eventually (func (g Gomega ) bool {
146+ return kubeClient .Get (ctx , client .ObjectKeyFromObject (sharedGroupNamespace ), sharedGroupNamespace ) == nil
147+ }).WithTimeout (time .Minute ).WithPolling (time .Second ).To (BeFalse ())
148+ })
149+ }
125150 if ako != nil {
126151 ako .Stop (GinkgoT ())
127152 }
@@ -149,6 +174,12 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
149174
150175 DescribeTable ("FlexCluster CRUD lifecycle" ,
151176 func (createYAML , updateYAML []byte , clusterName string ) {
177+ // Generate randomized group name for this test run (cluster names are unique per group)
178+ groupName := utils .RandomName ("flex-grp" )
179+
180+ // Track created objects for cleanup
181+ var createdObjects []client.Object
182+
152183 By ("Copy credentials secret to test namespace" , func () {
153184 globalCredsKey := client.ObjectKey {
154185 Name : DefaultGlobalCredentials ,
@@ -165,18 +196,21 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
165196 // Replace placeholders with actual values
166197 createYAMLStr := strings .ReplaceAll (string (createYAML ), "__GROUP_ID__" , groupID )
167198 createYAMLStr = strings .ReplaceAll (createYAMLStr , "__ORG_ID__" , orgID )
199+ createYAMLStr = strings .ReplaceAll (createYAMLStr , "__GROUP_NAME__" , groupName )
168200 objs := yml .MustParseObjects (strings .NewReader (createYAMLStr ))
169201 for _ , obj := range objs {
170- objToApply := kube . WithRenamedNamespace ( obj , testNamespace .Name )
202+ obj . SetNamespace ( testNamespace .Name )
171203 Expect (
172- kubeClient .Patch (ctx , objToApply , client .Apply , client .ForceOwnership , GinkGoFieldOwner ),
204+ kubeClient .Patch (ctx , obj , client .Apply , client .ForceOwnership , GinkGoFieldOwner ),
173205 ).To (Succeed ())
206+ createdObjects = append (createdObjects , obj )
174207 }
175208 })
176209
177210 By ("Wait for Group to be Ready (if using groupRef)" , func () {
178211 createYAMLStr := strings .ReplaceAll (string (createYAML ), "__GROUP_ID__" , groupID )
179212 createYAMLStr = strings .ReplaceAll (createYAMLStr , "__ORG_ID__" , orgID )
213+ createYAMLStr = strings .ReplaceAll (createYAMLStr , "__GROUP_NAME__" , groupName )
180214 objs := yml .MustParseObjects (strings .NewReader (createYAMLStr ))
181215 for _ , obj := range objs {
182216 if group , ok := obj .(* nextapiv1.Group ); ok {
@@ -223,11 +257,12 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
223257 // Replace placeholders with actual values
224258 updateYAMLStr := strings .ReplaceAll (string (updateYAML ), "__GROUP_ID__" , groupID )
225259 updateYAMLStr = strings .ReplaceAll (updateYAMLStr , "__ORG_ID__" , orgID )
260+ updateYAMLStr = strings .ReplaceAll (updateYAMLStr , "__GROUP_NAME__" , groupName )
226261 updateObjs := yml .MustParseObjects (strings .NewReader (updateYAMLStr ))
227262 for _ , obj := range updateObjs {
228- objToPatch := kube . WithRenamedNamespace ( obj , testNamespace .Name )
263+ obj . SetNamespace ( testNamespace .Name )
229264 Expect (
230- kubeClient .Patch (ctx , objToPatch , client .Apply , client .ForceOwnership , GinkGoFieldOwner ),
265+ kubeClient .Patch (ctx , obj , client .Apply , client .ForceOwnership , GinkGoFieldOwner ),
231266 ).To (Succeed ())
232267 }
233268 }
@@ -253,15 +288,18 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
253288 }
254289 })
255290
256- By ("Delete FlexCluster" , func () {
257- Expect (kubeClient .Delete (ctx , & cluster )).To (Succeed ())
291+ By ("Delete all created resources" , func () {
292+ for _ , obj := range createdObjects {
293+ _ = kubeClient .Delete (ctx , obj )
294+ }
258295 })
259296
260- By ("Wait for FlexCluster to be deleted" , func () {
261- Eventually (func (g Gomega ) error {
262- err := kubeClient .Get (ctx , client .ObjectKeyFromObject (& cluster ), & cluster )
263- return err
264- }).WithTimeout (5 * time .Minute ).WithPolling (5 * time .Second ).NotTo (Succeed ())
297+ By ("Wait for all resources to be deleted" , func () {
298+ for _ , obj := range createdObjects {
299+ Eventually (func (g Gomega ) error {
300+ return kubeClient .Get (ctx , client .ObjectKeyFromObject (obj ), obj )
301+ }).WithTimeout (5 * time .Minute ).WithPolling (5 * time .Second ).ShouldNot (Succeed ())
302+ }
265303 })
266304 },
267305 Entry ("With direct groupId" ,
0 commit comments