diff --git a/virtcontainers/interfaces.go b/virtcontainers/interfaces.go index 1d9921b060..906d54c938 100644 --- a/virtcontainers/interfaces.go +++ b/virtcontainers/interfaces.go @@ -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 diff --git a/virtcontainers/pkg/vcmock/sandbox.go b/virtcontainers/pkg/vcmock/sandbox.go index f3a3c0cf82..4e64a98bb1 100644 --- a/virtcontainers/pkg/vcmock/sandbox.go +++ b/virtcontainers/pkg/vcmock/sandbox.go @@ -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)) diff --git a/virtcontainers/pkg/vcmock/types.go b/virtcontainers/pkg/vcmock/types.go index bf06258fd0..ecfbfcefdd 100644 --- a/virtcontainers/pkg/vcmock/types.go +++ b/virtcontainers/pkg/vcmock/types.go @@ -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 diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index dda797c2ce..3fb0fd217d 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -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)) diff --git a/virtcontainers/sandbox_test.go b/virtcontainers/sandbox_test.go index fc3eb2652e..ce7d67f276 100644 --- a/virtcontainers/sandbox_test.go +++ b/virtcontainers/sandbox_test.go @@ -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) +}