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

Commit

Permalink
api: add sandbox CreateContainer API
Browse files Browse the repository at this point in the history
And make CreateContainer in api.go a wrapper of it.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Apr 24, 2018
1 parent ef89131 commit f6aa8a2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
25 changes: 3 additions & 22 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions virtcontainers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type VCSandbox interface {
Resume() error
Release() error
Delete() error
CreateContainer(contConfig ContainerConfig) (VCContainer, error)
}

// VCContainer is the Container interface
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 @@ -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
}
29 changes: 29 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit f6aa8a2

Please sign in to comment.