Skip to content

Commit 99b1c75

Browse files
authored
Merge pull request #12454 from sbueringer/pr-add-cluster-validation
🌱 Add validation for Cluster spec.controlPlaneRef, spec.infrastructureRef and spec.topology
2 parents a7cd691 + 9315471 commit 99b1c75

File tree

18 files changed

+316
-112
lines changed

18 files changed

+316
-112
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_reconciler_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"sigs.k8s.io/controller-runtime/pkg/client"
2525

2626
bootstrapv1 "sigs.k8s.io/cluster-api/api/bootstrap/kubeadm/v1beta2"
27-
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
2827
"sigs.k8s.io/cluster-api/util/test/builder"
2928
)
3029

@@ -37,9 +36,8 @@ func TestKubeadmConfigReconciler(t *testing.T) {
3736
g.Expect(err).ToNot(HaveOccurred())
3837

3938
cluster := builder.Cluster(ns.Name, "cluster1").
40-
WithClusterNetwork(&clusterv1.ClusterNetwork{
41-
ServiceDomain: "service.domain",
42-
}).
39+
WithControlPlane(
40+
builder.ControlPlane(ns.Name, "cp1").Build()).
4341
Build()
4442
g.Expect(env.Create(ctx, cluster)).To(Succeed())
4543
machine := newWorkerMachineForCluster(cluster)

controllers/clustercache/cluster_accessor_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
4242
"sigs.k8s.io/cluster-api/controllers/remote"
4343
"sigs.k8s.io/cluster-api/util/kubeconfig"
44+
"sigs.k8s.io/cluster-api/util/test/builder"
4445
)
4546

4647
func TestConnect(t *testing.T) {
@@ -52,8 +53,10 @@ func TestConnect(t *testing.T) {
5253
Namespace: metav1.NamespaceDefault,
5354
},
5455
Spec: clusterv1.ClusterSpec{
55-
ClusterNetwork: &clusterv1.ClusterNetwork{
56-
ServiceDomain: "service.domain",
56+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
57+
APIGroup: builder.ControlPlaneGroupVersion.Group,
58+
Kind: builder.GenericControlPlaneKind,
59+
Name: "cp1",
5760
},
5861
},
5962
}
@@ -153,8 +156,10 @@ func TestDisconnect(t *testing.T) {
153156
Namespace: metav1.NamespaceDefault,
154157
},
155158
Spec: clusterv1.ClusterSpec{
156-
ClusterNetwork: &clusterv1.ClusterNetwork{
157-
ServiceDomain: "service.domain",
159+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
160+
APIGroup: builder.ControlPlaneGroupVersion.Group,
161+
Kind: builder.GenericControlPlaneKind,
162+
Name: "cp1",
158163
},
159164
},
160165
}
@@ -207,8 +212,10 @@ func TestHealthCheck(t *testing.T) {
207212
Namespace: metav1.NamespaceDefault,
208213
},
209214
Spec: clusterv1.ClusterSpec{
210-
ClusterNetwork: &clusterv1.ClusterNetwork{
211-
ServiceDomain: "service.domain",
215+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
216+
APIGroup: builder.ControlPlaneGroupVersion.Group,
217+
Kind: builder.GenericControlPlaneKind,
218+
Name: "cp1",
212219
},
213220
},
214221
}
@@ -323,8 +330,10 @@ func TestWatch(t *testing.T) {
323330
Namespace: metav1.NamespaceDefault,
324331
},
325332
Spec: clusterv1.ClusterSpec{
326-
ClusterNetwork: &clusterv1.ClusterNetwork{
327-
ServiceDomain: "service.domain",
333+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
334+
APIGroup: builder.ControlPlaneGroupVersion.Group,
335+
Kind: builder.GenericControlPlaneKind,
336+
Name: "cp1",
328337
},
329338
},
330339
}

controllers/clustercache/cluster_cache_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
4848
"sigs.k8s.io/cluster-api/controllers/remote"
4949
"sigs.k8s.io/cluster-api/util/kubeconfig"
50+
"sigs.k8s.io/cluster-api/util/test/builder"
5051
)
5152

5253
func TestReconcile(t *testing.T) {
@@ -58,8 +59,10 @@ func TestReconcile(t *testing.T) {
5859
Namespace: metav1.NamespaceDefault,
5960
},
6061
Spec: clusterv1.ClusterSpec{
61-
ClusterNetwork: &clusterv1.ClusterNetwork{
62-
ServiceDomain: "service.domain",
62+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
63+
APIGroup: builder.ControlPlaneGroupVersion.Group,
64+
Kind: builder.GenericControlPlaneKind,
65+
Name: "cp1",
6366
},
6467
},
6568
}
@@ -738,8 +741,10 @@ func createCluster(g Gomega, testCluster testCluster) {
738741
Namespace: testCluster.cluster.Namespace,
739742
},
740743
Spec: clusterv1.ClusterSpec{
741-
ClusterNetwork: &clusterv1.ClusterNetwork{
742-
ServiceDomain: "service.domain",
744+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
745+
APIGroup: builder.ControlPlaneGroupVersion.Group,
746+
Kind: builder.GenericControlPlaneKind,
747+
Name: "cp1",
743748
},
744749
},
745750
}

controlplane/kubeadm/internal/cluster_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"sigs.k8s.io/cluster-api/util/collections"
4747
"sigs.k8s.io/cluster-api/util/kubeconfig"
4848
"sigs.k8s.io/cluster-api/util/secret"
49+
"sigs.k8s.io/cluster-api/util/test/builder"
4950
)
5051

5152
func TestGetMachinesForCluster(t *testing.T) {
@@ -119,8 +120,10 @@ func TestGetWorkloadCluster(t *testing.T) {
119120
Namespace: ns.Name,
120121
},
121122
Spec: clusterv1.ClusterSpec{
122-
ClusterNetwork: &clusterv1.ClusterNetwork{
123-
ServiceDomain: "service.domain",
123+
InfrastructureRef: &clusterv1.ContractVersionedObjectReference{
124+
APIGroup: builder.InfrastructureGroupVersion.Group,
125+
Kind: builder.GenericInfrastructureClusterKind,
126+
Name: "infracluster1",
124127
},
125128
},
126129
}

controlplane/kubeadm/internal/controllers/controller_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,11 @@ func TestReconcileInitializeControlPlane(t *testing.T) {
12061206
Host: "test.local",
12071207
Port: 9999,
12081208
},
1209+
InfrastructureRef: &clusterv1.ContractVersionedObjectReference{
1210+
APIGroup: builder.InfrastructureGroupVersion.Group,
1211+
Kind: builder.GenericInfrastructureClusterKind,
1212+
Name: "infracluster1",
1213+
},
12091214
}
12101215
g.Expect(env.Create(ctx, cluster)).To(Succeed())
12111216
patchHelper, err := patch.NewHelper(cluster, env)
@@ -1423,6 +1428,11 @@ func TestReconcileInitializeControlPlane_withUserCA(t *testing.T) {
14231428
Host: "test.local",
14241429
Port: 9999,
14251430
},
1431+
InfrastructureRef: &clusterv1.ContractVersionedObjectReference{
1432+
APIGroup: builder.InfrastructureGroupVersion.Group,
1433+
Kind: builder.GenericInfrastructureClusterKind,
1434+
Name: "infracluster1",
1435+
},
14261436
}
14271437

14281438
caCertificate := &secret.Certificate{
@@ -1457,7 +1467,7 @@ func TestReconcileInitializeControlPlane_withUserCA(t *testing.T) {
14571467

14581468
genericInfrastructureMachineTemplate := &unstructured.Unstructured{
14591469
Object: map[string]interface{}{
1460-
"kind": "GenericInfrastructureMachineTemplate",
1470+
"kind": builder.GenericInfrastructureMachineTemplateKind,
14611471
"apiVersion": clusterv1.GroupVersionInfrastructure.String(),
14621472
"metadata": map[string]interface{}{
14631473
"name": "infra-foo",
@@ -1649,8 +1659,10 @@ func TestKubeadmControlPlaneReconciler_syncMachines(t *testing.T) {
16491659
Name: "test-cluster",
16501660
},
16511661
Spec: clusterv1.ClusterSpec{
1652-
ClusterNetwork: &clusterv1.ClusterNetwork{
1653-
ServiceDomain: "service.domain",
1662+
InfrastructureRef: &clusterv1.ContractVersionedObjectReference{
1663+
APIGroup: builder.InfrastructureGroupVersion.Group,
1664+
Kind: builder.GenericInfrastructureClusterKind,
1665+
Name: "infracluster1",
16541666
},
16551667
},
16561668
}

exp/internal/controllers/machinepool_controller_phases_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,9 +1351,8 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
13511351
g.Expect(err).ToNot(HaveOccurred())
13521352

13531353
cluster := builder.Cluster(ns.Name, clusterName).
1354-
WithClusterNetwork(&clusterv1.ClusterNetwork{
1355-
ServiceDomain: "service.domain",
1356-
}).
1354+
WithControlPlane(
1355+
builder.ControlPlane(ns.Name, "cp1").Build()).
13571356
Build()
13581357
g.Expect(env.CreateAndWait(ctx, cluster)).To(Succeed())
13591358

@@ -1724,8 +1723,10 @@ func TestReconcileMachinePoolScaleToFromZero(t *testing.T) {
17241723
Namespace: ns.Name,
17251724
},
17261725
Spec: clusterv1.ClusterSpec{
1727-
ClusterNetwork: &clusterv1.ClusterNetwork{
1728-
ServiceDomain: "service.domain",
1726+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
1727+
APIGroup: builder.ControlPlaneGroupVersion.Group,
1728+
Kind: builder.GenericControlPlaneKind,
1729+
Name: "cp1",
17291730
},
17301731
},
17311732
}

internal/controllers/cluster/cluster_controller_test.go

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
3434
runtimev1 "sigs.k8s.io/cluster-api/api/runtime/v1beta2"
3535
"sigs.k8s.io/cluster-api/feature"
36+
"sigs.k8s.io/cluster-api/internal/contract"
3637
"sigs.k8s.io/cluster-api/util"
3738
"sigs.k8s.io/cluster-api/util/collections"
3839
"sigs.k8s.io/cluster-api/util/conditions"
@@ -65,8 +66,10 @@ func TestClusterReconciler(t *testing.T) {
6566
Namespace: ns.Name,
6667
},
6768
Spec: clusterv1.ClusterSpec{
68-
ClusterNetwork: &clusterv1.ClusterNetwork{
69-
ServiceDomain: "service.domain",
69+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
70+
APIGroup: builder.ControlPlaneGroupVersion.Group,
71+
Kind: builder.GenericControlPlaneKind,
72+
Name: "cp1",
7073
},
7174
},
7275
}
@@ -120,8 +123,10 @@ func TestClusterReconciler(t *testing.T) {
120123
Namespace: ns.Name,
121124
},
122125
Spec: clusterv1.ClusterSpec{
123-
ClusterNetwork: &clusterv1.ClusterNetwork{
124-
ServiceDomain: "service.domain",
126+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
127+
APIGroup: builder.ControlPlaneGroupVersion.Group,
128+
Kind: builder.GenericControlPlaneKind,
129+
Name: "cp1",
125130
},
126131
},
127132
}
@@ -179,8 +184,10 @@ func TestClusterReconciler(t *testing.T) {
179184
Namespace: ns.Name,
180185
},
181186
Spec: clusterv1.ClusterSpec{
182-
ClusterNetwork: &clusterv1.ClusterNetwork{
183-
ServiceDomain: "service.domain",
187+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
188+
APIGroup: builder.ControlPlaneGroupVersion.Group,
189+
Kind: builder.GenericControlPlaneKind,
190+
Name: "cp1",
184191
},
185192
},
186193
}
@@ -231,8 +238,10 @@ func TestClusterReconciler(t *testing.T) {
231238
Namespace: ns.Name,
232239
},
233240
Spec: clusterv1.ClusterSpec{
234-
ClusterNetwork: &clusterv1.ClusterNetwork{
235-
ServiceDomain: "service.domain",
241+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
242+
APIGroup: builder.ControlPlaneGroupVersion.Group,
243+
Kind: builder.GenericControlPlaneKind,
244+
Name: "cp1",
236245
},
237246
},
238247
}
@@ -284,8 +293,10 @@ func TestClusterReconciler(t *testing.T) {
284293
Namespace: ns.Name,
285294
},
286295
Spec: clusterv1.ClusterSpec{
287-
ClusterNetwork: &clusterv1.ClusterNetwork{
288-
ServiceDomain: "service.domain",
296+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
297+
APIGroup: builder.ControlPlaneGroupVersion.Group,
298+
Kind: builder.GenericControlPlaneKind,
299+
Name: "cp1",
289300
},
290301
},
291302
}
@@ -344,8 +355,10 @@ func TestClusterReconciler(t *testing.T) {
344355
Namespace: ns.Name,
345356
},
346357
Spec: clusterv1.ClusterSpec{
347-
ClusterNetwork: &clusterv1.ClusterNetwork{
348-
ServiceDomain: "service.domain",
358+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
359+
APIGroup: builder.ControlPlaneGroupVersion.Group,
360+
Kind: builder.GenericControlPlaneKind,
361+
Name: "cp1",
349362
},
350363
},
351364
}
@@ -388,19 +401,30 @@ func TestClusterReconciler(t *testing.T) {
388401
t.Run("Should successfully set ControlPlaneInitialized on the cluster object if controlplane is ready", func(t *testing.T) {
389402
g := NewWithT(t)
390403

404+
ic := builder.InfrastructureCluster(ns.Name, "infracluster1").Build()
405+
g.Expect(env.CreateAndWait(ctx, ic)).To(Succeed())
406+
defer func() {
407+
g.Expect(env.CleanupAndWait(ctx, ic)).To(Succeed())
408+
}()
409+
icOriginal := ic.DeepCopy()
410+
g.Expect(contract.InfrastructureCluster().Provisioned("v1beta2").Set(ic, true)).To(Succeed())
411+
g.Expect(env.Status().Patch(ctx, ic, client.MergeFrom(icOriginal))).To(Succeed())
412+
391413
cluster := &clusterv1.Cluster{
392414
ObjectMeta: metav1.ObjectMeta{
393415
GenerateName: "test6-",
394416
Namespace: ns.Name,
395417
},
396418
Spec: clusterv1.ClusterSpec{
397-
ClusterNetwork: &clusterv1.ClusterNetwork{
398-
ServiceDomain: "service.domain",
419+
InfrastructureRef: &clusterv1.ContractVersionedObjectReference{
420+
APIGroup: builder.InfrastructureGroupVersion.Group,
421+
Kind: builder.GenericInfrastructureClusterKind,
422+
Name: "infracluster1",
399423
},
400424
},
401425
}
402-
403426
g.Expect(env.Create(ctx, cluster)).To(Succeed())
427+
404428
key := client.ObjectKey{Name: cluster.Name, Namespace: cluster.Namespace}
405429
defer func() {
406430
err := env.Delete(ctx, cluster)

internal/controllers/clusterresourceset/clusterresourceset_controller_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"sigs.k8s.io/cluster-api/util"
3939
"sigs.k8s.io/cluster-api/util/conditions"
4040
v1beta1conditions "sigs.k8s.io/cluster-api/util/conditions/deprecated/v1beta1"
41+
"sigs.k8s.io/cluster-api/util/test/builder"
4142
)
4243

4344
const (
@@ -112,8 +113,10 @@ metadata:
112113
Namespace: ns.Name,
113114
},
114115
Spec: clusterv1.ClusterSpec{
115-
ClusterNetwork: &clusterv1.ClusterNetwork{
116-
ServiceDomain: "service.domain",
116+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
117+
APIGroup: builder.ControlPlaneGroupVersion.Group,
118+
Kind: builder.GenericControlPlaneKind,
119+
Name: "cp1",
117120
},
118121
},
119122
}

internal/controllers/machine/machine_controller_noderef_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,10 @@ func TestGetNode(t *testing.T) {
239239
Namespace: ns.Name,
240240
},
241241
Spec: clusterv1.ClusterSpec{
242-
ClusterNetwork: &clusterv1.ClusterNetwork{
243-
ServiceDomain: "service.domain",
242+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
243+
APIGroup: builder.ControlPlaneGroupVersion.Group,
244+
Kind: builder.GenericControlPlaneKind,
245+
Name: "cp1",
244246
},
245247
},
246248
}
@@ -387,8 +389,10 @@ func TestNodeLabelSync(t *testing.T) {
387389
Namespace: metav1.NamespaceDefault,
388390
},
389391
Spec: clusterv1.ClusterSpec{
390-
ClusterNetwork: &clusterv1.ClusterNetwork{
391-
ServiceDomain: "service.domain",
392+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
393+
APIGroup: builder.ControlPlaneGroupVersion.Group,
394+
Kind: builder.GenericControlPlaneKind,
395+
Name: "cp1",
392396
},
393397
},
394398
}

internal/controllers/machine/machine_controller_status_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"sigs.k8s.io/cluster-api/util/conditions"
3636
v1beta1conditions "sigs.k8s.io/cluster-api/util/conditions/deprecated/v1beta1"
3737
"sigs.k8s.io/cluster-api/util/kubeconfig"
38+
builder "sigs.k8s.io/cluster-api/util/test/builder"
3839
)
3940

4041
func TestSetBootstrapReadyCondition(t *testing.T) {
@@ -1906,8 +1907,10 @@ func TestReconcileMachinePhases(t *testing.T) {
19061907
Namespace: metav1.NamespaceDefault,
19071908
},
19081909
Spec: clusterv1.ClusterSpec{
1909-
ClusterNetwork: &clusterv1.ClusterNetwork{
1910-
ServiceDomain: "service.domain",
1910+
ControlPlaneRef: &clusterv1.ContractVersionedObjectReference{
1911+
APIGroup: builder.ControlPlaneGroupVersion.Group,
1912+
Kind: builder.GenericControlPlaneKind,
1913+
Name: "cp1",
19111914
},
19121915
},
19131916
}

0 commit comments

Comments
 (0)