2017-11-28 11:26:15 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
2017-11-28 11:26:15 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/rancher/rke/cluster"
|
2017-12-11 18:28:08 +00:00
|
|
|
"github.com/rancher/rke/hosts"
|
2018-02-20 11:51:57 +00:00
|
|
|
"github.com/rancher/rke/k8s"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2017-11-28 11:26:15 +00:00
|
|
|
"github.com/rancher/rke/pki"
|
2017-12-16 03:38:15 +00:00
|
|
|
"github.com/rancher/types/apis/management.cattle.io/v3"
|
2017-11-28 11:26:15 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
"k8s.io/client-go/util/cert"
|
|
|
|
)
|
|
|
|
|
2017-12-16 03:38:15 +00:00
|
|
|
var clusterFilePath string
|
|
|
|
|
2017-11-28 11:26:15 +00:00
|
|
|
func UpCommand() cli.Command {
|
|
|
|
upFlags := []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "Specify an alternate cluster YAML file",
|
2018-02-06 19:25:54 +00:00
|
|
|
Value: pki.ClusterConfig,
|
2017-11-28 11:26:15 +00:00
|
|
|
EnvVar: "RKE_CONFIG",
|
|
|
|
},
|
2017-12-22 01:01:53 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "local",
|
|
|
|
Usage: "Deploy Kubernetes cluster locally",
|
|
|
|
},
|
2017-11-28 11:26:15 +00:00
|
|
|
}
|
2018-03-06 00:52:43 +00:00
|
|
|
|
|
|
|
upFlags = append(upFlags, sshCliOptions...)
|
|
|
|
|
2017-11-28 11:26:15 +00:00
|
|
|
return cli.Command{
|
|
|
|
Name: "up",
|
|
|
|
Usage: "Bring the cluster up",
|
|
|
|
Action: clusterUpFromCli,
|
|
|
|
Flags: upFlags,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 01:01:53 +00:00
|
|
|
func ClusterUp(
|
|
|
|
ctx context.Context,
|
|
|
|
rkeConfig *v3.RancherKubernetesEngineConfig,
|
|
|
|
dockerDialerFactory, localConnDialerFactory hosts.DialerFactory,
|
2018-02-20 11:51:57 +00:00
|
|
|
k8sWrapTransport k8s.WrapTransport,
|
2017-12-22 01:01:53 +00:00
|
|
|
local bool, configDir string) (string, string, string, string, error) {
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "Building Kubernetes cluster")
|
2017-11-28 11:26:15 +00:00
|
|
|
var APIURL, caCrt, clientCert, clientKey string
|
2018-02-20 11:51:57 +00:00
|
|
|
kubeCluster, err := cluster.ParseCluster(ctx, rkeConfig, clusterFilePath, configDir, dockerDialerFactory, localConnDialerFactory, k8sWrapTransport)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2017-12-22 01:01:53 +00:00
|
|
|
err = kubeCluster.TunnelHosts(ctx, local)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:41:04 +00:00
|
|
|
currentCluster, err := kubeCluster.GetClusterState(ctx)
|
|
|
|
if err != nil {
|
2018-01-16 18:29:09 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:41:04 +00:00
|
|
|
if err = kubeCluster.CheckClusterPorts(ctx, currentCluster); err != nil {
|
2017-11-28 11:26:15 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
err = cluster.SetUpAuthentication(ctx, kubeCluster, currentCluster)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
err = cluster.ReconcileCluster(ctx, kubeCluster, currentCluster)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
err = kubeCluster.SetUpHosts(ctx)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-02-01 21:43:09 +00:00
|
|
|
if err := kubeCluster.PrePullK8sImages(ctx); err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
err = kubeCluster.DeployControlPlane(ctx)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-02-26 21:14:04 +00:00
|
|
|
// Apply Authz configuration after deploying controlplane
|
|
|
|
err = cluster.ApplyAuthzResources(ctx, kubeCluster.RancherKubernetesEngineConfig, clusterFilePath, configDir, k8sWrapTransport)
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
err = kubeCluster.SaveClusterState(ctx, rkeConfig)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
err = kubeCluster.DeployWorkerPlane(ctx)
|
2017-12-26 22:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-02-01 21:28:31 +00:00
|
|
|
err = kubeCluster.SyncLabelsAndTaints(ctx)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2018-03-01 21:32:25 +00:00
|
|
|
err = cluster.ConfigureCluster(ctx, kubeCluster.RancherKubernetesEngineConfig, kubeCluster.Certificates, clusterFilePath, configDir, k8sWrapTransport, false)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
2018-02-15 03:25:36 +00:00
|
|
|
if len(kubeCluster.ControlPlaneHosts) > 0 {
|
|
|
|
APIURL = fmt.Sprintf("https://" + kubeCluster.ControlPlaneHosts[0].Address + ":6443")
|
|
|
|
clientCert = string(cert.EncodeCertPEM(kubeCluster.Certificates[pki.KubeAdminCertName].Certificate))
|
|
|
|
clientKey = string(cert.EncodePrivateKeyPEM(kubeCluster.Certificates[pki.KubeAdminCertName].Key))
|
|
|
|
}
|
2017-11-28 11:26:15 +00:00
|
|
|
caCrt = string(cert.EncodeCertPEM(kubeCluster.Certificates[pki.CACertName].Certificate))
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "Finished building Kubernetes cluster successfully")
|
2017-11-28 11:26:15 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func clusterUpFromCli(ctx *cli.Context) error {
|
2018-01-15 04:36:28 +00:00
|
|
|
if ctx.Bool("local") {
|
|
|
|
return clusterUpLocal(ctx)
|
|
|
|
}
|
2017-12-16 03:38:15 +00:00
|
|
|
clusterFile, filePath, err := resolveClusterFile(ctx)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to resolve cluster file: %v", err)
|
|
|
|
}
|
2017-12-16 03:38:15 +00:00
|
|
|
clusterFilePath = filePath
|
|
|
|
|
|
|
|
rkeConfig, err := cluster.ParseConfig(clusterFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse cluster file: %v", err)
|
|
|
|
}
|
2018-03-06 00:52:43 +00:00
|
|
|
|
|
|
|
rkeConfig, err = setOptionsFromCLI(ctx, rkeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-20 11:51:57 +00:00
|
|
|
_, _, _, _, err = ClusterUp(context.Background(), rkeConfig, nil, nil, nil, false, "")
|
2018-01-15 04:36:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func clusterUpLocal(ctx *cli.Context) error {
|
|
|
|
var rkeConfig *v3.RancherKubernetesEngineConfig
|
|
|
|
clusterFile, filePath, err := resolveClusterFile(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Infof(context.Background(), "Failed to resolve cluster file, using default cluster instead")
|
|
|
|
rkeConfig = cluster.GetLocalRKEConfig()
|
|
|
|
} else {
|
|
|
|
clusterFilePath = filePath
|
|
|
|
rkeConfig, err = cluster.ParseConfig(clusterFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse cluster file: %v", err)
|
|
|
|
}
|
2017-12-22 01:01:53 +00:00
|
|
|
rkeConfig.Nodes = []v3.RKEConfigNode{*cluster.GetLocalRKENodeConfig()}
|
|
|
|
}
|
2018-02-20 11:51:57 +00:00
|
|
|
_, _, _, _, err = ClusterUp(context.Background(), rkeConfig, nil, hosts.LocalHealthcheckFactory, nil, true, "")
|
2017-11-28 11:26:15 +00:00
|
|
|
return err
|
|
|
|
}
|