From e8fcc44286444cb351b979e3a9705a6b5623db07 Mon Sep 17 00:00:00 2001 From: Ben Hu Date: Tue, 13 Oct 2020 03:01:33 +0000 Subject: [PATCH] Add client listening URLs flag to etcd migrate. --- .../images/etcd/migrate/integration_test.go | 25 +++++++++++-------- cluster/images/etcd/migrate/migrate.go | 8 +++--- cluster/images/etcd/migrate/migrate_server.go | 3 ++- cluster/images/etcd/migrate/migrator.go | 1 + cluster/images/etcd/migrate/options.go | 12 ++++++++- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/cluster/images/etcd/migrate/integration_test.go b/cluster/images/etcd/migrate/integration_test.go index 258be69dc0c0a..34edbfaafadff 100644 --- a/cluster/images/etcd/migrate/integration_test.go +++ b/cluster/images/etcd/migrate/integration_test.go @@ -56,21 +56,23 @@ func init() { func TestMigrate(t *testing.T) { migrations := []struct { - title string - memberCount int - startVersion string - endVersion string - protocol string + title string + memberCount int + startVersion string + endVersion string + protocol string + clientListenUrls string }{ // upgrades - {"v3-v3-up", 1, "3.0.17/etcd3", "3.1.12/etcd3", "https"}, - {"oldest-newest-up", 1, "3.0.17/etcd3", "3.1.12/etcd3", "https"}, + {"v3-v3-up", 1, "3.0.17/etcd3", "3.1.12/etcd3", "https", ""}, + {"oldest-newest-up", 1, "3.0.17/etcd3", "3.1.12/etcd3", "https", ""}, + {"v3-v3-up-with-additional-client-url", 1, "3.0.17/etcd3", "3.1.12/etcd3", "https", "http://127.0.0.1:2379,http://10.128.0.1:2379"}, // warning: v2->v3 ha upgrades not currently supported. - {"ha-v3-v3-up", 3, "3.0.17/etcd3", "3.1.12/etcd3", "https"}, + {"ha-v3-v3-up", 3, "3.0.17/etcd3", "3.1.12/etcd3", "https", ""}, // downgrades - {"v3-v3-down", 1, "3.1.12/etcd3", "3.0.17/etcd3", "https"}, + {"v3-v3-down", 1, "3.1.12/etcd3", "3.0.17/etcd3", "https", ""}, // warning: ha downgrades not yet supported. } @@ -80,7 +82,7 @@ func TestMigrate(t *testing.T) { start := MustParseEtcdVersionPair(m.startVersion) end := MustParseEtcdVersionPair(m.endVersion) - testCfgs := clusterConfig(t, m.title, m.memberCount, m.protocol) + testCfgs := clusterConfig(t, m.title, m.memberCount, m.protocol, m.clientListenUrls) servers := []*EtcdMigrateServer{} for _, cfg := range testCfgs { @@ -217,7 +219,7 @@ func checkPermissions(t *testing.T, path string, expected os.FileMode) { } } -func clusterConfig(t *testing.T, name string, memberCount int, protocol string) []*EtcdMigrateCfg { +func clusterConfig(t *testing.T, name string, memberCount int, protocol string, clientListenUrls string) []*EtcdMigrateCfg { peers := []string{} for i := 0; i < memberCount; i++ { memberName := fmt.Sprintf("%s-%d", name, i) @@ -243,6 +245,7 @@ func clusterConfig(t *testing.T, name string, memberCount int, protocol string) port: uint64(2379 + i*10000), peerListenUrls: peerURL, peerAdvertiseUrls: peerURL, + clientListenUrls: clientListenUrls, etcdDataPrefix: "/registry", ttlKeysDirectory: "/registry/events", supportedVersions: testSupportedVersions, diff --git a/cluster/images/etcd/migrate/migrate.go b/cluster/images/etcd/migrate/migrate.go index 8dd3a33c56e3c..e089786faf584 100644 --- a/cluster/images/etcd/migrate/migrate.go +++ b/cluster/images/etcd/migrate/migrate.go @@ -65,8 +65,8 @@ func runMigrate() { } migrate( - opts.name, opts.port, opts.peerListenUrls, opts.peerAdvertiseUrls, opts.binDir, - opts.dataDir, opts.etcdDataPrefix, opts.ttlKeysDirectory, opts.initialCluster, + opts.name, opts.port, opts.peerListenUrls, opts.peerAdvertiseUrls, opts.clientListenUrls, + opts.binDir, opts.dataDir, opts.etcdDataPrefix, opts.ttlKeysDirectory, opts.initialCluster, target, opts.supportedVersions, opts.etcdServerArgs) } @@ -84,7 +84,8 @@ func copyBinaries() { } // migrate opens or initializes the etcd data directory, configures the migrator, and starts the migration. -func migrate(name string, port uint64, peerListenUrls string, peerAdvertiseUrls string, binPath string, dataDirPath string, etcdDataPrefix string, ttlKeysDirectory string, +func migrate(name string, port uint64, peerListenUrls string, peerAdvertiseUrls string, clientListenUrls string, + binPath string, dataDirPath string, etcdDataPrefix string, ttlKeysDirectory string, initialCluster string, target *EtcdVersionPair, bundledVersions SupportedVersions, etcdServerArgs string) { dataDir, err := OpenOrCreateDataDirectory(dataDirPath) @@ -98,6 +99,7 @@ func migrate(name string, port uint64, peerListenUrls string, peerAdvertiseUrls port: port, peerListenUrls: peerListenUrls, peerAdvertiseUrls: peerAdvertiseUrls, + clientListenUrls: clientListenUrls, etcdDataPrefix: etcdDataPrefix, ttlKeysDirectory: ttlKeysDirectory, initialCluster: initialCluster, diff --git a/cluster/images/etcd/migrate/migrate_server.go b/cluster/images/etcd/migrate/migrate_server.go index b152311ad035a..b5e88ab14014c 100644 --- a/cluster/images/etcd/migrate/migrate_server.go +++ b/cluster/images/etcd/migrate/migrate_server.go @@ -39,6 +39,7 @@ func NewEtcdMigrateServer(cfg *EtcdMigrateCfg, client EtcdMigrateClient) *EtcdMi } // Start starts an etcd server as a separate process, waits until it has started, and returns a exec.Cmd. +// TODO: Add support for listening to client via TLS. func (r *EtcdMigrateServer) Start(version *EtcdVersion) error { etcdCmd := exec.Command( fmt.Sprintf("%s/etcd-%s", r.cfg.binPath, version), @@ -46,7 +47,7 @@ func (r *EtcdMigrateServer) Start(version *EtcdVersion) error { "--initial-cluster", r.cfg.initialCluster, "--debug", "--data-dir", r.cfg.dataDirectory, - "--listen-client-urls", fmt.Sprintf("http://127.0.0.1:%d", r.cfg.port), + "--listen-client-urls", r.cfg.clientListenUrls, "--advertise-client-urls", fmt.Sprintf("http://127.0.0.1:%d", r.cfg.port), "--listen-peer-urls", r.cfg.peerListenUrls, "--initial-advertise-peer-urls", r.cfg.peerAdvertiseUrls, diff --git a/cluster/images/etcd/migrate/migrator.go b/cluster/images/etcd/migrate/migrator.go index ffb0f0d5a3f3a..5cd4bcd1b8e0e 100644 --- a/cluster/images/etcd/migrate/migrator.go +++ b/cluster/images/etcd/migrate/migrator.go @@ -33,6 +33,7 @@ type EtcdMigrateCfg struct { port uint64 peerListenUrls string peerAdvertiseUrls string + clientListenUrls string etcdDataPrefix string ttlKeysDirectory string supportedVersions SupportedVersions diff --git a/cluster/images/etcd/migrate/options.go b/cluster/images/etcd/migrate/options.go index 254eef4f25a5c..e65ec63f356f5 100644 --- a/cluster/images/etcd/migrate/options.go +++ b/cluster/images/etcd/migrate/options.go @@ -42,6 +42,8 @@ const ( peerListenUrlsFmt = "http://localhost:%d" peerAdvertiseUrlsEnv = "INITIAL_ADVERTISE_PEER_URLS" peerAdvertiseUrlsFmt = "http://localhost:%d" + clientListenURLsEnv = "LISTEN_CLIENT_URLS" + clientListenURLFmt = "http://127.0.0.1:%d" targetVersionEnv = "TARGET_VERSION" targetStorageEnv = "TARGET_STORAGE" etcdDataPrefixEnv = "ETCD_DATA_PREFIX" @@ -66,6 +68,7 @@ type migrateOpts struct { targetVersion string targetStorage string etcdServerArgs string + clientListenUrls string } func registerFlags(flags *flag.FlagSet, opt *migrateOpts) { @@ -81,6 +84,8 @@ func registerFlags(flags *flag.FlagSet, opt *migrateOpts) { "etcd --listen-peer-urls flag. If unset, fallbacks to LISTEN_PEER_URLS env and if unset defaults to http://localhost:.") flags.StringVar(&opts.peerAdvertiseUrls, "initial-advertise-peer-urls", "", "etcd --initial-advertise-peer-urls flag. If unset fallbacks to INITIAL_ADVERTISE_PEER_URLS env and if unset defaults to http://localhost:.") + flags.StringVar(&opts.clientListenUrls, "listen-client-urls", "", + "etcd --listen-client-urls flag. If unset, fallbacks to LISTEN_CLIENT_URLS env, and if unset defaults to http://127.0.0.1:.") flags.StringVar(&opts.binDir, "bin-dir", "/usr/local/bin", "directory of etcd and etcdctl binaries, must contain etcd- and etcdctl- for each version listed in .") flags.StringVar(&opts.dataDir, "data-dir", "", @@ -119,7 +124,7 @@ func fallbackToEnvWithDefault(flag, env, def string) string { if value, err := lookupEnv(env); err == nil { return value } - klog.Warningf("%s variable unset - defaulting to %s", env, def) + klog.Warningf("%s variable for %s flag unset - defaulting to %s", env, flag, def) return def } @@ -187,6 +192,11 @@ func (opts *migrateOpts) validateAndDefault() error { opts.peerAdvertiseUrls = fallbackToEnvWithDefault("initial-advertise-peer-urls", peerAdvertiseUrlsEnv, def) } + if opts.clientListenUrls == "" { + def := fmt.Sprintf(clientListenURLFmt, opts.port) + opts.clientListenUrls = fallbackToEnvWithDefault("listen-client-urls", clientListenURLsEnv, def) + } + if opts.targetVersion == "" { if opts.targetVersion, err = fallbackToEnv("target-version", targetVersionEnv); err != nil { return err