Skip to content

Commit 040afcc

Browse files
Madhan Raj MookkandyMadhan Raj Mookkandy
Madhan Raj Mookkandy
authored and
Madhan Raj Mookkandy
committedMar 1, 2017
(*) Support --net:container:<containername/id> for windows
(*) (vdemeester) Removed duplicate code across Windows and Unix wrt Net:Containers (*) Return unsupported error for network sharing for hyperv isolation containers Signed-off-by: Madhan Raj Mookkandy <[email protected]>
1 parent 6e04fbf commit 040afcc

12 files changed

+121
-126
lines changed
 

‎api/types/container/host_config.go

+41-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import (
1010
"github.com/docker/go-units"
1111
)
1212

13-
// NetworkMode represents the container network stack.
14-
type NetworkMode string
15-
1613
// Isolation represents the isolation technology of a container. The supported
1714
// values are platform specific
1815
type Isolation string
@@ -66,6 +63,47 @@ func (n IpcMode) Container() string {
6663
return ""
6764
}
6865

66+
// NetworkMode represents the container network stack.
67+
type NetworkMode string
68+
69+
// IsNone indicates whether container isn't using a network stack.
70+
func (n NetworkMode) IsNone() bool {
71+
return n == "none"
72+
}
73+
74+
// IsDefault indicates whether container uses the default network stack.
75+
func (n NetworkMode) IsDefault() bool {
76+
return n == "default"
77+
}
78+
79+
// IsPrivate indicates whether container uses its private network stack.
80+
func (n NetworkMode) IsPrivate() bool {
81+
return !(n.IsHost() || n.IsContainer())
82+
}
83+
84+
// IsContainer indicates whether container uses a container network stack.
85+
func (n NetworkMode) IsContainer() bool {
86+
parts := strings.SplitN(string(n), ":", 2)
87+
return len(parts) > 1 && parts[0] == "container"
88+
}
89+
90+
// ConnectedContainer is the id of the container which network this container is connected to.
91+
func (n NetworkMode) ConnectedContainer() string {
92+
parts := strings.SplitN(string(n), ":", 2)
93+
if len(parts) > 1 {
94+
return parts[1]
95+
}
96+
return ""
97+
}
98+
99+
//UserDefined indicates user-created network
100+
func (n NetworkMode) UserDefined() string {
101+
if n.IsUserDefined() {
102+
return string(n)
103+
}
104+
return ""
105+
}
106+
69107
// UsernsMode represents userns mode in the container.
70108
type UsernsMode string
71109

‎api/types/container/hostconfig_unix.go

-40
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,11 @@
22

33
package container
44

5-
import "strings"
6-
75
// IsValid indicates if an isolation technology is valid
86
func (i Isolation) IsValid() bool {
97
return i.IsDefault()
108
}
119

12-
// IsPrivate indicates whether container uses its private network stack.
13-
func (n NetworkMode) IsPrivate() bool {
14-
return !(n.IsHost() || n.IsContainer())
15-
}
16-
17-
// IsDefault indicates whether container uses the default network stack.
18-
func (n NetworkMode) IsDefault() bool {
19-
return n == "default"
20-
}
21-
2210
// NetworkName returns the name of the network stack.
2311
func (n NetworkMode) NetworkName() string {
2412
if n.IsBridge() {
@@ -47,35 +35,7 @@ func (n NetworkMode) IsHost() bool {
4735
return n == "host"
4836
}
4937

50-
// IsContainer indicates whether container uses a container network stack.
51-
func (n NetworkMode) IsContainer() bool {
52-
parts := strings.SplitN(string(n), ":", 2)
53-
return len(parts) > 1 && parts[0] == "container"
54-
}
55-
56-
// IsNone indicates whether container isn't using a network stack.
57-
func (n NetworkMode) IsNone() bool {
58-
return n == "none"
59-
}
60-
61-
// ConnectedContainer is the id of the container which network this container is connected to.
62-
func (n NetworkMode) ConnectedContainer() string {
63-
parts := strings.SplitN(string(n), ":", 2)
64-
if len(parts) > 1 {
65-
return parts[1]
66-
}
67-
return ""
68-
}
69-
7038
// IsUserDefined indicates user-created network
7139
func (n NetworkMode) IsUserDefined() bool {
7240
return !n.IsDefault() && !n.IsBridge() && !n.IsHost() && !n.IsNone() && !n.IsContainer()
7341
}
74-
75-
//UserDefined indicates user-created network
76-
func (n NetworkMode) UserDefined() string {
77-
if n.IsUserDefined() {
78-
return string(n)
79-
}
80-
return ""
81-
}

‎api/types/container/hostconfig_windows.go

+3-36
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ import (
44
"strings"
55
)
66

7-
// IsDefault indicates whether container uses the default network stack.
8-
func (n NetworkMode) IsDefault() bool {
9-
return n == "default"
10-
}
11-
12-
// IsNone indicates whether container isn't using a network stack.
13-
func (n NetworkMode) IsNone() bool {
14-
return n == "none"
15-
}
16-
17-
// IsContainer indicates whether container uses a container network stack.
18-
// Returns false as windows doesn't support this mode
19-
func (n NetworkMode) IsContainer() bool {
20-
return false
21-
}
22-
237
// IsBridge indicates whether container uses the bridge network stack
248
// in windows it is given the name NAT
259
func (n NetworkMode) IsBridge() bool {
@@ -32,20 +16,9 @@ func (n NetworkMode) IsHost() bool {
3216
return false
3317
}
3418

35-
// IsPrivate indicates whether container uses its private network stack.
36-
func (n NetworkMode) IsPrivate() bool {
37-
return !(n.IsHost() || n.IsContainer())
38-
}
39-
40-
// ConnectedContainer is the id of the container which network this container is connected to.
41-
// Returns blank string on windows
42-
func (n NetworkMode) ConnectedContainer() string {
43-
return ""
44-
}
45-
4619
// IsUserDefined indicates user-created network
4720
func (n NetworkMode) IsUserDefined() bool {
48-
return !n.IsDefault() && !n.IsNone() && !n.IsBridge()
21+
return !n.IsDefault() && !n.IsNone() && !n.IsBridge() && !n.IsContainer()
4922
}
5023

5124
// IsHyperV indicates the use of a Hyper-V partition for isolation
@@ -71,17 +44,11 @@ func (n NetworkMode) NetworkName() string {
7144
return "nat"
7245
} else if n.IsNone() {
7346
return "none"
47+
} else if n.IsContainer() {
48+
return "container"
7449
} else if n.IsUserDefined() {
7550
return n.UserDefined()
7651
}
7752

7853
return ""
7954
}
80-
81-
//UserDefined indicates user-created network
82-
func (n NetworkMode) UserDefined() string {
83-
if n.IsUserDefined() {
84-
return string(n)
85-
}
86-
return ""
87-
}

‎container/container_windows.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Container struct {
1616
CommonContainer
1717

1818
// Fields below here are platform specific.
19+
NetworkSharedContainerID string
1920
}
2021

2122
// ExitStatus provides exit reasons for a container.

‎daemon/container_operations.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,13 @@ func (daemon *Daemon) allocateNetwork(container *container.Container) error {
505505
logrus.Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
506506
}
507507

508+
if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
509+
return nil
510+
}
511+
508512
updateSettings := false
509-
if len(container.NetworkSettings.Networks) == 0 {
510-
if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
511-
return nil
512-
}
513513

514+
if len(container.NetworkSettings.Networks) == 0 {
514515
daemon.updateContainerNetworkSettings(container, nil)
515516
updateSettings = true
516517
}

‎daemon/container_operations_windows.go

+1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ func setupPathsAndSandboxOptions(container *container.Container, sboxOptions *[]
5656
}
5757

5858
func initializeNetworkingPaths(container *container.Container, nc *container.Container) {
59+
container.NetworkSharedContainerID = nc.ID
5960
}

‎daemon/start_windows.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,17 @@ func (daemon *Daemon) getLibcontainerdCreateOptions(container *container.Contain
157157
createOptions = append(createOptions, &libcontainerd.FlushOption{IgnoreFlushesDuringBoot: !container.HasBeenStartedBefore})
158158
createOptions = append(createOptions, hvOpts)
159159
createOptions = append(createOptions, layerOpts)
160-
if epList != nil {
161-
createOptions = append(createOptions, &libcontainerd.NetworkEndpointsOption{
162-
Endpoints: epList,
163-
AllowUnqualifiedDNSQuery: AllowUnqualifiedDNSQuery,
164-
DNSSearchList: dnsSearch,
165-
})
166-
}
167160

161+
var networkSharedContainerID string
162+
if container.HostConfig.NetworkMode.IsContainer() {
163+
networkSharedContainerID = container.NetworkSharedContainerID
164+
}
165+
createOptions = append(createOptions, &libcontainerd.NetworkEndpointsOption{
166+
Endpoints: epList,
167+
AllowUnqualifiedDNSQuery: AllowUnqualifiedDNSQuery,
168+
DNSSearchList: dnsSearch,
169+
NetworkSharedContainerID: networkSharedContainerID,
170+
})
168171
return createOptions, nil
169172
}
170173

‎libcontainerd/client_windows.go

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func (clnt *client) Create(containerID string, checkpoint string, checkpointDir
170170
if n.DNSSearchList != nil {
171171
configuration.DNSSearchList = strings.Join(n.DNSSearchList, ",")
172172
}
173+
configuration.NetworkSharedContainerName = n.NetworkSharedContainerID
173174
continue
174175
}
175176
if c, ok := option.(*CredentialsOption); ok {

‎libcontainerd/types_windows.go

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type NetworkEndpointsOption struct {
6060
Endpoints []string
6161
AllowUnqualifiedDNSQuery bool
6262
DNSSearchList []string
63+
NetworkSharedContainerID string
6364
}
6465

6566
// CredentialsOption is a CreateOption that indicates the credentials from

‎runconfig/hostconfig.go

+46
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package runconfig
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io"
7+
"strings"
68

79
"github.com/docker/docker/api/types/container"
810
)
@@ -32,3 +34,47 @@ func SetDefaultNetModeIfBlank(hc *container.HostConfig) {
3234
}
3335
}
3436
}
37+
38+
// ValidateNetContainerMode ensures that the various combinations of requested
39+
// network settings wrt container mode are valid.
40+
func ValidateNetContainerMode(c *container.Config, hc *container.HostConfig) error {
41+
// We may not be passed a host config, such as in the case of docker commit
42+
if hc == nil {
43+
return nil
44+
}
45+
parts := strings.Split(string(hc.NetworkMode), ":")
46+
if parts[0] == "container" {
47+
if len(parts) < 2 || parts[1] == "" {
48+
return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
49+
}
50+
}
51+
52+
if hc.NetworkMode.IsContainer() && c.Hostname != "" {
53+
return ErrConflictNetworkHostname
54+
}
55+
56+
if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
57+
return ErrConflictContainerNetworkAndLinks
58+
}
59+
60+
if hc.NetworkMode.IsContainer() && len(hc.DNS) > 0 {
61+
return ErrConflictNetworkAndDNS
62+
}
63+
64+
if hc.NetworkMode.IsContainer() && len(hc.ExtraHosts) > 0 {
65+
return ErrConflictNetworkHosts
66+
}
67+
68+
if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
69+
return ErrConflictContainerNetworkAndMac
70+
}
71+
72+
if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
73+
return ErrConflictNetworkPublishPorts
74+
}
75+
76+
if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
77+
return ErrConflictNetworkExposePorts
78+
}
79+
return nil
80+
}

‎runconfig/hostconfig_unix.go

+3-32
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package runconfig
55
import (
66
"fmt"
77
"runtime"
8-
"strings"
98

109
"github.com/docker/docker/api/types/container"
1110
"github.com/docker/docker/pkg/sysinfo"
@@ -30,15 +29,10 @@ func ValidateNetMode(c *container.Config, hc *container.HostConfig) error {
3029
if hc == nil {
3130
return nil
3231
}
33-
parts := strings.Split(string(hc.NetworkMode), ":")
34-
if parts[0] == "container" {
35-
if len(parts) < 2 || parts[1] == "" {
36-
return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
37-
}
38-
}
3932

40-
if hc.NetworkMode.IsContainer() && c.Hostname != "" {
41-
return ErrConflictNetworkHostname
33+
err := ValidateNetContainerMode(c, hc)
34+
if err != nil {
35+
return err
4236
}
4337

4438
if hc.UTSMode.IsHost() && c.Hostname != "" {
@@ -49,29 +43,6 @@ func ValidateNetMode(c *container.Config, hc *container.HostConfig) error {
4943
return ErrConflictHostNetworkAndLinks
5044
}
5145

52-
if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
53-
return ErrConflictContainerNetworkAndLinks
54-
}
55-
56-
if hc.NetworkMode.IsContainer() && len(hc.DNS) > 0 {
57-
return ErrConflictNetworkAndDNS
58-
}
59-
60-
if hc.NetworkMode.IsContainer() && len(hc.ExtraHosts) > 0 {
61-
return ErrConflictNetworkHosts
62-
}
63-
64-
if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
65-
return ErrConflictContainerNetworkAndMac
66-
}
67-
68-
if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
69-
return ErrConflictNetworkPublishPorts
70-
}
71-
72-
if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
73-
return ErrConflictNetworkExposePorts
74-
}
7546
return nil
7647
}
7748

‎runconfig/hostconfig_windows.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package runconfig
22

33
import (
44
"fmt"
5-
"strings"
65

76
"github.com/docker/docker/api/types/container"
87
"github.com/docker/docker/pkg/sysinfo"
@@ -25,10 +24,16 @@ func ValidateNetMode(c *container.Config, hc *container.HostConfig) error {
2524
if hc == nil {
2625
return nil
2726
}
28-
parts := strings.Split(string(hc.NetworkMode), ":")
29-
if len(parts) > 1 {
30-
return fmt.Errorf("invalid --net: %s", hc.NetworkMode)
27+
28+
err := ValidateNetContainerMode(c, hc)
29+
if err != nil {
30+
return err
31+
}
32+
33+
if hc.NetworkMode.IsContainer() && hc.Isolation.IsHyperV() {
34+
return fmt.Errorf("net mode --net=container:<NameOrId> unsupported for hyperv isolation")
3135
}
36+
3237
return nil
3338
}
3439

0 commit comments

Comments
 (0)
Please sign in to comment.