migrate.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "github.com/spf13/cobra"
  19. "k8s.io/klog"
  20. )
  21. const (
  22. versionFilename = "version.txt"
  23. defaultPort uint64 = 18629
  24. )
  25. var (
  26. migrateCmd = &cobra.Command{
  27. Short: "Upgrade/downgrade etcd data across multiple versions",
  28. Long: `Upgrade or downgrade etcd data across multiple versions to the target version
  29. Given a 'bin-dir' directory of etcd and etcdctl binaries, an etcd 'data-dir' with a 'version.txt' file and
  30. a target etcd version, this tool will upgrade or downgrade the etcd data from the version specified in
  31. 'version.txt' to the target version.
  32. `,
  33. Run: func(cmd *cobra.Command, args []string) {
  34. runMigrate()
  35. },
  36. }
  37. opts = migrateOpts{}
  38. )
  39. type migrateOpts struct {
  40. name string
  41. port uint64
  42. peerListenUrls string
  43. peerAdvertiseUrls string
  44. binDir string
  45. dataDir string
  46. bundledVersionString string
  47. etcdDataPrefix string
  48. ttlKeysDirectory string
  49. initialCluster string
  50. targetVersion string
  51. targetStorage string
  52. etcdServerArgs string
  53. }
  54. func main() {
  55. flags := migrateCmd.Flags()
  56. flags.StringVar(&opts.name, "name", "", "etcd cluster member name. Defaults to etcd-{hostname}")
  57. flags.Uint64Var(&opts.port, "port", defaultPort, "etcd client port to use during migration operations. This should be a different port than typically used by etcd to avoid clients accidentally connecting during upgrade/downgrade operations.")
  58. flags.StringVar(&opts.peerListenUrls, "listen-peer-urls", "", "etcd --listen-peer-urls flag, required for HA clusters")
  59. flags.StringVar(&opts.peerAdvertiseUrls, "initial-advertise-peer-urls", "", "etcd --initial-advertise-peer-urls flag, required for HA clusters")
  60. 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 bindled-versions")
  61. flags.StringVar(&opts.dataDir, "data-dir", "", "etcd data directory of etcd server to migrate")
  62. flags.StringVar(&opts.bundledVersionString, "bundled-versions", "", "comma separated list of etcd binary versions present under the bin-dir")
  63. flags.StringVar(&opts.etcdDataPrefix, "etcd-data-prefix", "/registry", "etcd key prefix under which all objects are kept")
  64. flags.StringVar(&opts.ttlKeysDirectory, "ttl-keys-directory", "", "etcd key prefix under which all keys with TTLs are kept. Defaults to {etcd-data-prefix}/events")
  65. flags.StringVar(&opts.initialCluster, "initial-cluster", "", "comma separated list of name=endpoint pairs. Defaults to etcd-{hostname}=http://localhost:2380")
  66. flags.StringVar(&opts.targetVersion, "target-version", "", "version of etcd to migrate to. Format must be '<major>.<minor>.<patch>'")
  67. flags.StringVar(&opts.targetStorage, "target-storage", "", "storage version of etcd to migrate to, one of: etcd2, etcd3")
  68. flags.StringVar(&opts.etcdServerArgs, "etcd-server-extra-args", "", "additional etcd server args for starting etcd servers during migration steps, --peer-* TLS cert flags should be added for etcd clusters with more than 1 member that use mutual TLS for peer communication.")
  69. migrateCmd.Execute()
  70. }
  71. // runMigrate validates the command line flags and starts the migration.
  72. func runMigrate() {
  73. if opts.name == "" {
  74. hostname, err := os.Hostname()
  75. if err != nil {
  76. klog.Errorf("Error while getting hostname to supply default --name: %v", err)
  77. os.Exit(1)
  78. }
  79. opts.name = fmt.Sprintf("etcd-%s", hostname)
  80. }
  81. if opts.ttlKeysDirectory == "" {
  82. opts.ttlKeysDirectory = fmt.Sprintf("%s/events", opts.etcdDataPrefix)
  83. }
  84. if opts.initialCluster == "" {
  85. opts.initialCluster = fmt.Sprintf("%s=http://localhost:2380", opts.name)
  86. }
  87. if opts.targetStorage == "" {
  88. klog.Errorf("--target-storage is required")
  89. os.Exit(1)
  90. }
  91. if opts.targetVersion == "" {
  92. klog.Errorf("--target-version is required")
  93. os.Exit(1)
  94. }
  95. if opts.dataDir == "" {
  96. klog.Errorf("--data-dir is required")
  97. os.Exit(1)
  98. }
  99. if opts.bundledVersionString == "" {
  100. klog.Errorf("--bundled-versions is required")
  101. os.Exit(1)
  102. }
  103. bundledVersions, err := ParseSupportedVersions(opts.bundledVersionString)
  104. if err != nil {
  105. klog.Errorf("Failed to parse --supported-versions: %v", err)
  106. }
  107. err = validateBundledVersions(bundledVersions, opts.binDir)
  108. if err != nil {
  109. klog.Errorf("Failed to validate that 'etcd-<version>' and 'etcdctl-<version>' binaries exist in --bin-dir '%s' for all --bundled-versions '%s': %v",
  110. opts.binDir, opts.bundledVersionString, err)
  111. os.Exit(1)
  112. }
  113. target := &EtcdVersionPair{
  114. version: MustParseEtcdVersion(opts.targetVersion),
  115. storageVersion: MustParseEtcdStorageVersion(opts.targetStorage),
  116. }
  117. migrate(opts.name, opts.port, opts.peerListenUrls, opts.peerAdvertiseUrls, opts.binDir, opts.dataDir, opts.etcdDataPrefix, opts.ttlKeysDirectory, opts.initialCluster, target, bundledVersions, opts.etcdServerArgs)
  118. }
  119. // migrate opens or initializes the etcd data directory, configures the migrator, and starts the migration.
  120. func migrate(name string, port uint64, peerListenUrls string, peerAdvertiseUrls string, binPath string, dataDirPath string, etcdDataPrefix string, ttlKeysDirectory string,
  121. initialCluster string, target *EtcdVersionPair, bundledVersions SupportedVersions, etcdServerArgs string) {
  122. dataDir, err := OpenOrCreateDataDirectory(dataDirPath)
  123. if err != nil {
  124. klog.Errorf("Error opening or creating data directory %s: %v", dataDirPath, err)
  125. os.Exit(1)
  126. }
  127. cfg := &EtcdMigrateCfg{
  128. binPath: binPath,
  129. name: name,
  130. port: port,
  131. peerListenUrls: peerListenUrls,
  132. peerAdvertiseUrls: peerAdvertiseUrls,
  133. etcdDataPrefix: etcdDataPrefix,
  134. ttlKeysDirectory: ttlKeysDirectory,
  135. initialCluster: initialCluster,
  136. supportedVersions: bundledVersions,
  137. dataDirectory: dataDirPath,
  138. etcdServerArgs: etcdServerArgs,
  139. }
  140. client, err := NewEtcdMigrateClient(cfg)
  141. if err != nil {
  142. klog.Errorf("Migration failed: %v", err)
  143. os.Exit(1)
  144. }
  145. defer client.Close()
  146. migrator := &Migrator{cfg, dataDir, client}
  147. err = migrator.MigrateIfNeeded(target)
  148. if err != nil {
  149. klog.Errorf("Migration failed: %v", err)
  150. os.Exit(1)
  151. }
  152. }
  153. // validateBundledVersions checks that 'etcd-<version>' and 'etcdctl-<version>' binaries exist in the binDir
  154. // for each version in the bundledVersions list.
  155. func validateBundledVersions(bundledVersions SupportedVersions, binDir string) error {
  156. for _, v := range bundledVersions {
  157. for _, binaryName := range []string{"etcd", "etcdctl"} {
  158. fn := filepath.Join(binDir, fmt.Sprintf("%s-%s", binaryName, v))
  159. if _, err := os.Stat(fn); err != nil {
  160. return fmt.Errorf("failed to validate '%s' binary exists for bundled-version '%s': %v", fn, v, err)
  161. }
  162. }
  163. }
  164. return nil
  165. }