2017-10-29 09:45:21 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-11-02 10:07:10 +00:00
|
|
|
"github.com/rancher/rke/cluster"
|
2017-10-31 13:55:35 +00:00
|
|
|
"github.com/rancher/rke/pki"
|
2017-11-13 21:28:38 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-10-29 09:45:21 +00:00
|
|
|
"github.com/urfave/cli"
|
2017-11-02 10:07:10 +00:00
|
|
|
"k8s.io/client-go/util/cert"
|
2017-10-29 09:45:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ClusterCommand() cli.Command {
|
|
|
|
clusterUpFlags := []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cluster-file",
|
2017-11-02 10:07:10 +00:00
|
|
|
Usage: "Specify an alternate cluster YAML file",
|
2017-10-31 13:55:35 +00:00
|
|
|
Value: "cluster.yml",
|
2017-10-29 09:45:21 +00:00
|
|
|
EnvVar: "CLUSTER_FILE",
|
|
|
|
},
|
|
|
|
}
|
2017-11-15 02:54:26 +00:00
|
|
|
clusterUpgradeFlags := []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cluster-file",
|
|
|
|
Usage: "Specify an upgraded cluster YAML file",
|
|
|
|
Value: "cluster.yml",
|
|
|
|
EnvVar: "CLUSTER_FILE",
|
|
|
|
},
|
|
|
|
}
|
2017-10-29 09:45:21 +00:00
|
|
|
return cli.Command{
|
|
|
|
Name: "cluster",
|
|
|
|
ShortName: "cluster",
|
|
|
|
Usage: "Operations on the cluster",
|
|
|
|
Flags: clusterUpFlags,
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
cli.Command{
|
|
|
|
Name: "up",
|
|
|
|
Usage: "Bring the cluster up",
|
2017-11-02 10:07:10 +00:00
|
|
|
Action: clusterUpFromCli,
|
2017-10-29 09:45:21 +00:00
|
|
|
Flags: clusterUpFlags,
|
|
|
|
},
|
2017-11-13 00:30:13 +00:00
|
|
|
cli.Command{
|
|
|
|
Name: "version",
|
|
|
|
Usage: "Show Cluster Kubernetes version",
|
|
|
|
Action: getClusterVersion,
|
|
|
|
Flags: []cli.Flag{},
|
|
|
|
},
|
2017-11-15 02:54:26 +00:00
|
|
|
cli.Command{
|
|
|
|
Name: "upgrade",
|
|
|
|
Usage: "Upgrade Cluster Kubernetes version",
|
|
|
|
Action: clusterUpgradeFromCli,
|
|
|
|
Flags: clusterUpgradeFlags,
|
|
|
|
},
|
2017-10-29 09:45:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 19:50:49 +00:00
|
|
|
func ClusterUp(clusterFile string) (string, string, string, string, error) {
|
2017-11-02 10:07:10 +00:00
|
|
|
logrus.Infof("Building Kubernetes cluster")
|
2017-11-07 15:44:17 +00:00
|
|
|
var APIURL, caCrt, clientCert, clientKey string
|
2017-11-02 10:07:10 +00:00
|
|
|
kubeCluster, err := cluster.ParseConfig(clusterFile)
|
2017-10-29 09:45:21 +00:00
|
|
|
if err != nil {
|
2017-11-07 15:44:17 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
|
|
|
|
err = kubeCluster.TunnelHosts()
|
2017-10-29 09:45:21 +00:00
|
|
|
if err != nil {
|
2017-11-07 15:44:17 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
|
|
|
|
currentCluster, err := kubeCluster.GetClusterState()
|
|
|
|
if err != nil {
|
2017-11-07 15:44:17 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
|
2017-11-09 19:50:49 +00:00
|
|
|
err = cluster.SetUpAuthentication(kubeCluster, currentCluster)
|
2017-10-29 09:45:21 +00:00
|
|
|
if err != nil {
|
2017-11-07 15:44:17 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2017-11-09 19:50:49 +00:00
|
|
|
err = kubeCluster.SetUpHosts()
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
2017-11-07 15:44:17 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
|
|
|
|
err = kubeCluster.DeployClusterPlanes()
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
2017-11-07 15:44:17 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
|
2017-11-15 01:55:06 +00:00
|
|
|
err = kubeCluster.SaveClusterState(clusterFile)
|
2017-11-06 20:50:41 +00:00
|
|
|
if err != nil {
|
2017-11-08 00:32:55 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
2017-11-06 20:50:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 01:55:06 +00:00
|
|
|
err = kubeCluster.DeployNetworkPlugin()
|
2017-11-06 20:50:41 +00:00
|
|
|
if err != nil {
|
2017-11-08 00:32:55 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
2017-11-06 20:50:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 01:55:06 +00:00
|
|
|
err = kubeCluster.DeployK8sAddOns()
|
2017-10-29 09:45:21 +00:00
|
|
|
if err != nil {
|
2017-11-07 15:44:17 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2017-11-15 01:55:06 +00:00
|
|
|
|
2017-11-07 15:44:17 +00:00
|
|
|
APIURL = fmt.Sprintf("https://" + kubeCluster.ControlPlaneHosts[0].IP + ":6443")
|
2017-11-02 10:07:10 +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))
|
2017-11-07 15:44:17 +00:00
|
|
|
return APIURL, caCrt, clientCert, clientKey, nil
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func clusterUpFromCli(ctx *cli.Context) error {
|
|
|
|
clusterFile, err := resolveClusterFile(ctx)
|
2017-10-29 09:45:21 +00:00
|
|
|
if err != nil {
|
2017-11-02 10:07:10 +00:00
|
|
|
return fmt.Errorf("Failed to resolve cluster file: %v", err)
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2017-11-09 19:50:49 +00:00
|
|
|
_, _, _, _, err = ClusterUp(clusterFile)
|
2017-11-02 10:07:10 +00:00
|
|
|
return err
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func resolveClusterFile(ctx *cli.Context) (string, error) {
|
|
|
|
clusterFile := ctx.String("cluster-file")
|
|
|
|
fp, err := filepath.Abs(clusterFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to lookup current directory name: %v", err)
|
|
|
|
}
|
|
|
|
file, err := os.Open(fp)
|
|
|
|
if err != nil {
|
2017-10-31 13:55:35 +00:00
|
|
|
return "", fmt.Errorf("Can not find cluster configuration file: %v", err)
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
buf, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to read file: %v", err)
|
|
|
|
}
|
|
|
|
clusterFile = string(buf)
|
|
|
|
|
|
|
|
return clusterFile, nil
|
|
|
|
}
|
2017-11-13 00:30:13 +00:00
|
|
|
|
|
|
|
func getClusterVersion(ctx *cli.Context) error {
|
|
|
|
serverVersion, err := cluster.GetK8sVersion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Server Version: %s\n", serverVersion)
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-15 02:54:26 +00:00
|
|
|
|
|
|
|
func clusterUpgradeFromCli(ctx *cli.Context) error {
|
|
|
|
clusterFile, err := resolveClusterFile(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to resolve cluster file: %v", err)
|
|
|
|
}
|
|
|
|
_, _, _, _, err = ClusterUpgrade(clusterFile)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClusterUpgrade(clusterFile string) (string, string, string, string, error) {
|
|
|
|
logrus.Infof("Upgrading Kubernetes cluster")
|
|
|
|
var APIURL, caCrt, clientCert, clientKey string
|
|
|
|
kubeCluster, err := cluster.ParseConfig(clusterFile)
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Getting current cluster")
|
|
|
|
currentCluster, err := kubeCluster.GetClusterState()
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
logrus.Debugf("Setting up upgrade tunnels")
|
|
|
|
/*
|
|
|
|
kubeCluster is the cluster.yaml definition. It should have updated configuration
|
|
|
|
currentCluster is the current state fetched from kubernetes
|
|
|
|
we add currentCluster certs to kubeCluster, kubeCluster would have the latest configuration from cluster.yaml and the certs to connect to k8s and apply the upgrade
|
|
|
|
*/
|
|
|
|
kubeCluster.Certificates = currentCluster.Certificates
|
|
|
|
err = kubeCluster.TunnelHosts()
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
logrus.Debugf("Starting cluster upgrade")
|
|
|
|
err = kubeCluster.ClusterUpgrade()
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
2017-11-16 01:35:39 +00:00
|
|
|
|
|
|
|
err = kubeCluster.SaveClusterState(clusterFile)
|
|
|
|
if err != nil {
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, err
|
|
|
|
}
|
|
|
|
|
2017-11-15 02:54:26 +00:00
|
|
|
logrus.Infof("Cluster upgraded successfully")
|
|
|
|
|
|
|
|
APIURL = fmt.Sprintf("https://" + kubeCluster.ControlPlaneHosts[0].IP + ":6443")
|
|
|
|
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))
|
|
|
|
return APIURL, caCrt, clientCert, clientKey, nil
|
|
|
|
|
|
|
|
}
|