Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctlv3: support TLS endpoints for move-leader command #9807

Merged
merged 2 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions etcdctl/ctlv3/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ func (*discardValue) Type() string { return "" }

func clientConfigFromCmd(cmd *cobra.Command) *clientConfig {
fs := cmd.InheritedFlags()

// silence "pkg/flags: unrecognized environment variable ETCDCTL_WATCH_KEY=foo" warnings
// silence "pkg/flags: unrecognized environment variable ETCDCTL_WATCH_RANGE_END=bar" warnings
fs.AddFlag(&pflag.Flag{Name: "watch-key", Value: &discardValue{}})
fs.AddFlag(&pflag.Flag{Name: "watch-range-end", Value: &discardValue{}})
if strings.HasPrefix(cmd.Use, "watch") {
// silence "pkg/flags: unrecognized environment variable ETCDCTL_WATCH_KEY=foo" warnings
// silence "pkg/flags: unrecognized environment variable ETCDCTL_WATCH_RANGE_END=bar" warnings
fs.AddFlag(&pflag.Flag{Name: "watch-key", Value: &discardValue{}})
fs.AddFlag(&pflag.Flag{Name: "watch-range-end", Value: &discardValue{}})
}
flags.SetPflagsFromEnv("ETCDCTL", fs)

debug, err := cmd.Flags().GetBool("debug")
Expand Down
11 changes: 3 additions & 8 deletions etcdctl/ctlv3/command/move_leader_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package command
import (
"fmt"
"strconv"
"time"

"github.com/coreos/etcd/clientv3"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,13 +52,9 @@ func transferLeadershipCommandFunc(cmd *cobra.Command, args []string) {
var leaderCli *clientv3.Client
var leaderID uint64
for _, ep := range eps {
cli, cerr := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
DialTimeout: 3 * time.Second,
})
if cerr != nil {
ExitWithError(ExitError, cerr)
}
cfg := clientConfigFromCmd(cmd)
cfg.endpoints = []string{ep}
cli := cfg.mustClient()
resp, serr := cli.Status(ctx, ep)
if serr != nil {
ExitWithError(ExitError, serr)
Expand Down
29 changes: 27 additions & 2 deletions tests/e2e/ctl_v3_move_leader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,58 @@ package e2e

import (
"context"
"crypto/tls"
"fmt"
"os"
"testing"
"time"

"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/testutil"
"github.com/coreos/etcd/pkg/transport"
"github.com/coreos/etcd/pkg/types"
)

func TestCtlV3MoveLeader(t *testing.T) {
func TestCtlV3MoveLeaderSecure(t *testing.T) {
testCtlV3MoveLeader(t, configTLS)
}

func TestCtlV3MoveLeaderInsecure(t *testing.T) {
testCtlV3MoveLeader(t, configNoTLS)
}

func testCtlV3MoveLeader(t *testing.T, cfg etcdProcessClusterConfig) {
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, &configNoTLS, true)
epc := setupEtcdctlTest(t, &cfg, true)
defer func() {
if errC := epc.Close(); errC != nil {
t.Fatalf("error closing etcd processes (%v)", errC)
}
}()

var tcfg *tls.Config
if cfg.clientTLS == clientTLS {
tinfo := transport.TLSInfo{
CertFile: certPath,
KeyFile: privateKeyPath,
TrustedCAFile: caPath,
}
var err error
tcfg, err = tinfo.ClientConfig()
if err != nil {
t.Fatal(err)
}
}

var leadIdx int
var leaderID uint64
var transferee uint64
for i, ep := range epc.EndpointsV3() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
DialTimeout: 3 * time.Second,
TLS: tcfg,
})
if err != nil {
t.Fatal(err)
Expand Down