virtcontainers: sandbox: Add new getter to retrieve netns

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 <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2018-08-16 10:01:49 -07:00
parent cb351dca10
commit ec0fd1b67a
5 changed files with 28 additions and 0 deletions

View File

@ -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

View File

@ -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))

View File

@ -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

View File

@ -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))

View File

@ -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)
}