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 23, 2018
1 parent 90f165a commit 70581d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ func (c *CriManager) ListPodSandbox(ctx context.Context, r *runtime.ListPodSandb
}

// CreateContainer creates a new container in the given PodSandbox.
func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateContainerRequest) (*runtime.CreateContainerResponse, error) {
func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateContainerRequest) (_ *runtime.CreateContainerResponse, retErr error) {
config := r.GetConfig()
sandboxConfig := r.GetSandboxConfig()
podSandboxID := r.GetPodSandboxId()
Expand Down Expand Up @@ -550,6 +550,16 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta

containerID := createResp.ID

defer func() {
// If the container failed to be created, clean up the container.
if retErr != 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
12 changes: 11 additions & 1 deletion cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ func (c *CriManager) ListPodSandbox(ctx context.Context, r *runtime.ListPodSandb
}

// CreateContainer creates a new container in the given PodSandbox.
func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateContainerRequest) (*runtime.CreateContainerResponse, error) {
func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateContainerRequest) (_ *runtime.CreateContainerResponse, retErr error) {
config := r.GetConfig()
sandboxConfig := r.GetSandboxConfig()
podSandboxID := r.GetPodSandboxId()
Expand Down Expand Up @@ -566,6 +566,16 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta

containerID := createResp.ID

defer func() {
// If the container failed to be created, clean up the container.
if retErr != 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 70581d8

Please sign in to comment.