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"
|
2017-10-31 13:55:35 +00:00
|
|
|
"github.com/rancher/rke/docker"
|
2017-10-29 09:45:21 +00:00
|
|
|
"github.com/rancher/rke/hosts"
|
2017-10-31 13:55:35 +00:00
|
|
|
"github.com/rancher/rke/pki"
|
2017-11-14 18:11:21 +00:00
|
|
|
"github.com/rancher/types/apis/cluster.cattle.io/v1"
|
2017-11-15 02:54:26 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-10-29 09:45:21 +00:00
|
|
|
)
|
|
|
|
|
2017-11-09 19:50:49 +00:00
|
|
|
func runKubeAPI(host hosts.Host, etcdHosts []hosts.Host, kubeAPIService v1.KubeAPIService) error {
|
2017-10-29 09:45:21 +00:00
|
|
|
etcdConnString := getEtcdConnString(etcdHosts)
|
2017-10-31 13:55:35 +00:00
|
|
|
imageCfg, hostCfg := buildKubeAPIConfig(host, kubeAPIService, etcdConnString)
|
2017-11-14 18:11:21 +00:00
|
|
|
return docker.DoRunContainer(host.DClient, imageCfg, hostCfg, KubeAPIContainerName, host.AdvertisedHostname, ControlRole)
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 02:54:26 +00:00
|
|
|
func upgradeKubeAPI(host hosts.Host, etcdHosts []hosts.Host, kubeAPIService v1.KubeAPIService) error {
|
|
|
|
logrus.Debugf("[upgrade/KubeAPI] Checking for deployed version")
|
|
|
|
containerInspect, err := docker.InspectContainer(host.DClient, host.AdvertisedHostname, KubeAPIContainerName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if containerInspect.Config.Image == kubeAPIService.Image {
|
|
|
|
logrus.Infof("[upgrade/KubeAPI] KubeAPI is already up to date")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logrus.Debugf("[upgrade/KubeAPI] Stopping old container")
|
|
|
|
oldContainerName := "old-" + KubeAPIContainerName
|
|
|
|
if err := docker.StopRenameContainer(host.DClient, host.AdvertisedHostname, KubeAPIContainerName, oldContainerName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Container doesn't exist now!, lets deploy it!
|
|
|
|
logrus.Debugf("[upgrade/KubeAPI] Deploying new container")
|
|
|
|
if err := runKubeAPI(host, etcdHosts, kubeAPIService); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("[upgrade/KubeAPI] Removing old container")
|
|
|
|
err = docker.RemoveContainer(host.DClient, host.AdvertisedHostname, oldContainerName)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-09 19:50:49 +00:00
|
|
|
func buildKubeAPIConfig(host hosts.Host, kubeAPIService v1.KubeAPIService, etcdConnString string) (*container.Config, *container.HostConfig) {
|
2017-10-29 09:45:21 +00:00
|
|
|
imageCfg := &container.Config{
|
2017-11-02 10:07:10 +00:00
|
|
|
Image: kubeAPIService.Image,
|
2017-10-29 09:45:21 +00:00
|
|
|
Cmd: []string{"/hyperkube",
|
|
|
|
"apiserver",
|
2017-11-15 01:12:33 +00:00
|
|
|
"--insecure-bind-address=127.0.0.1",
|
2017-10-29 09:45:21 +00:00
|
|
|
"--insecure-port=8080",
|
2017-11-15 01:12:33 +00:00
|
|
|
"--secure-port=6443",
|
2017-10-29 09:45:21 +00:00
|
|
|
"--cloud-provider=",
|
|
|
|
"--allow_privileged=true",
|
2017-10-30 06:31:06 +00:00
|
|
|
"--service-cluster-ip-range=" + kubeAPIService.ServiceClusterIPRange,
|
2017-10-31 13:55:35 +00:00
|
|
|
"--admission-control=ServiceAccount,NamespaceLifecycle,LimitRanger,PersistentVolumeLabel,DefaultStorageClass,ResourceQuota,DefaultTolerationSeconds",
|
2017-10-29 09:45:21 +00:00
|
|
|
"--runtime-config=batch/v2alpha1",
|
|
|
|
"--runtime-config=authentication.k8s.io/v1beta1=true",
|
|
|
|
"--storage-backend=etcd3",
|
|
|
|
"--etcd-servers=" + etcdConnString,
|
2017-11-02 09:27:26 +00:00
|
|
|
"--advertise-address=" + host.AdvertiseAddress,
|
2017-10-31 13:55:35 +00:00
|
|
|
"--client-ca-file=" + pki.CACertPath,
|
|
|
|
"--tls-cert-file=" + pki.KubeAPICertPath,
|
|
|
|
"--tls-private-key-file=" + pki.KubeAPIKeyPath,
|
|
|
|
"--service-account-key-file=" + pki.KubeAPIKeyPath},
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
hostCfg := &container.HostConfig{
|
2017-10-31 13:55:35 +00:00
|
|
|
Binds: []string{
|
|
|
|
"/etc/kubernetes:/etc/kubernetes",
|
|
|
|
},
|
2017-10-29 09:45:21 +00:00
|
|
|
NetworkMode: "host",
|
|
|
|
RestartPolicy: container.RestartPolicy{Name: "always"},
|
|
|
|
PortBindings: nat.PortMap{
|
|
|
|
"8080/tcp": []nat.PortBinding{
|
|
|
|
{
|
|
|
|
HostIP: "0.0.0.0",
|
|
|
|
HostPort: "8080",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-11-14 18:11:21 +00:00
|
|
|
|
|
|
|
for arg, value := range kubeAPIService.ExtraArgs {
|
|
|
|
cmd := fmt.Sprintf("--%s=%s", arg, value)
|
|
|
|
imageCfg.Cmd = append(imageCfg.Cmd, cmd)
|
|
|
|
}
|
2017-10-31 13:55:35 +00:00
|
|
|
return imageCfg, hostCfg
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|