瀏覽代碼

restartmanager: do not apply restart policy on created containers

Signed-off-by: Antonio Murdaca <runcom@redhat.com>
Antonio Murdaca 7 年之前
父節點
當前提交
75d3214934
共有 2 個文件被更改,包括 14 次插入4 次删除
  1. 2 2
      daemon/daemon.go
  2. 12 2
      integration/container/restart_test.go

+ 2 - 2
daemon/daemon.go

@@ -320,7 +320,7 @@ func (daemon *Daemon) restore() error {
 			// not initialized yet. We will start
 			// it after the cluster is
 			// initialized.
-			if daemon.configStore.AutoRestart && c.ShouldRestart() && !c.NetworkSettings.HasSwarmEndpoint {
+			if daemon.configStore.AutoRestart && c.ShouldRestart() && !c.NetworkSettings.HasSwarmEndpoint && c.HasBeenStartedBefore {
 				mapLock.Lock()
 				restartContainers[c] = make(chan struct{})
 				mapLock.Unlock()
@@ -450,7 +450,7 @@ func (daemon *Daemon) RestartSwarmContainers() {
 			// Autostart all the containers which has a
 			// swarm endpoint now that the cluster is
 			// initialized.
-			if daemon.configStore.AutoRestart && c.ShouldRestart() && c.NetworkSettings.HasSwarmEndpoint {
+			if daemon.configStore.AutoRestart && c.ShouldRestart() && c.NetworkSettings.HasSwarmEndpoint && c.HasBeenStartedBefore {
 				group.Add(1)
 				go func(c *container.Container) {
 					defer group.Done()

+ 12 - 2
integration/container/restart_test.go

@@ -22,6 +22,7 @@ func TestDaemonRestartKillContainers(t *testing.T) {
 
 		xRunning            bool
 		xRunningLiveRestore bool
+		xStart              bool
 	}
 
 	for _, c := range []testCase{
@@ -29,6 +30,7 @@ func TestDaemonRestartKillContainers(t *testing.T) {
 			desc:                "container without restart policy",
 			config:              &container.Config{Image: "busybox", Cmd: []string{"top"}},
 			xRunningLiveRestore: true,
+			xStart:              true,
 		},
 		{
 			desc:                "container with restart=always",
@@ -36,6 +38,12 @@ func TestDaemonRestartKillContainers(t *testing.T) {
 			hostConfig:          &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
 			xRunning:            true,
 			xRunningLiveRestore: true,
+			xStart:              true,
+		},
+		{
+			desc:       "container created should not be restarted",
+			config:     &container.Config{Image: "busybox", Cmd: []string{"top"}},
+			hostConfig: &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
 		},
 	} {
 		for _, liveRestoreEnabled := range []bool{false, true} {
@@ -72,8 +80,10 @@ func TestDaemonRestartKillContainers(t *testing.T) {
 					assert.NilError(t, err)
 					defer client.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true})
 
-					err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
-					assert.NilError(t, err)
+					if c.xStart {
+						err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
+						assert.NilError(t, err)
+					}
 
 					stopDaemon(t, d)
 					d.Start(t, args...)