From 6761a1a3e190e411dd52fc851ace20bc5112231b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Steenis Date: Mon, 3 Aug 2020 15:35:38 +0200 Subject: [PATCH] Add restore flag to use local state --- cluster/defaults.go | 4 ++- cluster/state.go | 2 +- cmd/cert.go | 4 +-- cmd/encryption.go | 2 +- cmd/etcd.go | 77 +++++++++++++++++++++++++++------------------ cmd/remove.go | 4 +-- cmd/up.go | 6 ++-- 7 files changed, 58 insertions(+), 41 deletions(-) diff --git a/cluster/defaults.go b/cluster/defaults.go index b9c65e9e..42b6b579 100644 --- a/cluster/defaults.go +++ b/cluster/defaults.go @@ -120,6 +120,7 @@ type ExternalFlags struct { GenerateCSR bool Local bool UpdateOnly bool + UseLocalState bool } func setDefaultIfEmptyMapValue(configMap map[string]string, key string, value string) { @@ -606,13 +607,14 @@ func (c *Cluster) setCloudProvider() error { return nil } -func GetExternalFlags(local, updateOnly, disablePortCheck bool, configDir, clusterFilePath string) ExternalFlags { +func GetExternalFlags(local, updateOnly, disablePortCheck, useLocalState bool, configDir, clusterFilePath string) ExternalFlags { return ExternalFlags{ Local: local, UpdateOnly: updateOnly, DisablePortCheck: disablePortCheck, ConfigDir: configDir, ClusterFilePath: clusterFilePath, + UseLocalState: useLocalState, } } diff --git a/cluster/state.go b/cluster/state.go index 799ef356..6ff0682a 100644 --- a/cluster/state.go +++ b/cluster/state.go @@ -53,7 +53,7 @@ func (c *Cluster) GetClusterState(ctx context.Context, fullState *FullState) (*C } // resetup external flags - flags := GetExternalFlags(false, false, false, c.ConfigDir, c.ConfigPath) + flags := GetExternalFlags(false, false, false, false, c.ConfigDir, c.ConfigPath) currentCluster, err := InitClusterObject(ctx, fullState.CurrentState.RancherKubernetesEngineConfig, flags, fullState.CurrentState.EncryptionConfig) if err != nil { return nil, err diff --git a/cmd/cert.go b/cmd/cert.go index 6de3abf5..dc3c7fa1 100644 --- a/cmd/cert.go +++ b/cmd/cert.go @@ -91,7 +91,7 @@ func rotateRKECertificatesFromCli(ctx *cli.Context) error { return err } // setting up the flags - externalFlags := cluster.GetExternalFlags(false, false, false, "", filePath) + externalFlags := cluster.GetExternalFlags(false, false, false, false, "", filePath) // setting up rotate flags rkeConfig.RotateCertificates = &v3.RotateCertificates{ CACertificates: rotateCACerts, @@ -120,7 +120,7 @@ func generateCSRFromCli(ctx *cli.Context) error { return err } // setting up the flags - externalFlags := cluster.GetExternalFlags(false, false, false, "", filePath) + externalFlags := cluster.GetExternalFlags(false, false, false, false, "", filePath) externalFlags.CertificateDir = ctx.String("cert-dir") externalFlags.CustomCerts = ctx.Bool("custom-certs") diff --git a/cmd/encryption.go b/cmd/encryption.go index 62d60172..070d6368 100644 --- a/cmd/encryption.go +++ b/cmd/encryption.go @@ -55,7 +55,7 @@ func rotateEncryptionKeyFromCli(ctx *cli.Context) error { } // setting up the flags - flags := cluster.GetExternalFlags(false, false, false, "", filePath) + flags := cluster.GetExternalFlags(false, false, false, false, "", filePath) return RotateEncryptionKey(context.Background(), rkeConfig, hosts.DialersOptions{}, flags) } diff --git a/cmd/etcd.go b/cmd/etcd.go index 744a6c24..0e3d26aa 100644 --- a/cmd/etcd.go +++ b/cmd/etcd.go @@ -77,6 +77,10 @@ func EtcdCommand() cli.Command { Name: "custom-certs", Usage: "Use custom certificates from a cert dir", }, + cli.BoolFlag{ + Name: "use-local-state", + Usage: "Use local state file (do not check or use snapshot archive for state file)", + }, } snapshotRestoreFlags = append(append(snapshotFlags, snapshotRestoreFlags...), commonFlags...) @@ -144,41 +148,49 @@ func RestoreEtcdSnapshot( snapshotName string) (string, string, string, string, map[string]pki.CertificatePKI, error) { var APIURL, caCrt, clientCert, clientKey string - log.Infof(ctx, "Checking if state file is included in snapshot file for %s", snapshotName) - // Creating temp cluster to check if snapshot archive contains statefile and retrieve it - tempCluster, err := cluster.InitClusterObject(ctx, rkeConfig, flags, "") - if err != nil { - return APIURL, caCrt, clientCert, clientKey, nil, err - } - if err := tempCluster.SetupDialers(ctx, dialersOptions); err != nil { - return APIURL, caCrt, clientCert, clientKey, nil, err - } - if err := tempCluster.TunnelHosts(ctx, flags); err != nil { - return APIURL, caCrt, clientCert, clientKey, nil, err - } - rkeFullState := &cluster.FullState{} stateFileRetrieved := false - // Local state file stateFilePath := cluster.GetStateFilePath(flags.ClusterFilePath, flags.ConfigDir) - // Extract state file from snapshot - stateFile, err := tempCluster.GetStateFileFromSnapshot(ctx, snapshotName) - // If state file is not in snapshot (or can't be retrieved), fallback to local state file - if err != nil { - logrus.Infof("Could not extract state file from snapshot [%s] on any host, falling back to local state file: %v", snapshotName, err) - rkeFullState, _ = cluster.ReadStateFile(ctx, stateFilePath) - } else { - // Parse extracted statefile to FullState struct - rkeFullState, err = cluster.StringToFullState(ctx, stateFile) + + if !flags.UseLocalState { + log.Infof(ctx, "Checking if state file is included in snapshot file for [%s]", snapshotName) + // Creating temp cluster to check if snapshot archive contains state file and retrieve it + tempCluster, err := cluster.InitClusterObject(ctx, rkeConfig, flags, "") if err != nil { - logrus.Errorf("Error when converting state file contents to rkeFullState: %v", err) return APIURL, caCrt, clientCert, clientKey, nil, err } - logrus.Infof("State file is successfully extracted from snapshot [%s]", snapshotName) - stateFileRetrieved = true - } + if err := tempCluster.SetupDialers(ctx, dialersOptions); err != nil { + return APIURL, caCrt, clientCert, clientKey, nil, err + } + if err := tempCluster.TunnelHosts(ctx, flags); err != nil { + return APIURL, caCrt, clientCert, clientKey, nil, err + } + // Extract state file from snapshot + stateFile, err := tempCluster.GetStateFileFromSnapshot(ctx, snapshotName) + // If state file is not in snapshot (or can't be retrieved), fallback to local state file + if err != nil { + logrus.Infof("Could not extract state file from snapshot [%s] on any host, falling back to local state file: %v", snapshotName, err) + rkeFullState, _ = cluster.ReadStateFile(ctx, stateFilePath) + } else { + // Parse extracted state file to FullState struct + rkeFullState, err = cluster.StringToFullState(ctx, stateFile) + if err != nil { + logrus.Errorf("Error when converting state file contents to rkeFullState: %v", err) + return APIURL, caCrt, clientCert, clientKey, nil, err + } + logrus.Infof("State file is successfully extracted from snapshot [%s]", snapshotName) + stateFileRetrieved = true + } + } else { + var err error + log.Infof(ctx, "Not checking if state file is included in snapshot file for [%s], using local state file [%s]", snapshotName, stateFilePath) + rkeFullState, err = cluster.ReadStateFile(ctx, stateFilePath) + if err != nil { + return APIURL, caCrt, clientCert, clientKey, nil, err + } + } log.Infof(ctx, "Restoring etcd snapshot %s", snapshotName) kubeCluster, err := cluster.InitClusterObject(ctx, rkeConfig, flags, rkeFullState.DesiredState.EncryptionConfig) @@ -190,8 +202,8 @@ func RestoreEtcdSnapshot( return APIURL, caCrt, clientCert, clientKey, nil, err } - // If we can't retrieve statefile from snapshot, and we don't have local, we need to check for legacy cluster - if !stateFileRetrieved { + // If we can't retrieve state file from snapshot, and we don't have local, we need to check for legacy cluster + if !stateFileRetrieved || flags.UseLocalState { if err := checkLegacyCluster(ctx, kubeCluster, rkeFullState, flags); err != nil { return APIURL, caCrt, clientCert, clientKey, nil, err } @@ -305,7 +317,7 @@ func SnapshotSaveEtcdHostsFromCli(ctx *cli.Context) error { logrus.Warnf("Name of the snapshot is not specified using [%s]", etcdSnapshotName) } // setting up the flags - flags := cluster.GetExternalFlags(false, false, false, "", filePath) + flags := cluster.GetExternalFlags(false, false, false, false, "", filePath) return SnapshotSaveEtcdHosts(context.Background(), rkeConfig, hosts.DialersOptions{}, flags, etcdSnapshotName) } @@ -331,7 +343,10 @@ func RestoreEtcdSnapshotFromCli(ctx *cli.Context) error { return fmt.Errorf("you must specify the snapshot name to restore") } // setting up the flags - flags := cluster.GetExternalFlags(false, false, false, "", filePath) + // flag to use local state file + useLocalState := ctx.Bool("use-local-state") + + flags := cluster.GetExternalFlags(false, false, false, useLocalState, "", filePath) // Custom certificates and certificate dir flags flags.CertificateDir = ctx.String("cert-dir") flags.CustomCerts = ctx.Bool("custom-certs") diff --git a/cmd/remove.go b/cmd/remove.go index 41697ba7..962d6a3b 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -116,7 +116,7 @@ func clusterRemoveFromCli(ctx *cli.Context) error { } // setting up the flags - flags := cluster.GetExternalFlags(false, false, false, "", filePath) + flags := cluster.GetExternalFlags(false, false, false, false, "", filePath) return ClusterRemove(context.Background(), rkeConfig, hosts.DialersOptions{}, flags) } @@ -140,7 +140,7 @@ func clusterRemoveLocal(ctx *cli.Context) error { return err } // setting up the flags - flags := cluster.GetExternalFlags(true, false, false, "", filePath) + flags := cluster.GetExternalFlags(true, false, false, false, "", filePath) return ClusterRemove(context.Background(), rkeConfig, hosts.DialersOptions{}, flags) } diff --git a/cmd/up.go b/cmd/up.go index 2f4983ca..91d76d01 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -288,7 +288,7 @@ func clusterUpFromCli(ctx *cli.Context) error { updateOnly := ctx.Bool("update-only") disablePortCheck := ctx.Bool("disable-port-check") // setting up the flags - flags := cluster.GetExternalFlags(false, updateOnly, disablePortCheck, "", filePath) + flags := cluster.GetExternalFlags(false, updateOnly, disablePortCheck, false, "", filePath) // Custom certificates and certificate dir flags flags.CertificateDir = ctx.String("cert-dir") flags.CustomCerts = ctx.Bool("custom-certs") @@ -323,7 +323,7 @@ func clusterUpLocal(ctx *cli.Context) error { // setting up the dialers dialers := hosts.GetDialerOptions(nil, hosts.LocalHealthcheckFactory, nil) // setting up the flags - flags := cluster.GetExternalFlags(true, false, false, "", filePath) + flags := cluster.GetExternalFlags(true, false, false, false, "", filePath) if ctx.Bool("init") { return ClusterInit(context.Background(), rkeConfig, dialers, flags) @@ -349,7 +349,7 @@ func clusterUpDind(ctx *cli.Context) error { // setting up the dialers dialers := hosts.GetDialerOptions(hosts.DindConnFactory, hosts.DindHealthcheckConnFactory, nil) // setting up flags - flags := cluster.GetExternalFlags(false, false, disablePortCheck, "", filePath) + flags := cluster.GetExternalFlags(false, false, disablePortCheck, false, "", filePath) flags.DinD = true if ctx.Bool("init") {