2017-10-31 13:55:35 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
|
|
|
|
2017-10-31 13:55:35 +00:00
|
|
|
"github.com/rancher/rke/hosts"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2017-12-05 16:55:58 +00:00
|
|
|
"github.com/rancher/types/apis/management.cattle.io/v3"
|
2017-12-29 03:33:20 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-10-31 13:55:35 +00:00
|
|
|
)
|
|
|
|
|
2018-01-19 01:48:51 +00:00
|
|
|
func RunWorkerPlane(ctx context.Context, controlHosts, workerHosts, etcdHosts []*hosts.Host, workerServices v3.RKEConfigServices, nginxProxyImage, sidekickImage string, localConnDialerFactory hosts.DialerFactory) error {
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[%s] Building up Worker Plane..", WorkerRole)
|
2017-12-29 03:33:20 +00:00
|
|
|
var errgrp errgroup.Group
|
|
|
|
|
2018-01-19 01:48:51 +00:00
|
|
|
// Deploy worker components on etcd hosts
|
|
|
|
for _, host := range etcdHosts {
|
|
|
|
etcdHost := host
|
|
|
|
errgrp.Go(func() error {
|
|
|
|
return doDeployWorkerPlane(ctx, etcdHost, workerServices, nginxProxyImage, sidekickImage, localConnDialerFactory, controlHosts, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := errgrp.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-29 03:33:20 +00:00
|
|
|
// Deploy worker components on control hosts
|
2017-10-31 13:55:35 +00:00
|
|
|
for _, host := range controlHosts {
|
2017-12-29 03:33:20 +00:00
|
|
|
controlHost := host
|
|
|
|
errgrp.Go(func() error {
|
2018-01-19 01:48:51 +00:00
|
|
|
return doDeployWorkerPlane(ctx, controlHost, workerServices, nginxProxyImage, sidekickImage, localConnDialerFactory, controlHosts, false)
|
2017-12-29 03:33:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := errgrp.Wait(); err != nil {
|
|
|
|
return err
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
2017-12-29 03:33:20 +00:00
|
|
|
// Deploy worker components on worker hosts
|
2017-10-31 13:55:35 +00:00
|
|
|
for _, host := range workerHosts {
|
2017-12-29 03:33:20 +00:00
|
|
|
workerHost := host
|
|
|
|
errgrp.Go(func() error {
|
2018-01-19 01:48:51 +00:00
|
|
|
return doDeployWorkerPlane(ctx, workerHost, workerServices, nginxProxyImage, sidekickImage, localConnDialerFactory, controlHosts, false)
|
2017-12-29 03:33:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := errgrp.Wait(); err != nil {
|
|
|
|
return err
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[%s] Successfully started Worker Plane..", WorkerRole)
|
2017-10-31 13:55:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-15 02:54:26 +00:00
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func RemoveWorkerPlane(ctx context.Context, workerHosts []*hosts.Host, force bool) error {
|
|
|
|
log.Infof(ctx, "[%s] Tearing down Worker Plane..", WorkerRole)
|
2017-11-30 23:16:45 +00:00
|
|
|
for _, host := range workerHosts {
|
|
|
|
// check if the host already is a controlplane
|
|
|
|
if host.IsControl && !force {
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[%s] Host [%s] is already a controlplane host, nothing to do.", WorkerRole, host.Address)
|
2017-11-30 23:16:45 +00:00
|
|
|
return nil
|
2017-11-20 18:08:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := removeKubelet(ctx, host); err != nil {
|
2017-11-20 18:08:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := removeKubeproxy(ctx, host); err != nil {
|
2017-11-20 18:08:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := removeNginxProxy(ctx, host); err != nil {
|
2017-11-20 18:08:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := removeSidekick(ctx, host); err != nil {
|
2017-12-08 23:05:55 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[%s] Successfully teared down Worker Plane..", WorkerRole)
|
2017-11-20 18:08:50 +00:00
|
|
|
}
|
2017-11-30 23:16:45 +00:00
|
|
|
|
2017-11-20 18:08:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-29 03:33:20 +00:00
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func doDeployWorkerPlane(ctx context.Context, host *hosts.Host,
|
2017-12-29 03:33:20 +00:00
|
|
|
workerServices v3.RKEConfigServices,
|
|
|
|
nginxProxyImage, sidekickImage string,
|
2018-01-11 01:00:14 +00:00
|
|
|
localConnDialerFactory hosts.DialerFactory,
|
2018-01-19 01:48:51 +00:00
|
|
|
controlHosts []*hosts.Host,
|
|
|
|
unschedulable bool) error {
|
|
|
|
|
|
|
|
// skipping deploying unschedulable kubelet on etcd node
|
|
|
|
if unschedulable && host.IsWorker {
|
|
|
|
log.Infof(ctx, "[%s] Host [%s] is already worker host, skipping deploying unschedulable kubelet", WorkerRole, host.Address)
|
|
|
|
return nil
|
|
|
|
} else if unschedulable && host.IsControl {
|
|
|
|
log.Infof(ctx, "[%s] Host [%s] is already control host, skipping deploying unschedulable kubelet", WorkerRole, host.Address)
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-29 03:33:20 +00:00
|
|
|
// run nginx proxy
|
|
|
|
if !host.IsControl {
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := runNginxProxy(ctx, host, controlHosts, nginxProxyImage); err != nil {
|
2017-12-29 03:33:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// run sidekick
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := runSidekick(ctx, host, sidekickImage); err != nil {
|
2017-12-29 03:33:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// run kubelet
|
2018-01-19 01:48:51 +00:00
|
|
|
if err := runKubelet(ctx, host, workerServices.Kubelet, localConnDialerFactory, unschedulable); err != nil {
|
2017-12-29 03:33:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-01-11 01:00:14 +00:00
|
|
|
return runKubeproxy(ctx, host, workerServices.Kubeproxy, localConnDialerFactory)
|
2017-12-29 03:33:20 +00:00
|
|
|
}
|