2017-11-02 10:07:10 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
|
|
|
|
2017-11-02 10:07:10 +00:00
|
|
|
"github.com/rancher/rke/hosts"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2017-11-02 10:07:10 +00:00
|
|
|
"github.com/rancher/rke/pki"
|
|
|
|
"github.com/rancher/rke/services"
|
2017-11-13 21:28:38 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-02-01 16:25:19 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-11-21 20:26:26 +00:00
|
|
|
)
|
|
|
|
|
2018-02-01 21:28:31 +00:00
|
|
|
const (
|
|
|
|
etcdRoleLabel = "node-role.kubernetes.io/etcd"
|
|
|
|
masterRoleLabel = "node-role.kubernetes.io/master"
|
|
|
|
workerRoleLabel = "node-role.kubernetes.io/worker"
|
|
|
|
)
|
|
|
|
|
2017-12-22 01:01:53 +00:00
|
|
|
func (c *Cluster) TunnelHosts(ctx context.Context, local bool) error {
|
|
|
|
if local {
|
2018-02-14 20:58:35 +00:00
|
|
|
if err := c.ControlPlaneHosts[0].TunnelUpLocal(ctx); err != nil {
|
2017-12-22 01:01:53 +00:00
|
|
|
return fmt.Errorf("Failed to connect to docker for local host [%s]: %v", c.EtcdHosts[0].Address, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
for i := range c.EtcdHosts {
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := c.EtcdHosts[i].TunnelUp(ctx, c.DockerDialerFactory); err != nil {
|
2017-12-01 21:06:13 +00:00
|
|
|
return fmt.Errorf("Failed to set up SSH tunneling for Etcd host [%s]: %v", c.EtcdHosts[i].Address, err)
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range c.ControlPlaneHosts {
|
2018-01-09 22:10:56 +00:00
|
|
|
err := c.ControlPlaneHosts[i].TunnelUp(ctx, c.DockerDialerFactory)
|
2017-11-02 10:07:10 +00:00
|
|
|
if err != nil {
|
2017-12-01 21:06:13 +00:00
|
|
|
return fmt.Errorf("Failed to set up SSH tunneling for Control host [%s]: %v", c.ControlPlaneHosts[i].Address, err)
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range c.WorkerHosts {
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := c.WorkerHosts[i].TunnelUp(ctx, c.DockerDialerFactory); err != nil {
|
2017-12-01 21:06:13 +00:00
|
|
|
return fmt.Errorf("Failed to set up SSH tunneling for Worker host [%s]: %v", c.WorkerHosts[i].Address, err)
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) InvertIndexHosts() error {
|
2017-11-30 23:16:45 +00:00
|
|
|
c.EtcdHosts = make([]*hosts.Host, 0)
|
|
|
|
c.WorkerHosts = make([]*hosts.Host, 0)
|
|
|
|
c.ControlPlaneHosts = make([]*hosts.Host, 0)
|
2017-11-28 17:45:24 +00:00
|
|
|
for _, host := range c.Nodes {
|
2017-11-30 23:16:45 +00:00
|
|
|
newHost := hosts.Host{
|
|
|
|
RKEConfigNode: host,
|
2018-02-01 21:28:31 +00:00
|
|
|
ToAddLabels: map[string]string{},
|
|
|
|
ToDelLabels: map[string]string{},
|
|
|
|
ToAddTaints: []string{},
|
|
|
|
ToDelTaints: []string{},
|
|
|
|
}
|
|
|
|
for k, v := range host.Labels {
|
|
|
|
newHost.ToAddLabels[k] = v
|
2017-11-30 23:16:45 +00:00
|
|
|
}
|
2018-01-22 19:31:03 +00:00
|
|
|
newHost.IgnoreDockerVersion = c.IgnoreDockerVersion
|
2017-12-15 03:02:44 +00:00
|
|
|
|
2017-11-02 10:07:10 +00:00
|
|
|
for _, role := range host.Role {
|
2017-11-28 17:45:24 +00:00
|
|
|
logrus.Debugf("Host: " + host.Address + " has role: " + role)
|
2017-11-02 10:07:10 +00:00
|
|
|
switch role {
|
|
|
|
case services.ETCDRole:
|
2018-01-11 01:00:14 +00:00
|
|
|
newHost.IsEtcd = true
|
2018-02-01 21:28:31 +00:00
|
|
|
newHost.ToAddLabels[etcdRoleLabel] = "true"
|
2017-11-30 23:16:45 +00:00
|
|
|
c.EtcdHosts = append(c.EtcdHosts, &newHost)
|
2017-11-02 10:07:10 +00:00
|
|
|
case services.ControlRole:
|
2017-11-30 23:16:45 +00:00
|
|
|
newHost.IsControl = true
|
2018-02-01 21:28:31 +00:00
|
|
|
newHost.ToAddLabels[masterRoleLabel] = "true"
|
2017-11-30 23:16:45 +00:00
|
|
|
c.ControlPlaneHosts = append(c.ControlPlaneHosts, &newHost)
|
2017-11-02 10:07:10 +00:00
|
|
|
case services.WorkerRole:
|
2017-11-30 23:16:45 +00:00
|
|
|
newHost.IsWorker = true
|
2018-02-01 21:28:31 +00:00
|
|
|
newHost.ToAddLabels[workerRoleLabel] = "true"
|
2017-11-30 23:16:45 +00:00
|
|
|
c.WorkerHosts = append(c.WorkerHosts, &newHost)
|
2017-11-02 10:07:10 +00:00
|
|
|
default:
|
2017-11-28 17:45:24 +00:00
|
|
|
return fmt.Errorf("Failed to recognize host [%s] role %s", host.Address, role)
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-01 21:28:31 +00:00
|
|
|
if !newHost.IsEtcd {
|
|
|
|
newHost.ToDelLabels[etcdRoleLabel] = "true"
|
|
|
|
}
|
|
|
|
if !newHost.IsControl {
|
|
|
|
newHost.ToDelLabels[masterRoleLabel] = "true"
|
|
|
|
}
|
|
|
|
if !newHost.IsWorker {
|
|
|
|
newHost.ToDelLabels[workerRoleLabel] = "true"
|
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func (c *Cluster) SetUpHosts(ctx context.Context) error {
|
2017-11-14 18:11:21 +00:00
|
|
|
if c.Authentication.Strategy == X509AuthenticationProvider {
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[certificates] Deploying kubernetes certificates to Cluster nodes")
|
2018-02-06 19:25:54 +00:00
|
|
|
hosts := hosts.GetUniqueHostList(c.EtcdHosts, c.ControlPlaneHosts, c.WorkerHosts)
|
2018-02-01 16:25:19 +00:00
|
|
|
var errgrp errgroup.Group
|
|
|
|
|
|
|
|
for _, host := range hosts {
|
|
|
|
runHost := host
|
|
|
|
errgrp.Go(func() error {
|
2018-02-06 19:25:54 +00:00
|
|
|
return pki.DeployCertificatesOnPlaneHost(ctx, runHost, c.RancherKubernetesEngineConfig, c.Certificates, c.SystemImages.CertDownloader, c.PrivateRegistriesMap)
|
2018-02-01 16:25:19 +00:00
|
|
|
})
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
2018-02-01 16:25:19 +00:00
|
|
|
if err := errgrp.Wait(); err != nil {
|
2018-01-19 01:48:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-16 23:10:14 +00:00
|
|
|
if err := pki.DeployAdminConfig(ctx, c.Certificates[pki.KubeAdminCertName].Config, c.LocalKubeConfigPath); err != nil {
|
2017-11-02 10:07:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[certificates] Successfully deployed kubernetes certificates to Cluster nodes")
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-17 21:33:07 +00:00
|
|
|
|
2017-11-26 20:09:26 +00:00
|
|
|
func CheckEtcdHostsChanged(kubeCluster, currentCluster *Cluster) error {
|
2017-11-26 23:27:39 +00:00
|
|
|
if currentCluster != nil {
|
|
|
|
etcdChanged := hosts.IsHostListChanged(currentCluster.EtcdHosts, kubeCluster.EtcdHosts)
|
|
|
|
if etcdChanged {
|
|
|
|
return fmt.Errorf("Adding or removing Etcd nodes is not supported")
|
|
|
|
}
|
2017-11-26 20:09:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|