Skip to content

Commit d936d4b

Browse files
committed
add tests and fix bugs
1 parent 91b889e commit d936d4b

File tree

2 files changed

+106
-6
lines changed

2 files changed

+106
-6
lines changed

internal/controllers/scheduler/controller.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ func (r *ClusterScheduler) handleCreateOrUpdate(ctx context.Context, req reconci
288288
}
289289
}
290290
}
291-
} else if !apierrors.IsNotFound(err) {
291+
} else if apierrors.IsNotFound(err) {
292+
existingCluster = nil
293+
} else {
292294
rr.ReconcileError = errutils.WithReason(fmt.Errorf("error checking whether cluster '%s/%s' exists: %w", cluster.Namespace, cluster.Name, err), cconst.ReasonPlatformClusterInteractionProblem)
293295
return rr
294296
}
@@ -300,7 +302,14 @@ func (r *ClusterScheduler) handleCreateOrUpdate(ctx context.Context, req reconci
300302

301303
// create/update Cluster resource
302304
// Note that clusters are usually not updated. This should only happen if the status of a ClusterRequest was lost and an existing cluster is recovered.
303-
if _, err := controllerutil.CreateOrUpdate(ctx, r.PlatformCluster.Client(), existingCluster, func() error {
305+
if existingCluster == nil {
306+
log.Info("Creating new cluster for request", "clusterName", cluster.Name, "clusterNamespace", cluster.Namespace)
307+
if err := r.PlatformCluster.Client().Create(ctx, cluster); err != nil {
308+
rr.ReconcileError = errutils.WithReason(fmt.Errorf("error creating cluster '%s/%s': %w", cluster.Namespace, cluster.Name, err), cconst.ReasonPlatformClusterInteractionProblem)
309+
return rr
310+
}
311+
} else {
312+
log.Info("Recovering existing cluster for request", "clusterName", cluster.Name, "clusterNamespace", cluster.Namespace)
304313
// merge finalizers, labels, and annotations from initialized cluster into existing one
305314
finSet := sets.New(existingCluster.Finalizers...)
306315
finSet.Delete(finalizersToRemove.UnsortedList()...)
@@ -311,10 +320,10 @@ func (r *ClusterScheduler) handleCreateOrUpdate(ctx context.Context, req reconci
311320
// copy spec from initialized cluster
312321
existingCluster.Spec = cluster.Spec
313322

314-
return nil
315-
}); err != nil {
316-
rr.ReconcileError = errutils.WithReason(fmt.Errorf("error creating/updating cluster '%s/%s': %w", cluster.Namespace, cluster.Name, err), cconst.ReasonPlatformClusterInteractionProblem)
317-
return rr
323+
if err := r.PlatformCluster.Client().Update(ctx, existingCluster); err != nil {
324+
rr.ReconcileError = errutils.WithReason(fmt.Errorf("error updating existing cluster '%s/%s': %w", existingCluster.Namespace, existingCluster.Name, err), cconst.ReasonPlatformClusterInteractionProblem)
325+
return rr
326+
}
318327
}
319328
}
320329

internal/controllers/scheduler/controller_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scheduler_test
33
import (
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

Comments
 (0)