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

Commit 54bb858

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

File tree

8 files changed

+469
-421
lines changed

8 files changed

+469
-421
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: 3 additions & 3 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
@@ -52,7 +52,7 @@ type ControlPlaneInput struct {
5252
// NewInitControlPlane returns the user data string to be used on a controlplane instance.
5353
func NewInitControlPlane(input *ControlPlaneInput) ([]byte, error) {
5454
input.Header = cloudConfigHeader
55-
if err := input.Certificates.Validate(); err != nil {
55+
if err := input.Certificates.EnsureAllExist(); err != nil {
5656
return nil, err
5757
}
5858

cloudinit/controlplane_join.go

Lines changed: 4 additions & 4 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
@@ -50,8 +50,8 @@ type ControlPlaneJoinInput struct {
5050
// NewJoinControlPlane returns the user data string to be used on a new control plane instance.
5151
func NewJoinControlPlane(input *ControlPlaneJoinInput) ([]byte, error) {
5252
input.Header = cloudConfigHeader
53-
if err := input.Certificates.Validate(); err != nil {
54-
return nil, errors.Wrapf(err, "ControlPlaneInput is invalid")
53+
if err := input.Certificates.EnsureAllExist(); err != nil {
54+
return nil, err
5555
}
5656

5757
input.WriteFiles = input.Certificates.AsFiles()

controllers/kubeadmconfig_controller.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ 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"
3636
"sigs.k8s.io/cluster-api/util"
3737
"sigs.k8s.io/cluster-api/util/patch"
38+
"sigs.k8s.io/cluster-api/util/secret"
3839
ctrl "sigs.k8s.io/controller-runtime"
3940
"sigs.k8s.io/controller-runtime/pkg/client"
4041
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -217,8 +218,8 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
217218
return ctrl.Result{}, err
218219
}
219220

220-
certificates := internal.NewCertificates()
221-
if err := certificates.GetOrCreateCertificates(ctx, r.Client, cluster, config); err != nil {
221+
certificates := internalcluster.NewCertificates()
222+
if err := certificates.LookupOrGenerate(ctx, r.Client, cluster, config); err != nil {
222223
log.Error(err, "unable to lookup or create cluster certificates")
223224
return ctrl.Result{}, err
224225
}
@@ -258,22 +259,25 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
258259
return ctrl.Result{}, errors.New("Control plane already exists for the cluster, only KubeadmConfig objects with JoinConfiguration are allowed")
259260
}
260261

261-
certificates := internal.NewCertificates()
262-
if err := certificates.GetCertificates(ctx, r.Client, cluster); err != nil {
262+
certificates := internalcluster.NewCertificates()
263+
if err := certificates.Lookup(ctx, r.Client, cluster); err != nil {
263264
log.Error(err, "unable to lookup cluster certificates")
264265
return ctrl.Result{}, err
265266
}
266-
hashes, err := certificates.GetCertificateByName(internal.ClusterCAName).Hashes()
267+
if err := certificates.EnsureAllExist(); err != nil {
268+
return ctrl.Result{}, err
269+
}
270+
271+
hashes, err := certificates.GetByPurpose(secret.ClusterCA).Hashes()
267272
if err != nil {
268273
log.Error(err, "Unable to generate Cluster CA certificate hashes")
269274
return ctrl.Result{}, err
270275
}
271-
if hashes != nil {
272-
if config.Spec.JoinConfiguration.Discovery.BootstrapToken == nil {
273-
config.Spec.JoinConfiguration.Discovery.BootstrapToken = &kubeadmv1beta1.BootstrapTokenDiscovery{}
274-
}
275-
config.Spec.JoinConfiguration.Discovery.BootstrapToken.CACertHashes = hashes
276+
// TODO: move this into reconcile.Discovery so that defaults for the Discovery are all in the same place
277+
if config.Spec.JoinConfiguration.Discovery.BootstrapToken == nil {
278+
config.Spec.JoinConfiguration.Discovery.BootstrapToken = &kubeadmv1beta1.BootstrapTokenDiscovery{}
276279
}
280+
config.Spec.JoinConfiguration.Discovery.BootstrapToken.CACertHashes = hashes
277281

278282
// ensure that joinConfiguration.Discovery is properly set for joining node on the current cluster
279283
if err := r.reconcileDiscovery(cluster, config); err != nil {

controllers/kubeadmconfig_controller_test.go

Lines changed: 46 additions & 6 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+
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.
373373
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)
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

10731113
func 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

Comments
 (0)