From f6aa8a23fcc607dffeb82b947a78a443a0c35347 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Wed, 18 Apr 2018 17:25:10 +0800 Subject: [PATCH] api: add sandbox CreateContainer API And make CreateContainer in api.go a wrapper of it. Signed-off-by: Peng Tao --- virtcontainers/api.go | 25 +++--------------------- virtcontainers/interfaces.go | 1 + virtcontainers/pkg/vcmock/sandbox.go | 5 +++++ virtcontainers/sandbox.go | 29 ++++++++++++++++++++++++++++ virtcontainers/sandbox_test.go | 11 +++++++++++ 5 files changed, 49 insertions(+), 22 deletions(-) diff --git a/virtcontainers/api.go b/virtcontainers/api.go index 3603f8ee35..3dad7e38ad 100644 --- a/virtcontainers/api.go +++ b/virtcontainers/api.go @@ -299,36 +299,17 @@ func CreateContainer(sandboxID string, containerConfig ContainerConfig) (VCSandb } defer unlockSandbox(lockFile) - p, err := fetchSandbox(sandboxID) + s, err := fetchSandbox(sandboxID) if err != nil { return nil, nil, err } - // Create the container. - c, err := createContainer(p, containerConfig) - if err != nil { - return nil, nil, err - } - - // Add the container to the containers list in the sandbox. - if err := p.addContainer(c); err != nil { - return nil, nil, err - } - - // Store it. - err = c.storeContainer() - if err != nil { - return nil, nil, err - } - - // Update sandbox config. - p.config.Containers = append(p.config.Containers, containerConfig) - err = p.storage.storeSandboxResource(sandboxID, configFileType, *(p.config)) + c, err := s.CreateContainer(containerConfig) if err != nil { return nil, nil, err } - return p, c, nil + return s, c, nil } // DeleteContainer is the virtcontainers container deletion entry point. diff --git a/virtcontainers/interfaces.go b/virtcontainers/interfaces.go index 1787f942e2..b9d383da3e 100644 --- a/virtcontainers/interfaces.go +++ b/virtcontainers/interfaces.go @@ -50,6 +50,7 @@ type VCSandbox interface { Resume() error Release() error Delete() error + CreateContainer(contConfig ContainerConfig) (VCContainer, error) } // VCContainer is the Container interface diff --git a/virtcontainers/pkg/vcmock/sandbox.go b/virtcontainers/pkg/vcmock/sandbox.go index 2c33d74da4..d24366c860 100644 --- a/virtcontainers/pkg/vcmock/sandbox.go +++ b/virtcontainers/pkg/vcmock/sandbox.go @@ -69,3 +69,8 @@ func (p *Sandbox) Resume() error { func (p *Sandbox) Delete() error { return nil } + +// CreateContainer implements the VCSandbox function of the same name. +func (p *Sandbox) CreateContainer(conf vc.ContainerConfig) (vc.VCContainer, error) { + return &Container{}, nil +} diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index be96807668..0085a26ca3 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -865,6 +865,35 @@ func (s *Sandbox) newContainers() error { return nil } +// CreateContainer creates a new container in the sandbox +func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, error) { + // Create the container. + c, err := createContainer(s, contConfig) + if err != nil { + return nil, err + } + + // Add the container to the containers list in the sandbox. + if err := s.addContainer(c); err != nil { + return nil, err + } + + // Store it. + err = c.storeContainer() + if err != nil { + return nil, err + } + + // Update sandbox config. + s.config.Containers = append(s.config.Containers, contConfig) + err = s.storage.storeSandboxResource(s.id, configFileType, *(s.config)) + if err != nil { + return nil, err + } + + return c, nil +} + // createContainers registers all containers to the proxy, create the // containers in the guest and starts one shim per container. func (s *Sandbox) createContainers() error { diff --git a/virtcontainers/sandbox_test.go b/virtcontainers/sandbox_test.go index d58e9b9811..92bea4ede5 100644 --- a/virtcontainers/sandbox_test.go +++ b/virtcontainers/sandbox_test.go @@ -1288,3 +1288,14 @@ func TestRemoveContainerSuccess(t *testing.T) { assert.Equal(t, len(sandbox.containers), 0, "Containers list from sandbox structure should be empty") } + +func TestCreateContainer(t *testing.T) { + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NoopNetworkModel, NetworkConfig{}, nil, nil) + assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") + defer cleanUp() + + contID := "999" + contConfig := newTestContainerConfigNoop(contID) + _, err = s.CreateContainer(contConfig) + assert.Nil(t, err, "Failed to create container %+v in sandbox %+v: %v", contConfig, s, err) +}