Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improved "containers/{name}/wait" endpoint #10271

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions pkg/api/handlers/utils/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/containers/podman/v3/libpod/events"
"github.com/containers/podman/v3/pkg/domain/entities"
"github.com/containers/podman/v3/pkg/domain/infra/abi"

Expand Down Expand Up @@ -175,7 +176,7 @@ func waitDockerCondition(ctx context.Context, containerName string, interval tim
var code int32
switch dockerCondition {
case "next-exit":
code, err = waitNextExit(containerWait)
code, err = waitNextExit(ctx, containerName)
case "removed":
code, err = waitRemoved(containerWait)
case "not-running", "":
Expand All @@ -202,12 +203,32 @@ func waitRemoved(ctrWait containerWaitFn) (int32, error) {
return code, err
}

func waitNextExit(ctrWait containerWaitFn) (int32, error) {
_, err := ctrWait(define.ContainerStateRunning)
if err != nil {
return -1, err
}
return ctrWait(notRunningStates...)
func waitNextExit(ctx context.Context, containerName string) (int32, error) {
runtime := ctx.Value("runtime").(*libpod.Runtime)
containerEngine := &abi.ContainerEngine{Libpod: runtime}
eventChannel := make(chan *events.Event)
errChannel := make(chan error)
matejvasek marked this conversation as resolved.
Show resolved Hide resolved
opts := entities.EventsOptions{
EventChan: eventChannel,
Filter: []string{"event=died", fmt.Sprintf("container=%s", containerName)},
Stream: true,
}

// ctx is used to cancel event watching goroutine
ctx, cancel := context.WithCancel(ctx)
matejvasek marked this conversation as resolved.
Show resolved Hide resolved
defer cancel()
go func() {
errChannel <- containerEngine.Events(ctx, opts)
}()

evt, ok := <-eventChannel
if ok {
return int32(evt.ContainerExitCode), nil
}
// if ok == false then containerEngine.Events() has exited
// it may happen if request was canceled (e.g. client closed connection prematurely) or
// the server is in process of shutting down
return -1, <-errChannel
vrothberg marked this conversation as resolved.
Show resolved Hide resolved
}

func waitNotRunning(ctrWait containerWaitFn) (int32, error) {
Expand Down
2 changes: 1 addition & 1 deletion test/apiv2/26-containersWait.at
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CTR="WaitTestingCtr"

t POST "containers/nonExistent/wait?condition=next-exit" 404

podman create --name "${CTR}" --entrypoint '["sleep", "0.5"]' "${IMAGE}"
podman create --name "${CTR}" --entrypoint '["true"]' "${IMAGE}"

t POST "containers/${CTR}/wait?condition=non-existent-cond" 400

Expand Down