mirror of
https://github.com/rancher/rke.git
synced 2025-04-27 19:25:44 +00:00
Add restore flag to use local state
This commit is contained in:
parent
d2a92d9245
commit
6761a1a3e1
@ -120,6 +120,7 @@ type ExternalFlags struct {
|
|||||||
GenerateCSR bool
|
GenerateCSR bool
|
||||||
Local bool
|
Local bool
|
||||||
UpdateOnly bool
|
UpdateOnly bool
|
||||||
|
UseLocalState bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func setDefaultIfEmptyMapValue(configMap map[string]string, key string, value string) {
|
func setDefaultIfEmptyMapValue(configMap map[string]string, key string, value string) {
|
||||||
@ -606,13 +607,14 @@ func (c *Cluster) setCloudProvider() error {
|
|||||||
return nil
|
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{
|
return ExternalFlags{
|
||||||
Local: local,
|
Local: local,
|
||||||
UpdateOnly: updateOnly,
|
UpdateOnly: updateOnly,
|
||||||
DisablePortCheck: disablePortCheck,
|
DisablePortCheck: disablePortCheck,
|
||||||
ConfigDir: configDir,
|
ConfigDir: configDir,
|
||||||
ClusterFilePath: clusterFilePath,
|
ClusterFilePath: clusterFilePath,
|
||||||
|
UseLocalState: useLocalState,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ func (c *Cluster) GetClusterState(ctx context.Context, fullState *FullState) (*C
|
|||||||
}
|
}
|
||||||
|
|
||||||
// resetup external flags
|
// 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)
|
currentCluster, err := InitClusterObject(ctx, fullState.CurrentState.RancherKubernetesEngineConfig, flags, fullState.CurrentState.EncryptionConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -91,7 +91,7 @@ func rotateRKECertificatesFromCli(ctx *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// setting up the flags
|
// setting up the flags
|
||||||
externalFlags := cluster.GetExternalFlags(false, false, false, "", filePath)
|
externalFlags := cluster.GetExternalFlags(false, false, false, false, "", filePath)
|
||||||
// setting up rotate flags
|
// setting up rotate flags
|
||||||
rkeConfig.RotateCertificates = &v3.RotateCertificates{
|
rkeConfig.RotateCertificates = &v3.RotateCertificates{
|
||||||
CACertificates: rotateCACerts,
|
CACertificates: rotateCACerts,
|
||||||
@ -120,7 +120,7 @@ func generateCSRFromCli(ctx *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// setting up the flags
|
// 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.CertificateDir = ctx.String("cert-dir")
|
||||||
externalFlags.CustomCerts = ctx.Bool("custom-certs")
|
externalFlags.CustomCerts = ctx.Bool("custom-certs")
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ func rotateEncryptionKeyFromCli(ctx *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setting up the flags
|
// 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)
|
return RotateEncryptionKey(context.Background(), rkeConfig, hosts.DialersOptions{}, flags)
|
||||||
}
|
}
|
||||||
|
35
cmd/etcd.go
35
cmd/etcd.go
@ -77,6 +77,10 @@ func EtcdCommand() cli.Command {
|
|||||||
Name: "custom-certs",
|
Name: "custom-certs",
|
||||||
Usage: "Use custom certificates from a cert dir",
|
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...)
|
snapshotRestoreFlags = append(append(snapshotFlags, snapshotRestoreFlags...), commonFlags...)
|
||||||
|
|
||||||
@ -144,7 +148,13 @@ func RestoreEtcdSnapshot(
|
|||||||
snapshotName string) (string, string, string, string, map[string]pki.CertificatePKI, error) {
|
snapshotName string) (string, string, string, string, map[string]pki.CertificatePKI, error) {
|
||||||
var APIURL, caCrt, clientCert, clientKey string
|
var APIURL, caCrt, clientCert, clientKey string
|
||||||
|
|
||||||
log.Infof(ctx, "Checking if state file is included in snapshot file for %s", snapshotName)
|
rkeFullState := &cluster.FullState{}
|
||||||
|
stateFileRetrieved := false
|
||||||
|
// Local state file
|
||||||
|
stateFilePath := cluster.GetStateFilePath(flags.ClusterFilePath, flags.ConfigDir)
|
||||||
|
|
||||||
|
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
|
// Creating temp cluster to check if snapshot archive contains state file and retrieve it
|
||||||
tempCluster, err := cluster.InitClusterObject(ctx, rkeConfig, flags, "")
|
tempCluster, err := cluster.InitClusterObject(ctx, rkeConfig, flags, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -157,11 +167,6 @@ func RestoreEtcdSnapshot(
|
|||||||
return APIURL, caCrt, clientCert, clientKey, nil, err
|
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
|
// Extract state file from snapshot
|
||||||
stateFile, err := tempCluster.GetStateFileFromSnapshot(ctx, snapshotName)
|
stateFile, err := tempCluster.GetStateFileFromSnapshot(ctx, snapshotName)
|
||||||
// If state file is not in snapshot (or can't be retrieved), fallback to local state file
|
// If state file is not in snapshot (or can't be retrieved), fallback to local state file
|
||||||
@ -178,7 +183,14 @@ func RestoreEtcdSnapshot(
|
|||||||
logrus.Infof("State file is successfully extracted from snapshot [%s]", snapshotName)
|
logrus.Infof("State file is successfully extracted from snapshot [%s]", snapshotName)
|
||||||
stateFileRetrieved = true
|
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)
|
log.Infof(ctx, "Restoring etcd snapshot %s", snapshotName)
|
||||||
|
|
||||||
kubeCluster, err := cluster.InitClusterObject(ctx, rkeConfig, flags, rkeFullState.DesiredState.EncryptionConfig)
|
kubeCluster, err := cluster.InitClusterObject(ctx, rkeConfig, flags, rkeFullState.DesiredState.EncryptionConfig)
|
||||||
@ -191,7 +203,7 @@ func RestoreEtcdSnapshot(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we can't retrieve state file from snapshot, and we don't have local, we need to check for legacy cluster
|
// If we can't retrieve state file from snapshot, and we don't have local, we need to check for legacy cluster
|
||||||
if !stateFileRetrieved {
|
if !stateFileRetrieved || flags.UseLocalState {
|
||||||
if err := checkLegacyCluster(ctx, kubeCluster, rkeFullState, flags); err != nil {
|
if err := checkLegacyCluster(ctx, kubeCluster, rkeFullState, flags); err != nil {
|
||||||
return APIURL, caCrt, clientCert, clientKey, nil, err
|
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)
|
logrus.Warnf("Name of the snapshot is not specified using [%s]", etcdSnapshotName)
|
||||||
}
|
}
|
||||||
// setting up the flags
|
// 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)
|
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")
|
return fmt.Errorf("you must specify the snapshot name to restore")
|
||||||
}
|
}
|
||||||
// setting up the flags
|
// 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
|
// Custom certificates and certificate dir flags
|
||||||
flags.CertificateDir = ctx.String("cert-dir")
|
flags.CertificateDir = ctx.String("cert-dir")
|
||||||
flags.CustomCerts = ctx.Bool("custom-certs")
|
flags.CustomCerts = ctx.Bool("custom-certs")
|
||||||
|
@ -116,7 +116,7 @@ func clusterRemoveFromCli(ctx *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setting up the flags
|
// 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)
|
return ClusterRemove(context.Background(), rkeConfig, hosts.DialersOptions{}, flags)
|
||||||
}
|
}
|
||||||
@ -140,7 +140,7 @@ func clusterRemoveLocal(ctx *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// setting up the flags
|
// 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)
|
return ClusterRemove(context.Background(), rkeConfig, hosts.DialersOptions{}, flags)
|
||||||
}
|
}
|
||||||
|
@ -288,7 +288,7 @@ func clusterUpFromCli(ctx *cli.Context) error {
|
|||||||
updateOnly := ctx.Bool("update-only")
|
updateOnly := ctx.Bool("update-only")
|
||||||
disablePortCheck := ctx.Bool("disable-port-check")
|
disablePortCheck := ctx.Bool("disable-port-check")
|
||||||
// setting up the flags
|
// 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
|
// Custom certificates and certificate dir flags
|
||||||
flags.CertificateDir = ctx.String("cert-dir")
|
flags.CertificateDir = ctx.String("cert-dir")
|
||||||
flags.CustomCerts = ctx.Bool("custom-certs")
|
flags.CustomCerts = ctx.Bool("custom-certs")
|
||||||
@ -323,7 +323,7 @@ func clusterUpLocal(ctx *cli.Context) error {
|
|||||||
// setting up the dialers
|
// setting up the dialers
|
||||||
dialers := hosts.GetDialerOptions(nil, hosts.LocalHealthcheckFactory, nil)
|
dialers := hosts.GetDialerOptions(nil, hosts.LocalHealthcheckFactory, nil)
|
||||||
// setting up the flags
|
// setting up the flags
|
||||||
flags := cluster.GetExternalFlags(true, false, false, "", filePath)
|
flags := cluster.GetExternalFlags(true, false, false, false, "", filePath)
|
||||||
|
|
||||||
if ctx.Bool("init") {
|
if ctx.Bool("init") {
|
||||||
return ClusterInit(context.Background(), rkeConfig, dialers, flags)
|
return ClusterInit(context.Background(), rkeConfig, dialers, flags)
|
||||||
@ -349,7 +349,7 @@ func clusterUpDind(ctx *cli.Context) error {
|
|||||||
// setting up the dialers
|
// setting up the dialers
|
||||||
dialers := hosts.GetDialerOptions(hosts.DindConnFactory, hosts.DindHealthcheckConnFactory, nil)
|
dialers := hosts.GetDialerOptions(hosts.DindConnFactory, hosts.DindHealthcheckConnFactory, nil)
|
||||||
// setting up flags
|
// setting up flags
|
||||||
flags := cluster.GetExternalFlags(false, false, disablePortCheck, "", filePath)
|
flags := cluster.GetExternalFlags(false, false, disablePortCheck, false, "", filePath)
|
||||||
flags.DinD = true
|
flags.DinD = true
|
||||||
|
|
||||||
if ctx.Bool("init") {
|
if ctx.Bool("init") {
|
||||||
|
Loading…
Reference in New Issue
Block a user