@@ -32,7 +32,7 @@ import (
32
32
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
33
33
"k8s.io/klog/klogr"
34
34
bootstrapv1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
35
- "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal"
35
+ internalcluster "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal/cluster "
36
36
kubeadmv1beta1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/kubeadm/v1beta1"
37
37
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2"
38
38
ctrl "sigs.k8s.io/controller-runtime"
@@ -371,20 +371,24 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfAWorkerHasNoJoinConfigurationA
371
371
372
372
// If a controlplane has an invalid JoinConfiguration then user intervention is required.
373
373
func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidConfiguration (t * testing.T ) {
374
+ // TODO: extract this kind of code into a setup function that puts the state of objects into an initialized controlplane (implies secrets exist)
374
375
cluster := newCluster ("cluster" )
375
376
cluster .Status .InfrastructureReady = true
376
377
cluster .Status .ControlPlaneInitialized = true
377
378
cluster .Status .APIEndpoints = []clusterv1.APIEndpoint {{Host : "100.105.150.1" , Port : 6443 }}
378
-
379
379
controlPlaneMachine := newControlPlaneMachine (cluster )
380
- controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneMachine , "control-plane-join-cfg" )
380
+ controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneMachine , "control-plane-init-cfg" )
381
+
382
+ controlPlaneJoinMachine := newControlPlaneMachine (cluster )
383
+ controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig (controlPlaneJoinMachine , "control-plane-join-cfg" )
381
384
controlPlaneJoinConfig .Spec .JoinConfiguration .ControlPlane = nil // Makes controlPlaneJoinConfig invalid for a control plane machine
382
385
383
386
objects := []runtime.Object {
384
387
cluster ,
385
- controlPlaneMachine ,
388
+ controlPlaneJoinMachine ,
386
389
controlPlaneJoinConfig ,
387
390
}
391
+ objects = append (objects , createSecrets (t , cluster , controlPlaneInitConfig )... )
388
392
myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
389
393
390
394
k := & KubeadmConfigReconciler {
@@ -411,6 +415,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
411
415
cluster := newCluster ("cluster" )
412
416
cluster .Status .InfrastructureReady = true
413
417
cluster .Status .ControlPlaneInitialized = true
418
+ controlPlaneMachine := newControlPlaneMachine (cluster )
419
+ controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneMachine , "control-plane-init-cfg" )
414
420
415
421
workerMachine := newWorkerMachine (cluster )
416
422
workerJoinConfig := newWorkerJoinKubeadmConfig (workerMachine )
@@ -420,6 +426,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
420
426
workerMachine ,
421
427
workerJoinConfig ,
422
428
}
429
+ objects = append (objects , createSecrets (t , cluster , controlPlaneInitConfig )... )
430
+
423
431
myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
424
432
425
433
k := & KubeadmConfigReconciler {
@@ -966,6 +974,38 @@ func TestKubeadmConfigReconciler_ClusterToKubeadmConfigs(t *testing.T) {
966
974
}
967
975
}
968
976
977
+ // Reconcile should not fail if the Etcd CA Secret already exists
978
+ func TestKubeadmConfigReconciler_Reconcile_DoesNotFailIfCASecretsAlreadyExist (t * testing.T ) {
979
+ cluster := newCluster ("my-cluster" )
980
+ cluster .Status .InfrastructureReady = true
981
+ cluster .Status .ControlPlaneInitialized = false
982
+ m := newControlPlaneMachine (cluster )
983
+ configName := "my-config"
984
+ c := newControlPlaneInitKubeadmConfig (m , configName )
985
+ scrt := & corev1.Secret {
986
+ ObjectMeta : metav1.ObjectMeta {
987
+ Name : fmt .Sprintf ("%s-%s" , cluster .Name , internalcluster .EtcdCAName ),
988
+ Namespace : "default" ,
989
+ },
990
+ Data : map [string ][]byte {
991
+ "tls.crt" : []byte ("hello world" ),
992
+ "tls.key" : []byte ("hello world" ),
993
+ },
994
+ }
995
+ fakec := fake .NewFakeClientWithScheme (setupScheme (), []runtime.Object {cluster , m , c , scrt }... )
996
+ reconciler := & KubeadmConfigReconciler {
997
+ Log : log .Log ,
998
+ Client : fakec ,
999
+ KubeadmInitLock : & myInitLocker {},
1000
+ }
1001
+ req := ctrl.Request {
1002
+ NamespacedName : types.NamespacedName {Namespace : "default" , Name : configName },
1003
+ }
1004
+ if _ , err := reconciler .Reconcile (req ); err != nil {
1005
+ t .Fatal (err )
1006
+ }
1007
+ }
1008
+
969
1009
// test utils
970
1010
971
1011
// newCluster return a CAPI cluster object
@@ -1072,8 +1112,8 @@ func newControlPlaneInitKubeadmConfig(machine *clusterv1.Machine, name string) *
1072
1112
1073
1113
func createSecrets (t * testing.T , cluster * clusterv1.Cluster , owner * bootstrapv1.KubeadmConfig ) []runtime.Object {
1074
1114
out := []runtime.Object {}
1075
- certificates := internal .NewCertificates ()
1076
- if err := certificates .GenerateCertificates (); err != nil {
1115
+ certificates := internalcluster .NewCertificates ()
1116
+ if err := certificates .Generate (); err != nil {
1077
1117
t .Fatal (err )
1078
1118
}
1079
1119
for _ , certificate := range certificates {
0 commit comments