mirror of
https://github.com/rancher/rke.git
synced 2025-08-17 06:16:58 +00:00
Check legacy state if kubeconfig doesnt exist
This commit is contained in:
parent
9679aca20c
commit
7a0406c44f
@ -15,10 +15,10 @@ import (
|
|||||||
"github.com/rancher/rke/k8s"
|
"github.com/rancher/rke/k8s"
|
||||||
"github.com/rancher/rke/log"
|
"github.com/rancher/rke/log"
|
||||||
"github.com/rancher/rke/pki"
|
"github.com/rancher/rke/pki"
|
||||||
"github.com/rancher/types/apis/management.cattle.io/v3"
|
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -267,7 +267,6 @@ func removeStateFile(ctx context.Context, statePath string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetStateFromNodes(ctx context.Context, kubeCluster *Cluster) *Cluster {
|
func GetStateFromNodes(ctx context.Context, kubeCluster *Cluster) *Cluster {
|
||||||
log.Infof(ctx, "[state] Fetching cluster state from Nodes")
|
|
||||||
var currentCluster Cluster
|
var currentCluster Cluster
|
||||||
var clusterFile string
|
var clusterFile string
|
||||||
var err error
|
var err error
|
||||||
|
@ -6,10 +6,12 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/rancher/rke/cluster"
|
"github.com/rancher/rke/cluster"
|
||||||
"github.com/rancher/rke/hosts"
|
"github.com/rancher/rke/hosts"
|
||||||
"github.com/rancher/rke/log"
|
"github.com/rancher/rke/log"
|
||||||
|
"github.com/rancher/rke/util"
|
||||||
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
|
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
@ -82,8 +84,11 @@ func ClusterInit(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfi
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = doUpgradeLegacyCluster(ctx, kubeCluster, rkeFullState, flags)
|
err = checkLegacyCluster(ctx, kubeCluster, rkeFullState, flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "aborting upgrade") {
|
||||||
|
return err
|
||||||
|
}
|
||||||
log.Warnf(ctx, "[state] can't fetch legacy cluster state from Kubernetes")
|
log.Warnf(ctx, "[state] can't fetch legacy cluster state from Kubernetes")
|
||||||
}
|
}
|
||||||
// check if certificate rotate or normal init
|
// check if certificate rotate or normal init
|
||||||
@ -128,15 +133,32 @@ func setS3OptionsFromCLI(c *cli.Context) *v3.S3BackupConfig {
|
|||||||
return s3BackupBackend
|
return s3BackupBackend
|
||||||
}
|
}
|
||||||
|
|
||||||
func doUpgradeLegacyCluster(ctx context.Context, kubeCluster *cluster.Cluster, fullState *cluster.FullState, flags cluster.ExternalFlags) error {
|
func checkLegacyCluster(ctx context.Context, kubeCluster *cluster.Cluster, fullState *cluster.FullState, flags cluster.ExternalFlags) error {
|
||||||
if _, err := os.Stat(kubeCluster.LocalKubeConfigPath); os.IsNotExist(err) {
|
stateFileExists, err := util.IsFileExists(kubeCluster.StateFilePath)
|
||||||
// there is no kubeconfig. This is a new cluster
|
if err != nil {
|
||||||
logrus.Debug("[state] local kubeconfig not found, this is a new cluster")
|
return err
|
||||||
|
}
|
||||||
|
if stateFileExists {
|
||||||
|
logrus.Debug("[state] previous state found, this is not a legacy cluster")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(kubeCluster.StateFilePath); err == nil {
|
logrus.Debug("[state] previous state not found, possible legacy cluster")
|
||||||
// this cluster has a previous state, I don't need to upgrade!
|
return fetchAndUpdateStateFromLegacyCluster(ctx, kubeCluster, fullState, flags)
|
||||||
logrus.Debug("[state] previous state found, this is not a legacy cluster")
|
}
|
||||||
|
|
||||||
|
func fetchAndUpdateStateFromLegacyCluster(ctx context.Context, kubeCluster *cluster.Cluster, fullState *cluster.FullState, flags cluster.ExternalFlags) error {
|
||||||
|
kubeConfigExists, err := util.IsFileExists(kubeCluster.LocalKubeConfigPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !kubeConfigExists {
|
||||||
|
// if kubeconfig doesn't exist and its a legacy cluster then error out
|
||||||
|
if err := kubeCluster.TunnelHosts(ctx, flags); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if recoveredCluster := cluster.GetStateFromNodes(ctx, kubeCluster); recoveredCluster != nil {
|
||||||
|
return fmt.Errorf("This is a legacy cluster with no kube config, aborting upgrade. Please re-run rke up with rke 0.1.x to retrieve correct state")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// We have a kubeconfig and no current state. This is a legacy cluster or a new cluster with old kubeconfig
|
// We have a kubeconfig and no current state. This is a legacy cluster or a new cluster with old kubeconfig
|
||||||
@ -153,6 +175,8 @@ func doUpgradeLegacyCluster(ctx context.Context, kubeCluster *cluster.Cluster, f
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// fetching state/certs from nodes should be removed in rke 0.3.0
|
||||||
|
log.Infof(ctx, "[state] Fetching cluster state from Nodes")
|
||||||
recoveredCluster = cluster.GetStateFromNodes(ctx, kubeCluster)
|
recoveredCluster = cluster.GetStateFromNodes(ctx, kubeCluster)
|
||||||
}
|
}
|
||||||
// if we found a recovered cluster, we will need override the current state
|
// if we found a recovered cluster, we will need override the current state
|
||||||
|
@ -117,7 +117,7 @@ func RestoreEtcdSnapshot(
|
|||||||
}
|
}
|
||||||
stateFilePath := cluster.GetStateFilePath(flags.ClusterFilePath, flags.ConfigDir)
|
stateFilePath := cluster.GetStateFilePath(flags.ClusterFilePath, flags.ConfigDir)
|
||||||
rkeFullState, _ := cluster.ReadStateFile(ctx, stateFilePath)
|
rkeFullState, _ := cluster.ReadStateFile(ctx, stateFilePath)
|
||||||
if err := doUpgradeLegacyCluster(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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
util/util.go
10
util/util.go
@ -103,3 +103,13 @@ func GetTagMajorVersion(tag string) string {
|
|||||||
}
|
}
|
||||||
return strings.Join(splitTag[:2], ".")
|
return strings.Join(splitTag[:2], ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsFileExists(filePath string) (bool, error) {
|
||||||
|
if _, err := os.Stat(filePath); err == nil {
|
||||||
|
return true, nil
|
||||||
|
} else if os.IsNotExist(err) {
|
||||||
|
return false, nil
|
||||||
|
} else {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user