Skip to content

Commit 15d779b

Browse files
Use test builders in bootstrap
Signed-off-by: killianmuldoon <[email protected]>
1 parent 5352198 commit 15d779b

File tree

6 files changed

+598
-260
lines changed

6 files changed

+598
-260
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package builder
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
22+
"k8s.io/apimachinery/pkg/runtime"
23+
24+
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
25+
)
26+
27+
// KubeadmConfigBuilder contains the information needed to produce a KubeadmConfig.
28+
type KubeadmConfigBuilder struct {
29+
name string
30+
namespace string
31+
joinConfig *bootstrapv1.JoinConfiguration
32+
initConfig *bootstrapv1.InitConfiguration
33+
clusterConfig *bootstrapv1.ClusterConfiguration
34+
}
35+
36+
// KubeadmConfig returns a KubeadmConfigBuilder with the supplied name and namespace.
37+
func KubeadmConfig(namespace, name string) *KubeadmConfigBuilder {
38+
return &KubeadmConfigBuilder{
39+
name: name,
40+
namespace: namespace,
41+
}
42+
}
43+
44+
// WithJoinConfig adds the passed JoinConfig to the KubeadmConfigBuilder.
45+
func (k *KubeadmConfigBuilder) WithJoinConfig(joinConf *bootstrapv1.JoinConfiguration) *KubeadmConfigBuilder {
46+
k.joinConfig = joinConf
47+
return k
48+
}
49+
50+
// WithClusterConfig adds the passed ClusterConfig to the KubeadmConfigBuilder.
51+
func (k *KubeadmConfigBuilder) WithClusterConfig(clusterConf *bootstrapv1.ClusterConfiguration) *KubeadmConfigBuilder {
52+
k.clusterConfig = clusterConf
53+
return k
54+
}
55+
56+
// WithInitConfig adds the passed InitConfig to the KubeadmConfigBuilder.
57+
func (k *KubeadmConfigBuilder) WithInitConfig(initConf *bootstrapv1.InitConfiguration) *KubeadmConfigBuilder {
58+
k.initConfig = initConf
59+
return k
60+
}
61+
62+
// Unstructured produces a KubeadmConfig as an unstructured Kubernetes object.
63+
func (k *KubeadmConfigBuilder) Unstructured() *unstructured.Unstructured {
64+
config := k.Build()
65+
rawMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(config)
66+
if err != nil {
67+
panic(err)
68+
}
69+
return &unstructured.Unstructured{Object: rawMap}
70+
}
71+
72+
// Build produces a KubeadmConfig from the variable in the KubeadmConfigBuilder.
73+
func (k *KubeadmConfigBuilder) Build() *bootstrapv1.KubeadmConfig {
74+
config := &bootstrapv1.KubeadmConfig{
75+
TypeMeta: metav1.TypeMeta{
76+
Kind: "KubeadmConfig",
77+
APIVersion: bootstrapv1.GroupVersion.String(),
78+
},
79+
ObjectMeta: metav1.ObjectMeta{
80+
Namespace: k.namespace,
81+
Name: k.name,
82+
},
83+
}
84+
if k.initConfig != nil {
85+
config.Spec.InitConfiguration = k.initConfig
86+
}
87+
if k.joinConfig != nil {
88+
config.Spec.JoinConfiguration = k.joinConfig
89+
}
90+
if k.clusterConfig != nil {
91+
config.Spec.ClusterConfiguration = k.clusterConfig
92+
}
93+
return config
94+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/*
18+
Package builder implements builder and CRDs for creating Bootstrap objects for testing.
19+
Important: KubeadmConfig specific builders should not be used in core CAPI, because this
20+
can lead to false assumptions on what we should expect from a bootstrap config provider.
21+
*/
22+
package builder

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_reconciler_test.go

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

2626
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
27+
"sigs.k8s.io/cluster-api/internal/test/builder"
2728
)
2829

2930
func TestKubeadmConfigReconciler(t *testing.T) {
@@ -34,13 +35,12 @@ func TestKubeadmConfigReconciler(t *testing.T) {
3435
ns, err := env.CreateNamespace(ctx, "test-kubeadm-config-reconciler")
3536
g.Expect(err).To(BeNil())
3637

37-
cluster := newCluster("cluster1", ns.Name)
38+
cluster := builder.Cluster(ns.Name, "cluster1").Build()
3839
g.Expect(env.Create(ctx, cluster)).To(Succeed())
39-
40-
machine := newMachine(cluster, "my-machine", ns.Name)
40+
machine := newWorkerMachineForCluster(cluster)
4141
g.Expect(env.Create(ctx, machine)).To(Succeed())
4242

43-
config := newKubeadmConfig(machine, "my-machine-config", ns.Name)
43+
config := newKubeadmConfig(ns.Name, "my-machine-config")
4444
g.Expect(env.Create(ctx, config)).To(Succeed())
4545
defer func(do ...client.Object) {
4646
g.Expect(env.Cleanup(ctx, do...)).To(Succeed())

0 commit comments

Comments
 (0)