mirror of
https://github.com/rancher/rke.git
synced 2025-05-10 01:15:43 +00:00
Start containers if stopped and use net=host for etcd
This commit is contained in:
parent
3bfa9bd49b
commit
d7ffdc04b5
@ -22,11 +22,26 @@ var K8sDockerVersions = map[string][]string{
|
||||
}
|
||||
|
||||
func DoRunContainer(ctx context.Context, dClient *client.Client, imageCfg *container.Config, hostCfg *container.HostConfig, containerName string, hostname string, plane string) error {
|
||||
isRunning, err := IsContainerRunning(ctx, dClient, hostname, containerName, false)
|
||||
container, err := dClient.ContainerInspect(ctx, containerName)
|
||||
if err != nil {
|
||||
return err
|
||||
if !client.IsErrNotFound(err) {
|
||||
return err
|
||||
}
|
||||
if err := UseLocalOrPull(ctx, dClient, hostname, imageCfg.Image, plane); err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := dClient.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
if err := dClient.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
|
||||
return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
log.Infof(ctx, "[%s] Successfully started [%s] container on host [%s]", plane, containerName, hostname)
|
||||
return nil
|
||||
}
|
||||
if isRunning {
|
||||
// Check for upgrades
|
||||
if container.State.Running {
|
||||
log.Infof(ctx, "[%s] Container [%s] is already running on host [%s]", plane, containerName, hostname)
|
||||
isUpgradable, err := IsContainerUpgradable(ctx, dClient, imageCfg, containerName, hostname, plane)
|
||||
if err != nil {
|
||||
@ -38,18 +53,11 @@ func DoRunContainer(ctx context.Context, dClient *client.Client, imageCfg *conta
|
||||
return nil
|
||||
}
|
||||
|
||||
err = UseLocalOrPull(ctx, dClient, hostname, imageCfg.Image, plane)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := dClient.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
if err := dClient.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
|
||||
// start if not running
|
||||
log.Infof(ctx, "[%s] Starting stopped container [%s] on host [%s]", plane, containerName, hostname)
|
||||
if err := dClient.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}); err != nil {
|
||||
return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
logrus.Debugf("[%s] Successfully started [%s] container: [%s]", plane, containerName, resp.ID)
|
||||
log.Infof(ctx, "[%s] Successfully started [%s] container on host [%s]", plane, containerName, hostname)
|
||||
return nil
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ import (
|
||||
|
||||
etcdclient "github.com/coreos/etcd/client"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/rancher/rke/docker"
|
||||
"github.com/rancher/rke/hosts"
|
||||
"github.com/rancher/rke/log"
|
||||
@ -64,20 +63,7 @@ func buildEtcdConfig(host *hosts.Host, etcdService v3.ETCDService, initCluster s
|
||||
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",
|
||||
},
|
||||
},
|
||||
},
|
||||
NetworkMode: "host",
|
||||
}
|
||||
for arg, value := range etcdService.ExtraArgs {
|
||||
cmd := fmt.Sprintf("--%s=%s", arg, value)
|
||||
|
Loading…
Reference in New Issue
Block a user