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 DeleteContainer API
Browse files Browse the repository at this point in the history
DeleteContainer in api.go is now a wrapper of it.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Apr 24, 2018
1 parent f6aa8a2 commit d9144c8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
28 changes: 2 additions & 26 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,36 +330,12 @@ func DeleteContainer(sandboxID, containerID string) (VCContainer, error) {
}
defer unlockSandbox(lockFile)

p, err := fetchSandbox(sandboxID)
if err != nil {
return nil, err
}

// Fetch the container.
c, err := p.findContainer(containerID)
if err != nil {
return nil, err
}

// Delete it.
err = c.delete()
if err != nil {
return nil, err
}

// Update sandbox config
for idx, contConfig := range p.config.Containers {
if contConfig.ID == containerID {
p.config.Containers = append(p.config.Containers[:idx], p.config.Containers[idx+1:]...)
break
}
}
err = p.storage.storeSandboxResource(sandboxID, configFileType, *(p.config))
s, err := fetchSandbox(sandboxID)
if err != nil {
return nil, err
}

return c, nil
return s.DeleteContainer(containerID)
}

// StartContainer is the virtcontainers container starting 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 @@ -51,6 +51,7 @@ type VCSandbox interface {
Release() error
Delete() error
CreateContainer(contConfig ContainerConfig) (VCContainer, error)
DeleteContainer(contID string) (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 @@ -74,3 +74,8 @@ func (p *Sandbox) Delete() error {
func (p *Sandbox) CreateContainer(conf vc.ContainerConfig) (vc.VCContainer, error) {
return &Container{}, nil
}

// DeleteContainer implements the VCSandbox function of the same name.
func (p *Sandbox) DeleteContainer(contID string) (vc.VCContainer, error) {
return &Container{}, nil
}
35 changes: 35 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,41 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro
return c, nil
}

// DeleteContainer deletes a container from the sandbox
func (s *Sandbox) DeleteContainer(containerID string) (VCContainer, error) {
if containerID == "" {
return nil, errNeedContainerID
}

// Fetch the container.
c, err := s.findContainer(containerID)
if err != nil {
return nil, err
}

// Delete it.
err = c.delete()
if err != nil {
return nil, err
}

// Update sandbox config
for idx, contConfig := range s.config.Containers {
if contConfig.ID == containerID {
s.config.Containers = append(s.config.Containers[:idx], s.config.Containers[idx+1:]...)
break
}
}

// Store sandbox config
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
17 changes: 17 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1299,3 +1299,20 @@ func TestCreateContainer(t *testing.T) {
_, err = s.CreateContainer(contConfig)
assert.Nil(t, err, "Failed to create container %+v in sandbox %+v: %v", contConfig, s, err)
}

func TestDeleteContainer(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"
_, err = s.DeleteContainer(contID)
assert.NotNil(t, err, "Deletng non-existing container should fail")

contConfig := newTestContainerConfigNoop(contID)
_, err = s.CreateContainer(contConfig)
assert.Nil(t, err, "Failed to create container %+v in sandbox %+v: %v", contConfig, s, err)

_, err = s.DeleteContainer(contID)
assert.Nil(t, err, "Failed to delete container %s in sandbox %s: %v", contID, s.ID(), err)
}

0 comments on commit d9144c8

Please sign in to comment.