etcdctl: fix move-leader for multiple endpoints

Due to a duplicate call of clientConfigFromCmd, the move-leader command
would fail with "conflicting environment variable is shadowed by corresponding command-line flag".
Also in scenarios where no command-line flag was supplied.

Signed-off-by: Thomas Jungblut <tjungblu@redhat.com>
This commit is contained in:
Thomas Jungblut 2022-09-08 11:20:15 +02:00
parent 0e1c895108
commit 3fc16608f7
2 changed files with 32 additions and 16 deletions

View File

@ -43,9 +43,10 @@ func transferLeadershipCommandFunc(cmd *cobra.Command, args []string) {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err) cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)
} }
c := mustClientFromCmd(cmd) cfg := clientConfigFromCmd(cmd)
eps := c.Endpoints() cli := mustClient(cfg)
c.Close() eps := cli.Endpoints()
cli.Close()
ctx, cancel := commandCtx(cmd) ctx, cancel := commandCtx(cmd)
@ -53,7 +54,6 @@ func transferLeadershipCommandFunc(cmd *cobra.Command, args []string) {
var leaderCli *clientv3.Client var leaderCli *clientv3.Client
var leaderID uint64 var leaderID uint64
for _, ep := range eps { for _, ep := range eps {
cfg := clientConfigFromCmd(cmd)
cfg.Endpoints = []string{ep} cfg.Endpoints = []string{ep}
cli := mustClient(cfg) cli := mustClient(cfg)
resp, serr := cli.Status(ctx, ep) resp, serr := cli.Status(ctx, ep)

View File

@ -18,7 +18,6 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"os"
"testing" "testing"
"time" "time"
@ -28,17 +27,31 @@ import (
"go.etcd.io/etcd/tests/v3/framework/e2e" "go.etcd.io/etcd/tests/v3/framework/e2e"
) )
func TestCtlV3MoveLeaderSecure(t *testing.T) { func TestCtlV3MoveLeaderScenarios(t *testing.T) {
testCtlV3MoveLeader(t, *e2e.NewConfigTLS()) securityParent := map[string]struct {
cfg e2e.EtcdProcessClusterConfig
}{
"Secure": {cfg: *e2e.NewConfigTLS()},
"Insecure": {cfg: *e2e.NewConfigNoTLS()},
}
tests := map[string]struct {
env map[string]string
}{
"happy path": {env: map[string]string{}},
"with env": {env: map[string]string{"ETCDCTL_ENDPOINTS": "something-else-is-set"}},
}
for testName, tc := range securityParent {
for subTestName, tx := range tests {
t.Run(testName+" "+subTestName, func(t *testing.T) {
testCtlV3MoveLeader(t, tc.cfg, tx.env)
})
}
}
} }
func TestCtlV3MoveLeaderInsecure(t *testing.T) { func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig, envVars map[string]string) {
testCtlV3MoveLeader(t, *e2e.NewConfigNoTLS())
}
func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig) {
e2e.BeforeTest(t)
epc := setupEtcdctlTest(t, &cfg, true) epc := setupEtcdctlTest(t, &cfg, true)
defer func() { defer func() {
if errC := epc.Close(); errC != nil { if errC := epc.Close(); errC != nil {
@ -88,13 +101,12 @@ func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig) {
} }
} }
os.Setenv("ETCDCTL_API", "3")
defer os.Unsetenv("ETCDCTL_API")
cx := ctlCtx{ cx := ctlCtx{
t: t, t: t,
cfg: *e2e.NewConfigNoTLS(), cfg: *e2e.NewConfigNoTLS(),
dialTimeout: 7 * time.Second, dialTimeout: 7 * time.Second,
epc: epc, epc: epc,
envMap: envVars,
} }
tests := []struct { tests := []struct {
@ -109,6 +121,10 @@ func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig) {
[]string{cx.epc.EndpointsV3()[leadIdx]}, []string{cx.epc.EndpointsV3()[leadIdx]},
fmt.Sprintf("Leadership transferred from %s to %s", types.ID(leaderID), types.ID(transferee)), fmt.Sprintf("Leadership transferred from %s to %s", types.ID(leaderID), types.ID(transferee)),
}, },
{ // request to all endpoints
cx.epc.EndpointsV3(),
fmt.Sprintf("Leadership transferred"),
},
} }
for i, tc := range tests { for i, tc := range tests {
prefix := cx.prefixArgs(tc.eps) prefix := cx.prefixArgs(tc.eps)