etcdctl/ctlv3: etcd v3.4 makes ETCDCTL_API=3 by default

release-3.4
Vimal K 2018-05-29 02:44:45 +05:30
parent 476c9cbeed
commit 25bc65794f
8 changed files with 50 additions and 18 deletions

View File

@ -26,7 +26,7 @@ import (
"github.com/urfave/cli"
)
func Start(apiv string) {
func Start() {
app := cli.NewApp()
app.Name = "etcdctl"
app.Version = version.Version
@ -36,13 +36,6 @@ func Start(apiv string) {
}
app.Usage = "A simple command line client for etcd."
if apiv == "" {
app.Usage += "\n\n" +
"WARNING:\n" +
" Environment variable ETCDCTL_API is not set; defaults to etcdctl v2.\n" +
" Set environment variable ETCDCTL_API=3 to use v3 API or ETCDCTL_API=2 to use v2 API."
}
app.Flags = []cli.Flag{
cli.BoolFlag{Name: "debug", Usage: "output cURL commands which can be used to reproduce the request"},
cli.BoolFlag{Name: "no-sync", Usage: "don't synchronize cluster information before sending request"},

View File

@ -23,9 +23,16 @@ import (
"github.com/coreos/etcd/etcdctl/ctlv3/command"
)
func Start() {
func Start(apiv string) {
// ETCDCTL_ARGS=etcdctl_test arg1 arg2...
// SetArgs() takes arg1 arg2...
if apiv == "" {
rootCmd.Short += "\n\n" +
"WARNING:\n" +
" Environment variable ETCDCTL_API is not set; defaults to etcdctl v3.\n" +
" Set environment variable ETCDCTL_API=2 to use v2 API or ETCDCTL_API=3 to use v3 API."
}
rootCmd.SetArgs(strings.Split(os.Getenv("ETCDCTL_ARGS"), "\xe7\xcd")[1:])
os.Unsetenv("ETCDCTL_ARGS")
if err := rootCmd.Execute(); err != nil {

View File

@ -18,7 +18,14 @@ package ctlv3
import "github.com/coreos/etcd/etcdctl/ctlv3/command"
func Start() {
func Start(apiv string) {
if apiv == "" {
rootCmd.Short += "\n\n" +
"WARNING:\n" +
" Environment variable ETCDCTL_API is not set; defaults to etcdctl v3.\n" +
" Set environment variable ETCDCTL_API=2 to use v2 API or ETCDCTL_API=3 to use v3 API."
}
rootCmd.SetUsageFunc(usageFunc)
// Make help just show the usage
rootCmd.SetHelpTemplate(`{{.UsageString}}`)

View File

@ -31,13 +31,13 @@ func main() {
apiv := os.Getenv(apiEnv)
// unset apiEnv to avoid side-effect for future env and flag parsing.
os.Unsetenv(apiEnv)
if len(apiv) == 0 || apiv == "2" {
ctlv2.Start(apiv)
if len(apiv) == 0 || apiv == "3" {
ctlv3.Start(apiv)
return
}
if apiv == "3" {
ctlv3.Start()
if apiv == "2" {
ctlv2.Start()
return
}

View File

@ -64,8 +64,8 @@ $ make build-etcd-proxy
$ ./bin/etcd-proxy --help
$ ./bin/etcd-proxy --from localhost:23790 --to localhost:2379 --http-port 2378 --verbose
$ ETCDCTL_API=3 ./bin/etcdctl --endpoints localhost:2379 put foo bar
$ ETCDCTL_API=3 ./bin/etcdctl --endpoints localhost:23790 put foo bar`)
$ ./bin/etcdctl --endpoints localhost:2379 put foo bar
$ ./bin/etcdctl --endpoints localhost:23790 put foo bar`)
flag.PrintDefaults()
}

View File

@ -31,6 +31,8 @@ func TestCtlV2SetClientTLS(t *testing.T) { testCtlV2Set(t, &configClientTLS, fal
func TestCtlV2SetPeerTLS(t *testing.T) { testCtlV2Set(t, &configPeerTLS, false) }
func TestCtlV2SetTLS(t *testing.T) { testCtlV2Set(t, &configTLS, false) }
func testCtlV2Set(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, cfg, quorum)
@ -55,6 +57,8 @@ func TestCtlV2Mk(t *testing.T) { testCtlV2Mk(t, &configNoTLS, false) }
func TestCtlV2MkQuorum(t *testing.T) { testCtlV2Mk(t, &configNoTLS, true) }
func TestCtlV2MkTLS(t *testing.T) { testCtlV2Mk(t, &configTLS, false) }
func testCtlV2Mk(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, cfg, quorum)
@ -81,6 +85,8 @@ func testCtlV2Mk(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
func TestCtlV2Rm(t *testing.T) { testCtlV2Rm(t, &configNoTLS) }
func TestCtlV2RmTLS(t *testing.T) { testCtlV2Rm(t, &configTLS) }
func testCtlV2Rm(t *testing.T, cfg *etcdProcessClusterConfig) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, cfg, true)
@ -108,6 +114,8 @@ func TestCtlV2Ls(t *testing.T) { testCtlV2Ls(t, &configNoTLS, false) }
func TestCtlV2LsQuorum(t *testing.T) { testCtlV2Ls(t, &configNoTLS, true) }
func TestCtlV2LsTLS(t *testing.T) { testCtlV2Ls(t, &configTLS, false) }
func testCtlV2Ls(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, cfg, quorum)
@ -132,6 +140,8 @@ func TestCtlV2Watch(t *testing.T) { testCtlV2Watch(t, &configNoTLS, false) }
func TestCtlV2WatchTLS(t *testing.T) { testCtlV2Watch(t, &configTLS, false) }
func testCtlV2Watch(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, cfg, true)
@ -158,6 +168,8 @@ func testCtlV2Watch(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
}
func TestCtlV2GetRoleUser(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, &configNoTLS, false)
@ -191,6 +203,8 @@ func TestCtlV2GetRoleUser(t *testing.T) {
func TestCtlV2UserListUsername(t *testing.T) { testCtlV2UserList(t, "username") }
func TestCtlV2UserListRoot(t *testing.T) { testCtlV2UserList(t, "root") }
func testCtlV2UserList(t *testing.T, username string) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, &configNoTLS, false)
@ -209,6 +223,8 @@ func testCtlV2UserList(t *testing.T, username string) {
}
func TestCtlV2RoleList(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, &configNoTLS, false)
@ -233,6 +249,8 @@ func TestCtlV2BackupV3(t *testing.T) { testCtlV2Backup(t, 0, true) }
func TestCtlV2BackupV3Snapshot(t *testing.T) { testCtlV2Backup(t, 1, true) }
func testCtlV2Backup(t *testing.T, snapCount int, v3 bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
backupDir, err := ioutil.TempDir("", "testbackup0.etcd")
@ -305,6 +323,8 @@ func testCtlV2Backup(t *testing.T, snapCount int, v3 bool) {
}
func TestCtlV2AuthWithCommonName(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
copiedCfg := configClientTLS
@ -341,6 +361,8 @@ func TestCtlV2AuthWithCommonName(t *testing.T) {
}
func TestCtlV2ClusterHealth(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, &configNoTLS, true)
defer func() {

View File

@ -41,6 +41,7 @@ func TestCtlV3Migrate(t *testing.T) {
keys[i] = fmt.Sprintf("foo_%d", i)
vals[i] = fmt.Sprintf("bar_%d", i)
}
os.Setenv("ETCDCTL_API", "2")
for i := range keys {
if err := etcdctlSet(epc, keys[i], vals[i]); err != nil {
t.Fatal(err)
@ -52,8 +53,7 @@ func TestCtlV3Migrate(t *testing.T) {
t.Fatalf("error closing etcd processes (%v)", err)
}
os.Setenv("ETCDCTL_API", "3")
defer os.Unsetenv("ETCDCTL_API")
os.Unsetenv("ETCDCTL_API")
cx := ctlCtx{
t: t,
cfg: configNoTLS,

View File

@ -17,6 +17,7 @@ package e2e
import (
"fmt"
"math/rand"
"os"
"strings"
"testing"
@ -64,6 +65,8 @@ func testCurlPutGet(t *testing.T, cfg *etcdProcessClusterConfig) {
}
func TestV2CurlIssue5182(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, &configNoTLS, false)