1
0
mirror of https://github.com/rancher/rke.git synced 2025-04-28 19:43:26 +00:00
rke/cmd/up.go

278 lines
8.3 KiB
Go
Raw Normal View History

package cmd
import (
"context"
"fmt"
2018-04-18 06:04:30 +00:00
"strings"
2018-07-10 19:21:27 +00:00
"time"
"github.com/rancher/rke/cluster"
2018-07-10 19:21:27 +00:00
"github.com/rancher/rke/dind"
"github.com/rancher/rke/hosts"
"github.com/rancher/rke/k8s"
"github.com/rancher/rke/log"
"github.com/rancher/rke/pki"
2017-12-16 03:38:15 +00:00
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/urfave/cli"
"k8s.io/client-go/util/cert"
)
2017-12-16 03:38:15 +00:00
var clusterFilePath string
2018-07-10 19:21:27 +00:00
const DINDWaitTime = 3
func UpCommand() cli.Command {
upFlags := []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "Specify an alternate cluster YAML file",
Value: pki.ClusterConfig,
EnvVar: "RKE_CONFIG",
},
cli.BoolFlag{
Name: "local",
Usage: "Deploy Kubernetes cluster locally",
},
2018-07-10 19:21:27 +00:00
cli.BoolFlag{
Name: "dind",
Usage: "Deploy Kubernetes cluster in docker containers (experimental)",
},
cli.BoolFlag{
Name: "update-only",
Usage: "Skip idempotent deployment of control and etcd plane",
},
cli.BoolFlag{
Name: "disable-port-check",
Usage: "Disable port check validation between nodes",
},
}
2018-05-15 17:35:52 +00:00
upFlags = append(upFlags, commonFlags...)
return cli.Command{
Name: "up",
Usage: "Bring the cluster up",
Action: clusterUpFromCli,
Flags: upFlags,
}
}
func ClusterUp(
ctx context.Context,
rkeConfig *v3.RancherKubernetesEngineConfig,
dockerDialerFactory, localConnDialerFactory hosts.DialerFactory,
k8sWrapTransport k8s.WrapTransport,
2018-04-02 11:02:00 +00:00
local bool, configDir string, updateOnly, disablePortCheck bool) (string, string, string, string, map[string]pki.CertificatePKI, error) {
log.Infof(ctx, "Building Kubernetes cluster")
var APIURL, caCrt, clientCert, clientKey string
kubeCluster, err := cluster.ParseCluster(ctx, rkeConfig, clusterFilePath, configDir, dockerDialerFactory, localConnDialerFactory, k8sWrapTransport)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
err = kubeCluster.TunnelHosts(ctx, local)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
2018-01-18 20:41:04 +00:00
currentCluster, err := kubeCluster.GetClusterState(ctx)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
2018-01-16 18:29:09 +00:00
}
if !disablePortCheck {
if err = kubeCluster.CheckClusterPorts(ctx, currentCluster); err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
}
err = cluster.SetUpAuthentication(ctx, kubeCluster, currentCluster)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
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))
caCrt = string(cert.EncodeCertPEM(kubeCluster.Certificates[pki.CACertName].Certificate))
err = cluster.ReconcileCluster(ctx, kubeCluster, currentCluster, updateOnly)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
// update APIURL after reconcile
if len(kubeCluster.ControlPlaneHosts) > 0 {
APIURL = fmt.Sprintf("https://" + kubeCluster.ControlPlaneHosts[0].Address + ":6443")
}
err = kubeCluster.SetUpHosts(ctx)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
2018-02-01 21:43:09 +00:00
if err := kubeCluster.PrePullK8sImages(ctx); err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
2018-02-01 21:43:09 +00:00
}
err = kubeCluster.DeployControlPlane(ctx)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, 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 {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
2018-02-26 21:14:04 +00:00
}
err = kubeCluster.SaveClusterState(ctx, &kubeCluster.RancherKubernetesEngineConfig)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
err = kubeCluster.DeployWorkerPlane(ctx)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
if err = kubeCluster.CleanDeadLogs(ctx); err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
err = kubeCluster.SyncLabelsAndTaints(ctx, currentCluster)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
2018-03-01 21:32:25 +00:00
err = cluster.ConfigureCluster(ctx, kubeCluster.RancherKubernetesEngineConfig, kubeCluster.Certificates, clusterFilePath, configDir, k8sWrapTransport, false)
if err != nil {
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, nil, err
}
2018-04-18 06:04:30 +00:00
if err := checkAllIncluded(kubeCluster); err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, err
}
log.Infof(ctx, "Finished building Kubernetes cluster successfully")
2018-04-02 11:02:00 +00:00
return APIURL, caCrt, clientCert, clientKey, kubeCluster.Certificates, nil
}
2018-04-18 06:04:30 +00:00
func checkAllIncluded(cluster *cluster.Cluster) error {
if len(cluster.InactiveHosts) == 0 {
return nil
}
var names []string
for _, host := range cluster.InactiveHosts {
names = append(names, host.Address)
}
return fmt.Errorf("Provisioning incomplete, host(s) [%s] skipped because they could not be contacted", strings.Join(names, ","))
}
func clusterUpFromCli(ctx *cli.Context) error {
2018-01-15 04:36:28 +00:00
if ctx.Bool("local") {
return clusterUpLocal(ctx)
}
2018-07-10 19:21:27 +00:00
if ctx.Bool("dind") {
return clusterUpDind(ctx)
}
2017-12-16 03:38:15 +00:00
clusterFile, filePath, err := resolveClusterFile(ctx)
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)
}
rkeConfig, err = setOptionsFromCLI(ctx, rkeConfig)
if err != nil {
return err
}
updateOnly := ctx.Bool("update-only")
disablePortCheck := ctx.Bool("disable-port-check")
2018-04-02 11:02:00 +00:00
_, _, _, _, _, err = ClusterUp(context.Background(), rkeConfig, nil, nil, nil, false, "", updateOnly, disablePortCheck)
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)
}
rkeConfig.Nodes = []v3.RKEConfigNode{*cluster.GetLocalRKENodeConfig()}
}
2018-05-15 17:35:52 +00:00
rkeConfig.IgnoreDockerVersion = ctx.Bool("ignore-docker-version")
2018-04-02 11:02:00 +00:00
_, _, _, _, _, err = ClusterUp(context.Background(), rkeConfig, nil, hosts.LocalHealthcheckFactory, nil, true, "", false, false)
return err
}
2018-07-10 19:21:27 +00:00
func clusterUpDind(ctx *cli.Context) error {
// get dind config
rkeConfig, disablePortCheck, err := getDindConfig(ctx)
2018-07-10 19:21:27 +00:00
if err != nil {
return err
}
// setup dind environment
if err = createDINDEnv(context.Background(), rkeConfig); err != nil {
2018-07-10 19:21:27 +00:00
return err
}
// start cluster
_, _, _, _, _, err = ClusterUp(context.Background(), rkeConfig, hosts.DindConnFactory, hosts.DindHealthcheckConnFactory, nil, false, "", false, disablePortCheck)
return err
}
func getDindConfig(ctx *cli.Context) (*v3.RancherKubernetesEngineConfig, bool, error) {
2018-07-10 19:21:27 +00:00
disablePortCheck := ctx.Bool("disable-port-check")
clusterFile, filePath, err := resolveClusterFile(ctx)
if err != nil {
return nil, disablePortCheck, fmt.Errorf("Failed to resolve cluster file: %v", err)
2018-07-10 19:21:27 +00:00
}
clusterFilePath = filePath
rkeConfig, err := cluster.ParseConfig(clusterFile)
if err != nil {
return nil, disablePortCheck, fmt.Errorf("Failed to parse cluster file: %v", err)
2018-07-10 19:21:27 +00:00
}
rkeConfig, err = setOptionsFromCLI(ctx, rkeConfig)
if err != nil {
return nil, disablePortCheck, err
2018-07-10 19:21:27 +00:00
}
// Setting conntrack max for kubeproxy to 0
if rkeConfig.Services.Kubeproxy.ExtraArgs == nil {
rkeConfig.Services.Kubeproxy.ExtraArgs = make(map[string]string)
}
rkeConfig.Services.Kubeproxy.ExtraArgs["conntrack-max-per-core"] = "0"
return rkeConfig, disablePortCheck, nil
2018-07-10 19:21:27 +00:00
}
func createDINDEnv(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfig) error {
for i := range rkeConfig.Nodes {
address, err := dind.StartUpDindContainer(ctx, rkeConfig.Nodes[i].Address, dind.DINDNetwork)
if err != nil {
2018-07-10 19:21:27 +00:00
return err
}
if rkeConfig.Nodes[i].HostnameOverride == "" {
rkeConfig.Nodes[i].HostnameOverride = rkeConfig.Nodes[i].Address
}
rkeConfig.Nodes[i].Address = address
2018-07-10 19:21:27 +00:00
}
time.Sleep(DINDWaitTime * time.Second)
return nil
}