@@ -21,6 +21,7 @@ import (
2121 "errors"
2222 "testing"
2323
24+ "github.com/blang/semver"
2425 . "github.com/onsi/gomega"
2526
2627 "go.etcd.io/etcd/clientv3"
@@ -466,21 +467,24 @@ func TestReconcileEtcdMembers(t *testing.T) {
466467 Namespace : metav1 .NamespaceSystem ,
467468 },
468469 Data : map [string ]string {
469- clusterStatusKey : ` apiEndpoints:
470- ip-10-0-0-1.ec2.internal:
471- advertiseAddress: 10.0.0.1
472- bindPort: 6443
473- ip-10-0-0-2.ec2.internal:
474- advertiseAddress: 10.0.0.2
475- bindPort: 6443
476- someFieldThatIsAddedInTheFuture: bar
477- ip-10-0-0-3.ec2.internal:
478- advertiseAddress: 10.0.0.3
479- bindPort: 6443
480- apiVersion: kubeadm.k8s.io/vNbetaM
481- kind: ClusterStatus` ,
470+ clusterStatusKey : " apiEndpoints:\n " +
471+ " ip-10-0-0-1.ec2.internal:\n " +
472+ " advertiseAddress: 10.0.0.1\n " +
473+ " bindPort: 6443\n " +
474+ " ip-10-0-0-2.ec2.internal:\n " +
475+ " advertiseAddress: 10.0.0.2\n " +
476+ " bindPort: 6443\n " +
477+ " someFieldThatIsAddedInTheFuture: bar\n " +
478+ " ip-10-0-0-3.ec2.internal:\n " +
479+ " advertiseAddress: 10.0.0.3\n " +
480+ " bindPort: 6443\n " +
481+ " apiVersion: kubeadm.k8s.io/vNbetaM\n " +
482+ " kind: ClusterStatus\n " ,
482483 },
483484 }
485+ kubeadmConfigWithoutClusterStatus := kubeadmConfig .DeepCopy ()
486+ delete (kubeadmConfigWithoutClusterStatus .Data , clusterStatusKey )
487+
484488 node1 := & corev1.Node {
485489 ObjectMeta : metav1.ObjectMeta {
486490 Name : "ip-10-0-0-1.ec2.internal" ,
@@ -508,25 +512,62 @@ kind: ClusterStatus`,
508512
509513 tests := []struct {
510514 name string
515+ kubernetesVersion semver.Version
511516 objs []runtime.Object
512517 nodes []string
513518 etcdClientGenerator etcdClientFor
514519 expectErr bool
515- assert func (* WithT )
520+ assert func (* WithT , client. Client )
516521 }{
517522 {
518523 // the node to be removed is ip-10-0-0-3.ec2.internal since the
519524 // other two have nodes
520- name : "successfully removes the etcd member without a node and removes the node from kubeadm config" ,
521- objs : []runtime.Object {node1 .DeepCopy (), node2 .DeepCopy (), kubeadmConfig .DeepCopy ()},
522- nodes : []string {node1 .Name , node2 .Name },
525+ name : "successfully removes the etcd member without a node and removes the node from kubeadm config for Kubernetes version < 1.22.0" ,
526+ kubernetesVersion : kubernetesVersionWithClusterStatus , // Kubernetes version < 1.22.0 has ClusterStatus
527+ objs : []runtime.Object {node1 .DeepCopy (), node2 .DeepCopy (), kubeadmConfig .DeepCopy ()},
528+ nodes : []string {node1 .Name , node2 .Name },
529+ etcdClientGenerator : & fakeEtcdClientGenerator {
530+ forNodesClient : & etcd.Client {
531+ EtcdClient : fakeEtcdClient ,
532+ },
533+ },
534+ expectErr : false ,
535+ assert : func (g * WithT , c client.Client ) {
536+ g .Expect (fakeEtcdClient .RemovedMember ).To (Equal (uint64 (3 )))
537+
538+ var actualConfig corev1.ConfigMap
539+ g .Expect (c .Get (
540+ ctx ,
541+ ctrlclient.ObjectKey {Name : kubeadmConfigKey , Namespace : metav1 .NamespaceSystem },
542+ & actualConfig ,
543+ )).To (Succeed ())
544+ g .Expect (actualConfig .Data [clusterStatusKey ]).To (Equal ("apiEndpoints:\n " +
545+ " ip-10-0-0-1.ec2.internal:\n " +
546+ " advertiseAddress: 10.0.0.1\n " +
547+ " bindPort: 6443\n " +
548+ " ip-10-0-0-2.ec2.internal:\n " +
549+ " advertiseAddress: 10.0.0.2\n " +
550+ " bindPort: 6443\n " +
551+ " someFieldThatIsAddedInTheFuture: bar\n " +
552+ "apiVersion: kubeadm.k8s.io/vNbetaM\n " +
553+ "kind: ClusterStatus\n " ))
554+
555+ },
556+ },
557+ {
558+ // the node to be removed is ip-10-0-0-3.ec2.internal since the
559+ // other two have nodes
560+ name : "successfully removes the etcd member without a node for Kubernetes version >= 1.22.0" ,
561+ kubernetesVersion : minKubernetesVersionWithoutClusterStatus , // Kubernetes version >= 1.22.0 should not manage ClusterStatus
562+ objs : []runtime.Object {node1 .DeepCopy (), node2 .DeepCopy (), kubeadmConfigWithoutClusterStatus .DeepCopy ()},
563+ nodes : []string {node1 .Name , node2 .Name },
523564 etcdClientGenerator : & fakeEtcdClientGenerator {
524565 forNodesClient : & etcd.Client {
525566 EtcdClient : fakeEtcdClient ,
526567 },
527568 },
528569 expectErr : false ,
529- assert : func (g * WithT ) {
570+ assert : func (g * WithT , c client. Client ) {
530571 g .Expect (fakeEtcdClient .RemovedMember ).To (Equal (uint64 (3 )))
531572 },
532573 },
@@ -559,15 +600,15 @@ kind: ClusterStatus`,
559600 etcdClientGenerator : tt .etcdClientGenerator ,
560601 }
561602 ctx := context .TODO ()
562- _ , err := w .ReconcileEtcdMembers (ctx , tt .nodes )
603+ _ , err := w .ReconcileEtcdMembers (ctx , tt .nodes , tt . kubernetesVersion )
563604 if tt .expectErr {
564605 g .Expect (err ).To (HaveOccurred ())
565606 return
566607 }
567608 g .Expect (err ).ToNot (HaveOccurred ())
568609
569610 if tt .assert != nil {
570- tt .assert (g )
611+ tt .assert (g , testEnv . Client )
571612 }
572613 })
573614 }
0 commit comments