api: add sandbox CreateContainer API

And make CreateContainer in api.go a wrapper of it.

Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
Peng Tao 2018-04-18 17:25:10 +08:00
parent ef89131b85
commit f6aa8a23fc
5 changed files with 49 additions and 22 deletions

View File

@ -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)
c, err := s.CreateContainer(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))
if err != nil {
return nil, nil, err
}
return p, c, nil
return s, c, nil
}
// DeleteContainer is the virtcontainers container deletion entry point.

View File

@ -50,6 +50,7 @@ type VCSandbox interface {
Resume() error
Release() error
Delete() error
CreateContainer(contConfig ContainerConfig) (VCContainer, error)
}
// VCContainer is the Container interface

View File

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

View File

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

View File

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