Browse code

Merge pull request #12432 from Mashimiao/optimize-code-to-clarify-loagic

change code to clarify logic

Brian Goff authored on 2015/04/19 20:39:24
Showing 2 changed files
... ...
@@ -2,7 +2,6 @@ package daemon
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"strings"
6 5
 
7 6
 	"github.com/docker/docker/graph"
8 7
 	"github.com/docker/docker/image"
... ...
@@ -12,27 +11,9 @@ import (
12 12
 )
13 13
 
14 14
 func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig) (string, []string, error) {
15
-	var warnings []string
16
-
17
-	if hostConfig.LxcConf.Len() > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") {
18
-		return "", warnings, fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name())
19
-	}
20
-	if hostConfig.Memory != 0 && hostConfig.Memory < 4194304 {
21
-		return "", warnings, fmt.Errorf("Minimum memory limit allowed is 4MB")
22
-	}
23
-	if hostConfig.Memory > 0 && !daemon.SystemConfig().MemoryLimit {
24
-		warnings = append(warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.\n")
25
-		hostConfig.Memory = 0
26
-	}
27
-	if hostConfig.Memory > 0 && hostConfig.MemorySwap != -1 && !daemon.SystemConfig().SwapLimit {
28
-		warnings = append(warnings, "Your kernel does not support swap limit capabilities. Limitation discarded.\n")
29
-		hostConfig.MemorySwap = -1
30
-	}
31
-	if hostConfig.Memory > 0 && hostConfig.MemorySwap > 0 && hostConfig.MemorySwap < hostConfig.Memory {
32
-		return "", warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.\n")
33
-	}
34
-	if hostConfig.Memory == 0 && hostConfig.MemorySwap > 0 {
35
-		return "", warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.\n")
15
+	warnings, err := daemon.verifyHostConfig(hostConfig)
16
+	if err != nil {
17
+		return "", warnings, err
36 18
 	}
37 19
 
38 20
 	container, buildWarnings, err := daemon.Create(config, hostConfig, name)
... ...
@@ -46,9 +27,6 @@ func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hos
46 46
 		}
47 47
 		return "", warnings, err
48 48
 	}
49
-	if !container.Config.NetworkDisabled && daemon.SystemConfig().IPv4ForwardingDisabled {
50
-		warnings = append(warnings, "IPv4 forwarding is disabled.\n")
51
-	}
52 49
 
53 50
 	container.LogEvent("create")
54 51
 	warnings = append(warnings, buildWarnings...)
... ...
@@ -80,6 +58,9 @@ func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.Hos
80 80
 	if warnings, err = daemon.mergeAndVerifyConfig(config, img); err != nil {
81 81
 		return nil, nil, err
82 82
 	}
83
+	if !config.NetworkDisabled && daemon.SystemConfig().IPv4ForwardingDisabled {
84
+		warnings = append(warnings, "IPv4 forwarding is disabled.\n")
85
+	}
83 86
 	if hostConfig == nil {
84 87
 		hostConfig = &runconfig.HostConfig{}
85 88
 	}
... ...
@@ -1223,3 +1223,30 @@ func checkKernel() error {
1223 1223
 	}
1224 1224
 	return nil
1225 1225
 }
1226
+
1227
+func (daemon *Daemon) verifyHostConfig(hostConfig *runconfig.HostConfig) ([]string, error) {
1228
+	var warnings []string
1229
+
1230
+	if hostConfig.LxcConf.Len() > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") {
1231
+		return warnings, fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name())
1232
+	}
1233
+	if hostConfig.Memory != 0 && hostConfig.Memory < 4194304 {
1234
+		return warnings, fmt.Errorf("Minimum memory limit allowed is 4MB")
1235
+	}
1236
+	if hostConfig.Memory > 0 && !daemon.SystemConfig().MemoryLimit {
1237
+		warnings = append(warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.\n")
1238
+		hostConfig.Memory = 0
1239
+	}
1240
+	if hostConfig.Memory > 0 && hostConfig.MemorySwap != -1 && !daemon.SystemConfig().SwapLimit {
1241
+		warnings = append(warnings, "Your kernel does not support swap limit capabilities. Limitation discarded.\n")
1242
+		hostConfig.MemorySwap = -1
1243
+	}
1244
+	if hostConfig.Memory > 0 && hostConfig.MemorySwap > 0 && hostConfig.MemorySwap < hostConfig.Memory {
1245
+		return warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.\n")
1246
+	}
1247
+	if hostConfig.Memory == 0 && hostConfig.MemorySwap > 0 {
1248
+		return warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.\n")
1249
+	}
1250
+
1251
+	return warnings, nil
1252
+}