k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cluster/images/etcd/migrate/migrate.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "path/filepath" 22 23 "github.com/spf13/cobra" 24 "k8s.io/klog/v2" 25 ) 26 27 const ( 28 versionFilename = "version.txt" 29 ) 30 31 var ( 32 migrateCmd = &cobra.Command{ 33 Short: "Upgrade/downgrade etcd data across multiple versions", 34 Long: `Upgrade or downgrade etcd data across multiple versions to the target version 35 36 Given a 'bin-dir' directory of etcd and etcdctl binaries, an etcd 'data-dir' with a 'version.txt' file and 37 a target etcd version, this tool will upgrade or downgrade the etcd data from the version specified in 38 'version.txt' to the target version. 39 `, 40 Run: func(cmd *cobra.Command, args []string) { 41 runMigrate() 42 }, 43 } 44 opts = migrateOpts{} 45 ) 46 47 func main() { 48 registerFlags(migrateCmd.Flags(), &opts) 49 err := migrateCmd.Execute() 50 if err != nil { 51 klog.Errorf("Failed to execute migratecmd: %s", err) 52 } 53 } 54 55 // runMigrate starts the migration. 56 func runMigrate() { 57 if err := opts.validateAndDefault(); err != nil { 58 klog.Fatalf("%v", err) 59 } 60 copyBinaries() 61 62 target := &EtcdVersionPair{ 63 version: MustParseEtcdVersion(opts.targetVersion), 64 storageVersion: MustParseEtcdStorageVersion(opts.targetStorage), 65 } 66 67 migrate( 68 opts.name, opts.port, opts.peerListenUrls, opts.peerAdvertiseUrls, opts.clientListenUrls, 69 opts.binDir, opts.dataDir, opts.etcdDataPrefix, opts.ttlKeysDirectory, opts.initialCluster, 70 target, opts.supportedVersions, opts.etcdServerArgs) 71 } 72 73 func copyBinaries() { 74 if val, err := lookupEnv("DO_NOT_MOVE_BINARIES"); err != nil || val != "true" { 75 etcdVersioned := fmt.Sprintf("etcd-%s", opts.targetVersion) 76 etcdctlVersioned := fmt.Sprintf("etcdctl-%s", opts.targetVersion) 77 if err := copyFile(filepath.Join(opts.binDir, etcdVersioned), filepath.Join(opts.binDir, "etcd")); err != nil { 78 klog.Fatalf("Failed to copy %s: %v", etcdVersioned, err) 79 } 80 if err := copyFile(filepath.Join(opts.binDir, etcdctlVersioned), filepath.Join(opts.binDir, "etcdctl")); err != nil { 81 klog.Fatalf("Failed to copy %s: %v", etcdctlVersioned, err) 82 } 83 } 84 } 85 86 // migrate opens or initializes the etcd data directory, configures the migrator, and starts the migration. 87 func migrate(name string, port uint64, peerListenUrls string, peerAdvertiseUrls string, clientListenUrls string, 88 binPath string, dataDirPath string, etcdDataPrefix string, ttlKeysDirectory string, 89 initialCluster string, target *EtcdVersionPair, bundledVersions SupportedVersions, etcdServerArgs string) { 90 91 dataDir, err := OpenOrCreateDataDirectory(dataDirPath) 92 if err != nil { 93 klog.Fatalf("Error opening or creating data directory %s: %v", dataDirPath, err) 94 } 95 96 cfg := &EtcdMigrateCfg{ 97 binPath: binPath, 98 name: name, 99 port: port, 100 peerListenUrls: peerListenUrls, 101 peerAdvertiseUrls: peerAdvertiseUrls, 102 clientListenUrls: clientListenUrls, 103 etcdDataPrefix: etcdDataPrefix, 104 ttlKeysDirectory: ttlKeysDirectory, 105 initialCluster: initialCluster, 106 supportedVersions: bundledVersions, 107 dataDirectory: dataDirPath, 108 etcdServerArgs: etcdServerArgs, 109 } 110 client, err := NewEtcdMigrateClient(cfg) 111 if err != nil { 112 klog.Fatalf("Migration failed: %v", err) 113 } 114 defer client.Close() 115 116 migrator := &Migrator{cfg, dataDir, client} 117 118 err = migrator.MigrateIfNeeded(target) 119 if err != nil { 120 klog.Fatalf("Migration failed: %v", err) 121 } 122 }