Skip to content

Commit

Permalink
Merge pull request #2796 from mheon/fix_cni_multinetwork
Browse files Browse the repository at this point in the history
Ensure that we make a netns for CNI non-default nets
  • Loading branch information
openshift-merge-robot authored Mar 29, 2019
2 parents fdf979a + e25924f commit 6ab27c6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ func WithNetNS(portMappings []ocicni.PortMapping, postConfigureNetNS bool, netmo

ctr.config.PostConfigureNetNS = postConfigureNetNS
ctr.config.NetMode = namespaces.NetworkMode(netmode)
ctr.config.CreateNetNS = !ctr.config.NetMode.IsUserDefined()
ctr.config.CreateNetNS = true
ctr.config.PortMappings = portMappings
ctr.config.Networks = networks

Expand Down
21 changes: 20 additions & 1 deletion pkg/namespaces/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,26 @@ func (n NetworkMode) IsSlirp4netns() bool {
return n == "slirp4netns"
}

// IsNS indicates a network namespace passed in by path (ns:<path>)
func (n NetworkMode) IsNS() bool {
return strings.HasPrefix(string(n), "ns:")
}

// NS gets the path associated with a ns:<path> network ns
func (n NetworkMode) NS() string {
parts := strings.SplitN(string(n), ":", 2)
if len(parts) > 1 {
return parts[1]
}
return ""
}

// IsPod returns whether the network refers to pod networking
func (n NetworkMode) IsPod() bool {
return n == "pod"
}

// IsUserDefined indicates user-created network
func (n NetworkMode) IsUserDefined() bool {
return !n.IsDefault() && !n.IsBridge() && !n.IsHost() && !n.IsNone() && !n.IsContainer() && !n.IsSlirp4netns()
return !n.IsDefault() && !n.IsBridge() && !n.IsHost() && !n.IsNone() && !n.IsContainer() && !n.IsSlirp4netns() && !n.IsNS()
}
11 changes: 5 additions & 6 deletions pkg/spec/createconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,15 @@ func (c *CreateConfig) GetContainerCreateOptions(runtime *libpod.Runtime, pod *l
}
}

if IsNS(string(c.NetMode)) {
split := strings.SplitN(string(c.NetMode), ":", 2)
if len(split[0]) != 2 {
return nil, errors.Errorf("invalid user defined network namespace %q", c.NetMode.UserDefined())
if c.NetMode.IsNS() {
ns := c.NetMode.NS()
if ns == "" {
return nil, errors.Errorf("invalid empty user-defined network namespace")
}
_, err := os.Stat(split[1])
_, err := os.Stat(ns)
if err != nil {
return nil, err
}
options = append(options, libpod.WithNetNS(portBindings, false, string(c.NetMode), networks))
} else if c.NetMode.IsContainer() {
connectedCtr, err := c.Runtime.LookupContainer(c.NetMode.Container())
if err != nil {
Expand Down

0 comments on commit 6ab27c6

Please sign in to comment.