@@ -32,7 +32,7 @@ import (
3232 typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
3333 "k8s.io/klog/klogr"
3434 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 "
3636 kubeadmv1beta1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/kubeadm/v1beta1"
3737 clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2"
3838 ctrl "sigs.k8s.io/controller-runtime"
@@ -371,20 +371,24 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfAWorkerHasNoJoinConfigurationA
371371
372372// If a controlplane has an invalid JoinConfiguration then user intervention is required.
373373func 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)
374375 cluster := newCluster ("cluster" )
375376 cluster .Status .InfrastructureReady = true
376377 cluster .Status .ControlPlaneInitialized = true
377378 cluster .Status .APIEndpoints = []clusterv1.APIEndpoint {{Host : "100.105.150.1" , Port : 6443 }}
378-
379379 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" )
381384 controlPlaneJoinConfig .Spec .JoinConfiguration .ControlPlane = nil // Makes controlPlaneJoinConfig invalid for a control plane machine
382385
383386 objects := []runtime.Object {
384387 cluster ,
385- controlPlaneMachine ,
388+ controlPlaneJoinMachine ,
386389 controlPlaneJoinConfig ,
387390 }
391+ objects = append (objects , createSecrets (t , cluster , controlPlaneInitConfig )... )
388392 myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
389393
390394 k := & KubeadmConfigReconciler {
@@ -411,6 +415,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
411415 cluster := newCluster ("cluster" )
412416 cluster .Status .InfrastructureReady = true
413417 cluster .Status .ControlPlaneInitialized = true
418+ controlPlaneMachine := newControlPlaneMachine (cluster )
419+ controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneMachine , "control-plane-init-cfg" )
414420
415421 workerMachine := newWorkerMachine (cluster )
416422 workerJoinConfig := newWorkerJoinKubeadmConfig (workerMachine )
@@ -420,6 +426,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
420426 workerMachine ,
421427 workerJoinConfig ,
422428 }
429+ objects = append (objects , createSecrets (t , cluster , controlPlaneInitConfig )... )
430+
423431 myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
424432
425433 k := & KubeadmConfigReconciler {
@@ -966,6 +974,38 @@ func TestKubeadmConfigReconciler_ClusterToKubeadmConfigs(t *testing.T) {
966974 }
967975}
968976
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 .EtcdCA ),
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+
9691009// test utils
9701010
9711011// newCluster return a CAPI cluster object
@@ -1072,8 +1112,8 @@ func newControlPlaneInitKubeadmConfig(machine *clusterv1.Machine, name string) *
10721112
10731113func createSecrets (t * testing.T , cluster * clusterv1.Cluster , owner * bootstrapv1.KubeadmConfig ) []runtime.Object {
10741114 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 {
10771117 t .Fatal (err )
10781118 }
10791119 for _ , certificate := range certificates {
0 commit comments