Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 06ae0a0

Browse files
authored
Merge pull request #216 from chuckha/reconcile-test
🏃 Adds a test to reconcile kubeadmconfig based on clusters
2 parents cc2b1e4 + 20470c3 commit 06ae0a0

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

controllers/kubeadmconfig_controller_test.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
ctrl "sigs.k8s.io/controller-runtime"
3939
"sigs.k8s.io/controller-runtime/pkg/client"
4040
"sigs.k8s.io/controller-runtime/pkg/client/fake"
41+
"sigs.k8s.io/controller-runtime/pkg/handler"
4142
"sigs.k8s.io/controller-runtime/pkg/runtime/log"
4243
)
4344

@@ -889,6 +890,45 @@ func TestCACertHashesAndUnsafeCAVerifySkip(t *testing.T) {
889890
}
890891
}
891892

893+
// If a cluster object changes then all associated KubeadmConfigs should be re-reconciled.
894+
// This allows us to not requeue a kubeadm config while we wait for InfrastructureReady.
895+
func TestKubeadmConfigReconciler_ClusterToKubeadmConfigs(t *testing.T) {
896+
cluster := newCluster("my-cluster")
897+
objs := []runtime.Object{cluster}
898+
expectedNames := []string{}
899+
for i := 0; i < 3; i++ {
900+
m := newMachine(cluster, fmt.Sprintf("my-machine-%d", i))
901+
configName := fmt.Sprintf("my-config-%d", i)
902+
c := newKubeadmConfig(m, configName)
903+
expectedNames = append(expectedNames, configName)
904+
objs = append(objs, m, c)
905+
}
906+
fakeClient := fake.NewFakeClientWithScheme(setupScheme(), objs...)
907+
reconciler := &KubeadmConfigReconciler{
908+
Log: log.Log,
909+
Client: fakeClient,
910+
}
911+
o := handler.MapObject{
912+
Object: cluster,
913+
}
914+
configs := reconciler.ClusterToKubeadmConfigs(o)
915+
names := make([]string, 3)
916+
for i := range configs {
917+
names[i] = configs[i].Name
918+
}
919+
for _, name := range expectedNames {
920+
found := false
921+
for _, foundName := range names {
922+
if foundName == name {
923+
found = true
924+
}
925+
}
926+
if !found {
927+
t.Fatalf("did not find %s in %v", name, names)
928+
}
929+
}
930+
}
931+
892932
// test utils
893933

894934
// newCluster return a CAPI cluster object
@@ -920,7 +960,7 @@ func newMachine(cluster *clusterv1.Cluster, name string) *clusterv1.Machine {
920960
Bootstrap: clusterv1.Bootstrap{
921961
ConfigRef: &corev1.ObjectReference{
922962
Kind: "KubeadmConfig",
923-
APIVersion: "v1alpha2",
963+
APIVersion: bootstrapv1.GroupVersion.String(),
924964
},
925965
},
926966
},
@@ -964,6 +1004,8 @@ func newKubeadmConfig(machine *clusterv1.Machine, name string) *bootstrapv1.Kube
9641004
UID: types.UID(fmt.Sprintf("%s uid", machine.Name)),
9651005
},
9661006
}
1007+
machine.Spec.Bootstrap.ConfigRef.Name = config.Name
1008+
machine.Spec.Bootstrap.ConfigRef.Namespace = config.Namespace
9671009
}
9681010
return config
9691011
}

0 commit comments

Comments
 (0)