Skip to content

Commit 366530a

Browse files
committed
Add initial tests for kubeadmcontrolplane controller reconcile method
- Add additional generators for kubeadmcontrolplane reconciliation testing
1 parent 83c7edf commit 366530a

7 files changed

+1148
-41
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2019 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 generator
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
"github.com/pkg/errors"
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
28+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
29+
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
30+
)
31+
32+
type KubeadmConfigGenerator struct{}
33+
34+
func (kcg *KubeadmConfigGenerator) GenerateKubeadmConfig(ctx context.Context, c client.Client, namespace, namePrefix, clusterName string, spec *bootstrapv1.KubeadmConfigSpec, owner *metav1.OwnerReference) (*corev1.ObjectReference, error) {
35+
bootstrapConfig := &bootstrapv1.KubeadmConfig{
36+
ObjectMeta: metav1.ObjectMeta{
37+
GenerateName: fmt.Sprintf("%s-", namePrefix),
38+
Namespace: namespace,
39+
Labels: map[string]string{clusterv1.ClusterLabelName: clusterName},
40+
},
41+
Spec: *spec,
42+
}
43+
if owner != nil {
44+
bootstrapConfig.SetOwnerReferences([]metav1.OwnerReference{*owner})
45+
}
46+
if err := c.Create(ctx, bootstrapConfig); err != nil {
47+
return nil, errors.Wrap(err, "Failed to create bootstrap configuration")
48+
}
49+
50+
bootstrapRef := &corev1.ObjectReference{
51+
APIVersion: bootstrapv1.GroupVersion.String(),
52+
Kind: "KubeadmConfig",
53+
Name: bootstrapConfig.GetName(),
54+
Namespace: bootstrapConfig.GetNamespace(),
55+
UID: bootstrapConfig.GetUID(),
56+
}
57+
58+
return bootstrapRef, nil
59+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Copyright 2019 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 generator
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/client-go/kubernetes/scheme"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
28+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
29+
30+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
31+
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
32+
)
33+
34+
func TestGenerateKubeadmConfigWithOwner(t *testing.T) {
35+
g := gomega.NewWithT(t)
36+
g.Expect(bootstrapv1.AddToScheme(scheme.Scheme)).To(gomega.Succeed())
37+
fakeClient := fake.NewFakeClientWithScheme(scheme.Scheme)
38+
39+
namespace := "test"
40+
namePrefix := "generate"
41+
clusterName := "foo"
42+
spec := &bootstrapv1.KubeadmConfigSpec{}
43+
owner := &metav1.OwnerReference{
44+
Kind: "Cluster",
45+
APIVersion: clusterv1.GroupVersion.String(),
46+
Name: clusterName,
47+
}
48+
expectedReference := &corev1.ObjectReference{
49+
Name: "",
50+
Namespace: namespace,
51+
APIVersion: bootstrapv1.GroupVersion.String(),
52+
Kind: "KubeadmConfig",
53+
}
54+
expectedConfig := &bootstrapv1.KubeadmConfig{
55+
TypeMeta: metav1.TypeMeta{
56+
Kind: "KubeadmConfig",
57+
APIVersion: bootstrapv1.GroupVersion.String(),
58+
},
59+
ObjectMeta: metav1.ObjectMeta{
60+
GenerateName: namePrefix + "-",
61+
Namespace: namespace,
62+
ResourceVersion: "1",
63+
Labels: map[string]string{clusterv1.ClusterLabelName: "foo"},
64+
OwnerReferences: []metav1.OwnerReference{*owner},
65+
},
66+
Spec: *spec,
67+
Status: bootstrapv1.KubeadmConfigStatus{},
68+
}
69+
70+
kcg := &KubeadmConfigGenerator{}
71+
got, err := kcg.GenerateKubeadmConfig(
72+
context.Background(),
73+
fakeClient,
74+
namespace,
75+
namePrefix,
76+
clusterName,
77+
spec,
78+
owner,
79+
)
80+
g.Expect(err).NotTo(gomega.HaveOccurred())
81+
g.Expect(got).To(gomega.Equal(expectedReference))
82+
83+
bootstrapConfig := &bootstrapv1.KubeadmConfig{}
84+
key := client.ObjectKey{Name: got.Name, Namespace: got.Namespace}
85+
g.Expect(fakeClient.Get(context.Background(), key, bootstrapConfig)).To(gomega.Succeed())
86+
g.Expect(bootstrapConfig).To(gomega.Equal(expectedConfig))
87+
}
88+
89+
func TestGenerateKubeadmConfigWithoutOwner(t *testing.T) {
90+
g := gomega.NewWithT(t)
91+
g.Expect(bootstrapv1.AddToScheme(scheme.Scheme)).To(gomega.Succeed())
92+
fakeClient := fake.NewFakeClientWithScheme(scheme.Scheme)
93+
94+
namespace := "test"
95+
namePrefix := "generated"
96+
clusterName := "foo"
97+
spec := &bootstrapv1.KubeadmConfigSpec{}
98+
expectedReference := &corev1.ObjectReference{
99+
Name: "",
100+
Namespace: namespace,
101+
APIVersion: bootstrapv1.GroupVersion.String(),
102+
Kind: "KubeadmConfig",
103+
}
104+
expectedConfig := &bootstrapv1.KubeadmConfig{
105+
TypeMeta: metav1.TypeMeta{
106+
Kind: "KubeadmConfig",
107+
APIVersion: bootstrapv1.GroupVersion.String(),
108+
},
109+
ObjectMeta: metav1.ObjectMeta{
110+
GenerateName: namePrefix + "-",
111+
Namespace: namespace,
112+
ResourceVersion: "1",
113+
Labels: map[string]string{clusterv1.ClusterLabelName: "foo"},
114+
},
115+
Spec: *spec,
116+
Status: bootstrapv1.KubeadmConfigStatus{},
117+
}
118+
119+
kcg := &KubeadmConfigGenerator{}
120+
got, err := kcg.GenerateKubeadmConfig(
121+
context.Background(),
122+
fakeClient,
123+
namespace,
124+
namePrefix,
125+
clusterName,
126+
spec,
127+
nil,
128+
)
129+
g.Expect(err).NotTo(gomega.HaveOccurred())
130+
g.Expect(got).To(gomega.Equal(expectedReference))
131+
132+
bootstrapConfig := &bootstrapv1.KubeadmConfig{}
133+
key := client.ObjectKey{Name: got.Name, Namespace: got.Namespace}
134+
g.Expect(fakeClient.Get(context.Background(), key, bootstrapConfig)).To(gomega.Succeed())
135+
g.Expect(bootstrapConfig).To(gomega.Equal(expectedConfig))
136+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2019 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 generator
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
"github.com/pkg/errors"
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
28+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
29+
)
30+
31+
type MachineGenerator struct{}
32+
33+
func (mgg *MachineGenerator) GenerateMachine(ctx context.Context, c client.Client, namespace, namePrefix, clusterName, version string, infraRef, bootstrapRef *corev1.ObjectReference, labels map[string]string, owner *metav1.OwnerReference) error {
34+
machine := &clusterv1.Machine{
35+
ObjectMeta: metav1.ObjectMeta{
36+
GenerateName: fmt.Sprintf("%s-", namePrefix),
37+
Labels: labels,
38+
Namespace: namespace,
39+
},
40+
Spec: clusterv1.MachineSpec{
41+
ClusterName: clusterName,
42+
Version: &version,
43+
InfrastructureRef: *infraRef,
44+
Bootstrap: clusterv1.Bootstrap{
45+
ConfigRef: bootstrapRef,
46+
},
47+
},
48+
}
49+
50+
if owner != nil {
51+
machine.SetOwnerReferences([]metav1.OwnerReference{*owner})
52+
}
53+
54+
if err := c.Create(ctx, machine); err != nil {
55+
return errors.Wrap(err, "Failed to create machine")
56+
}
57+
58+
return nil
59+
}

0 commit comments

Comments
 (0)