Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers: sandbox: Add new getter to retrieve netns
Browse files Browse the repository at this point in the history
As we want to call the OCI hook from the CLI, we need a way for the
CLI to figure out what is the network namespace used by the sandbox.
This is needed particularly because virtcontainers creates the netns
if none was provided.

Signed-off-by: Sebastien Boeuf <[email protected]>
  • Loading branch information
Sebastien Boeuf committed Aug 24, 2018
1 parent cb351dc commit ec0fd1b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions virtcontainers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type VC interface {
// (required since virtcontainers.Sandbox only contains private fields)
type VCSandbox interface {
Annotations(key string) (string, error)
GetNetNs() string
GetAllContainers() []VCContainer
GetAnnotations() map[string]string
GetContainer(containerID string) VCContainer
Expand Down
5 changes: 5 additions & 0 deletions virtcontainers/pkg/vcmock/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func (s *Sandbox) GetAnnotations() map[string]string {
return s.MockAnnotations
}

// GetNetNs returns the network namespace of the current sandbox.
func (s *Sandbox) GetNetNs() string {
return s.MockNetNs
}

// GetAllContainers implements the VCSandbox function of the same name.
func (s *Sandbox) GetAllContainers() []vc.VCContainer {
var ifa = make([]vc.VCContainer, len(s.MockContainers))
Expand Down
1 change: 1 addition & 0 deletions virtcontainers/pkg/vcmock/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Sandbox struct {
MockURL string
MockAnnotations map[string]string
MockContainers []*Container
MockNetNs string
}

// Container is a fake Container type used for testing
Expand Down
5 changes: 5 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ func (s *Sandbox) GetAnnotations() map[string]string {
return s.config.Annotations
}

// GetNetNs returns the network namespace of the current sandbox.
func (s *Sandbox) GetNetNs() string {
return s.networkNS.NetNsPath
}

// GetAllContainers returns all containers.
func (s *Sandbox) GetAllContainers() []VCContainer {
ifa := make([]VCContainer, len(s.containers))
Expand Down
16 changes: 16 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1706,3 +1706,19 @@ func TestPreAddDevice(t *testing.T) {
assert.Equal(t, len(mounts), 0,
"mounts should contain nothing because it only contains a block device")
}

func TestGetNetNs(t *testing.T) {
s := Sandbox{}

expected := ""
netNs := s.GetNetNs()
assert.Equal(t, netNs, expected)

expected = "/foo/bar/ns/net"
s.networkNS = NetworkNamespace{
NetNsPath: expected,
}

netNs = s.GetNetNs()
assert.Equal(t, netNs, expected)
}

0 comments on commit ec0fd1b

Please sign in to comment.