@@ -57,6 +57,9 @@ func TestReconcile(t *testing.T) {
57
57
ObjectMeta : metav1.ObjectMeta {
58
58
Name : "test-cluster" ,
59
59
Namespace : metav1 .NamespaceDefault ,
60
+ Labels : map [string ]string {
61
+ "cluster.x-k8s.io/included-in-clustercache-tests" : "true" ,
62
+ },
60
63
},
61
64
Spec : clusterv1.ClusterSpec {
62
65
ControlPlaneRef : clusterv1.ContractVersionedObjectReference {
@@ -87,6 +90,9 @@ func TestReconcile(t *testing.T) {
87
90
clusterAccessorConfig : accessorConfig ,
88
91
clusterAccessors : make (map [client.ObjectKey ]* clusterAccessor ),
89
92
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
+ },
90
96
}
91
97
92
98
// Add a Cluster source and start it (queue will be later used to verify the source works correctly)
@@ -110,6 +116,31 @@ func TestReconcile(t *testing.T) {
110
116
testCluster .Status .Initialization .InfrastructureProvisioned = ptr .To (true )
111
117
g .Expect (env .Status ().Patch (ctx , testCluster , patch )).To (Succeed ())
112
118
119
+ // Exclude from clustercache by changing the label
120
+ patch = client .MergeFrom (testCluster .DeepCopy ())
121
+ testCluster .ObjectMeta .Labels = map [string ]string {
122
+ "cluster.x-k8s.io/included-in-clustercache-tests" : "false" ,
123
+ }
124
+ g .Expect (env .Patch (ctx , testCluster , patch )).To (Succeed ())
125
+ // Sanity check that the clusterFIlter does not include the cluster now
126
+ g .Expect (cc .clusterFilter (testCluster )).To ((BeFalse ()))
127
+
128
+ // Reconcile, cluster should be ignored now
129
+ // => no requeue, no cluster accessor created
130
+ res , err = cc .Reconcile (ctx , reconcile.Request {NamespacedName : clusterKey })
131
+ g .Expect (err ).ToNot (HaveOccurred ())
132
+ g .Expect (res ).To (Equal (ctrl.Result {}))
133
+ g .Expect (res .IsZero ()).To (BeTrue ())
134
+
135
+ // Put the label back
136
+ patch = client .MergeFrom (testCluster .DeepCopy ())
137
+ testCluster .ObjectMeta .Labels = map [string ]string {
138
+ "cluster.x-k8s.io/included-in-clustercache-tests" : "true" ,
139
+ }
140
+ g .Expect (env .Patch (ctx , testCluster , patch )).To (Succeed ())
141
+ // Sanity check that the clusterFIlter does include the cluster now
142
+ g .Expect (cc .clusterFilter (testCluster )).To ((BeTrue ()))
143
+
113
144
// Reconcile, kubeconfig Secret doesn't exist
114
145
// => accessor.Connect will fail so we expect a retry with ConnectionCreationRetryInterval.
115
146
res , err = cc .Reconcile (ctx , reconcile.Request {NamespacedName : clusterKey })
0 commit comments