mirror of
https://github.com/rancher/rke.git
synced 2025-05-07 07:47:41 +00:00
183 lines
5.4 KiB
Go
183 lines
5.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/rancher/rke/cluster"
|
|
"github.com/rancher/rke/hosts"
|
|
"github.com/rancher/rke/k8s"
|
|
"github.com/rancher/rke/log"
|
|
"github.com/rancher/rke/pki"
|
|
"github.com/rancher/types/apis/management.cattle.io/v3"
|
|
"github.com/urfave/cli"
|
|
"k8s.io/client-go/util/cert"
|
|
)
|
|
|
|
var clusterFilePath string
|
|
|
|
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",
|
|
},
|
|
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",
|
|
},
|
|
}
|
|
|
|
upFlags = append(upFlags, sshCliOptions...)
|
|
|
|
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,
|
|
local bool, configDir string, updateOnly, disablePortCheck bool) (string, string, string, string, 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 {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
err = kubeCluster.TunnelHosts(ctx, local)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
currentCluster, err := kubeCluster.GetClusterState(ctx)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
if !disablePortCheck {
|
|
if err = kubeCluster.CheckClusterPorts(ctx, currentCluster); err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
}
|
|
|
|
err = cluster.SetUpAuthentication(ctx, kubeCluster, currentCluster)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
err = cluster.ReconcileCluster(ctx, kubeCluster, currentCluster, updateOnly)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
err = kubeCluster.SetUpHosts(ctx)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
if err := kubeCluster.PrePullK8sImages(ctx); err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
err = kubeCluster.DeployControlPlane(ctx)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
// Apply Authz configuration after deploying controlplane
|
|
err = cluster.ApplyAuthzResources(ctx, kubeCluster.RancherKubernetesEngineConfig, clusterFilePath, configDir, k8sWrapTransport)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
err = kubeCluster.SaveClusterState(ctx, rkeConfig)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
err = kubeCluster.DeployWorkerPlane(ctx)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
err = kubeCluster.SyncLabelsAndTaints(ctx)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
}
|
|
|
|
err = cluster.ConfigureCluster(ctx, kubeCluster.RancherKubernetesEngineConfig, kubeCluster.Certificates, clusterFilePath, configDir, k8sWrapTransport, false)
|
|
if err != nil {
|
|
return APIURL, caCrt, clientCert, clientKey, 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))
|
|
|
|
log.Infof(ctx, "Finished building Kubernetes cluster successfully")
|
|
return APIURL, caCrt, clientCert, clientKey, nil
|
|
}
|
|
|
|
func clusterUpFromCli(ctx *cli.Context) error {
|
|
if ctx.Bool("local") {
|
|
return clusterUpLocal(ctx)
|
|
}
|
|
clusterFile, filePath, err := resolveClusterFile(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to resolve cluster file: %v", err)
|
|
}
|
|
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")
|
|
|
|
_, _, _, _, err = ClusterUp(context.Background(), rkeConfig, nil, nil, nil, false, "", updateOnly, disablePortCheck)
|
|
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()}
|
|
}
|
|
_, _, _, _, err = ClusterUp(context.Background(), rkeConfig, nil, hosts.LocalHealthcheckFactory, nil, true, "", false, false)
|
|
return err
|
|
}
|