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

Commit

Permalink
virtcontainers: cleanup the container config once failed
Browse files Browse the repository at this point in the history
When create container failed, it should delete the container
config from sandbox, otherwise, the following new creating container
would get a wrong resources caculating which would contain the previous
failed container resources such as memory and cpu.

Fixes: #1997

Signed-off-by: lifupan <[email protected]>
  • Loading branch information
lifupan authored and Eric Ernst committed Aug 29, 2019
1 parent a85252e commit 05c8b02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,13 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro
// Update sandbox config.
s.config.Containers = append(s.config.Containers, contConfig)

defer func() {
if err != nil && len(s.config.Containers) > 0 {
// delete container config
s.config.Containers = s.config.Containers[:len(s.config.Containers)-1]
}
}()

// Sandbox is reponsable to update VM resources needed by Containers
err = s.updateResources()
if err != nil {
Expand All @@ -1104,7 +1111,7 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro
}

// Add the container to the containers list in the sandbox.
if err := s.addContainer(c); err != nil {
if err = s.addContainer(c); err != nil {
return nil, err
}

Expand All @@ -1114,7 +1121,7 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro
return nil, err
}

if err := s.updateCgroups(); err != nil {
if err = s.updateCgroups(); err != nil {
return nil, err
}

Expand Down
6 changes: 6 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,12 @@ func TestCreateContainer(t *testing.T) {
contConfig := newTestContainerConfigNoop(contID)
_, err = s.CreateContainer(contConfig)
assert.Nil(t, err, "Failed to create container %+v in sandbox %+v: %v", contConfig, s, err)

assert.Equal(t, len(s.config.Containers), 1, "Container config list length from sandbox structure should be 1")

_, err = s.CreateContainer(contConfig)
assert.NotNil(t, err, "Should failed to create a duplicated container")
assert.Equal(t, len(s.config.Containers), 1, "Container config list length from sandbox structure should be 1")
}

func TestDeleteContainer(t *testing.T) {
Expand Down

0 comments on commit 05c8b02

Please sign in to comment.