2017-10-29 09:45:21 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
2017-11-14 18:11:21 +00:00
|
|
|
"fmt"
|
|
|
|
|
2017-10-29 09:45:21 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
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-12-05 16:55:58 +00:00
|
|
|
"github.com/rancher/types/apis/management.cattle.io/v3"
|
2017-10-29 09:45:21 +00:00
|
|
|
)
|
|
|
|
|
2018-01-31 17:50:55 +00:00
|
|
|
func runKubeAPI(ctx context.Context, host *hosts.Host, etcdHosts []*hosts.Host, kubeAPIService v3.KubeAPIService, authorizationMode string, df hosts.DialerFactory, prsMap map[string]v3.PrivateRegistry) error {
|
2017-11-21 23:49:30 +00:00
|
|
|
etcdConnString := GetEtcdConnString(etcdHosts)
|
2017-12-14 21:56:19 +00:00
|
|
|
imageCfg, hostCfg := buildKubeAPIConfig(host, kubeAPIService, etcdConnString, authorizationMode)
|
2018-01-31 17:50:55 +00:00
|
|
|
if err := docker.DoRunContainer(ctx, host.DClient, imageCfg, hostCfg, KubeAPIContainerName, host.Address, ControlRole, prsMap); err != nil {
|
2017-12-19 22:18:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-12-22 01:01:53 +00:00
|
|
|
return runHealthcheck(ctx, host, KubeAPIPort, true, KubeAPIContainerName, df)
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func removeKubeAPI(ctx context.Context, host *hosts.Host) error {
|
|
|
|
return docker.DoRemoveContainer(ctx, host.DClient, KubeAPIContainerName, host.Address)
|
2017-11-20 18:08:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 21:56:19 +00:00
|
|
|
func buildKubeAPIConfig(host *hosts.Host, kubeAPIService v3.KubeAPIService, etcdConnString, authorizationMode 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-12-08 23:05:55 +00:00
|
|
|
Entrypoint: []string{"/opt/rke/entrypoint.sh",
|
|
|
|
"kube-apiserver",
|
2017-11-15 01:12:33 +00:00
|
|
|
"--insecure-bind-address=127.0.0.1",
|
2017-11-28 17:45:24 +00:00
|
|
|
"--bind-address=0.0.0.0",
|
2017-12-22 01:01:53 +00:00
|
|
|
"--insecure-port=0",
|
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-11-28 17:45:24 +00:00
|
|
|
"--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname",
|
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",
|
2018-01-16 23:10:14 +00:00
|
|
|
"--client-ca-file=" + pki.GetCertPath(pki.CACertName),
|
|
|
|
"--tls-cert-file=" + pki.GetCertPath(pki.KubeAPICertName),
|
|
|
|
"--tls-private-key-file=" + pki.GetKeyPath(pki.KubeAPICertName),
|
|
|
|
"--service-account-key-file=" + pki.GetKeyPath(pki.KubeAPICertName),
|
|
|
|
"--etcd-cafile=" + pki.GetCertPath(pki.CACertName),
|
|
|
|
"--etcd-certfile=" + pki.GetCertPath(pki.KubeAPICertName),
|
|
|
|
"--etcd-keyfile=" + pki.GetKeyPath(pki.KubeAPICertName)},
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
2018-01-24 03:19:44 +00:00
|
|
|
imageCfg.Cmd = append(imageCfg.Cmd, "--etcd-servers="+etcdConnString)
|
|
|
|
|
2017-12-14 21:56:19 +00:00
|
|
|
if authorizationMode == RBACAuthorizationMode {
|
|
|
|
imageCfg.Cmd = append(imageCfg.Cmd, "--authorization-mode=RBAC")
|
|
|
|
}
|
2017-12-20 01:51:07 +00:00
|
|
|
if kubeAPIService.PodSecurityPolicy {
|
|
|
|
imageCfg.Cmd = append(imageCfg.Cmd, "--runtime-config=extensions/v1beta1/podsecuritypolicy=true", "--admission-control=PodSecurityPolicy")
|
|
|
|
}
|
2017-10-29 09:45:21 +00:00
|
|
|
hostCfg := &container.HostConfig{
|
2017-12-08 23:05:55 +00:00
|
|
|
VolumesFrom: []string{
|
|
|
|
SidekickContainerName,
|
|
|
|
},
|
2017-10-31 13:55:35 +00:00
|
|
|
Binds: []string{
|
2018-01-25 21:29:21 +00:00
|
|
|
"/etc/kubernetes:/etc/kubernetes:z",
|
2017-10-31 13:55:35 +00:00
|
|
|
},
|
2017-10-29 09:45:21 +00:00
|
|
|
NetworkMode: "host",
|
|
|
|
RestartPolicy: container.RestartPolicy{Name: "always"},
|
|
|
|
}
|
2017-11-14 18:11:21 +00:00
|
|
|
|
|
|
|
for arg, value := range kubeAPIService.ExtraArgs {
|
|
|
|
cmd := fmt.Sprintf("--%s=%s", arg, value)
|
2017-11-30 23:16:45 +00:00
|
|
|
imageCfg.Entrypoint = append(imageCfg.Entrypoint, cmd)
|
2017-11-14 18:11:21 +00:00
|
|
|
}
|
2017-10-31 13:55:35 +00:00
|
|
|
return imageCfg, hostCfg
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|