Browse code

Move validation functions

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2015/11/01 10:49:14
Showing 4 changed files
... ...
@@ -3,6 +3,8 @@
3 3
 package runconfig
4 4
 
5 5
 import (
6
+	"fmt"
7
+	"runtime"
6 8
 	"strings"
7 9
 )
8 10
 
... ...
@@ -103,3 +105,69 @@ func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrappe
103 103
 		"", nil,
104 104
 	}
105 105
 }
106
+
107
+// ValidateNetMode ensures that the various combinations of requested
108
+// network settings are valid.
109
+func ValidateNetMode(c *Config, hc *HostConfig) error {
110
+	// We may not be passed a host config, such as in the case of docker commit
111
+	if hc == nil {
112
+		return nil
113
+	}
114
+	parts := strings.Split(string(hc.NetworkMode), ":")
115
+	if parts[0] == "container" {
116
+		if len(parts) < 2 || parts[1] == "" {
117
+			return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
118
+		}
119
+	}
120
+
121
+	if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" {
122
+		return ErrConflictNetworkHostname
123
+	}
124
+
125
+	if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
126
+		return ErrConflictHostNetworkAndLinks
127
+	}
128
+
129
+	if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
130
+		return ErrConflictContainerNetworkAndLinks
131
+	}
132
+
133
+	if hc.NetworkMode.IsUserDefined() && len(hc.Links) > 0 {
134
+		return ErrConflictUserDefinedNetworkAndLinks
135
+	}
136
+
137
+	if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 {
138
+		return ErrConflictNetworkAndDNS
139
+	}
140
+
141
+	if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && len(hc.ExtraHosts) > 0 {
142
+		return ErrConflictNetworkHosts
143
+	}
144
+
145
+	if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
146
+		return ErrConflictContainerNetworkAndMac
147
+	}
148
+
149
+	if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
150
+		return ErrConflictNetworkPublishPorts
151
+	}
152
+
153
+	if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
154
+		return ErrConflictNetworkExposePorts
155
+	}
156
+	return nil
157
+}
158
+
159
+// ValidateIsolationLevel performs platform specific validation of the
160
+// isolation level in the hostconfig structure. Linux only supports "default"
161
+// which is LXC container isolation
162
+func ValidateIsolationLevel(hc *HostConfig) error {
163
+	// We may not be passed a host config, such as in the case of docker commit
164
+	if hc == nil {
165
+		return nil
166
+	}
167
+	if !hc.Isolation.IsValid() {
168
+		return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS)
169
+	}
170
+	return nil
171
+}
... ...
@@ -1,6 +1,9 @@
1 1
 package runconfig
2 2
 
3
-import "strings"
3
+import (
4
+	"fmt"
5
+	"strings"
6
+)
4 7
 
5 8
 // IsDefault indicates whether container uses the default network stack.
6 9
 func (n NetworkMode) IsDefault() bool {
... ...
@@ -45,3 +48,34 @@ func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrappe
45 45
 func IsPreDefinedNetwork(network string) bool {
46 46
 	return false
47 47
 }
48
+
49
+// ValidateNetMode ensures that the various combinations of requested
50
+// network settings are valid.
51
+func ValidateNetMode(c *Config, hc *HostConfig) error {
52
+	// We may not be passed a host config, such as in the case of docker commit
53
+	if hc == nil {
54
+		return nil
55
+	}
56
+	parts := strings.Split(string(hc.NetworkMode), ":")
57
+	switch mode := parts[0]; mode {
58
+	case "default", "none":
59
+	default:
60
+		return fmt.Errorf("invalid --net: %s", hc.NetworkMode)
61
+	}
62
+	return nil
63
+}
64
+
65
+// ValidateIsolationLevel performs platform specific validation of the
66
+// isolation level in the hostconfig structure. Windows supports 'default' (or
67
+// blank), and 'hyperv'. These refer to Windows Server Containers and
68
+// Hyper-V Containers respectively.
69
+func ValidateIsolationLevel(hc *HostConfig) error {
70
+	// We may not be passed a host config, such as in the case of docker commit
71
+	if hc == nil {
72
+		return nil
73
+	}
74
+	if !hc.Isolation.IsValid() {
75
+		return fmt.Errorf("invalid --isolation: %q. Windows supports 'default' (Windows Server Container) or 'hyperv' (Hyper-V Container)", hc.Isolation)
76
+	}
77
+	return nil
78
+}
48 79
deleted file mode 100644
... ...
@@ -1,74 +0,0 @@
1
-// +build !windows
2
-
3
-package runconfig
4
-
5
-import (
6
-	"fmt"
7
-	"runtime"
8
-	"strings"
9
-)
10
-
11
-// ValidateNetMode ensures that the various combinations of requested
12
-// network settings are valid.
13
-func ValidateNetMode(c *Config, hc *HostConfig) error {
14
-	// We may not be passed a host config, such as in the case of docker commit
15
-	if hc == nil {
16
-		return nil
17
-	}
18
-	parts := strings.Split(string(hc.NetworkMode), ":")
19
-	if parts[0] == "container" {
20
-		if len(parts) < 2 || parts[1] == "" {
21
-			return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
22
-		}
23
-	}
24
-
25
-	if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" {
26
-		return ErrConflictNetworkHostname
27
-	}
28
-
29
-	if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
30
-		return ErrConflictHostNetworkAndLinks
31
-	}
32
-
33
-	if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
34
-		return ErrConflictContainerNetworkAndLinks
35
-	}
36
-
37
-	if hc.NetworkMode.IsUserDefined() && len(hc.Links) > 0 {
38
-		return ErrConflictUserDefinedNetworkAndLinks
39
-	}
40
-
41
-	if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 {
42
-		return ErrConflictNetworkAndDNS
43
-	}
44
-
45
-	if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && len(hc.ExtraHosts) > 0 {
46
-		return ErrConflictNetworkHosts
47
-	}
48
-
49
-	if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
50
-		return ErrConflictContainerNetworkAndMac
51
-	}
52
-
53
-	if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
54
-		return ErrConflictNetworkPublishPorts
55
-	}
56
-
57
-	if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
58
-		return ErrConflictNetworkExposePorts
59
-	}
60
-	return nil
61
-}
62
-
63
-// ValidateIsolationLevel performs platform specific validation of the
64
-// isolation level in the hostconfig structure. Linux only supports "default".
65
-func ValidateIsolationLevel(hc *HostConfig) error {
66
-	// We may not be passed a host config, such as in the case of docker commit
67
-	if hc == nil {
68
-		return nil
69
-	}
70
-	if !hc.Isolation.IsValid() {
71
-		return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS)
72
-	}
73
-	return nil
74
-}
75 1
deleted file mode 100644
... ...
@@ -1,37 +0,0 @@
1
-package runconfig
2
-
3
-import (
4
-	"fmt"
5
-	"strings"
6
-)
7
-
8
-// ValidateNetMode ensures that the various combinations of requested
9
-// network settings are valid.
10
-func ValidateNetMode(c *Config, hc *HostConfig) error {
11
-	// We may not be passed a host config, such as in the case of docker commit
12
-	if hc == nil {
13
-		return nil
14
-	}
15
-	parts := strings.Split(string(hc.NetworkMode), ":")
16
-	switch mode := parts[0]; mode {
17
-	case "default", "none":
18
-	default:
19
-		return fmt.Errorf("invalid --net: %s", hc.NetworkMode)
20
-	}
21
-	return nil
22
-}
23
-
24
-// ValidateIsolationLevel performs platform specific validation of the
25
-// isolation level in the hostconfig structure. Windows supports 'default' (or
26
-// blank), and 'hyperv'. These refer to Windows Server Containers and
27
-// Hyper-V Containers respectively.
28
-func ValidateIsolationLevel(hc *HostConfig) error {
29
-	// We may not be passed a host config, such as in the case of docker commit
30
-	if hc == nil {
31
-		return nil
32
-	}
33
-	if !hc.Isolation.IsValid() {
34
-		return fmt.Errorf("invalid --isolation: %q. Windows supports 'default' (Windows Server Container) or 'hyperv' (Hyper-V Container)", hc.Isolation)
35
-	}
36
-	return nil
37
-}