1
0
mirror of https://github.com/rancher/rke.git synced 2025-08-31 06:34:03 +00:00
Files
rke/services/services.go
2018-02-13 03:33:29 +02:00

91 lines
2.8 KiB
Go

package services
import (
"context"
"fmt"
"github.com/docker/docker/api/types/container"
"github.com/rancher/rke/docker"
"github.com/rancher/rke/hosts"
"github.com/rancher/rke/log"
"github.com/rancher/types/apis/management.cattle.io/v3"
)
const (
ETCDRole = "etcd"
ControlRole = "controlplane"
WorkerRole = "worker"
SidekickServiceName = "sidekick"
RBACAuthorizationMode = "rbac"
KubeAPIContainerName = "kube-api"
KubeletContainerName = "kubelet"
KubeproxyContainerName = "kube-proxy"
KubeControllerContainerName = "kube-controller"
SchedulerContainerName = "scheduler"
EtcdContainerName = "etcd"
NginxProxyContainerName = "nginx-proxy"
SidekickContainerName = "service-sidekick"
KubeAPIPort = 6443
SchedulerPort = 10251
KubeControllerPort = 10252
KubeletPort = 10250
KubeproxyPort = 10256
)
func runSidekick(ctx context.Context, host *hosts.Host, prsMap map[string]v3.PrivateRegistry, sidecarProcess v3.Process) error {
isRunning, err := docker.IsContainerRunning(ctx, host.DClient, host.Address, SidekickContainerName, true)
if err != nil {
return err
}
if isRunning {
log.Infof(ctx, "[%s] Sidekick container already created on host [%s]", SidekickServiceName, host.Address)
return nil
}
imageCfg, hostCfg, _ := getProcessConfig(sidecarProcess)
sidecarImage := sidecarProcess.Image
if err := docker.UseLocalOrPull(ctx, host.DClient, host.Address, sidecarImage, SidekickServiceName, prsMap); err != nil {
return err
}
if _, err := docker.CreateContiner(ctx, host.DClient, host.Address, SidekickContainerName, imageCfg, hostCfg); err != nil {
return err
}
return nil
}
func removeSidekick(ctx context.Context, host *hosts.Host) error {
return docker.DoRemoveContainer(ctx, host.DClient, SidekickContainerName, host.Address)
}
func getProcessConfig(process v3.Process) (*container.Config, *container.HostConfig, string) {
imageCfg := &container.Config{
Entrypoint: process.Command,
Cmd: process.Args,
Env: process.Env,
Image: process.Image,
}
// var pidMode container.PidMode
// pidMode = process.PidMode
hostCfg := &container.HostConfig{
VolumesFrom: process.VolumesFrom,
Binds: process.Binds,
NetworkMode: container.NetworkMode(process.NetworkMode),
PidMode: container.PidMode(process.PidMode),
Privileged: process.Privileged,
}
if len(process.RestartPolicy) > 0 {
hostCfg.RestartPolicy = container.RestartPolicy{Name: process.RestartPolicy}
}
return imageCfg, hostCfg, process.HealthCheck.URL
}
func GetHealthCheckURL(useTLS bool, port int) string {
if useTLS {
return fmt.Sprintf("%s%s:%d%s", HTTPSProtoPrefix, HealthzAddress, port, HealthzEndpoint)
}
return fmt.Sprintf("%s%s:%d%s", HTTPProtoPrefix, HealthzAddress, port, HealthzEndpoint)
}