@@ -3,6 +3,7 @@ package scheduler_test
33import (
44 "fmt"
55 "path/filepath"
6+ "strings"
67
78 . "github.com/onsi/ginkgo/v2"
89 . "github.com/onsi/gomega"
@@ -489,7 +490,97 @@ var _ = Describe("Scheduler", func() {
489490 Expect (cr .Status .Cluster ).ToNot (BeNil ())
490491 Expect (cr .Status .Cluster .Name ).ToNot (Equal (c .Name ), "Cluster is in deletion and should not be considered for scheduling" )
491492 Expect (cr .Status .Cluster .Namespace ).To (Equal (c .Namespace ))
493+ })
494+
495+ It ("should choose the Cluster name correctly" , func () {
496+ clusterNamespace := "exclusive"
497+ sc , env := defaultTestSetup ("testdata" , "test-01" )
498+ Expect (sc .Config .Scope ).To (Equal (config .SCOPE_NAMESPACED ))
499+ Expect (env .Client ().DeleteAllOf (env .Ctx , & clustersv1alpha1.Cluster {}, client .InNamespace (clusterNamespace ))).To (Succeed ())
500+ existingClusters := & clustersv1alpha1.ClusterList {}
501+ Expect (env .Client ().List (env .Ctx , existingClusters , client .InNamespace (clusterNamespace ))).To (Succeed ())
502+ Expect (existingClusters .Items ).To (BeEmpty ())
503+
504+ req := & clustersv1alpha1.ClusterRequest {}
505+ Expect (env .Client ().Get (env .Ctx , ctrlutils .ObjectKey ("exclusive" , "foo" ), req )).To (Succeed ())
506+ Expect (req .Status .Cluster ).To (BeNil ())
507+
508+ env .ShouldReconcile (testutils .RequestFromObject (req ))
509+ Expect (env .Client ().Get (env .Ctx , client .ObjectKeyFromObject (req ), req )).To (Succeed ())
510+ Expect (req .Status .Cluster ).ToNot (BeNil ())
511+
512+ Expect (env .Client ().List (env .Ctx , existingClusters , client .InNamespace (clusterNamespace ))).To (Succeed ())
513+ Expect (existingClusters .Items ).To (HaveLen (1 ))
514+ cluster := existingClusters .Items [0 ]
515+ Expect (cluster .Name ).To (Equal (req .Status .Cluster .Name ))
516+ expectedName , err := scheduler .GenerateClusterName (req .Spec .Purpose + "-" , req )
517+ Expect (err ).ToNot (HaveOccurred ())
518+ Expect (cluster .Name ).To (Equal (expectedName ))
519+
520+ req2 := & clustersv1alpha1.ClusterRequest {}
521+ req2 .SetName ("exclusive-2" )
522+ req2 .SetNamespace ("foo" )
523+ req2 .Spec .Purpose = "exclusive"
524+ req2 .Labels = map [string ]string {
525+ clustersv1alpha1 .RandomizeClusterNameLabel : "true" ,
526+ }
527+ Expect (env .Client ().Create (env .Ctx , req2 )).To (Succeed ())
528+
529+ env .ShouldReconcile (testutils .RequestFromObject (req2 ))
530+ Expect (env .Client ().Get (env .Ctx , client .ObjectKeyFromObject (req2 ), req2 )).To (Succeed ())
531+ Expect (req2 .Status .Cluster ).ToNot (BeNil ())
532+ expectedName2 , err := scheduler .GenerateClusterName (req2 .Spec .Purpose + "-" , req2 )
533+ Expect (req2 .Status .Cluster .Name ).ToNot (Equal (expectedName2 ))
534+ Expect (strings .HasPrefix (req2 .Status .Cluster .Name , req2 .Spec .Purpose + "-" )).To (BeTrue ())
535+
536+ cluster2 := & clustersv1alpha1.Cluster {}
537+ Expect (env .Client ().Get (env .Ctx , ctrlutils .ObjectKey (req2 .Status .Cluster .Name , req2 .Status .Cluster .Namespace ), cluster2 )).To (Succeed ())
538+ Expect (cluster2 .GenerateName ).To (Equal (req2 .Spec .Purpose + "-" ))
539+ })
540+
541+ It ("should recreate a deleted cluster for an existing request" , func () {
542+ clusterNamespace := "exclusive"
543+ sc , env := defaultTestSetup ("testdata" , "test-01" )
544+ Expect (sc .Config .Scope ).To (Equal (config .SCOPE_NAMESPACED ))
545+ Expect (env .Client ().DeleteAllOf (env .Ctx , & clustersv1alpha1.Cluster {}, client .InNamespace (clusterNamespace ))).To (Succeed ())
546+ existingClusters := & clustersv1alpha1.ClusterList {}
547+ Expect (env .Client ().List (env .Ctx , existingClusters , client .InNamespace (clusterNamespace ))).To (Succeed ())
548+ Expect (existingClusters .Items ).To (BeEmpty ())
549+
550+ req := & clustersv1alpha1.ClusterRequest {}
551+ Expect (env .Client ().Get (env .Ctx , ctrlutils .ObjectKey ("exclusive" , "foo" ), req )).To (Succeed ())
552+ Expect (req .Status .Cluster ).To (BeNil ())
553+
554+ env .ShouldReconcile (testutils .RequestFromObject (req ))
555+ Expect (env .Client ().Get (env .Ctx , client .ObjectKeyFromObject (req ), req )).To (Succeed ())
556+ Expect (req .Status .Cluster ).ToNot (BeNil ())
557+
558+ Expect (env .Client ().List (env .Ctx , existingClusters , client .InNamespace (clusterNamespace ))).To (Succeed ())
559+ Expect (existingClusters .Items ).To (HaveLen (1 ))
560+ cluster := existingClusters .Items [0 ]
561+ Expect (cluster .Name ).To (Equal (req .Status .Cluster .Name ))
562+ Expect (cluster .Namespace ).To (Equal (clusterNamespace ))
563+
564+ // remove all finalizers and delete the cluster
565+ cluster .Finalizers = []string {}
566+ Expect (env .Client ().Update (env .Ctx , & cluster )).To (Succeed ())
567+ Expect (env .Client ().Delete (env .Ctx , & cluster )).To (Succeed ())
568+ Eventually (func () bool {
569+ err := env .Client ().Get (env .Ctx , client .ObjectKeyFromObject (& cluster ), & cluster )
570+ return apierrors .IsNotFound (err )
571+ }, 3 ).Should (BeTrue (), "Cluster should be deleted" )
572+
573+ // reconcile again, this should recreate the cluster
574+ env .ShouldReconcile (testutils .RequestFromObject (req ))
575+ Expect (env .Client ().Get (env .Ctx , client .ObjectKeyFromObject (req ), req )).To (Succeed ())
576+ Expect (req .Status .Cluster ).ToNot (BeNil ())
492577
578+ Expect (env .Client ().List (env .Ctx , existingClusters , client .InNamespace (clusterNamespace ))).To (Succeed ())
579+ Expect (existingClusters .Items ).To (HaveLen (1 ))
580+ newCluster := existingClusters .Items [0 ]
581+ Expect (newCluster .Name ).To (Equal (req .Status .Cluster .Name ))
582+ Expect (newCluster .Namespace ).To (Equal (clusterNamespace ))
583+ Expect (newCluster .Name ).To (Equal (cluster .Name ), "Recreated cluster should have the same name" )
493584 })
494585
495586})
0 commit comments