2017-11-28 11:26:15 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/rancher/rke/cluster"
|
2017-12-11 18:28:08 +00:00
|
|
|
"github.com/rancher/rke/hosts"
|
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/sirupsen/logrus"
|
|
|
|
"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",
|
|
|
|
Value: cluster.DefaultClusterConfig,
|
|
|
|
EnvVar: "RKE_CONFIG",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return cli.Command{
|
|
|
|
Name: "up",
|
|
|
|
Usage: "Bring the cluster up",
|
|
|
|
Action: clusterUpFromCli,
|
|
|
|
Flags: upFlags,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:18:27 +00:00
|
|
|
func ClusterUp(rkeConfig *v3.RancherKubernetesEngineConfig, dockerDialerFactory, healthcheckDialerFactory hosts.DialerFactory) (string, string, string, string, error) {
|
2017-11-28 11:26:15 +00:00
|
|
|
logrus.Infof("Building Kubernetes cluster")
|
|
|
|
var APIURL, caCrt, clientCert, clientKey string
|
2017-12-19 22:18:27 +00:00
|
|
|
kubeCluster, err := cluster.ParseCluster(rkeConfig, clusterFilePath, dockerDialerFactory, healthcheckDialerFactory)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kubeCluster.TunnelHosts()
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
|
|
|
currentCluster, err := kubeCluster.GetClusterState()
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cluster.CheckEtcdHostsChanged(kubeCluster, currentCluster); err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cluster.SetUpAuthentication(kubeCluster, currentCluster)
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:16:45 +00:00
|
|
|
err = cluster.ReconcileCluster(kubeCluster, currentCluster)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:16:45 +00:00
|
|
|
err = kubeCluster.SetUpHosts()
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:16:45 +00:00
|
|
|
err = kubeCluster.DeployClusterPlanes()
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2017-12-16 03:38:15 +00:00
|
|
|
err = kubeCluster.SaveClusterState(rkeConfig)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kubeCluster.DeployNetworkPlugin()
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kubeCluster.DeployK8sAddOns()
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kubeCluster.DeployUserAddOns()
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2017-11-28 17:45:24 +00:00
|
|
|
APIURL = fmt.Sprintf("https://" + kubeCluster.ControlPlaneHosts[0].Address + ":6443")
|
2017-11-28 11:26:15 +00:00
|
|
|
caCrt = string(cert.EncodeCertPEM(kubeCluster.Certificates[pki.CACertName].Certificate))
|
|
|
|
clientCert = string(cert.EncodeCertPEM(kubeCluster.Certificates[pki.KubeAdminCommonName].Certificate))
|
|
|
|
clientKey = string(cert.EncodePrivateKeyPEM(kubeCluster.Certificates[pki.KubeAdminCommonName].Key))
|
|
|
|
|
|
|
|
logrus.Infof("Finished building Kubernetes cluster successfully")
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func clusterUpFromCli(ctx *cli.Context) error {
|
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)
|
|
|
|
}
|
2017-12-19 22:18:27 +00:00
|
|
|
_, _, _, _, err = ClusterUp(rkeConfig, nil, nil)
|
2017-11-28 11:26:15 +00:00
|
|
|
return err
|
|
|
|
}
|