Skip to content

Commit

Permalink
Improved StopTask in case that container was lost outside nomad (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
towe75 committed Jun 14, 2021
1 parent 3298265 commit c2cb63f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions api/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (c *API) ContainerInspect(ctx context.Context, name string) (InspectContain

defer res.Body.Close()

if res.StatusCode == http.StatusNotFound {
return inspectData, ContainerNotFound
}

if res.StatusCode != http.StatusOK {
return inspectData, fmt.Errorf("unknown error, status code: %d", res.StatusCode)
}
Expand Down
8 changes: 6 additions & 2 deletions api/container_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import (
// timeout value. The timeout value the time before a forcible stop to the container is applied.
// If the container cannot be found, a [ContainerNotFound](#ContainerNotFound)
// error will be returned instead.
func (c *API) ContainerStop(ctx context.Context, name string, timeout int) error {
func (c *API) ContainerStop(ctx context.Context, name string, timeout int, ignoreStopped bool) error {

res, err := c.Post(ctx, fmt.Sprintf("/v1.0.0/libpod/containers/%s/stop?timeout=%d", name, timeout), nil)
res, err := c.Post(ctx, fmt.Sprintf("/v1.0.0/libpod/containers/%s/stop?timeout=%d&ignore=%t", name, timeout, ignoreStopped), nil)
if err != nil {
return err
}

defer res.Body.Close()

if res.StatusCode == http.StatusNotFound {
return ContainerNotFound
}

if res.StatusCode == http.StatusNoContent {
return nil
}
Expand Down
15 changes: 10 additions & 5 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ func (d *Driver) createImage(image string, auth *AuthConfig, forcePull bool) (st
d.logger.Warn("Unable to check for local image", "image", imageName, "err", err)
}
if !forcePull && imageID != "" {
d.logger.Info("Found imageID", imageID, "for image", imageName, "in local storage")
d.logger.Debug("Found imageID", imageID, "for image", imageName, "in local storage")
return imageID, nil
}

Expand Down Expand Up @@ -725,13 +725,18 @@ func (d *Driver) StopTask(taskID string, timeout time.Duration, signal string) e
if !ok {
return drivers.ErrTaskNotFound
}

// fixme send proper signal to container
err := d.podman.ContainerStop(d.ctx, handle.containerID, int(timeout.Seconds()))
if err != nil {
err := d.podman.ContainerStop(d.ctx, handle.containerID, int(timeout.Seconds()), true)
if err == nil {
return nil
} else if err == api.ContainerNotFound {
d.logger.Debug("Container not found while we wanted to stop it", "task", taskID, "container", handle.containerID, "err", err)
return nil
} else {
d.logger.Error("Could not stop/kill container", "containerID", handle.containerID, "err", err)
return err
}
return nil
}

// DestroyTask function cleans up and removes a task that has terminated.
Expand All @@ -749,7 +754,7 @@ func (d *Driver) DestroyTask(taskID string, force bool) error {
if handle.isRunning() {
d.logger.Debug("Have to destroyTask but container is still running", "containerID", handle.containerID)
// we can not do anything, so catching the error is useless
err := d.podman.ContainerStop(d.ctx, handle.containerID, 60)
err := d.podman.ContainerStop(d.ctx, handle.containerID, 60, true)
if err != nil {
d.logger.Warn("failed to stop/kill container during destroy", "error", err)
}
Expand Down
2 changes: 1 addition & 1 deletion driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ func readLogfile(t *testing.T, task *drivers.TaskConfig) string {

func newTaskConfig(image string, command []string) TaskConfig {
if len(image) == 0 {
image = "docker://docker.io/library/busybox:latest"
image = "docker.io/library/busybox:latest"
}

return TaskConfig{
Expand Down

0 comments on commit c2cb63f

Please sign in to comment.