spec: refactor ns modes to a common interface

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>

Closes: #1507
Approved by: rhatdan
This commit is contained in:
Giuseppe Scrivano 2018-09-19 10:12:04 +02:00 committed by Atomic Bot
parent fbd1392a46
commit 8b9b493b53
2 changed files with 14 additions and 4 deletions

View File

@ -28,6 +28,16 @@ func (n UsernsMode) Valid() bool {
return true
}
// IsContainer indicates whether container uses a container userns.
func (n UsernsMode) IsContainer() bool {
return false
}
// Container is the id of the container which network this container is connected to.
func (n UsernsMode) Container() string {
return ""
}
// UTSMode represents the UTS namespace of the container.
type UTSMode string
@ -191,8 +201,8 @@ func (n NetworkMode) IsContainer() bool {
return len(parts) > 1 && parts[0] == "container"
}
// ConnectedContainer is the id of the container which network this container is connected to.
func (n NetworkMode) ConnectedContainer() string {
// Container is the id of the container which network this container is connected to.
func (n NetworkMode) Container() string {
parts := strings.SplitN(string(n), ":", 2)
if len(parts) > 1 {
return parts[1]

View File

@ -374,9 +374,9 @@ func (c *CreateConfig) GetContainerCreateOptions(runtime *libpod.Runtime) ([]lib
if IsNS(string(c.NetMode)) {
// pass
} else if c.NetMode.IsContainer() {
connectedCtr, err := c.Runtime.LookupContainer(c.NetMode.ConnectedContainer())
connectedCtr, err := c.Runtime.LookupContainer(c.NetMode.Container())
if err != nil {
return nil, errors.Wrapf(err, "container %q not found", c.NetMode.ConnectedContainer())
return nil, errors.Wrapf(err, "container %q not found", c.NetMode.Container())
}
options = append(options, libpod.WithNetNSFrom(connectedCtr))
} else if !c.NetMode.IsHost() && !c.NetMode.IsNone() {