2017-11-02 10:07:10 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-11-15 01:12:33 +00:00
|
|
|
"path/filepath"
|
2017-11-30 23:16:45 +00:00
|
|
|
"strings"
|
2017-11-02 10:07:10 +00:00
|
|
|
|
|
|
|
"github.com/rancher/rke/hosts"
|
|
|
|
"github.com/rancher/rke/pki"
|
|
|
|
"github.com/rancher/rke/services"
|
2017-12-05 16:55:58 +00:00
|
|
|
"github.com/rancher/types/apis/management.cattle.io/v3"
|
2017-11-13 21:28:38 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-12-05 16:55:58 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2017-11-02 10:07:10 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2017-11-30 23:16:45 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2017-11-17 00:45:51 +00:00
|
|
|
"k8s.io/client-go/util/cert"
|
2017-11-02 10:07:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Cluster struct {
|
2017-12-05 16:55:58 +00:00
|
|
|
v3.RancherKubernetesEngineConfig `yaml:",inline"`
|
2017-11-15 01:12:33 +00:00
|
|
|
ConfigPath string `yaml:"config_path"`
|
|
|
|
LocalKubeConfigPath string
|
2017-11-30 23:16:45 +00:00
|
|
|
EtcdHosts []*hosts.Host
|
|
|
|
WorkerHosts []*hosts.Host
|
|
|
|
ControlPlaneHosts []*hosts.Host
|
2017-11-14 18:11:21 +00:00
|
|
|
KubeClient *kubernetes.Clientset
|
|
|
|
KubernetesServiceIP net.IP
|
|
|
|
Certificates map[string]pki.CertificatePKI
|
|
|
|
ClusterDomain string
|
|
|
|
ClusterCIDR string
|
|
|
|
ClusterDNSServer string
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2017-12-05 01:29:29 +00:00
|
|
|
X509AuthenticationProvider = "x509"
|
|
|
|
StateConfigMapName = "cluster-state"
|
|
|
|
UpdateStateTimeout = 30
|
|
|
|
GetStateTimeout = 30
|
|
|
|
KubernetesClientTimeOut = 30
|
|
|
|
AplineImage = "alpine"
|
|
|
|
NginxProxyImage = "nginx_proxy"
|
|
|
|
CertDownloaderImage = "cert_downloader"
|
|
|
|
KubeDNSImage = "kubedns_image"
|
|
|
|
DNSMasqImage = "dnsmasq_image"
|
|
|
|
KubeDNSSidecarImage = "kubedns_sidecar_image"
|
|
|
|
KubeDNSAutoScalerImage = "kubedns_autoscaler_image"
|
2017-11-02 10:07:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Cluster) DeployClusterPlanes() error {
|
|
|
|
// Deploy Kubernetes Planes
|
|
|
|
err := services.RunEtcdPlane(c.EtcdHosts, c.Services.Etcd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[etcd] Failed to bring up Etcd Plane: %v", err)
|
|
|
|
}
|
|
|
|
err = services.RunControlPlane(c.ControlPlaneHosts, c.EtcdHosts, c.Services)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[controlPlane] Failed to bring up Control Plane: %v", err)
|
|
|
|
}
|
2017-12-05 01:29:29 +00:00
|
|
|
err = services.RunWorkerPlane(c.ControlPlaneHosts, c.WorkerHosts, c.Services, c.SystemImages[NginxProxyImage])
|
2017-11-02 10:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[workerPlane] Failed to bring up Worker Plane: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseConfig(clusterFile string) (*Cluster, error) {
|
|
|
|
logrus.Debugf("Parsing cluster file [%v]", clusterFile)
|
|
|
|
var err error
|
|
|
|
c, err := parseClusterFile(clusterFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse the cluster file: %v", err)
|
|
|
|
}
|
|
|
|
err = c.InvertIndexHosts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to classify hosts from config file: %v", err)
|
|
|
|
}
|
2017-11-21 19:25:08 +00:00
|
|
|
|
|
|
|
err = c.ValidateCluster()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to validate cluster: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-07 15:44:17 +00:00
|
|
|
c.KubernetesServiceIP, err = services.GetKubernetesServiceIP(c.Services.KubeAPI.ServiceClusterIPRange)
|
2017-11-02 10:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to get Kubernetes Service IP: %v", err)
|
|
|
|
}
|
|
|
|
c.ClusterDomain = c.Services.Kubelet.ClusterDomain
|
2017-11-06 20:50:41 +00:00
|
|
|
c.ClusterCIDR = c.Services.KubeController.ClusterCIDR
|
2017-11-08 00:32:55 +00:00
|
|
|
c.ClusterDNSServer = c.Services.Kubelet.ClusterDNSServer
|
2017-11-15 01:12:33 +00:00
|
|
|
if len(c.ConfigPath) == 0 {
|
|
|
|
c.ConfigPath = DefaultClusterConfig
|
|
|
|
}
|
|
|
|
c.LocalKubeConfigPath = GetLocalKubeConfig(c.ConfigPath)
|
2017-11-02 10:07:10 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseClusterFile(clusterFile string) (*Cluster, error) {
|
|
|
|
// parse hosts
|
|
|
|
var kubeCluster Cluster
|
|
|
|
err := yaml.Unmarshal([]byte(clusterFile), &kubeCluster)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-28 17:45:24 +00:00
|
|
|
// Setting cluster Defaults
|
|
|
|
kubeCluster.setClusterDefaults()
|
|
|
|
|
|
|
|
return &kubeCluster, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) setClusterDefaults() {
|
2017-12-02 17:07:47 +00:00
|
|
|
if len(c.SSHKeyPath) == 0 {
|
|
|
|
c.SSHKeyPath = DefaultClusterSSHKeyPath
|
|
|
|
}
|
2017-11-28 17:45:24 +00:00
|
|
|
for i, host := range c.Nodes {
|
|
|
|
if len(host.InternalAddress) == 0 {
|
|
|
|
c.Nodes[i].InternalAddress = c.Nodes[i].Address
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
2017-11-28 17:45:24 +00:00
|
|
|
if len(host.HostnameOverride) == 0 {
|
|
|
|
// This is a temporary modification
|
|
|
|
c.Nodes[i].HostnameOverride = c.Nodes[i].Address
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
2017-12-02 17:07:47 +00:00
|
|
|
if len(host.SSHKeyPath) == 0 {
|
|
|
|
c.Nodes[i].SSHKeyPath = c.SSHKeyPath
|
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
2017-12-05 01:29:29 +00:00
|
|
|
c.setClusterServicesDefaults()
|
|
|
|
c.setClusterNetworkDefaults()
|
|
|
|
c.setClusterImageDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) setClusterServicesDefaults() {
|
|
|
|
serviceConfigDefaultsMap := map[string]string{
|
|
|
|
c.Services.KubeAPI.ServiceClusterIPRange: DefaultServiceClusterIPRange,
|
|
|
|
c.Services.KubeController.ServiceClusterIPRange: DefaultServiceClusterIPRange,
|
|
|
|
c.Services.KubeController.ClusterCIDR: DefaultClusterCIDR,
|
|
|
|
c.Services.Kubelet.ClusterDNSServer: DefaultClusterDNSService,
|
|
|
|
c.Services.Kubelet.ClusterDomain: DefaultClusterDomain,
|
|
|
|
c.Services.Kubelet.InfraContainerImage: DefaultInfraContainerImage,
|
|
|
|
c.Authentication.Strategy: DefaultAuthStrategy,
|
|
|
|
}
|
|
|
|
for k, v := range serviceConfigDefaultsMap {
|
|
|
|
setDefaultIfEmpty(&k, v)
|
2017-11-28 17:45:24 +00:00
|
|
|
}
|
2017-12-05 01:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) setClusterImageDefaults() {
|
|
|
|
if c.SystemImages == nil {
|
|
|
|
// don't break if the user didn't define rke_images
|
|
|
|
c.SystemImages = make(map[string]string)
|
|
|
|
}
|
|
|
|
systemImagesDefaultsMap := map[string]string{
|
|
|
|
AplineImage: DefaultAplineImage,
|
|
|
|
NginxProxyImage: DefaultNginxProxyImage,
|
|
|
|
CertDownloaderImage: DefaultCertDownloaderImage,
|
|
|
|
KubeDNSImage: DefaultKubeDNSImage,
|
|
|
|
DNSMasqImage: DefaultDNSMasqImage,
|
|
|
|
KubeDNSSidecarImage: DefaultKubeDNSSidecarImage,
|
|
|
|
KubeDNSAutoScalerImage: DefaultKubeDNSAutoScalerImage,
|
|
|
|
}
|
|
|
|
for k, v := range systemImagesDefaultsMap {
|
|
|
|
setDefaultIfEmptyMapValue(c.SystemImages, k, v)
|
2017-11-28 17:45:24 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
2017-11-15 01:12:33 +00:00
|
|
|
|
|
|
|
func GetLocalKubeConfig(configPath string) string {
|
|
|
|
baseDir := filepath.Dir(configPath)
|
|
|
|
fileName := filepath.Base(configPath)
|
|
|
|
baseDir += "/"
|
|
|
|
return fmt.Sprintf("%s%s%s", baseDir, pki.KubeAdminConfigPrefix, fileName)
|
|
|
|
}
|
2017-11-17 00:45:51 +00:00
|
|
|
|
2017-11-30 23:16:45 +00:00
|
|
|
func rebuildLocalAdminConfig(kubeCluster *Cluster) error {
|
|
|
|
logrus.Infof("[reconcile] Rebuilding and update local kube config")
|
|
|
|
var workingConfig string
|
|
|
|
currentKubeConfig := kubeCluster.Certificates[pki.KubeAdminCommonName]
|
|
|
|
caCrt := kubeCluster.Certificates[pki.CACertName].Certificate
|
|
|
|
for _, cpHost := range kubeCluster.ControlPlaneHosts {
|
|
|
|
newConfig := pki.GetKubeConfigX509WithData(
|
|
|
|
"https://"+cpHost.Address+":6443",
|
|
|
|
pki.KubeAdminCommonName,
|
|
|
|
string(cert.EncodeCertPEM(caCrt)),
|
|
|
|
string(cert.EncodeCertPEM(currentKubeConfig.Certificate)),
|
|
|
|
string(cert.EncodePrivateKeyPEM(currentKubeConfig.Key)))
|
|
|
|
|
|
|
|
if err := pki.DeployAdminConfig(newConfig, kubeCluster.LocalKubeConfigPath); err != nil {
|
|
|
|
return fmt.Errorf("Failed to redeploy local admin config with new host")
|
2017-11-21 19:25:08 +00:00
|
|
|
}
|
2017-11-30 23:16:45 +00:00
|
|
|
workingConfig = newConfig
|
|
|
|
if _, err := GetK8sVersion(kubeCluster.LocalKubeConfigPath); err != nil {
|
|
|
|
logrus.Infof("[reconcile] host [%s] is not active master on the cluster", cpHost.Address)
|
2017-11-26 18:23:06 +00:00
|
|
|
continue
|
2017-11-30 23:16:45 +00:00
|
|
|
} else {
|
|
|
|
break
|
2017-11-26 18:23:06 +00:00
|
|
|
}
|
2017-11-17 00:45:51 +00:00
|
|
|
}
|
2017-11-30 23:16:45 +00:00
|
|
|
currentKubeConfig.Config = workingConfig
|
|
|
|
kubeCluster.Certificates[pki.KubeAdminCommonName] = currentKubeConfig
|
2017-11-17 00:45:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:16:45 +00:00
|
|
|
func isLocalConfigWorking(localKubeConfigPath string) bool {
|
|
|
|
if _, err := GetK8sVersion(localKubeConfigPath); err != nil {
|
|
|
|
logrus.Infof("[reconcile] Local config is not vaild, rebuilding admin config")
|
|
|
|
return false
|
2017-11-26 18:23:06 +00:00
|
|
|
}
|
2017-11-30 23:16:45 +00:00
|
|
|
return true
|
2017-11-26 18:23:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 23:16:45 +00:00
|
|
|
func getLocalConfigAddress(localConfigPath string) (string, error) {
|
|
|
|
config, err := clientcmd.BuildConfigFromFlags("", localConfigPath)
|
2017-11-17 00:45:51 +00:00
|
|
|
if err != nil {
|
2017-11-30 23:16:45 +00:00
|
|
|
return "", err
|
2017-11-17 00:45:51 +00:00
|
|
|
}
|
2017-11-30 23:16:45 +00:00
|
|
|
splittedAdress := strings.Split(config.Host, ":")
|
|
|
|
address := splittedAdress[1]
|
|
|
|
return address[2:], nil
|
2017-11-17 00:45:51 +00:00
|
|
|
}
|