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

Commit 6a23e88

Browse files
committed
Add test for existing Etcd cert
Signed-off-by: Chuck Ha <[email protected]>
1 parent b612d5d commit 6a23e88

File tree

7 files changed

+100
-87
lines changed

7 files changed

+100
-87
lines changed

Dockerfile.dev

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ COPY api/ api/
3333
COPY controllers/ controllers/
3434
COPY kubeadm/ kubeadm/
3535
COPY cloudinit/ cloudinit/
36-
COPY certs/ certs/
3736
COPY internal/ internal/
3837

3938
# Allow containerd to restart pods by calling /restart.sh (mostly for tilt + fast dev cycles)

cloudinit/cloudinit_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import (
2121
"testing"
2222

2323
infrav1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
24-
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal"
24+
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal/cluster"
25+
"sigs.k8s.io/cluster-api/util/certs"
2526
)
2627

2728
func TestNewInitControlPlaneAdditionalFileEncodings(t *testing.T) {
@@ -45,14 +46,16 @@ func TestNewInitControlPlaneAdditionalFileEncodings(t *testing.T) {
4546
Users: nil,
4647
NTP: nil,
4748
},
48-
Certificates: internal.NewCertificates(),
49+
Certificates: cluster.NewCertificates(),
4950
ClusterConfiguration: "my-cluster-config",
5051
InitConfiguration: "my-init-config",
5152
}
5253

5354
for _, certificate := range cpinput.Certificates {
54-
certificate.KeyPair.Cert = []byte("some certificate")
55-
certificate.KeyPair.Key = []byte("some key")
55+
certificate.KeyPair = &certs.KeyPair{
56+
Cert: []byte("some certificate"),
57+
Key: []byte("some key"),
58+
}
5659
}
5760

5861
out, err := NewInitControlPlane(cpinput)

cloudinit/controlplane_init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package cloudinit
1818

1919
import (
20-
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal"
20+
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal/cluster"
2121
)
2222

2323
const (
@@ -43,7 +43,7 @@ runcmd:
4343
// ControlPlaneInput defines the context to generate a controlplane instance user data.
4444
type ControlPlaneInput struct {
4545
BaseUserData
46-
internal.Certificates
46+
cluster.Certificates
4747

4848
ClusterConfiguration string
4949
InitConfiguration string

cloudinit/controlplane_join.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package cloudinit
1818

1919
import (
2020
"github.com/pkg/errors"
21-
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal"
21+
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal/cluster"
2222
)
2323

2424
const (
@@ -41,7 +41,7 @@ runcmd:
4141
// ControlPlaneJoinInput defines context to generate controlplane instance user data for control plane node join.
4242
type ControlPlaneJoinInput struct {
4343
BaseUserData
44-
internal.Certificates
44+
cluster.Certificates
4545

4646
BootstrapToken string
4747
JoinConfiguration string

controllers/kubeadmconfig_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
3030
bootstrapv1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
3131
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/cloudinit"
32-
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal"
32+
internalcluster "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal/cluster"
3333
kubeadmv1beta1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/kubeadm/v1beta1"
3434
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2"
3535
capierrors "sigs.k8s.io/cluster-api/errors"
@@ -217,7 +217,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
217217
return ctrl.Result{}, err
218218
}
219219

220-
certificates := internal.NewCertificates()
220+
certificates := internalcluster.NewCertificates()
221221
if err := certificates.GetOrCreateCertificates(ctx, r.Client, cluster, config); err != nil {
222222
log.Error(err, "unable to lookup or create cluster certificates")
223223
return ctrl.Result{}, err
@@ -258,12 +258,12 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
258258
return ctrl.Result{}, errors.New("Control plane already exists for the cluster, only KubeadmConfig objects with JoinConfiguration are allowed")
259259
}
260260

261-
certificates := internal.NewCertificates()
261+
certificates := internalcluster.NewCertificates()
262262
if err := certificates.GetCertificates(ctx, r.Client, cluster); err != nil {
263263
log.Error(err, "unable to lookup cluster certificates")
264264
return ctrl.Result{}, err
265265
}
266-
hashes, err := certificates.GetCertificateByName(internal.ClusterCAName).Hashes()
266+
hashes, err := certificates.GetCertificateByName(internalcluster.ClusterCAName).Hashes()
267267
if err != nil {
268268
log.Error(err, "Unable to generate Cluster CA certificate hashes")
269269
return ctrl.Result{}, err

controllers/kubeadmconfig_controller_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
cluster2 "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"
@@ -966,6 +966,38 @@ func TestKubeadmConfigReconciler_ClusterToKubeadmConfigs(t *testing.T) {
966966
}
967967
}
968968

969+
// Reconcile should not fail if the Etcd CA Secret already exists
970+
func TestKubeadmConfigReconciler_Reconcile_DoesNotFailIfCASecretsAlreadyExist(t *testing.T) {
971+
cluster := newCluster("my-cluster")
972+
cluster.Status.InfrastructureReady = true
973+
cluster.Status.ControlPlaneInitialized = false
974+
m := newControlPlaneMachine(cluster)
975+
configName := "my-config"
976+
c := newControlPlaneInitKubeadmConfig(m, configName)
977+
scrt := &corev1.Secret{
978+
ObjectMeta: metav1.ObjectMeta{
979+
Name: fmt.Sprintf("%s-%s", cluster.Name, cluster2.EtcdCAName),
980+
Namespace: "default",
981+
},
982+
Data: map[string][]byte{
983+
"tls.crt": []byte("hello world"),
984+
"tls.key": []byte("hello world"),
985+
},
986+
}
987+
fakec := fake.NewFakeClientWithScheme(setupScheme(), []runtime.Object{cluster, m, c, scrt}...)
988+
reconciler := &KubeadmConfigReconciler{
989+
Log: log.Log,
990+
Client: fakec,
991+
KubeadmInitLock: &myInitLocker{},
992+
}
993+
req := ctrl.Request{
994+
NamespacedName: types.NamespacedName{Namespace: "default", Name: configName},
995+
}
996+
if _, err := reconciler.Reconcile(req); err != nil {
997+
t.Fatal(err)
998+
}
999+
}
1000+
9691001
// test utils
9701002

9711003
// newCluster return a CAPI cluster object
@@ -1072,7 +1104,7 @@ func newControlPlaneInitKubeadmConfig(machine *clusterv1.Machine, name string) *
10721104

10731105
func createSecrets(t *testing.T, cluster *clusterv1.Cluster, owner *bootstrapv1.KubeadmConfig) []runtime.Object {
10741106
out := []runtime.Object{}
1075-
certificates := internal.NewCertificates()
1107+
certificates := cluster2.NewCertificates()
10761108
if err := certificates.GenerateCertificates(); err != nil {
10771109
t.Fatal(err)
10781110
}

0 commit comments

Comments
 (0)