Skip to content

Commit

Permalink
bugfix: clean up the container if the creation fails
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Aug 28, 2018
1 parent 90f165a commit 1776bab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,24 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta

containerName := makeContainerName(sandboxConfig, config)

createResp, err := c.ContainerMgr.Create(ctx, containerName, createConfig)
if err != nil {
var createErr error
createResp, createErr := c.ContainerMgr.Create(ctx, containerName, createConfig)
if createErr != nil {
return nil, fmt.Errorf("failed to create container for sandbox %q: %v", podSandboxID, err)
}

containerID := createResp.ID

defer func() {
// If the container failed to be created, clean up the container.
if createErr != nil {
err := c.ContainerMgr.Remove(ctx, containerID, &apitypes.ContainerRemoveOptions{Volumes: true, Force: true})
if err != nil {
logrus.Errorf("failed to remove the container when creating container failed: %v", err)
}
}
}()

// Get container log.
if config.GetLogPath() != "" {
logPath := filepath.Join(sandboxConfig.GetLogDirectory(), config.GetLogPath())
Expand Down
15 changes: 13 additions & 2 deletions cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,13 +559,24 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta

containerName := makeContainerName(sandboxConfig, config)

createResp, err := c.ContainerMgr.Create(ctx, containerName, createConfig)
if err != nil {
var createErr error
createResp, createErr := c.ContainerMgr.Create(ctx, containerName, createConfig)
if createErr != nil {
return nil, fmt.Errorf("failed to create container for sandbox %q: %v", podSandboxID, err)
}

containerID := createResp.ID

defer func() {
// If the container failed to be created, clean up the container.
if createErr != nil {
err := c.ContainerMgr.Remove(ctx, containerID, &apitypes.ContainerRemoveOptions{Volumes: true, Force: true})
if err != nil {
logrus.Errorf("failed to remove the container when creating container failed: %v", err)
}
}
}()

// Get container log.
if config.GetLogPath() != "" {
logPath := filepath.Join(sandboxConfig.GetLogDirectory(), config.GetLogPath())
Expand Down

0 comments on commit 1776bab

Please sign in to comment.