Browse Source

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

Vimal K 7 years ago
parent
commit
25bc65794f

+ 1 - 8
etcdctl/ctlv2/ctl.go

@@ -26,7 +26,7 @@ import (
 	"github.com/urfave/cli"
 	"github.com/urfave/cli"
 )
 )
 
 
-func Start(apiv string) {
+func Start() {
 	app := cli.NewApp()
 	app := cli.NewApp()
 	app.Name = "etcdctl"
 	app.Name = "etcdctl"
 	app.Version = version.Version
 	app.Version = version.Version
@@ -36,13 +36,6 @@ func Start(apiv string) {
 	}
 	}
 	app.Usage = "A simple command line client for etcd."
 	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{
 	app.Flags = []cli.Flag{
 		cli.BoolFlag{Name: "debug", Usage: "output cURL commands which can be used to reproduce the request"},
 		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"},
 		cli.BoolFlag{Name: "no-sync", Usage: "don't synchronize cluster information before sending request"},

+ 8 - 1
etcdctl/ctlv3/ctl_cov.go

@@ -23,9 +23,16 @@ import (
 	"github.com/coreos/etcd/etcdctl/ctlv3/command"
 	"github.com/coreos/etcd/etcdctl/ctlv3/command"
 )
 )
 
 
-func Start() {
+func Start(apiv string) {
 	// ETCDCTL_ARGS=etcdctl_test arg1 arg2...
 	// ETCDCTL_ARGS=etcdctl_test arg1 arg2...
 	// SetArgs() takes 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:])
 	rootCmd.SetArgs(strings.Split(os.Getenv("ETCDCTL_ARGS"), "\xe7\xcd")[1:])
 	os.Unsetenv("ETCDCTL_ARGS")
 	os.Unsetenv("ETCDCTL_ARGS")
 	if err := rootCmd.Execute(); err != nil {
 	if err := rootCmd.Execute(); err != nil {

+ 8 - 1
etcdctl/ctlv3/ctl_nocov.go

@@ -18,7 +18,14 @@ package ctlv3
 
 
 import "github.com/coreos/etcd/etcdctl/ctlv3/command"
 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)
 	rootCmd.SetUsageFunc(usageFunc)
 	// Make help just show the usage
 	// Make help just show the usage
 	rootCmd.SetHelpTemplate(`{{.UsageString}}`)
 	rootCmd.SetHelpTemplate(`{{.UsageString}}`)

+ 4 - 4
etcdctl/main.go

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

+ 2 - 2
functional/cmd/etcd-proxy/main.go

@@ -64,8 +64,8 @@ $ make build-etcd-proxy
 $ ./bin/etcd-proxy --help
 $ ./bin/etcd-proxy --help
 $ ./bin/etcd-proxy --from localhost:23790 --to localhost:2379 --http-port 2378 --verbose
 $ ./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()
 		flag.PrintDefaults()
 	}
 	}
 
 

+ 22 - 0
tests/e2e/ctl_v2_test.go

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

+ 2 - 2
tests/e2e/ctl_v3_migrate_test.go

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

+ 3 - 0
tests/e2e/v2_curl_test.go

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