2017-10-29 09:45:21 +00:00
|
|
|
package hosts
|
|
|
|
|
2017-10-30 06:31:06 +00:00
|
|
|
import (
|
2017-10-31 13:55:35 +00:00
|
|
|
"github.com/docker/docker/client"
|
2017-11-02 10:07:10 +00:00
|
|
|
"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"
|
2017-11-02 10:07:10 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2017-10-30 06:31:06 +00:00
|
|
|
)
|
2017-10-29 09:45:21 +00:00
|
|
|
|
|
|
|
type Host struct {
|
2017-11-09 19:50:49 +00:00
|
|
|
v1.RKEConfigHost
|
|
|
|
DClient *client.Client
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:45:51 +00:00
|
|
|
func DeleteNode(toDeleteHost *Host, kubeClient *kubernetes.Clientset) error {
|
|
|
|
logrus.Infof("[hosts] Cordoning host [%s]", toDeleteHost.AdvertisedHostname)
|
|
|
|
err := k8s.CordonUncordon(kubeClient, toDeleteHost.AdvertisedHostname, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logrus.Infof("[hosts] Deleting host [%s] from the cluster", toDeleteHost.AdvertisedHostname)
|
|
|
|
err = k8s.DeleteNode(kubeClient, toDeleteHost.AdvertisedHostname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("[hosts] Successfully deleted host [%s] from the cluster", toDeleteHost.AdvertisedHostname)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetToDeleteHosts(currentHosts, configHosts []Host) []Host {
|
|
|
|
toDeleteHosts := []Host{}
|
|
|
|
for _, currentHost := range currentHosts {
|
2017-11-02 10:07:10 +00:00
|
|
|
found := false
|
2017-11-17 00:45:51 +00:00
|
|
|
for _, newHost := range configHosts {
|
|
|
|
if currentHost.AdvertisedHostname == newHost.AdvertisedHostname {
|
2017-11-02 10:07:10 +00:00
|
|
|
found = true
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
if !found {
|
2017-11-17 00:45:51 +00:00
|
|
|
toDeleteHosts = append(toDeleteHosts, currentHost)
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-17 00:45:51 +00:00
|
|
|
return toDeleteHosts
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:45:51 +00:00
|
|
|
func IsHostListChanged(currentHosts, configHosts []Host) bool {
|
|
|
|
changed := false
|
|
|
|
for _, host := range currentHosts {
|
|
|
|
found := false
|
|
|
|
for _, configHost := range configHosts {
|
|
|
|
if host.AdvertisedHostname == configHost.AdvertisedHostname {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return true
|
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
2017-11-17 00:45:51 +00:00
|
|
|
for _, host := range configHosts {
|
|
|
|
found := false
|
|
|
|
for _, currentHost := range currentHosts {
|
|
|
|
if host.AdvertisedHostname == currentHost.AdvertisedHostname {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|