2017-10-29 09:45:21 +00:00
|
|
|
package hosts
|
|
|
|
|
2017-10-30 06:31:06 +00:00
|
|
|
import (
|
|
|
|
"github.com/Sirupsen/logrus"
|
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"
|
|
|
|
"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-02 09:27:26 +00:00
|
|
|
IP string `yaml:"ip"`
|
|
|
|
AdvertiseAddress string `yaml:"advertise_address"`
|
|
|
|
Role []string `yaml:"role"`
|
|
|
|
Hostname string `yaml:"hostname"`
|
|
|
|
User string `yaml:"user"`
|
|
|
|
DockerSocket string `yaml:"docker_socket"`
|
|
|
|
DClient *client.Client
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 15:44:17 +00:00
|
|
|
func ReconcileWorkers(currentWorkers []Host, newWorkers []Host, kubeClient *kubernetes.Clientset) error {
|
2017-11-02 10:07:10 +00:00
|
|
|
for _, currentWorker := range currentWorkers {
|
|
|
|
found := false
|
|
|
|
for _, newWorker := range newWorkers {
|
|
|
|
if currentWorker.Hostname == newWorker.Hostname {
|
|
|
|
found = true
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
}
|
|
|
|
if !found {
|
2017-11-07 15:44:17 +00:00
|
|
|
if err := deleteWorkerNode(¤tWorker, kubeClient); err != nil {
|
2017-11-02 10:07:10 +00:00
|
|
|
return err
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-07 15:44:17 +00:00
|
|
|
func deleteWorkerNode(workerNode *Host, kubeClient *kubernetes.Clientset) error {
|
2017-11-02 10:07:10 +00:00
|
|
|
logrus.Infof("[hosts] Deleting host [%s] from the cluster", workerNode.Hostname)
|
2017-11-07 15:44:17 +00:00
|
|
|
err := k8s.DeleteNode(kubeClient, workerNode.Hostname)
|
2017-11-02 10:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("[hosts] Successfully deleted host [%s] from the cluster", workerNode.Hostname)
|
|
|
|
return nil
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|