2017-11-15 01:12:33 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
2017-11-15 01:12:33 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/rancher/rke/docker"
|
|
|
|
"github.com/rancher/rke/hosts"
|
2018-01-31 17:50:55 +00:00
|
|
|
"github.com/rancher/types/apis/management.cattle.io/v3"
|
2017-11-15 01:12:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-11-29 01:35:18 +00:00
|
|
|
NginxProxyImage = "rancher/rke-nginx-proxy:0.1.0"
|
2017-11-15 01:12:33 +00:00
|
|
|
NginxProxyEnvName = "CP_HOSTS"
|
|
|
|
)
|
|
|
|
|
2018-01-31 17:50:55 +00:00
|
|
|
func RollingUpdateNginxProxy(ctx context.Context, cpHosts []*hosts.Host, workerHosts []*hosts.Host, nginxProxyImage string, prsMap map[string]v3.PrivateRegistry) error {
|
2017-11-17 00:45:51 +00:00
|
|
|
nginxProxyEnv := buildProxyEnv(cpHosts)
|
|
|
|
for _, host := range workerHosts {
|
2017-12-05 01:29:29 +00:00
|
|
|
imageCfg, hostCfg := buildNginxProxyConfig(host, nginxProxyEnv, nginxProxyImage)
|
2018-01-31 17:50:55 +00:00
|
|
|
if err := docker.DoRollingUpdateContainer(ctx, host.DClient, imageCfg, hostCfg, NginxProxyContainerName, host.Address, WorkerRole, prsMap); err != nil {
|
2017-11-28 22:18:00 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-11-17 00:45:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:50:55 +00:00
|
|
|
func runNginxProxy(ctx context.Context, host *hosts.Host, cpHosts []*hosts.Host, nginxProxyImage string, prsMap map[string]v3.PrivateRegistry) error {
|
2017-11-15 01:12:33 +00:00
|
|
|
nginxProxyEnv := buildProxyEnv(cpHosts)
|
2017-12-05 01:29:29 +00:00
|
|
|
imageCfg, hostCfg := buildNginxProxyConfig(host, nginxProxyEnv, nginxProxyImage)
|
2018-01-31 17:50:55 +00:00
|
|
|
return docker.DoRunContainer(ctx, host.DClient, imageCfg, hostCfg, NginxProxyContainerName, host.Address, WorkerRole, prsMap)
|
2017-11-15 01:12:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func removeNginxProxy(ctx context.Context, host *hosts.Host) error {
|
|
|
|
return docker.DoRemoveContainer(ctx, host.DClient, NginxProxyContainerName, host.Address)
|
2017-11-20 18:08:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 01:29:29 +00:00
|
|
|
func buildNginxProxyConfig(host *hosts.Host, nginxProxyEnv, nginxProxyImage string) (*container.Config, *container.HostConfig) {
|
2017-11-15 01:12:33 +00:00
|
|
|
imageCfg := &container.Config{
|
2017-12-05 01:29:29 +00:00
|
|
|
Image: nginxProxyImage,
|
2017-11-15 01:12:33 +00:00
|
|
|
Env: []string{fmt.Sprintf("%s=%s", NginxProxyEnvName, nginxProxyEnv)},
|
2018-02-06 23:07:09 +00:00
|
|
|
Cmd: []string{fmt.Sprintf("%s=%s", NginxProxyEnvName, nginxProxyEnv)},
|
2017-11-15 01:12:33 +00:00
|
|
|
}
|
|
|
|
hostCfg := &container.HostConfig{
|
|
|
|
NetworkMode: "host",
|
|
|
|
RestartPolicy: container.RestartPolicy{Name: "always"},
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageCfg, hostCfg
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:16:45 +00:00
|
|
|
func buildProxyEnv(cpHosts []*hosts.Host) string {
|
2017-11-15 01:12:33 +00:00
|
|
|
proxyEnv := ""
|
|
|
|
for i, cpHost := range cpHosts {
|
2017-11-28 17:45:24 +00:00
|
|
|
proxyEnv += fmt.Sprintf("%s", cpHost.InternalAddress)
|
2017-11-15 01:12:33 +00:00
|
|
|
if i < (len(cpHosts) - 1) {
|
|
|
|
proxyEnv += ","
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return proxyEnv
|
|
|
|
}
|