@@ -3,14 +3,10 @@ package cmd
33import (
44 "context"
55 "fmt"
6- "strings"
7- "time"
86
97 "github.com/openshift/microshift/pkg/config"
10- "github.com/openshift/microshift/pkg/util/cryptomaterial "
8+ "github.com/openshift/microshift/pkg/controllers "
119 "github.com/spf13/cobra"
12- "go.etcd.io/etcd/client/pkg/v3/transport"
13- clientv3 "go.etcd.io/etcd/client/v3"
1410
1511 "k8s.io/klog/v2"
1612)
@@ -42,19 +38,8 @@ func runPromoteLearner(ctx context.Context) error {
4238 return fmt .Errorf ("failed to load MicroShift configuration: %w" , err )
4339 }
4440
45- klog .Infof ("Creating Kubernetes client from %s" , cfg .KubeConfigPath (config .KubeAdmin ))
46- client , err := createKubernetesClient (cfg .KubeConfigPath (config .KubeAdmin ))
47- if err != nil {
48- return fmt .Errorf ("failed to create Kubernetes client: %w" , err )
49- }
50-
51- etcdMembers , err := getEtcdClusterNodes (ctx , client )
52- if err != nil {
53- return fmt .Errorf ("failed to get cluster information: %w" , err )
54- }
55-
5641 klog .Info ("Promoting etcd learner to member" )
57- if err := promoteEtcdLearner (ctx , etcdMembers , cfg ); err != nil {
42+ if err := promoteEtcdLearner (ctx , cfg ); err != nil {
5843 return fmt .Errorf ("failed to promote etcd learner: %w" , err )
5944 }
6045
@@ -63,38 +48,12 @@ func runPromoteLearner(ctx context.Context) error {
6348 return nil
6449}
6550
66- func promoteEtcdLearner (ctx context.Context , clusterMembers []string , cfg * config.Config ) error {
67- certsDir := cryptomaterial .CertsDirectory (config .DataDir )
68- etcdPeerClientCertDir := cryptomaterial .EtcdPeerCertDir (certsDir )
69-
70- tlsInfo := transport.TLSInfo {
71- CertFile : cryptomaterial .PeerCertPath (etcdPeerClientCertDir ),
72- KeyFile : cryptomaterial .PeerKeyPath (etcdPeerClientCertDir ),
73- TrustedCAFile : cryptomaterial .CACertPath (cryptomaterial .EtcdSignerDir (certsDir )),
74- }
75- tlsConfig , err := tlsInfo .ClientConfig ()
76- if err != nil {
77- return fmt .Errorf ("failed to create etcd client TLS config: %v" , err )
78- }
79- var endpoints []string
80- for _ , member := range clusterMembers {
81- parts := strings .SplitN (member , "=" , 2 )
82- if len (parts ) == 2 {
83- endpoint := strings .Replace (parts [1 ], ":2380" , ":2379" , 1 )
84- endpoints = append (endpoints , endpoint )
85- }
86- }
87-
88- client , err := clientv3 .New (clientv3.Config {
89- Endpoints : endpoints ,
90- DialTimeout : 5 * time .Second ,
91- TLS : tlsConfig ,
92- Context : ctx ,
93- })
51+ func promoteEtcdLearner (ctx context.Context , cfg * config.Config ) error {
52+ client , err := controllers .GetClusterEtcdClient (ctx , cfg .KubeConfigPath (config .KubeAdmin ))
9453 if err != nil {
9554 return fmt .Errorf ("failed to create etcd client: %v" , err )
9655 }
97- defer client .Close ()
56+ defer func (){ _ = client .Close () } ()
9857
9958 memberResponse , err := client .MemberList (ctx )
10059 if err != nil {
@@ -126,6 +85,5 @@ func promoteEtcdLearner(ctx context.Context, clusterMembers []string, cfg *confi
12685 return fmt .Errorf ("failed to promote etcd learner: %v" , err )
12786 }
12887 klog .Infof ("Successfully promoted etcd learner %s with response: %v" , cfg .CanonicalNodeName (), response )
129-
13088 return nil
13189}
0 commit comments