Skip to content

Commit 5ac3629

Browse files
committed
WIP: Test cluster cache with labels
Signed-off-by: Lennart Jern <[email protected]>
1 parent 165bddb commit 5ac3629

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

controllers/clustercache/cluster_cache_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func TestReconcile(t *testing.T) {
5757
ObjectMeta: metav1.ObjectMeta{
5858
Name: "test-cluster",
5959
Namespace: metav1.NamespaceDefault,
60+
Labels: map[string]string{
61+
"cluster.x-k8s.io/included-in-clustercache-tests": "true",
62+
},
6063
},
6164
Spec: clusterv1.ClusterSpec{
6265
ControlPlaneRef: clusterv1.ContractVersionedObjectReference{
@@ -87,6 +90,9 @@ func TestReconcile(t *testing.T) {
8790
clusterAccessorConfig: accessorConfig,
8891
clusterAccessors: make(map[client.ObjectKey]*clusterAccessor),
8992
cacheCtx: context.Background(),
93+
clusterFilter: func(cluster *clusterv1.Cluster) bool {
94+
return (cluster.ObjectMeta.Labels["cluster.x-k8s.io/included-in-clustercache-tests"] == "true")
95+
},
9096
}
9197

9298
// Add a Cluster source and start it (queue will be later used to verify the source works correctly)
@@ -110,6 +116,32 @@ func TestReconcile(t *testing.T) {
110116
testCluster.Status.Initialization.InfrastructureProvisioned = ptr.To(true)
111117
g.Expect(env.Status().Patch(ctx, testCluster, patch)).To(Succeed())
112118

119+
// Sanity check that the clusterFilter does pick up the testCluster
120+
g.Expect(testCluster.ObjectMeta.Labels["cluster.x-k8s.io/included-in-clustercache-tests"]).To(Equal("true"))
121+
g.Expect(cc.clusterFilter(testCluster)).To((BeTrue()))
122+
// Exclude from clustercache by changing the label
123+
patch = client.MergeFrom(testCluster.DeepCopy())
124+
testCluster.ObjectMeta.Labels = map[string]string{
125+
"cluster.x-k8s.io/included-in-clustercache-tests": "false",
126+
}
127+
g.Expect(testCluster.ObjectMeta.Labels["cluster.x-k8s.io/included-in-clustercache-tests"]).To(Equal("false"))
128+
g.Expect(cc.clusterFilter(testCluster)).To((BeFalse()))
129+
g.Expect(env.Status().Patch(ctx, testCluster, patch)).To(Succeed())
130+
131+
// Reconcile, cluster should be ignored now
132+
// => no requeue, no cluster accessor created
133+
res, err = cc.Reconcile(ctx, reconcile.Request{NamespacedName: clusterKey})
134+
g.Expect(err).ToNot(HaveOccurred())
135+
g.Expect(res).To(Equal(ctrl.Result{}))
136+
g.Expect(res.IsZero()).To(BeTrue())
137+
138+
// Put the label back
139+
patch = client.MergeFrom(testCluster.DeepCopy())
140+
testCluster.ObjectMeta.Labels = map[string]string{
141+
"cluster.x-k8s.io/included-in-clustercache-tests": "true",
142+
}
143+
g.Expect(env.Status().Patch(ctx, testCluster, patch)).To(Succeed())
144+
113145
// Reconcile, kubeconfig Secret doesn't exist
114146
// => accessor.Connect will fail so we expect a retry with ConnectionCreationRetryInterval.
115147
res, err = cc.Reconcile(ctx, reconcile.Request{NamespacedName: clusterKey})

0 commit comments

Comments
 (0)