From 4eff0c8cf284a6007122aec731e4d97059750166 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 13 Apr 2022 16:21:21 +0200 Subject: pod: add exit policies Add the notion of an "exit policy" to a pod. This policy controls the behaviour when the last container of pod exits. Initially, there are two policies: - "continue" : the pod continues running. This is the default policy when creating a pod. - "stop" : stop the pod when the last container exits. This is the default behaviour for `play kube`. In order to implement the deferred stop of a pod, add a worker queue to the libpod runtime. The queue will pick up work items and in this case helps resolve dead locks that would otherwise occur if we attempted to stop a pod during container cleanup. Note that the default restart policy of `play kube` is "Always". Hence, in order to really solve #13464, the YAML files must set a custom restart policy; the tests use "OnFailure". Fixes: #13464 Signed-off-by: Valentin Rothberg --- test/system/200-pod.bats | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'test/system') diff --git a/test/system/200-pod.bats b/test/system/200-pod.bats index 64f95f723..39982848f 100644 --- a/test/system/200-pod.bats +++ b/test/system/200-pod.bats @@ -406,7 +406,76 @@ EOF run_podman pod inspect test --format {{.InfraConfig.HostNetwork}} is "$output" "true" "Host network sharing with only ipc should be true" run_podman pod rm test +} + +# Wait for the pod (1st arg) to transition into the state (2nd arg) +function _ensure_pod_state() { + for i in {0..5}; do + run_podman pod inspect $1 --format "{{.State}}" + if [[ $output == "$2" ]]; then + break + fi + sleep 0.5 + done + + is "$output" "$2" "unexpected pod state" +} + +@test "pod exit policies" { + # Test setting exit policies + run_podman pod create + podID="$output" + run_podman pod inspect $podID --format "{{.ExitPolicy}}" + is "$output" "continue" "default exit policy" + run_podman pod rm $podID + + run_podman pod create --exit-policy stop + podID="$output" + run_podman pod inspect $podID --format "{{.ExitPolicy}}" + is "$output" "stop" "custom exit policy" + run_podman pod rm $podID + + run_podman 125 pod create --exit-policy invalid + is "$output" "Error: .*error running pod create option: invalid pod exit policy: \"invalid\"" "invalid exit policy" + + # Test exit-policy behaviour + run_podman pod create --exit-policy continue + podID="$output" + run_podman run --pod $podID $IMAGE true + run_podman pod inspect $podID --format "{{.State}}" + _ensure_pod_state $podID Degraded + run_podman pod rm $podID + + run_podman pod create --exit-policy stop + podID="$output" + run_podman run --pod $podID $IMAGE true + run_podman pod inspect $podID --format "{{.State}}" + _ensure_pod_state $podID Exited + run_podman pod rm $podID +} +@test "pod exit policies - play kube" { + # play-kube sets the exit policy to "stop" + local name="$(random_string 10 | tr A-Z a-z)" + + kubeFile="apiVersion: v1 +kind: Pod +metadata: + name: $name-pod +spec: + containers: + - command: + - \"true\" + image: $IMAGE + name: ctr + restartPolicy: OnFailure" + + echo "$kubeFile" > $PODMAN_TMPDIR/test.yaml + run_podman play kube $PODMAN_TMPDIR/test.yaml + run_podman pod inspect $name-pod --format "{{.ExitPolicy}}" + is "$output" "stop" "custom exit policy" + _ensure_pod_state $name-pod Exited + run_podman pod rm $name-pod } # vim: filetype=sh -- cgit v1.2.3-54-g00ecf