1
0
mirror of https://github.com/rancher/rke.git synced 2025-07-19 17:50:16 +00:00
rke/services/etcd.go

102 lines
2.9 KiB
Go
Raw Normal View History

2017-10-29 09:45:21 +00:00
package services
import (
2017-11-14 18:11:21 +00:00
"fmt"
2017-10-29 09:45:21 +00:00
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/rancher/rke/docker"
2017-10-29 09:45:21 +00:00
"github.com/rancher/rke/hosts"
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-10-29 09:45:21 +00:00
)
2017-11-09 19:50:49 +00:00
func RunEtcdPlane(etcdHosts []hosts.Host, etcdService v1.ETCDService) error {
logrus.Infof("[%s] Building up Etcd Plane..", ETCDRole)
initCluster := getEtcdInitialCluster(etcdHosts)
2017-10-29 09:45:21 +00:00
for _, host := range etcdHosts {
imageCfg, hostCfg := buildEtcdConfig(host, etcdService, initCluster)
2017-11-28 17:45:24 +00:00
err := docker.DoRunContainer(host.DClient, imageCfg, hostCfg, EtcdContainerName, host.Address, ETCDRole)
2017-10-29 09:45:21 +00:00
if err != nil {
return err
}
}
logrus.Infof("[%s] Successfully started Etcd Plane..", ETCDRole)
2017-10-29 09:45:21 +00:00
return nil
}
func RemoveEtcdPlane(etcdHosts []hosts.Host) error {
logrus.Infof("[%s] Tearing down Etcd Plane..", ETCDRole)
for _, host := range etcdHosts {
2017-11-28 17:45:24 +00:00
err := docker.DoRemoveContainer(host.DClient, EtcdContainerName, host.Address)
if err != nil {
return err
}
}
logrus.Infof("[%s] Successfully teared down Etcd Plane..", ETCDRole)
return nil
}
func buildEtcdConfig(host hosts.Host, etcdService v1.ETCDService, initCluster string) (*container.Config, *container.HostConfig) {
2017-10-29 09:45:21 +00:00
imageCfg := &container.Config{
Image: etcdService.Image,
2017-10-29 09:45:21 +00:00
Cmd: []string{"/usr/local/bin/etcd",
2017-11-28 17:45:24 +00:00
"--name=etcd-" + host.HostnameOverride,
2017-10-29 09:45:21 +00:00
"--data-dir=/etcd-data",
2017-11-28 17:45:24 +00:00
"--advertise-client-urls=http://" + host.InternalAddress + ":2379,http://" + host.InternalAddress + ":4001",
2017-10-29 09:45:21 +00:00
"--listen-client-urls=http://0.0.0.0:2379",
2017-11-28 17:45:24 +00:00
"--initial-advertise-peer-urls=http://" + host.InternalAddress + ":2380",
2017-10-29 09:45:21 +00:00
"--listen-peer-urls=http://0.0.0.0:2380",
"--initial-cluster-token=etcd-cluster-1",
"--initial-cluster=" + initCluster,
"--initial-cluster-state=new"},
2017-10-29 09:45:21 +00:00
}
hostCfg := &container.HostConfig{
RestartPolicy: container.RestartPolicy{Name: "always"},
Binds: []string{
"/var/lib/etcd:/etcd-data"},
PortBindings: nat.PortMap{
"2379/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "2379",
},
},
"2380/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "2380",
},
},
},
}
2017-11-14 18:11:21 +00:00
for arg, value := range etcdService.ExtraArgs {
cmd := fmt.Sprintf("--%s=%s", arg, value)
imageCfg.Cmd = append(imageCfg.Cmd, cmd)
}
return imageCfg, hostCfg
2017-10-29 09:45:21 +00:00
}
func GetEtcdConnString(hosts []hosts.Host) string {
2017-10-29 09:45:21 +00:00
connString := ""
for i, host := range hosts {
2017-11-28 17:45:24 +00:00
connString += "http://" + host.InternalAddress + ":2379"
2017-10-29 09:45:21 +00:00
if i < (len(hosts) - 1) {
connString += ","
}
}
return connString
}
func getEtcdInitialCluster(hosts []hosts.Host) string {
initialCluster := ""
for i, host := range hosts {
2017-11-28 17:45:24 +00:00
initialCluster += fmt.Sprintf("etcd-%s=http://%s:2380", host.HostnameOverride, host.InternalAddress)
if i < (len(hosts) - 1) {
initialCluster += ","
}
}
return initialCluster
}