Skip to content

Commit

Permalink
Merge pull request kubernetes#95506 from benhxy/gke/etcd-migrate
Browse files Browse the repository at this point in the history
Add client listening URLs configuration to etcd migrate.
  • Loading branch information
k8s-ci-robot authored Oct 17, 2020
2 parents c1a781c + e8fcc44 commit db8258c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
25 changes: 14 additions & 11 deletions cluster/images/etcd/migrate/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions cluster/images/etcd/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion cluster/images/etcd/migrate/migrate_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ 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),
"--name", r.cfg.name,
"--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,
Expand Down
1 change: 1 addition & 0 deletions cluster/images/etcd/migrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type EtcdMigrateCfg struct {
port uint64
peerListenUrls string
peerAdvertiseUrls string
clientListenUrls string
etcdDataPrefix string
ttlKeysDirectory string
supportedVersions SupportedVersions
Expand Down
12 changes: 11 additions & 1 deletion cluster/images/etcd/migrate/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -66,6 +68,7 @@ type migrateOpts struct {
targetVersion string
targetStorage string
etcdServerArgs string
clientListenUrls string
}

func registerFlags(flags *flag.FlagSet, opt *migrateOpts) {
Expand All @@ -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:<peer-port>.")
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:<peer-port>.")
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:<port>.")
flags.StringVar(&opts.binDir, "bin-dir", "/usr/local/bin",
"directory of etcd and etcdctl binaries, must contain etcd-<version> and etcdctl-<version> for each version listed in <bundled-versions>.")
flags.StringVar(&opts.dataDir, "data-dir", "",
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit db8258c

Please sign in to comment.