2017-11-28 11:26:15 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-11-19 19:30:45 +00:00
|
|
|
"context"
|
2017-11-28 11:26:15 +00:00
|
|
|
"fmt"
|
2018-11-19 19:30:45 +00:00
|
|
|
"github.com/rancher/rke/cluster"
|
|
|
|
"github.com/rancher/rke/hosts"
|
|
|
|
"github.com/rancher/rke/log"
|
|
|
|
"github.com/rancher/types/apis/management.cattle.io/v3"
|
|
|
|
"github.com/urfave/cli"
|
2017-11-28 11:26:15 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2018-05-15 17:35:52 +00:00
|
|
|
var commonFlags = []cli.Flag{
|
2018-03-06 00:52:43 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "ssh-agent-auth",
|
|
|
|
Usage: "Use SSH Agent Auth defined by SSH_AUTH_SOCK",
|
|
|
|
},
|
2018-05-15 17:35:52 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "ignore-docker-version",
|
|
|
|
Usage: "Disable Docker version check",
|
|
|
|
},
|
2018-03-06 00:52:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 03:38:15 +00:00
|
|
|
func resolveClusterFile(ctx *cli.Context) (string, string, error) {
|
2017-11-28 11:26:15 +00:00
|
|
|
clusterFile := ctx.String("config")
|
|
|
|
fp, err := filepath.Abs(clusterFile)
|
|
|
|
if err != nil {
|
2017-12-16 03:38:15 +00:00
|
|
|
return "", "", fmt.Errorf("failed to lookup current directory name: %v", err)
|
2017-11-28 11:26:15 +00:00
|
|
|
}
|
|
|
|
file, err := os.Open(fp)
|
|
|
|
if err != nil {
|
2017-12-16 03:38:15 +00:00
|
|
|
return "", "", fmt.Errorf("Can not find cluster configuration file: %v", err)
|
2017-11-28 11:26:15 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
buf, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
2017-12-16 03:38:15 +00:00
|
|
|
return "", "", fmt.Errorf("failed to read file: %v", err)
|
2017-11-28 11:26:15 +00:00
|
|
|
}
|
|
|
|
clusterFileBuff := string(buf)
|
2017-12-16 03:38:15 +00:00
|
|
|
return clusterFileBuff, clusterFile, nil
|
2017-11-28 11:26:15 +00:00
|
|
|
}
|
2018-03-06 00:52:43 +00:00
|
|
|
|
|
|
|
func setOptionsFromCLI(c *cli.Context, rkeConfig *v3.RancherKubernetesEngineConfig) (*v3.RancherKubernetesEngineConfig, error) {
|
|
|
|
// If true... override the file.. else let file value go through
|
|
|
|
if c.Bool("ssh-agent-auth") {
|
|
|
|
rkeConfig.SSHAgentAuth = c.Bool("ssh-agent-auth")
|
|
|
|
}
|
2018-05-15 17:35:52 +00:00
|
|
|
|
|
|
|
if c.Bool("ignore-docker-version") {
|
|
|
|
rkeConfig.IgnoreDockerVersion = c.Bool("ignore-docker-version")
|
|
|
|
}
|
|
|
|
|
2018-03-06 00:52:43 +00:00
|
|
|
return rkeConfig, nil
|
|
|
|
}
|
2018-11-19 19:30:45 +00:00
|
|
|
|
|
|
|
func ClusterInit(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfig, dialersOptions hosts.DialersOptions, flags cluster.ExternalFlags) error {
|
|
|
|
log.Infof(ctx, "Initiating Kubernetes cluster")
|
|
|
|
var fullState *cluster.FullState
|
|
|
|
stateFilePath := cluster.GetStateFilePath(flags.ClusterFilePath, flags.ConfigDir)
|
|
|
|
rkeFullState, _ := cluster.ReadStateFile(ctx, stateFilePath)
|
|
|
|
|
|
|
|
kubeCluster, err := cluster.InitClusterObject(ctx, rkeConfig, flags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := kubeCluster.SetupDialers(ctx, dialersOptions); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = doUpgradeLegacyCluster(ctx, kubeCluster, rkeFullState)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf(ctx, "[state] can't fetch legacy cluster state from Kubernetes")
|
|
|
|
}
|
|
|
|
// check if certificate rotate or normal init
|
|
|
|
if kubeCluster.RancherKubernetesEngineConfig.RotateCertificates != nil {
|
|
|
|
fullState, err = rotateRKECertificates(ctx, kubeCluster, flags, rkeFullState)
|
|
|
|
} else {
|
|
|
|
fullState, err = cluster.RebuildState(ctx, &kubeCluster.RancherKubernetesEngineConfig, rkeFullState, flags)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rkeState := cluster.FullState{
|
|
|
|
DesiredState: fullState.DesiredState,
|
|
|
|
CurrentState: fullState.CurrentState,
|
|
|
|
}
|
|
|
|
return rkeState.WriteStateFile(ctx, stateFilePath)
|
|
|
|
}
|