1
0
mirror of https://github.com/rancher/rke.git synced 2025-04-28 11:36:27 +00:00
rke/hosts/hosts.go

132 lines
3.3 KiB
Go
Raw Normal View History

2017-10-29 09:45:21 +00:00
package hosts
import (
"fmt"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/rancher/rke/docker"
"github.com/rancher/rke/k8s"
2017-11-14 18:11:21 +00:00
"github.com/rancher/types/apis/cluster.cattle.io/v1"
2017-11-13 21:28:38 +00:00
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
)
2017-10-29 09:45:21 +00:00
type Host struct {
2017-11-28 17:45:24 +00:00
v1.RKEConfigNode
2017-11-09 19:50:49 +00:00
DClient *client.Client
2017-10-29 09:45:21 +00:00
}
const (
ToCleanEtcdDir = "/var/lib/etcd"
ToCleanSSLDir = "/etc/kubernetes/ssl"
ToCleanCNIConf = "/etc/cni"
ToCleanCNIBin = "/opt/cni"
ToCleanCalicoRun = "/var/run/calico"
CleanerContainerName = "kube-cleaner"
CleanerImage = "alpine:latest"
)
func (h *Host) CleanUp() error {
2017-11-28 17:45:24 +00:00
logrus.Infof("[hosts] Cleaning up host [%s]", h.Address)
toCleanDirs := []string{
ToCleanEtcdDir,
ToCleanSSLDir,
ToCleanCNIConf,
ToCleanCNIBin,
ToCleanCalicoRun,
}
2017-11-28 17:45:24 +00:00
logrus.Infof("[hosts] Running cleaner container on host [%s]", h.Address)
imageCfg, hostCfg := buildCleanerConfig(h, toCleanDirs)
2017-11-28 17:45:24 +00:00
if err := docker.DoRunContainer(h.DClient, imageCfg, hostCfg, CleanerContainerName, h.Address, CleanerContainerName); err != nil {
return err
}
if err := docker.WaitForContainer(h.DClient, CleanerContainerName); err != nil {
return err
}
2017-11-28 17:45:24 +00:00
logrus.Infof("[hosts] Removing cleaner container on host [%s]", h.Address)
if err := docker.RemoveContainer(h.DClient, h.Address, CleanerContainerName); err != nil {
return err
}
2017-11-28 17:45:24 +00:00
logrus.Infof("[hosts] Successfully cleaned up host [%s]", h.Address)
return nil
}
func DeleteNode(toDeleteHost *Host, kubeClient *kubernetes.Clientset) error {
2017-11-28 17:45:24 +00:00
logrus.Infof("[hosts] Cordoning host [%s]", toDeleteHost.Address)
err := k8s.CordonUncordon(kubeClient, toDeleteHost.HostnameOverride, true)
if err != nil {
return err
}
2017-11-28 17:45:24 +00:00
logrus.Infof("[hosts] Deleting host [%s] from the cluster", toDeleteHost.Address)
err = k8s.DeleteNode(kubeClient, toDeleteHost.HostnameOverride)
if err != nil {
return err
}
2017-11-28 17:45:24 +00:00
logrus.Infof("[hosts] Successfully deleted host [%s] from the cluster", toDeleteHost.Address)
return nil
}
func GetToDeleteHosts(currentHosts, configHosts []Host) []Host {
toDeleteHosts := []Host{}
for _, currentHost := range currentHosts {
found := false
for _, newHost := range configHosts {
2017-11-28 17:45:24 +00:00
if currentHost.Address == newHost.Address {
found = true
2017-10-29 09:45:21 +00:00
}
}
if !found {
toDeleteHosts = append(toDeleteHosts, currentHost)
2017-10-29 09:45:21 +00:00
}
}
return toDeleteHosts
}
func IsHostListChanged(currentHosts, configHosts []Host) bool {
changed := false
for _, host := range currentHosts {
found := false
for _, configHost := range configHosts {
2017-11-28 17:45:24 +00:00
if host.Address == configHost.Address {
found = true
2017-11-28 17:45:24 +00:00
break
}
}
if !found {
return true
}
}
for _, host := range configHosts {
found := false
for _, currentHost := range currentHosts {
2017-11-28 17:45:24 +00:00
if host.Address == currentHost.Address {
found = true
2017-11-28 17:45:24 +00:00
break
}
}
if !found {
return true
}
}
return changed
2017-10-29 09:45:21 +00:00
}
func buildCleanerConfig(host *Host, toCleanDirs []string) (*container.Config, *container.HostConfig) {
cmd := append([]string{"rm", "-rf"}, toCleanDirs...)
imageCfg := &container.Config{
Image: CleanerImage,
Cmd: cmd,
}
bindMounts := []string{}
for _, vol := range toCleanDirs {
bindMounts = append(bindMounts, fmt.Sprintf("%s:%s", vol, vol))
}
hostCfg := &container.HostConfig{
Binds: bindMounts,
}
return imageCfg, hostCfg
}