2017-11-02 10:07:10 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
2017-11-02 10:07:10 +00:00
|
|
|
"fmt"
|
2017-11-21 19:25:08 +00:00
|
|
|
"os"
|
2017-11-02 10:07:10 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rancher/rke/k8s"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2018-04-25 05:11:57 +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-13 21:28:38 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-01-09 22:10:56 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2017-11-13 21:28:38 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-11-02 10:07:10 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func (c *Cluster) SaveClusterState(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfig) error {
|
2018-02-15 03:25:36 +00:00
|
|
|
if len(c.ControlPlaneHosts) > 0 {
|
|
|
|
// Reinitialize kubernetes Client
|
|
|
|
var err error
|
2018-02-20 11:51:57 +00:00
|
|
|
c.KubeClient, err = k8s.NewClient(c.LocalKubeConfigPath, c.K8sWrapTransport)
|
2018-02-15 03:25:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to re-initialize Kubernetes Client: %v", err)
|
|
|
|
}
|
|
|
|
err = saveClusterCerts(ctx, c.KubeClient, c.Certificates)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[certificates] Failed to Save Kubernetes certificates: %v", err)
|
|
|
|
}
|
|
|
|
err = saveStateToKubernetes(ctx, c.KubeClient, c.LocalKubeConfigPath, rkeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[state] Failed to save configuration state: %v", err)
|
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func (c *Cluster) GetClusterState(ctx context.Context) (*Cluster, error) {
|
2017-11-02 10:07:10 +00:00
|
|
|
var err error
|
|
|
|
var currentCluster *Cluster
|
2017-11-21 19:25:08 +00:00
|
|
|
|
|
|
|
// check if local kubeconfig file exists
|
|
|
|
if _, err = os.Stat(c.LocalKubeConfigPath); !os.IsNotExist(err) {
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[state] Found local kube config file, trying to get state from cluster")
|
2017-11-21 19:25:08 +00:00
|
|
|
|
2017-12-06 02:22:50 +00:00
|
|
|
// to handle if current local admin is down and we need to use new cp from the list
|
2018-02-20 11:51:57 +00:00
|
|
|
if !isLocalConfigWorking(ctx, c.LocalKubeConfigPath, c.K8sWrapTransport) {
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := rebuildLocalAdminConfig(ctx, c); err != nil {
|
2017-12-06 02:22:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:25:08 +00:00
|
|
|
// initiate kubernetes client
|
2018-02-20 11:51:57 +00:00
|
|
|
c.KubeClient, err = k8s.NewClient(c.LocalKubeConfigPath, c.K8sWrapTransport)
|
2017-11-21 19:25:08 +00:00
|
|
|
if err != nil {
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Warnf(ctx, "Failed to initiate new Kubernetes Client: %v", err)
|
2017-11-21 19:25:08 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-09 11:53:38 +00:00
|
|
|
// Get previous kubernetes state
|
2018-01-09 22:10:56 +00:00
|
|
|
currentCluster = getStateFromKubernetes(ctx, c.KubeClient, c.LocalKubeConfigPath)
|
2017-11-21 19:25:08 +00:00
|
|
|
// Get previous kubernetes certificates
|
2017-11-02 10:07:10 +00:00
|
|
|
if currentCluster != nil {
|
2018-01-16 23:10:14 +00:00
|
|
|
if err := currentCluster.InvertIndexHosts(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to classify hosts from fetched cluster: %v", err)
|
|
|
|
}
|
2018-04-22 19:03:36 +00:00
|
|
|
activeEtcdHosts := currentCluster.EtcdHosts
|
|
|
|
for _, inactiveHost := range c.InactiveHosts {
|
|
|
|
activeEtcdHosts = removeFromHosts(inactiveHost, activeEtcdHosts)
|
|
|
|
}
|
|
|
|
currentCluster.Certificates, err = getClusterCerts(ctx, c.KubeClient, activeEtcdHosts)
|
2017-12-19 22:18:27 +00:00
|
|
|
currentCluster.DockerDialerFactory = c.DockerDialerFactory
|
2018-02-07 19:39:56 +00:00
|
|
|
currentCluster.LocalConnDialerFactory = c.LocalConnDialerFactory
|
2017-11-15 02:54:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to Get Kubernetes certificates: %v", err)
|
|
|
|
}
|
2018-04-25 05:11:57 +00:00
|
|
|
|
|
|
|
// make sure I have all the etcd certs, We need handle dialer failure for etcd nodes https://github.com/rancher/rancher/issues/12898
|
|
|
|
for _, host := range activeEtcdHosts {
|
|
|
|
certName := pki.GetEtcdCrtName(host.InternalAddress)
|
|
|
|
if (currentCluster.Certificates[certName] == pki.CertificatePKI{}) {
|
|
|
|
if currentCluster.Certificates, err = pki.RegenerateEtcdCertificate(ctx,
|
|
|
|
currentCluster.Certificates,
|
|
|
|
host,
|
|
|
|
activeEtcdHosts,
|
|
|
|
currentCluster.ClusterDomain,
|
|
|
|
currentCluster.KubernetesServiceIP); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-28 22:18:00 +00:00
|
|
|
// setting cluster defaults for the fetched cluster as well
|
2018-01-09 22:10:56 +00:00
|
|
|
currentCluster.setClusterDefaults(ctx)
|
2017-11-28 22:18:00 +00:00
|
|
|
|
2017-11-21 19:25:08 +00:00
|
|
|
currentCluster.Certificates, err = regenerateAPICertificate(c, currentCluster.Certificates)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to regenerate KubeAPI certificate %v", err)
|
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return currentCluster, nil
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func saveStateToKubernetes(ctx context.Context, kubeClient *kubernetes.Clientset, kubeConfigPath string, rkeConfig *v3.RancherKubernetesEngineConfig) error {
|
|
|
|
log.Infof(ctx, "[state] Saving cluster state to Kubernetes")
|
2017-12-16 03:38:15 +00:00
|
|
|
clusterFile, err := yaml.Marshal(*rkeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
timeout := make(chan bool, 1)
|
|
|
|
go func() {
|
|
|
|
for {
|
2017-11-07 15:44:17 +00:00
|
|
|
err := k8s.UpdateConfigMap(kubeClient, clusterFile, StateConfigMapName)
|
2017-11-02 10:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
continue
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[state] Successfully Saved cluster state to Kubernetes ConfigMap: %s", StateConfigMapName)
|
2017-11-02 10:07:10 +00:00
|
|
|
timeout <- true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return nil
|
|
|
|
case <-time.After(time.Second * UpdateStateTimeout):
|
|
|
|
return fmt.Errorf("[state] Timeout waiting for kubernetes to be ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func getStateFromKubernetes(ctx context.Context, kubeClient *kubernetes.Clientset, kubeConfigPath string) *Cluster {
|
|
|
|
log.Infof(ctx, "[state] Fetching cluster state from Kubernetes")
|
2017-11-02 10:07:10 +00:00
|
|
|
var cfgMap *v1.ConfigMap
|
|
|
|
var currentCluster Cluster
|
|
|
|
var err error
|
|
|
|
timeout := make(chan bool, 1)
|
|
|
|
go func() {
|
|
|
|
for {
|
2017-11-07 15:44:17 +00:00
|
|
|
cfgMap, err = k8s.GetConfigMap(kubeClient, StateConfigMapName)
|
2017-11-02 10:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
continue
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[state] Successfully Fetched cluster state to Kubernetes ConfigMap: %s", StateConfigMapName)
|
2017-11-02 10:07:10 +00:00
|
|
|
timeout <- true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
clusterData := cfgMap.Data[StateConfigMapName]
|
|
|
|
err := yaml.Unmarshal([]byte(clusterData), ¤tCluster)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ¤tCluster
|
|
|
|
case <-time.After(time.Second * GetStateTimeout):
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "Timed out waiting for kubernetes cluster to get state")
|
2017-11-02 10:07:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 00:30:13 +00:00
|
|
|
|
2018-02-20 11:51:57 +00:00
|
|
|
func GetK8sVersion(localConfigPath string, k8sWrapTransport k8s.WrapTransport) (string, error) {
|
2017-11-30 23:16:45 +00:00
|
|
|
logrus.Debugf("[version] Using %s to connect to Kubernetes cluster..", localConfigPath)
|
2018-02-20 11:51:57 +00:00
|
|
|
k8sClient, err := k8s.NewClient(localConfigPath, k8sWrapTransport)
|
2017-11-13 00:30:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to create Kubernetes Client: %v", err)
|
|
|
|
}
|
|
|
|
discoveryClient := k8sClient.DiscoveryClient
|
|
|
|
logrus.Debugf("[version] Getting Kubernetes server version..")
|
|
|
|
serverVersion, err := discoveryClient.ServerVersion()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to get Kubernetes server version: %v", err)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%#v", *serverVersion), nil
|
|
|
|
}
|