From 8e9248272acf613af2664207b08c7ff10223c561 Mon Sep 17 00:00:00 2001 From: Lars Francke Date: Tue, 1 Dec 2026 19:34:03 +0100 Subject: [PATCH 1/2] fix(operator): Let capture_shell_output and the Vector shutdown trap share one EXIT trap --- crates/stackable-operator/CHANGELOG.md | 5 +- ..._tests__multiple_git_syncs_containers.snap | 39 ++- ...s__multiple_git_syncs_init_containers.snap | 39 ++- ...l__tests__git_sync_ca_cert_containers.snap | 13 +- ...sts__git_sync_ca_cert_init_containers.snap | 13 +- ..._impl__tests__git_sync_ssh_containers.snap | 13 +- ...__tests__git_sync_ssh_init_containers.snap | 13 +- ..._tests__multiple_git_syncs_containers.snap | 39 ++- ...s__multiple_git_syncs_init_containers.snap | 39 ++- .../src/product_logging/framework.rs | 244 +++++++++++++++++- 10 files changed, 428 insertions(+), 29 deletions(-) diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index d47145e7b..024a3d8e0 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -45,8 +45,9 @@ All notable changes to this project will be documented in this file. - `product_logging::framework::capture_shell_output` no longer loses the output of short-lived containers such as init containers. When stdout or stderr go to the console and to a file, the generated command now sets an EXIT trap that waits up to five seconds for its `tee` processes. - Callers must not set their own EXIT trap. With the trap, Bash handles SIGTERM even as PID 1, so a - container without a TERM trap stops at once on Pod deletion instead of running until the + Callers must not set their own EXIT trap. `manage_vector_shutdown_file_command` and + `remove_vector_shutdown_file_command` share it. With the trap, Bash handles SIGTERM even as PID 1, + so a container without a TERM trap stops at once on Pod deletion instead of running until the termination grace period expires ([#1336]). ### Deprecated diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap index 9e98acbf1..c91356eed 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap @@ -4,6 +4,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe --- - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -17,7 +26,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe stackable_tee_pids[/stackable/log/git-sync-0/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-0/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } prepare_signal_handlers() { @@ -89,6 +100,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe name: extra-volume - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -102,7 +122,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe stackable_tee_pids[/stackable/log/git-sync-1/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-1/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-1/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } prepare_signal_handlers() { @@ -179,6 +201,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe name: extra-volume - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -192,7 +223,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe stackable_tee_pids[/stackable/log/git-sync-2/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-2/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-2/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } prepare_signal_handlers() { diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap index d85323c3e..3eb7db272 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap @@ -4,6 +4,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) --- - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -17,7 +26,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) stackable_tee_pids[/stackable/log/git-sync-0-init/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-0-init/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git' --link=current --one-time=true --period=20s --ref=main --repo=https://github.com/stackabletech/repo1 --root=/tmp/git command: - /bin/bash @@ -49,6 +60,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) name: extra-volume - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -62,7 +82,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) stackable_tee_pids[/stackable/log/git-sync-1-init/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-1-init/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-1-init/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=true --period=60s --ref=trunk --repo=https://github.com/stackabletech/repo2 --rev=HEAD --root=/tmp/git command: - /bin/bash @@ -99,6 +121,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) name: extra-volume - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -112,7 +143,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) stackable_tee_pids[/stackable/log/git-sync-2-init/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-2-init/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-2-init/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git,key:value,safe.directory:/safe-dir' --link=current --one-time=true --period=20s --ref=feat/git-sync --repo=https://github.com/stackabletech/repo3 --root=/tmp/git command: - /bin/bash diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap index a7e9b5de5..396e720d9 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap @@ -4,6 +4,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe --- - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -17,7 +26,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe stackable_tee_pids[/stackable/log/git-sync-0/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-0/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } prepare_signal_handlers() { diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap index 1d349aa78..5ee57e68c 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap @@ -4,6 +4,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) --- - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -17,7 +26,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) stackable_tee_pids[/stackable/log/git-sync-0-init/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-0-init/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } /stackable/git-sync --depth=3 --git-config='http.sslCAInfo:/stackable/gitca-0/ca.crt,safe.directory:/tmp/git' --link=current --one-time=true --period=60s --ref=trunk --repo=ssh://git@github.com/stackabletech/repo.git --rev=HEAD --root=/tmp/git command: - /bin/bash diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap index 45bff4240..1b51c0b87 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap @@ -4,6 +4,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe --- - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -17,7 +26,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe stackable_tee_pids[/stackable/log/git-sync-0/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-0/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } prepare_signal_handlers() { diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap index 57453cd59..d22fab852 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap @@ -4,6 +4,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) --- - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -17,7 +26,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) stackable_tee_pids[/stackable/log/git-sync-0-init/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-0-init/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=true --period=60s --ref=trunk --repo=ssh://git@github.com/stackabletech/repo.git --rev=HEAD --root=/tmp/git command: - /bin/bash diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap index 624508786..84dc9590e 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap @@ -4,6 +4,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe --- - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -17,7 +26,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe stackable_tee_pids[/stackable/log/git-sync-0/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-0/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } prepare_signal_handlers() { @@ -89,6 +100,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe name: extra-volume - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -102,7 +122,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe stackable_tee_pids[/stackable/log/git-sync-1/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-1/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-1/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } prepare_signal_handlers() { @@ -179,6 +201,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe name: extra-volume - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -192,7 +223,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe stackable_tee_pids[/stackable/log/git-sync-2/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-2/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-2/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } prepare_signal_handlers() { diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap index 64ba69b04..fc6e90343 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap @@ -4,6 +4,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) --- - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -17,7 +26,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) stackable_tee_pids[/stackable/log/git-sync-0-init/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-0-init/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git' --link=current --one-time=true --period=20s --ref=main --repo=https://github.com/stackabletech/repo1 --root=/tmp/git command: - /bin/bash @@ -49,6 +60,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) name: extra-volume - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -62,7 +82,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) stackable_tee_pids[/stackable/log/git-sync-1-init/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-1-init/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-1-init/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=true --period=60s --ref=trunk --repo=https://github.com/stackabletech/repo2 --rev=HEAD --root=/tmp/git command: - /bin/bash @@ -99,6 +121,15 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) name: extra-volume - args: - |- + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -112,7 +143,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) stackable_tee_pids[/stackable/log/git-sync-2-init/container.stdout.log]=$! && exec 2> >(tee /stackable/log/git-sync-2-init/container.stderr.log >&2) && stackable_tee_pids[/stackable/log/git-sync-2-init/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + } /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git,key:value,safe.directory:/safe-dir' --link=current --one-time=true --period=20s --ref=feat/git-sync --repo=https://github.com/stackabletech/repo3 --root=/tmp/git command: - /bin/bash diff --git a/crates/stackable-operator/src/product_logging/framework.rs b/crates/stackable-operator/src/product_logging/framework.rs index 4cb0b0d2e..5bdb292d4 100644 --- a/crates/stackable-operator/src/product_logging/framework.rs +++ b/crates/stackable-operator/src/product_logging/framework.rs @@ -120,6 +120,25 @@ pub fn calculate_log_volume_size_limit(max_log_files_size: &[MemoryQuantity]) -> log_volume_size_limit.into() } +/// Runs the EXIT hooks of [`capture_shell_output`] and [`manage_vector_shutdown_file_command`]. +/// +/// Both commands set the same EXIT trap to this function, so they can be combined in one script +/// in any order. The `tee` processes are waited for first, so the log files are complete before +/// Vector is told to shut down. +/// +/// Each hook only runs in the shell that registered it. A subshell inherits the variables and +/// functions. If one of the two commands runs in a subshell, the trap it installs there would +/// otherwise also run the other command's hook when the subshell exits, for example shut down +/// Vector too early. +const RUN_EXIT_HOOKS_FUNCTION: &str = r#"stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == "$BASHPID" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == "$BASHPID" ]]; then + stackable_create_vector_shutdown_file + fi +}"#; + /// Waits for the `tee` processes in `stackable_tee_pids`, at most for five seconds. /// /// Redirecting to /dev/null closes the shell's ends of the pipes, so tee reads EOF once no child @@ -149,15 +168,16 @@ stackable_wait_for_tee() { /// background process which still has stdout or stderr open delays the exit by that long. This /// has the following consequences for the caller: /// -/// - The caller must not set its own EXIT trap, because it would replace this one. That includes -/// the trap of [`manage_vector_shutdown_file_command`] and [`remove_vector_shutdown_file_command`]. +/// - The caller must not set its own EXIT trap, because it would replace this one. The trap is +/// shared with [`manage_vector_shutdown_file_command`] and [`remove_vector_shutdown_file_command`], +/// so these can be combined with this command in any order. /// - With the EXIT trap, Bash handles SIGTERM even as PID 1 of the container. A SIGTERM that no /// TERM trap handles, for example one before `prepare_signal_handlers` of /// [`COMMON_BASH_TRAP_FUNCTIONS`](crate::utils::COMMON_BASH_TRAP_FUNCTIONS), exits the script /// with status `143` at once instead of being ignored until the termination grace period /// expires. Commands still running at that point are killed with the container. -/// - The shell variable `stackable_tee_pids` and the function `stackable_wait_for_tee` are -/// reserved. +/// - The shell variables `stackable_tee_pids` and `stackable_wait_for_tee_shell` and the +/// functions `stackable_wait_for_tee` and `stackable_run_exit_hooks` are reserved. /// - A caller that ends with `exec` replaces Bash, so the trap does not run. That is fine for a /// long-running process, but a short-lived one can lose the end of its output. /// @@ -269,7 +289,8 @@ pub fn capture_shell_output( } if uses_tee { - commands.push("trap stackable_wait_for_tee EXIT".into()); + commands.push("stackable_wait_for_tee_shell=$BASHPID".into()); + commands.push("trap stackable_run_exit_hooks EXIT".into()); } if commands.is_empty() { @@ -281,9 +302,13 @@ pub fn capture_shell_output( let commands = commands.join(" &&\n "); if uses_tee { + // The output has several commands on separate lines. The group makes a `&&` before or + // after it apply to all of them. // `-g`, because a caller can run this command inside a function, and the EXIT trap needs // the array after that function has returned. - format!("{WAIT_FOR_TEE_FUNCTION}\ndeclare -gA stackable_tee_pids\n{commands}") + format!( + "{{\n{RUN_EXIT_HOOKS_FUNCTION}\n{WAIT_FOR_TEE_FUNCTION}\ndeclare -gA stackable_tee_pids\n{commands}\n}}" + ) } else { commands } @@ -1585,11 +1610,20 @@ touch {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE}" /// The command must be executed in the top-level shell of the script which owns the lifetime of /// the Vector container. In a subshell, the trap would fire when the subshell exits and shut down /// Vector too early. The script must not install another `EXIT` trap, because it would replace -/// this one. +/// this one. The trap is shared with [`capture_shell_output`], so both can be combined in one +/// script in any order. The shell variable `stackable_vector_shutdown_shell` and the functions +/// `stackable_create_vector_shutdown_file` and `stackable_run_exit_hooks` are reserved. pub fn manage_vector_shutdown_file_command(stackable_log_dir: &str) -> String { format!( - "rm -f {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE} && \ -trap '{create_shutdown_file_command}' EXIT", + "{{ +{RUN_EXIT_HOOKS_FUNCTION} +stackable_create_vector_shutdown_file() {{ + {create_shutdown_file_command} +}} +rm -f {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE} && + stackable_vector_shutdown_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT +}}", create_shutdown_file_command = create_vector_shutdown_file_command(stackable_log_dir), ) } @@ -1756,6 +1790,15 @@ mod tests { LogLevel::INFO, LogLevel::INFO, indoc! {" + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == \"$BASHPID\" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == \"$BASHPID\" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -1769,12 +1812,23 @@ mod tests { stackable_tee_pids[/log/test/container.stdout.log]=$! && exec 2> >(tee /log/test/container.stderr.log >&2) && stackable_tee_pids[/log/test/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT"} + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + }"} )] #[case::console_and_file_only_for_errors( LogLevel::INFO, LogLevel::ERROR, indoc! {" + { + stackable_run_exit_hooks() { + if [[ ${stackable_wait_for_tee_shell-} == \"$BASHPID\" ]]; then + stackable_wait_for_tee + fi + if [[ ${stackable_vector_shutdown_shell-} == \"$BASHPID\" ]]; then + stackable_create_vector_shutdown_file + fi + } stackable_wait_for_tee() { exec > /dev/null 2>&1 set +e @@ -1786,7 +1840,9 @@ mod tests { mkdir --parents /log/test && exec 2> >(tee /log/test/container.stderr.log >&2) && stackable_tee_pids[/log/test/container.stderr.log]=$! && - trap stackable_wait_for_tee EXIT"} + stackable_wait_for_tee_shell=$BASHPID && + trap stackable_run_exit_hooks EXIT + }"} )] #[case::file_only( LogLevel::NONE, @@ -1912,6 +1968,172 @@ mod tests { ); } + struct RunWithBothHooks { + status: std::process::ExitStatus, + duration: std::time::Duration, + log_dir: tempfile::TempDir, + } + + impl RunWithBothHooks { + fn stdout_log_path(&self) -> std::path::PathBuf { + self.log_dir + .path() + .join("test") + .join("container.stdout.log") + } + + fn stdout_log(&self) -> String { + std::fs::read_to_string(self.stdout_log_path()).unwrap_or_default() + } + + fn shutdown_file_path(&self) -> std::path::PathBuf { + self.log_dir.path().join(VECTOR_LOG_DIR).join(SHUTDOWN_FILE) + } + } + + /// Runs the script that `build_script` builds from the output capturing command, the Vector + /// shutdown file command and the log directory, like a product container does. + fn run_with_both_hooks( + build_script: impl FnOnce(&str, &str, &str) -> String, + ) -> RunWithBothHooks { + let log_dir = tempfile::tempdir().expect("a temporary directory can be created"); + let log_dir_path = log_dir + .path() + .to_str() + .expect("the temporary directory path is valid UTF-8") + .to_owned(); + let capture = capture_shell_output( + &log_dir_path, + "test", + &shell_log_config(LogLevel::INFO, LogLevel::INFO), + ); + let manage = manage_vector_shutdown_file_command(&log_dir_path); + + let start = std::time::Instant::now(); + let status = std::process::Command::new("bash") + .args([ + "-euo", + "pipefail", + "-c", + &build_script(&capture, &manage, &log_dir_path), + ]) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status() + .expect("bash can be started"); + + RunWithBothHooks { + status, + duration: start.elapsed(), + log_dir, + } + } + + #[rstest] + #[case::capture_first(true)] + #[case::vector_first(false)] + fn both_exit_hooks_run_in_any_order(#[case] capture_first: bool) { + let run = run_with_both_hooks(|capture, manage, _| { + let (first, second) = if capture_first { + (capture, manage) + } else { + (manage, capture) + }; + format!("{first}\n{second}\necho output\nexit 3") + }); + + assert_eq!(Some(3), run.status.code()); + assert_eq!("output\n", run.stdout_log()); + assert!(run.shutdown_file_path().exists()); + } + + #[test] + fn vector_shutdown_file_is_created_after_the_output_is_captured() { + // The background process keeps the pipes to tee open for a second after the script ends. + let run = run_with_both_hooks(|capture, manage, _| { + format!("{manage}\n{capture}\n(sleep 1; echo late) &\necho output") + }); + + assert!(run.status.success()); + assert_eq!("output\nlate\n", run.stdout_log()); + let modified = |path: std::path::PathBuf| { + std::fs::metadata(path) + .and_then(|metadata| metadata.modified()) + .expect("the file exists") + }; + assert!(modified(run.shutdown_file_path()) >= modified(run.stdout_log_path())); + } + + #[test] + fn output_capturing_in_a_subshell_does_not_shut_down_vector() { + let run = run_with_both_hooks(|capture, manage, log_dir| { + formatdoc! {" + {manage} + ( + {capture} + echo inner + ) + [ -e {log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE} ] && echo early > {log_dir}/early + echo output" + } + }); + + assert!(run.status.success()); + assert!(!run.log_dir.path().join("early").exists()); + assert!(run.shutdown_file_path().exists()); + } + + #[test] + fn both_exit_hooks_run_with_output_capturing_inside_a_function() { + let run = run_with_both_hooks(|capture, manage, _| { + format!("{manage}\nstart_capture() {{\n{capture}\n}}\nstart_capture\necho output") + }); + + assert!(run.status.success()); + assert!(run.duration < std::time::Duration::from_secs(4)); + assert_eq!("output\n", run.stdout_log()); + assert!(run.shutdown_file_path().exists()); + } + + #[rstest] + #[case::output_capturing(true)] + #[case::vector_shutdown(false)] + fn whole_command_is_skipped_when_chained_after_a_failure(#[case] output_capturing: bool) { + let log_dir = tempfile::tempdir().expect("a temporary directory can be created"); + let log_dir_path = log_dir + .path() + .to_str() + .expect("the temporary directory path is valid UTF-8"); + let (command, created_path) = if output_capturing { + ( + capture_shell_output( + log_dir_path, + "test", + &shell_log_config(LogLevel::INFO, LogLevel::INFO), + ), + log_dir.path().join("test"), + ) + } else { + ( + manage_vector_shutdown_file_command(log_dir_path), + log_dir.path().join(VECTOR_LOG_DIR), + ) + }; + + let output = std::process::Command::new("bash") + .args(["-c", &format!("false && {command}\necho after")]) + .stdin(std::process::Stdio::null()) + .output() + .expect("bash can be started"); + + let stderr = String::from_utf8_lossy(&output.stderr); + assert_eq!(Some(0), output.status.code(), "stderr: {stderr}"); + assert_eq!("after\n", String::from_utf8_lossy(&output.stdout)); + assert!(!stderr.contains("command not found"), "stderr: {stderr}"); + assert!(!created_path.exists()); + } + #[test] fn log4j2_config() { let log_config = AutomaticContainerLogConfig { From 1c9af323a07a5130ef054260a731a6c84985e8df Mon Sep 17 00:00:00 2001 From: Lars Francke Date: Fri, 4 Dec 2026 14:28:59 +0100 Subject: [PATCH 2/2] fix(operator): Defer SIGTERM until prepare_signal_handlers in scripts using the EXIT hooks --- crates/stackable-operator/CHANGELOG.md | 13 +- ..._tests__multiple_git_syncs_containers.snap | 21 +++ ...s__multiple_git_syncs_init_containers.snap | 12 ++ ...l__tests__git_sync_ca_cert_containers.snap | 7 + ...sts__git_sync_ca_cert_init_containers.snap | 4 + ..._impl__tests__git_sync_ssh_containers.snap | 7 + ...__tests__git_sync_ssh_init_containers.snap | 4 + ..._tests__multiple_git_syncs_containers.snap | 21 +++ ...s__multiple_git_syncs_init_containers.snap | 12 ++ .../src/product_logging/framework.rs | 133 ++++++++++++++++-- crates/stackable-operator/src/utils/bash.rs | 30 +++- 11 files changed, 236 insertions(+), 28 deletions(-) diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index 024a3d8e0..6931c8459 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -16,9 +16,11 @@ All notable changes to this project will be documented in this file. - Bump `kube` to `5.2.0`, which fixes RUSTSEC-2026-0600 ([#1340]). - `remove_vector_shutdown_file_command` now also installs the `EXIT` trap of `manage_vector_shutdown_file_command`, replacing any `EXIT` trap the script set before ([#1265]). -- With the `EXIT` trap of `manage_vector_shutdown_file_command`, a SIGTERM before `prepare_signal_handlers` - exits the script with status `143` at once and kills the commands still running, e.g. a database - migration. Before, the script ignored it as PID 1 until the termination grace period expired ([#1265]). +- A SIGTERM before `prepare_signal_handlers` still lets the running command finish, e.g. a database + migration, but `prepare_signal_handlers` then exits the script with status `143` instead of starting + the product. This applies to scripts using `manage_vector_shutdown_file_command` or + `capture_shell_output`. Before, the script ignored the SIGTERM as PID 1 and started the product + ([#1265], [#1345]). ### Fixed @@ -46,9 +48,7 @@ All notable changes to this project will be documented in this file. containers such as init containers. When stdout or stderr go to the console and to a file, the generated command now sets an EXIT trap that waits up to five seconds for its `tee` processes. Callers must not set their own EXIT trap. `manage_vector_shutdown_file_command` and - `remove_vector_shutdown_file_command` share it. With the trap, Bash handles SIGTERM even as PID 1, - so a container without a TERM trap stops at once on Pod deletion instead of running until the - termination grace period expires ([#1336]). + `remove_vector_shutdown_file_command` share it ([#1336]). ### Deprecated @@ -67,6 +67,7 @@ All notable changes to this project will be documented in this file. [#1339]: https://github.com/stackabletech/operator-rs/pull/1339 [#1340]: https://github.com/stackabletech/operator-rs/pull/1340 [#1341]: https://github.com/stackabletech/operator-rs/pull/1341 +[#1345]: https://github.com/stackabletech/operator-rs/pull/1345 ## [0.122.0] - 2026-11-03 diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap index c91356eed..c790ed50c 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap @@ -21,6 +21,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-0/container.stdout.log]=$! && @@ -34,6 +38,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { unset term_child_pid trap 'handle_term_signal' TERM + if [[ -v stackable_sigterm_received ]]; then + exit 143 + fi } handle_term_signal() @@ -117,6 +124,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-1 && exec > >(tee /stackable/log/git-sync-1/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-1/container.stdout.log]=$! && @@ -130,6 +141,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { unset term_child_pid trap 'handle_term_signal' TERM + if [[ -v stackable_sigterm_received ]]; then + exit 143 + fi } handle_term_signal() @@ -218,6 +232,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-2 && exec > >(tee /stackable/log/git-sync-2/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-2/container.stdout.log]=$! && @@ -231,6 +249,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { unset term_child_pid trap 'handle_term_signal' TERM + if [[ -v stackable_sigterm_received ]]; then + exit 143 + fi } handle_term_signal() diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap index 3eb7db272..675c0d036 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap @@ -21,6 +21,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-0-init/container.stdout.log]=$! && @@ -77,6 +81,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-1-init && exec > >(tee /stackable/log/git-sync-1-init/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-1-init/container.stdout.log]=$! && @@ -138,6 +146,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-2-init && exec > >(tee /stackable/log/git-sync-2-init/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-2-init/container.stdout.log]=$! && diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap index 396e720d9..e7ed85af3 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap @@ -21,6 +21,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-0/container.stdout.log]=$! && @@ -34,6 +38,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { unset term_child_pid trap 'handle_term_signal' TERM + if [[ -v stackable_sigterm_received ]]; then + exit 143 + fi } handle_term_signal() diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap index 5ee57e68c..cd71ee97e 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap @@ -21,6 +21,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-0-init/container.stdout.log]=$! && diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap index 1b51c0b87..7e94dd72b 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap @@ -21,6 +21,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-0/container.stdout.log]=$! && @@ -34,6 +38,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { unset term_child_pid trap 'handle_term_signal' TERM + if [[ -v stackable_sigterm_received ]]; then + exit 143 + fi } handle_term_signal() diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap index d22fab852..ca2a13de5 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap @@ -21,6 +21,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-0-init/container.stdout.log]=$! && diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap index 84dc9590e..80067b00e 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap @@ -21,6 +21,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-0/container.stdout.log]=$! && @@ -34,6 +38,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { unset term_child_pid trap 'handle_term_signal' TERM + if [[ -v stackable_sigterm_received ]]; then + exit 143 + fi } handle_term_signal() @@ -117,6 +124,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-1 && exec > >(tee /stackable/log/git-sync-1/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-1/container.stdout.log]=$! && @@ -130,6 +141,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { unset term_child_pid trap 'handle_term_signal' TERM + if [[ -v stackable_sigterm_received ]]; then + exit 143 + fi } handle_term_signal() @@ -218,6 +232,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-2 && exec > >(tee /stackable/log/git-sync-2/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-2/container.stdout.log]=$! && @@ -231,6 +249,9 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).expe { unset term_child_pid trap 'handle_term_signal' TERM + if [[ -v stackable_sigterm_received ]]; then + exit 143 + fi } handle_term_signal() diff --git a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap index fc6e90343..be68de182 100644 --- a/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap @@ -21,6 +21,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-0-init/container.stdout.log]=$! && @@ -77,6 +81,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-1-init && exec > >(tee /stackable/log/git-sync-1-init/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-1-init/container.stdout.log]=$! && @@ -138,6 +146,10 @@ expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers) kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /stackable/log/git-sync-2-init && exec > >(tee /stackable/log/git-sync-2-init/container.stdout.log) && stackable_tee_pids[/stackable/log/git-sync-2-init/container.stdout.log]=$! && diff --git a/crates/stackable-operator/src/product_logging/framework.rs b/crates/stackable-operator/src/product_logging/framework.rs index 5bdb292d4..5c43003f1 100644 --- a/crates/stackable-operator/src/product_logging/framework.rs +++ b/crates/stackable-operator/src/product_logging/framework.rs @@ -139,6 +139,19 @@ const RUN_EXIT_HOOKS_FUNCTION: &str = r#"stackable_run_exit_hooks() { fi }"#; +/// Records a SIGTERM which arrives before the script installs its own TERM trap. +/// +/// Once a script has an EXIT trap, Bash handles SIGTERM even as PID 1 of the container. Without a +/// TERM trap, it then exits at once and the running command is killed with the container, for +/// example a NameNode format in an init container. Bash runs a trapped signal's handler only after +/// the running foreground command ends, so the command completes, and `prepare_signal_handlers` of +/// [`COMMON_BASH_TRAP_FUNCTIONS`](crate::utils::COMMON_BASH_TRAP_FUNCTIONS) exits with status +/// `143` instead of starting the product. A TERM trap the script installed before is kept. +const RECORD_SIGTERM: &str = r"stackable_record_sigterm() { + stackable_sigterm_received=1 +} +[[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM"; + /// Waits for the `tee` processes in `stackable_tee_pids`, at most for five seconds. /// /// Redirecting to /dev/null closes the shell's ends of the pipes, so tee reads EOF once no child @@ -171,13 +184,14 @@ stackable_wait_for_tee() { /// - The caller must not set its own EXIT trap, because it would replace this one. The trap is /// shared with [`manage_vector_shutdown_file_command`] and [`remove_vector_shutdown_file_command`], /// so these can be combined with this command in any order. -/// - With the EXIT trap, Bash handles SIGTERM even as PID 1 of the container. A SIGTERM that no -/// TERM trap handles, for example one before `prepare_signal_handlers` of -/// [`COMMON_BASH_TRAP_FUNCTIONS`](crate::utils::COMMON_BASH_TRAP_FUNCTIONS), exits the script -/// with status `143` at once instead of being ignored until the termination grace period -/// expires. Commands still running at that point are killed with the container. -/// - The shell variables `stackable_tee_pids` and `stackable_wait_for_tee_shell` and the -/// functions `stackable_wait_for_tee` and `stackable_run_exit_hooks` are reserved. +/// - A SIGTERM before the script's own TERM trap does not interrupt the running command. If the +/// script has no TERM trap yet, the command installs one that only records the signal, so an +/// init container finishes its work. `prepare_signal_handlers` of +/// [`COMMON_BASH_TRAP_FUNCTIONS`](crate::utils::COMMON_BASH_TRAP_FUNCTIONS) then exits with +/// status `143` instead of starting the product. +/// - The shell variables `stackable_tee_pids`, `stackable_wait_for_tee_shell` and +/// `stackable_sigterm_received` and the functions `stackable_wait_for_tee`, +/// `stackable_run_exit_hooks` and `stackable_record_sigterm` are reserved. /// - A caller that ends with `exec` replaces Bash, so the trap does not run. That is fine for a /// long-running process, but a short-lived one can lose the end of its output. /// @@ -307,7 +321,7 @@ pub fn capture_shell_output( // `-g`, because a caller can run this command inside a function, and the EXIT trap needs // the array after that function has returned. format!( - "{{\n{RUN_EXIT_HOOKS_FUNCTION}\n{WAIT_FOR_TEE_FUNCTION}\ndeclare -gA stackable_tee_pids\n{commands}\n}}" + "{{\n{RUN_EXIT_HOOKS_FUNCTION}\n{WAIT_FOR_TEE_FUNCTION}\ndeclare -gA stackable_tee_pids\n{RECORD_SIGTERM}\n{commands}\n}}" ) } else { commands @@ -1601,18 +1615,19 @@ touch {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE}" /// not changed by the trap. A trailing [`create_vector_shutdown_file_command`] is therefore not /// needed. /// -/// With an `EXIT` trap, bash handles SIGTERM even as PID 1 of the container. A SIGTERM before -/// `prepare_signal_handlers` of [`COMMON_BASH_TRAP_FUNCTIONS`](crate::utils::COMMON_BASH_TRAP_FUNCTIONS) -/// therefore exits the script with status `143` at once instead of being ignored until the -/// termination grace period expires. Commands still running at that point, such as a database -/// migration, are killed with the container. +/// A SIGTERM before `prepare_signal_handlers` of +/// [`COMMON_BASH_TRAP_FUNCTIONS`](crate::utils::COMMON_BASH_TRAP_FUNCTIONS) does not interrupt the +/// running command, such as a database migration. If the script has no TERM trap yet, the command +/// installs one that only records the signal, and `prepare_signal_handlers` then exits with status +/// `143` instead of starting the product. /// /// The command must be executed in the top-level shell of the script which owns the lifetime of /// the Vector container. In a subshell, the trap would fire when the subshell exits and shut down /// Vector too early. The script must not install another `EXIT` trap, because it would replace /// this one. The trap is shared with [`capture_shell_output`], so both can be combined in one -/// script in any order. The shell variable `stackable_vector_shutdown_shell` and the functions -/// `stackable_create_vector_shutdown_file` and `stackable_run_exit_hooks` are reserved. +/// script in any order. The shell variables `stackable_vector_shutdown_shell` and +/// `stackable_sigterm_received` and the functions `stackable_create_vector_shutdown_file`, +/// `stackable_run_exit_hooks` and `stackable_record_sigterm` are reserved. pub fn manage_vector_shutdown_file_command(stackable_log_dir: &str) -> String { format!( "{{ @@ -1620,6 +1635,7 @@ pub fn manage_vector_shutdown_file_command(stackable_log_dir: &str) -> String { stackable_create_vector_shutdown_file() {{ {create_shutdown_file_command} }} +{RECORD_SIGTERM} rm -f {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE} && stackable_vector_shutdown_shell=$BASHPID && trap stackable_run_exit_hooks EXIT @@ -1807,6 +1823,10 @@ mod tests { kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /log/test && exec > >(tee /log/test/container.stdout.log) && stackable_tee_pids[/log/test/container.stdout.log]=$! && @@ -1837,6 +1857,10 @@ mod tests { kill $! } declare -gA stackable_tee_pids + stackable_record_sigterm() { + stackable_sigterm_received=1 + } + [[ -n $(trap -p TERM) ]] || trap stackable_record_sigterm TERM mkdir --parents /log/test && exec 2> >(tee /log/test/container.stderr.log >&2) && stackable_tee_pids[/log/test/container.stderr.log]=$! && @@ -2134,6 +2158,85 @@ mod tests { assert!(!created_path.exists()); } + /// Sends SIGTERM to the script while a foreground command runs. + const SIGTERM_DURING_A_COMMAND: &str = "(sleep 0.3; kill -TERM $$) &\nsleep 1"; + + #[test] + fn sigterm_lets_the_running_command_finish_with_output_capturing() { + let run = run_captured(&[], |capture| { + format!("{capture}\n{SIGTERM_DURING_A_COMMAND}\necho done") + }); + + assert_eq!(Some(0), run.status.code()); + assert_eq!("done\n", run.stdout_log); + } + + #[test] + fn sigterm_lets_the_running_command_finish_with_the_vector_shutdown_trap() { + let run = run_with_both_hooks(|_, manage, _| { + format!("{manage}\n{SIGTERM_DURING_A_COMMAND}\necho done") + }); + + assert_eq!(Some(0), run.status.code()); + assert!(run.shutdown_file_path().exists()); + } + + #[test] + fn sigterm_before_prepare_signal_handlers_keeps_the_product_from_starting() { + let log_dir = tempfile::tempdir().expect("a temporary directory can be created"); + let log_dir_path = log_dir + .path() + .to_str() + .expect("the temporary directory path is valid UTF-8"); + + let (exit_code, stdout) = run_container_script(&formatdoc! {" + {COMMON_BASH_TRAP_FUNCTIONS} + {manage} + {SIGTERM_DURING_A_COMMAND} + echo after-the-command + prepare_signal_handlers + echo product-started", + manage = manage_vector_shutdown_file_command(log_dir_path), + }); + + assert_eq!(143, exit_code); + assert!(stdout.contains("after-the-command\n")); + assert!(!stdout.contains("product-started")); + assert!( + log_dir + .path() + .join(VECTOR_LOG_DIR) + .join(SHUTDOWN_FILE) + .exists() + ); + } + + #[test] + fn term_trap_set_before_the_commands_is_kept() { + let log_dir = tempfile::tempdir().expect("a temporary directory can be created"); + let log_dir_path = log_dir + .path() + .to_str() + .expect("the temporary directory path is valid UTF-8"); + + let (exit_code, stdout) = run_container_script(&formatdoc! {" + trap 'echo own-trap' TERM + {manage} + {capture} + trap -p TERM", + manage = manage_vector_shutdown_file_command(log_dir_path), + capture = capture_shell_output( + log_dir_path, + "test", + &shell_log_config(LogLevel::INFO, LogLevel::INFO), + ), + }); + + assert_eq!(0, exit_code); + assert!(stdout.contains("trap -- 'echo own-trap' SIGTERM")); + assert!(!stdout.contains("stackable_record_sigterm")); + } + #[test] fn log4j2_config() { let log_config = AutomaticContainerLogConfig { diff --git a/crates/stackable-operator/src/utils/bash.rs b/crates/stackable-operator/src/utils/bash.rs index 9bd8d7bf5..e6312e148 100644 --- a/crates/stackable-operator/src/utils/bash.rs +++ b/crates/stackable-operator/src/utils/bash.rs @@ -8,13 +8,13 @@ /// called exits the script with status `143`. A product already started in that window is killed /// with the container instead of shut down gracefully. /// -/// A SIGTERM before `prepare_signal_handlers` depends on whether the script has set an `EXIT` -/// trap, for example with -/// [`manage_vector_shutdown_file_command`](crate::product_logging::framework::manage_vector_shutdown_file_command). -/// Without one, the script runs as PID 1 of the container and ignores the signal, so the commands -/// before `prepare_signal_handlers` keep running until the termination grace period expires. With -/// one, bash handles the signal: the script runs the `EXIT` trap and exits with status `143` at -/// once, and the commands still running are killed with the container. +/// A SIGTERM before `prepare_signal_handlers` does not interrupt the commands before it. Without +/// a TERM trap, the script runs as PID 1 of the container and ignores the signal until the +/// termination grace period expires. +/// [`manage_vector_shutdown_file_command`](crate::product_logging::framework::manage_vector_shutdown_file_command) +/// and [`capture_shell_output`](crate::product_logging::framework::capture_shell_output) install a +/// TERM trap that records the signal, and `prepare_signal_handlers` then exits with status `143` +/// instead of starting the product. /// /// An example use could be /// ```text @@ -53,6 +53,9 @@ prepare_signal_handlers() { unset term_child_pid trap 'handle_term_signal' TERM + if [[ -v stackable_sigterm_received ]]; then + exit 143 + fi } handle_term_signal() @@ -163,6 +166,19 @@ mod tests { assert!(stdout.contains("tail-ran\n")); } + #[test] + fn recorded_sigterm_keeps_the_product_from_starting() { + let (exit_code, stdout) = run_container_script(&formatdoc! {" + {COMMON_BASH_TRAP_FUNCTIONS} + stackable_sigterm_received=1 + prepare_signal_handlers + echo product-started" + }); + + assert_eq!(143, exit_code); + assert!(!stdout.contains("product-started")); + } + #[test] fn sigterm_before_child_pid_is_known() { let (exit_code, stdout) = run_container_script(&formatdoc! {"