2017-10-29 09:45:21 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
|
|
|
|
2017-10-29 09:45:21 +00:00
|
|
|
"github.com/rancher/rke/hosts"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2018-05-01 00:25:52 +00:00
|
|
|
"github.com/rancher/rke/pki"
|
2018-10-17 22:26:54 +00:00
|
|
|
"github.com/rancher/rke/util"
|
2017-12-05 16:55:58 +00:00
|
|
|
"github.com/rancher/types/apis/management.cattle.io/v3"
|
2018-02-01 15:16:02 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-10-29 09:45:21 +00:00
|
|
|
)
|
|
|
|
|
2018-05-01 00:25:52 +00:00
|
|
|
func RunControlPlane(ctx context.Context, controlHosts []*hosts.Host, localConnDialerFactory hosts.DialerFactory, prsMap map[string]v3.PrivateRegistry, cpNodePlanMap map[string]v3.RKEConfigNodePlan, updateWorkersOnly bool, alpineImage string, certMap map[string]pki.CertificatePKI) error {
|
2018-10-17 22:26:54 +00:00
|
|
|
if updateWorkersOnly {
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[%s] Building up Controller Plane..", ControlRole)
|
2018-02-01 15:16:02 +00:00
|
|
|
var errgrp errgroup.Group
|
2018-10-17 22:26:54 +00:00
|
|
|
|
|
|
|
hostsQueue := util.GetObjectQueue(controlHosts)
|
|
|
|
for w := 0; w < WorkerThreads; w++ {
|
2018-02-01 15:16:02 +00:00
|
|
|
errgrp.Go(func() error {
|
2018-10-17 22:26:54 +00:00
|
|
|
var errList []error
|
|
|
|
for host := range hostsQueue {
|
|
|
|
runHost := host.(*hosts.Host)
|
|
|
|
err := doDeployControlHost(ctx, runHost, localConnDialerFactory, prsMap, cpNodePlanMap[runHost.Address].Processes, alpineImage, certMap)
|
|
|
|
if err != nil {
|
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.ErrList(errList)
|
2018-02-01 15:16:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := errgrp.Wait(); err != nil {
|
|
|
|
return err
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[%s] Successfully started Controller Plane..", ControlRole)
|
2017-10-29 09:45:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-15 02:54:26 +00:00
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func RemoveControlPlane(ctx context.Context, controlHosts []*hosts.Host, force bool) error {
|
|
|
|
log.Infof(ctx, "[%s] Tearing down the Controller Plane..", ControlRole)
|
2018-10-23 23:38:00 +00:00
|
|
|
var errgrp errgroup.Group
|
|
|
|
hostsQueue := util.GetObjectQueue(controlHosts)
|
|
|
|
for w := 0; w < WorkerThreads; w++ {
|
|
|
|
errgrp.Go(func() error {
|
|
|
|
var errList []error
|
|
|
|
for host := range hostsQueue {
|
|
|
|
runHost := host.(*hosts.Host)
|
|
|
|
if err := removeKubeAPI(ctx, runHost); err != nil {
|
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
if err := removeKubeController(ctx, runHost); err != nil {
|
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
if err := removeScheduler(ctx, runHost); err != nil {
|
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
// force is true in remove, false in reconcile
|
|
|
|
if force {
|
|
|
|
if err := removeKubelet(ctx, runHost); err != nil {
|
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
if err := removeKubeproxy(ctx, runHost); err != nil {
|
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
if err := removeSidekick(ctx, runHost); err != nil {
|
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 23:05:55 +00:00
|
|
|
}
|
2018-10-23 23:38:00 +00:00
|
|
|
return util.ErrList(errList)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := errgrp.Wait(); err != nil {
|
|
|
|
return err
|
2017-11-20 18:08:50 +00:00
|
|
|
}
|
2018-10-23 23:38:00 +00:00
|
|
|
|
2018-01-23 23:02:22 +00:00
|
|
|
log.Infof(ctx, "[%s] Successfully tore down Controller Plane..", ControlRole)
|
2017-11-20 18:08:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-01 15:16:02 +00:00
|
|
|
|
2018-08-20 04:37:04 +00:00
|
|
|
func RestartControlPlane(ctx context.Context, controlHosts []*hosts.Host) error {
|
|
|
|
log.Infof(ctx, "[%s] Restarting the Controller Plane..", ControlRole)
|
|
|
|
var errgrp errgroup.Group
|
|
|
|
|
|
|
|
hostsQueue := util.GetObjectQueue(controlHosts)
|
|
|
|
for w := 0; w < WorkerThreads; w++ {
|
|
|
|
errgrp.Go(func() error {
|
|
|
|
var errList []error
|
|
|
|
for host := range hostsQueue {
|
|
|
|
runHost := host.(*hosts.Host)
|
|
|
|
// restart KubeAPI
|
2019-01-14 17:51:20 +00:00
|
|
|
if err := RestartKubeAPI(ctx, runHost); err != nil {
|
2018-08-20 04:37:04 +00:00
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// restart KubeController
|
2019-01-14 17:51:20 +00:00
|
|
|
if err := RestartKubeController(ctx, runHost); err != nil {
|
2018-08-20 04:37:04 +00:00
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// restart scheduler
|
2019-01-14 17:51:20 +00:00
|
|
|
err := RestartScheduler(ctx, runHost)
|
2018-08-20 04:37:04 +00:00
|
|
|
if err != nil {
|
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.ErrList(errList)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := errgrp.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof(ctx, "[%s] Successfully restarted Controller Plane..", ControlRole)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:25:52 +00:00
|
|
|
func doDeployControlHost(ctx context.Context, host *hosts.Host, localConnDialerFactory hosts.DialerFactory, prsMap map[string]v3.PrivateRegistry, processMap map[string]v3.Process, alpineImage string, certMap map[string]pki.CertificatePKI) error {
|
2018-02-01 15:16:02 +00:00
|
|
|
if host.IsWorker {
|
|
|
|
if err := removeNginxProxy(ctx, host); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// run sidekick
|
2018-02-13 00:47:56 +00:00
|
|
|
if err := runSidekick(ctx, host, prsMap, processMap[SidekickContainerName]); err != nil {
|
2018-02-01 15:16:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// run kubeapi
|
2018-05-01 00:25:52 +00:00
|
|
|
if err := runKubeAPI(ctx, host, localConnDialerFactory, prsMap, processMap[KubeAPIContainerName], alpineImage, certMap); err != nil {
|
2018-02-01 15:16:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// run kubecontroller
|
2018-03-21 17:20:58 +00:00
|
|
|
if err := runKubeController(ctx, host, localConnDialerFactory, prsMap, processMap[KubeControllerContainerName], alpineImage); err != nil {
|
2018-02-01 15:16:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// run scheduler
|
2018-03-21 17:20:58 +00:00
|
|
|
return runScheduler(ctx, host, localConnDialerFactory, prsMap, processMap[SchedulerContainerName], alpineImage)
|
2018-02-01 15:16:02 +00:00
|
|
|
}
|