浏览代码

Merge 988e94868a2ea0298230a142538df2285effb54b into 801fd16e3e48b7638e6fee83facbb62de9a03cef

Tycho Andersen 1 年之前
父节点
当前提交
c9acfc2589
共有 5 个文件被更改,包括 51 次插入9 次删除
  1. 7 6
      api/types/container/config.go
  2. 12 2
      container/container.go
  3. 20 0
      container/container_unit_test.go
  4. 2 1
      daemon/monitor.go
  5. 10 0
      image/cache/compare.go

+ 7 - 6
api/types/container/config.go

@@ -73,10 +73,11 @@ type Config struct {
 	// Mac Address of the container.
 	// Mac Address of the container.
 	//
 	//
 	// Deprecated: this field is deprecated since API v1.44. Use EndpointSettings.MacAddress instead.
 	// Deprecated: this field is deprecated since API v1.44. Use EndpointSettings.MacAddress instead.
-	MacAddress  string            `json:",omitempty"`
-	OnBuild     []string          // ONBUILD metadata that were defined on the image Dockerfile
-	Labels      map[string]string // List of labels set to this container
-	StopSignal  string            `json:",omitempty"` // Signal to stop a container
-	StopTimeout *int              `json:",omitempty"` // Timeout (in seconds) to stop a container
-	Shell       strslice.StrSlice `json:",omitempty"` // Shell for shell-form of RUN, CMD, ENTRYPOINT
+	MacAddress    string            `json:",omitempty"`
+	OnBuild       []string          // ONBUILD metadata that were defined on the image Dockerfile
+	Labels        map[string]string // List of labels set to this container
+	StopSignal    string            `json:",omitempty"` // Signal to stop a container
+	StopTimeout   *int              `json:",omitempty"` // Timeout (in seconds) to stop a container
+	DeleteTimeout *int              `json:",omitempty"` // Timeout (in seconds) to wait for a container to be deleted
+	Shell         strslice.StrSlice `json:",omitempty"` // Shell for shell-form of RUN, CMD, ENTRYPOINT
 }
 }

+ 12 - 2
container/container.go

@@ -45,8 +45,9 @@ import (
 )
 )
 
 
 const (
 const (
-	configFileName     = "config.v2.json"
-	hostConfigFileName = "hostconfig.json"
+	configFileName       = "config.v2.json"
+	hostConfigFileName   = "hostconfig.json"
+	defaultDeleteTimeout = 30
 )
 )
 
 
 // ExitStatus provides exit reasons for a container.
 // ExitStatus provides exit reasons for a container.
@@ -562,6 +563,15 @@ func (container *Container) StopTimeout() int {
 	return defaultStopTimeout
 	return defaultStopTimeout
 }
 }
 
 
+// DeleteTimeout returns the timeout (in seconds) to wait for a container to be deleted.
+func (container *Container) DeleteTimeout() int {
+	if container.Config.DeleteTimeout != nil {
+		return *container.Config.DeleteTimeout
+	}
+
+	return defaultDeleteTimeout
+}
+
 // InitDNSHostConfig ensures that the dns fields are never nil.
 // InitDNSHostConfig ensures that the dns fields are never nil.
 // New containers don't ever have those fields nil,
 // New containers don't ever have those fields nil,
 // but pre created containers can still have those nil values.
 // but pre created containers can still have those nil values.

+ 20 - 0
container/container_unit_test.go

@@ -57,6 +57,26 @@ func TestContainerStopTimeout(t *testing.T) {
 	}
 	}
 }
 }
 
 
+func TestContainerDeleteTimeout(t *testing.T) {
+	c := &Container{
+		Config: &container.Config{},
+	}
+
+	s := c.DeleteTimeout()
+	if s != defaultDeleteTimeout {
+		t.Fatalf("Expected %v, got %v", defaultDeleteTimeout, s)
+	}
+
+	deleteTimeout := 15
+	c = &Container{
+		Config: &container.Config{DeleteTimeout: &deleteTimeout},
+	}
+	s = c.DeleteTimeout()
+	if s != deleteTimeout {
+		t.Fatalf("Expected %v, got %v", deleteTimeout, s)
+	}
+}
+
 func TestContainerSecretReferenceDestTarget(t *testing.T) {
 func TestContainerSecretReferenceDestTarget(t *testing.T) {
 	ref := &swarmtypes.SecretReference{
 	ref := &swarmtypes.SecretReference{
 		File: &swarmtypes.SecretReferenceFileTarget{
 		File: &swarmtypes.SecretReferenceFileTarget{

+ 2 - 1
daemon/monitor.go

@@ -39,7 +39,8 @@ func (daemon *Daemon) handleContainerExit(c *container.Container, e *libcontaine
 
 
 	tsk, ok := c.Task()
 	tsk, ok := c.Task()
 	if ok {
 	if ok {
-		ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+		deleteTimeout := time.Duration(c.DeleteTimeout()) * time.Second
+		ctx, cancel := context.WithTimeout(context.Background(), deleteTimeout)
 		es, err := tsk.Delete(ctx)
 		es, err := tsk.Delete(ctx)
 		cancel()
 		cancel()
 		if err != nil {
 		if err != nil {

+ 10 - 0
image/cache/compare.go

@@ -149,6 +149,16 @@ func compare(a, b *container.Config) bool {
 			return false
 			return false
 		}
 		}
 	}
 	}
+
+	if (a.DeleteTimeout == nil) != (b.DeleteTimeout == nil) {
+		return false
+	}
+	if a.DeleteTimeout != nil && b.DeleteTimeout != nil {
+		if *a.DeleteTimeout != *b.DeleteTimeout {
+			return false
+		}
+	}
+
 	if (a.Healthcheck == nil) != (b.Healthcheck == nil) {
 	if (a.Healthcheck == nil) != (b.Healthcheck == nil) {
 		return false
 		return false
 	}
 	}