diff --git a/Cargo.lock b/Cargo.lock index 5c259b419..e965b6bc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3871,6 +3871,7 @@ dependencies = [ "http", "indexmap", "indoc", + "insta", "java-properties", "jiff 1.0.1", "json-patch", @@ -3893,6 +3894,7 @@ dependencies = [ "stackable-versioned", "stackable-webhook", "strum", + "tempfile", "tokio", "tracing", "tracing-appender", diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index 2531f7406..d94d01e40 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -7,15 +7,29 @@ All notable changes to this project will be documented in this file. ### Added - Add `utils::crds::raw_any_nullable_value_schema`, which accepts any JSON value (not only objects) ([#1326]). +- Add `product_logging::framework::manage_vector_shutdown_file_command`. It removes the Vector shutdown + file and installs an `EXIT` trap which creates it again, so Vector also shuts down when `set -e` aborts + the script. A trailing `create_vector_shutdown_file_command` is not needed ([#1265]). ### Changed - 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]). ### Fixed - `ProductImage::resolve` no longer panics on long product versions or image tags containing non-ASCII characters and returns an error instead ([#1339]). +- BREAKING: `wait_for_termination` in `COMMON_BASH_TRAP_FUNCTIONS` now returns the exit status of the process + it waited for instead of always returning `0`. Containers whose product exits non-zero on SIGTERM (e.g. + `143` for JVM products) now report that status on Pod deletion instead of `0` ([#1265]). +- `handle_term_signal` in `COMMON_BASH_TRAP_FUNCTIONS` exits the script with status `143` when SIGTERM arrives + before the child process ID is known, instead of aborting under `set -u` with status `1` ([#1265]). + +### Deprecated + +- Deprecate `remove_vector_shutdown_file_command` in favour of `manage_vector_shutdown_file_command` ([#1265]). ### Removed @@ -23,6 +37,7 @@ All notable changes to this project will be documented in this file. It was only needed when upgrading from SDP <= 26.3 to 26.7. The `DeleteRandomSecret` error variant is removed as well ([#1338]). +[#1265]: https://github.com/stackabletech/operator-rs/pull/1265 [#1326]: https://github.com/stackabletech/operator-rs/pull/1326 [#1338]: https://github.com/stackabletech/operator-rs/pull/1338 [#1339]: https://github.com/stackabletech/operator-rs/pull/1339 diff --git a/crates/stackable-operator/Cargo.toml b/crates/stackable-operator/Cargo.toml index 9e18ee666..b4fd60213 100644 --- a/crates/stackable-operator/Cargo.toml +++ b/crates/stackable-operator/Cargo.toml @@ -69,7 +69,9 @@ xml.workspace = true [dev-dependencies] indoc.workspace = true +insta.workspace = true rstest.workspace = true +tempfile.workspace = true [lints] workspace = true 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 new file mode 100644 index 000000000..5a0842605 --- /dev/null +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_containers.snap @@ -0,0 +1,204 @@ +--- +source: crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs +expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).unwrap()" +--- +- args: + - |- + mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) + + prepare_signal_handlers() + { + unset term_child_pid + trap 'handle_term_signal' TERM + } + + handle_term_signal() + { + if [ -n "${term_child_pid:-}" ]; then + kill -TERM "${term_child_pid}" 2>/dev/null + else + exit 143 + fi + } + + wait_for_termination() + { + set +e + term_child_pid=$1 + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + trap - TERM + if [ "${term_child_status}" -gt 128 ]; then + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + fi + set -e + return ${term_child_status} + } + + prepare_signal_handlers + /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git' --link=current --one-time=false --period=20s --ref=main --repo=https://github.com/stackabletech/repo1 --root=/tmp/git & + wait_for_termination $! + command: + - /bin/bash + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-0 + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-0 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume +- args: + - |- + mkdir --parents /stackable/log/git-sync-1 && exec > >(tee /stackable/log/git-sync-1/container.stdout.log) 2> >(tee /stackable/log/git-sync-1/container.stderr.log >&2) + + prepare_signal_handlers() + { + unset term_child_pid + trap 'handle_term_signal' TERM + } + + handle_term_signal() + { + if [ -n "${term_child_pid:-}" ]; then + kill -TERM "${term_child_pid}" 2>/dev/null + else + exit 143 + fi + } + + wait_for_termination() + { + set +e + term_child_pid=$1 + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + trap - TERM + if [ "${term_child_status}" -gt 128 ]; then + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + fi + set -e + return ${term_child_status} + } + + prepare_signal_handlers + /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=false --period=60s --ref=trunk --repo=https://github.com/stackabletech/repo2 --rev=HEAD --root=/tmp/git & + wait_for_termination $! + command: + - /bin/bash + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_PASSWORD + valueFrom: + secretKeyRef: + key: password + name: git-credentials + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-1 + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-1 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume +- args: + - |- + mkdir --parents /stackable/log/git-sync-2 && exec > >(tee /stackable/log/git-sync-2/container.stdout.log) 2> >(tee /stackable/log/git-sync-2/container.stderr.log >&2) + + prepare_signal_handlers() + { + unset term_child_pid + trap 'handle_term_signal' TERM + } + + handle_term_signal() + { + if [ -n "${term_child_pid:-}" ]; then + kill -TERM "${term_child_pid}" 2>/dev/null + else + exit 143 + fi + } + + wait_for_termination() + { + set +e + term_child_pid=$1 + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + trap - TERM + if [ "${term_child_status}" -gt 128 ]; then + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + fi + set -e + return ${term_child_status} + } + + prepare_signal_handlers + /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git,key:value,safe.directory:/safe-dir' --link=current --one-time=false --period=20s --ref=feat/git-sync --repo=https://github.com/stackabletech/repo3 --root=/tmp/git & + wait_for_termination $! + command: + - /bin/bash + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-2 + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-2 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume 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 new file mode 100644 index 000000000..986630eaf --- /dev/null +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha1_impl__tests__multiple_git_syncs_init_containers.snap @@ -0,0 +1,105 @@ +--- +source: crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs +expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers).unwrap()" +--- +- args: + - |- + mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) + /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 + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-0-init + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-0 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume +- args: + - |- + mkdir --parents /stackable/log/git-sync-1-init && exec > >(tee /stackable/log/git-sync-1-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-1-init/container.stderr.log >&2) + /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 + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_PASSWORD + valueFrom: + secretKeyRef: + key: password + name: git-credentials + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-1-init + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-1 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume +- args: + - |- + mkdir --parents /stackable/log/git-sync-2-init && exec > >(tee /stackable/log/git-sync-2-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-2-init/container.stderr.log >&2) + /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 + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-2-init + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-2 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume 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 new file mode 100644 index 000000000..4253fb080 --- /dev/null +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_containers.snap @@ -0,0 +1,69 @@ +--- +source: crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs +expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).unwrap()" +--- +- args: + - |- + mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) + + prepare_signal_handlers() + { + unset term_child_pid + trap 'handle_term_signal' TERM + } + + handle_term_signal() + { + if [ -n "${term_child_pid:-}" ]; then + kill -TERM "${term_child_pid}" 2>/dev/null + else + exit 143 + fi + } + + wait_for_termination() + { + set +e + term_child_pid=$1 + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + trap - TERM + if [ "${term_child_status}" -gt 128 ]; then + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + fi + set -e + return ${term_child_status} + } + + prepare_signal_handlers + /stackable/git-sync --depth=3 --git-config='http.sslCAInfo:/stackable/gitca-0/ca.crt,safe.directory:/tmp/git' --link=current --one-time=false --period=60s --ref=trunk --repo=ssh://git@github.com/stackabletech/repo.git --rev=HEAD --root=/tmp/git & + wait_for_termination $! + command: + - /bin/bash + - -x + - -euo + - pipefail + - -c + env: + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-0 + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-0 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume + - mountPath: /stackable/gitca-0 + name: ca-cert-0 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 new file mode 100644 index 000000000..8dbd6e8f6 --- /dev/null +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ca_cert_init_containers.snap @@ -0,0 +1,36 @@ +--- +source: crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs +expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers).unwrap()" +--- +- args: + - |- + mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) + /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 + - -x + - -euo + - pipefail + - -c + env: + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-0-init + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-0 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume + - mountPath: /stackable/gitca-0 + name: ca-cert-0 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 new file mode 100644 index 000000000..a3101000b --- /dev/null +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_containers.snap @@ -0,0 +1,73 @@ +--- +source: crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs +expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).unwrap()" +--- +- args: + - |- + mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) + + prepare_signal_handlers() + { + unset term_child_pid + trap 'handle_term_signal' TERM + } + + handle_term_signal() + { + if [ -n "${term_child_pid:-}" ]; then + kill -TERM "${term_child_pid}" 2>/dev/null + else + exit 143 + fi + } + + wait_for_termination() + { + set +e + term_child_pid=$1 + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + trap - TERM + if [ "${term_child_status}" -gt 128 ]; then + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + fi + set -e + return ${term_child_status} + } + + prepare_signal_handlers + /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=false --period=60s --ref=trunk --repo=ssh://git@github.com/stackabletech/repo.git --rev=HEAD --root=/tmp/git & + wait_for_termination $! + command: + - /bin/bash + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_SSH_KEY_FILE + value: /stackable/gitssh-0/key + - name: GITSYNC_SSH_KNOWN_HOSTS_FILE + value: /stackable/gitssh-0/knownHosts + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-0 + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-0 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume + - mountPath: /stackable/gitssh-0 + name: ssh-keys-info-0 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 new file mode 100644 index 000000000..a099a9f81 --- /dev/null +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__git_sync_ssh_init_containers.snap @@ -0,0 +1,40 @@ +--- +source: crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs +expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers).unwrap()" +--- +- args: + - |- + mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) + /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 + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_SSH_KEY_FILE + value: /stackable/gitssh-0/key + - name: GITSYNC_SSH_KNOWN_HOSTS_FILE + value: /stackable/gitssh-0/knownHosts + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-0-init + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-0 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume + - mountPath: /stackable/gitssh-0 + name: ssh-keys-info-0 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 new file mode 100644 index 000000000..a95c5e7e0 --- /dev/null +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_containers.snap @@ -0,0 +1,204 @@ +--- +source: crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs +expression: "yaml_serde::to_string(&git_sync_resources.git_sync_containers).unwrap()" +--- +- args: + - |- + mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) + + prepare_signal_handlers() + { + unset term_child_pid + trap 'handle_term_signal' TERM + } + + handle_term_signal() + { + if [ -n "${term_child_pid:-}" ]; then + kill -TERM "${term_child_pid}" 2>/dev/null + else + exit 143 + fi + } + + wait_for_termination() + { + set +e + term_child_pid=$1 + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + trap - TERM + if [ "${term_child_status}" -gt 128 ]; then + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + fi + set -e + return ${term_child_status} + } + + prepare_signal_handlers + /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git' --link=current --one-time=false --period=20s --ref=main --repo=https://github.com/stackabletech/repo1 --root=/tmp/git & + wait_for_termination $! + command: + - /bin/bash + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-0 + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-0 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume +- args: + - |- + mkdir --parents /stackable/log/git-sync-1 && exec > >(tee /stackable/log/git-sync-1/container.stdout.log) 2> >(tee /stackable/log/git-sync-1/container.stderr.log >&2) + + prepare_signal_handlers() + { + unset term_child_pid + trap 'handle_term_signal' TERM + } + + handle_term_signal() + { + if [ -n "${term_child_pid:-}" ]; then + kill -TERM "${term_child_pid}" 2>/dev/null + else + exit 143 + fi + } + + wait_for_termination() + { + set +e + term_child_pid=$1 + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + trap - TERM + if [ "${term_child_status}" -gt 128 ]; then + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + fi + set -e + return ${term_child_status} + } + + prepare_signal_handlers + /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=false --period=60s --ref=trunk --repo=https://github.com/stackabletech/repo2 --rev=HEAD --root=/tmp/git & + wait_for_termination $! + command: + - /bin/bash + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_PASSWORD + valueFrom: + secretKeyRef: + key: password + name: git-credentials + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-1 + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-1 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume +- args: + - |- + mkdir --parents /stackable/log/git-sync-2 && exec > >(tee /stackable/log/git-sync-2/container.stdout.log) 2> >(tee /stackable/log/git-sync-2/container.stderr.log >&2) + + prepare_signal_handlers() + { + unset term_child_pid + trap 'handle_term_signal' TERM + } + + handle_term_signal() + { + if [ -n "${term_child_pid:-}" ]; then + kill -TERM "${term_child_pid}" 2>/dev/null + else + exit 143 + fi + } + + wait_for_termination() + { + set +e + term_child_pid=$1 + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + trap - TERM + if [ "${term_child_status}" -gt 128 ]; then + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + fi + set -e + return ${term_child_status} + } + + prepare_signal_handlers + /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git,key:value,safe.directory:/safe-dir' --link=current --one-time=false --period=20s --ref=feat/git-sync --repo=https://github.com/stackabletech/repo3 --root=/tmp/git & + wait_for_termination $! + command: + - /bin/bash + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-2 + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-2 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume 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 new file mode 100644 index 000000000..8f99b18c5 --- /dev/null +++ b/crates/stackable-operator/src/crd/git_sync/snapshots/stackable_operator__crd__git_sync__v1alpha2_impl__tests__multiple_git_syncs_init_containers.snap @@ -0,0 +1,105 @@ +--- +source: crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs +expression: "yaml_serde::to_string(&git_sync_resources.git_sync_init_containers).unwrap()" +--- +- args: + - |- + mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) + /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 + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-0-init + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-0 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume +- args: + - |- + mkdir --parents /stackable/log/git-sync-1-init && exec > >(tee /stackable/log/git-sync-1-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-1-init/container.stderr.log >&2) + /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 + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_PASSWORD + valueFrom: + secretKeyRef: + key: password + name: git-credentials + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-1-init + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-1 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume +- args: + - |- + mkdir --parents /stackable/log/git-sync-2-init && exec > >(tee /stackable/log/git-sync-2-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-2-init/container.stderr.log >&2) + /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 + - -x + - -euo + - pipefail + - -c + env: + - name: GITSYNC_USERNAME + value: overridden-username + - name: VAR1 + value: value1 + image: oci.stackable.tech/sdp/product:latest + imagePullPolicy: Always + name: git-sync-2-init + resources: + limits: + cpu: 200m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi + volumeMounts: + - mountPath: /tmp/git + name: content-from-git-2 + - mountPath: /stackable/log + name: log-volume + - mountPath: /mnt/extra-volume + name: extra-volume diff --git a/crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs b/crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs index 2b575929c..c4bcfbc37 100644 --- a/crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs +++ b/crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs @@ -484,334 +484,16 @@ mod tests { assert_eq!(3, git_sync_resources.git_sync_containers.len()); - assert_eq!( - r#"args: -- |- - mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) - - prepare_signal_handlers() - { - unset term_child_pid - unset term_kill_needed - trap 'handle_term_signal' TERM - } - - handle_term_signal() - { - if [ "${term_child_pid}" ]; then - kill -TERM "${term_child_pid}" 2>/dev/null - else - term_kill_needed="yes" - fi - } - - wait_for_termination() - { - set +e - term_child_pid=$1 - if [[ -v term_kill_needed ]]; then - kill -TERM "${term_child_pid}" 2>/dev/null - fi - wait ${term_child_pid} 2>/dev/null - trap - TERM - wait ${term_child_pid} 2>/dev/null - set -e - } - - prepare_signal_handlers - /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git' --link=current --one-time=false --period=20s --ref=main --repo=https://github.com/stackabletech/repo1 --root=/tmp/git & - wait_for_termination $! -command: -- /bin/bash -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-0 -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-0 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -"#, - yaml_serde::to_string(&git_sync_resources.git_sync_containers.first()).unwrap() - ); - - assert_eq!( - r#"args: -- |- - mkdir --parents /stackable/log/git-sync-1 && exec > >(tee /stackable/log/git-sync-1/container.stdout.log) 2> >(tee /stackable/log/git-sync-1/container.stderr.log >&2) - - prepare_signal_handlers() - { - unset term_child_pid - unset term_kill_needed - trap 'handle_term_signal' TERM - } - - handle_term_signal() - { - if [ "${term_child_pid}" ]; then - kill -TERM "${term_child_pid}" 2>/dev/null - else - term_kill_needed="yes" - fi - } - - wait_for_termination() - { - set +e - term_child_pid=$1 - if [[ -v term_kill_needed ]]; then - kill -TERM "${term_child_pid}" 2>/dev/null - fi - wait ${term_child_pid} 2>/dev/null - trap - TERM - wait ${term_child_pid} 2>/dev/null - set -e - } - - prepare_signal_handlers - /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=false --period=60s --ref=trunk --repo=https://github.com/stackabletech/repo2 --rev=HEAD --root=/tmp/git & - wait_for_termination $! -command: -- /bin/bash -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_PASSWORD - valueFrom: - secretKeyRef: - key: password - name: git-credentials -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-1 -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-1 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -"#, - yaml_serde::to_string(&git_sync_resources.git_sync_containers.get(1)).unwrap() - ); - - assert_eq!( - r#"args: -- |- - mkdir --parents /stackable/log/git-sync-2 && exec > >(tee /stackable/log/git-sync-2/container.stdout.log) 2> >(tee /stackable/log/git-sync-2/container.stderr.log >&2) - - prepare_signal_handlers() - { - unset term_child_pid - unset term_kill_needed - trap 'handle_term_signal' TERM - } - - handle_term_signal() - { - if [ "${term_child_pid}" ]; then - kill -TERM "${term_child_pid}" 2>/dev/null - else - term_kill_needed="yes" - fi - } - - wait_for_termination() - { - set +e - term_child_pid=$1 - if [[ -v term_kill_needed ]]; then - kill -TERM "${term_child_pid}" 2>/dev/null - fi - wait ${term_child_pid} 2>/dev/null - trap - TERM - wait ${term_child_pid} 2>/dev/null - set -e - } - - prepare_signal_handlers - /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git,key:value,safe.directory:/safe-dir' --link=current --one-time=false --period=20s --ref=feat/git-sync --repo=https://github.com/stackabletech/repo3 --root=/tmp/git & - wait_for_termination $! -command: -- /bin/bash -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-2 -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-2 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -"#, - yaml_serde::to_string(&git_sync_resources.git_sync_containers.get(2)).unwrap() + insta::assert_snapshot!( + "multiple_git_syncs_containers", + yaml_serde::to_string(&git_sync_resources.git_sync_containers).unwrap() ); assert_eq!(3, git_sync_resources.git_sync_init_containers.len()); - assert_eq!( - r"args: -- |- - mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) - /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 -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-0-init -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-0 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -", - yaml_serde::to_string(&git_sync_resources.git_sync_init_containers.first()).unwrap() - ); - - assert_eq!( - r"args: -- |- - mkdir --parents /stackable/log/git-sync-1-init && exec > >(tee /stackable/log/git-sync-1-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-1-init/container.stderr.log >&2) - /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 -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_PASSWORD - valueFrom: - secretKeyRef: - key: password - name: git-credentials -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-1-init -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-1 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -", - yaml_serde::to_string(&git_sync_resources.git_sync_init_containers.get(1)).unwrap() - ); - - assert_eq!( - r"args: -- |- - mkdir --parents /stackable/log/git-sync-2-init && exec > >(tee /stackable/log/git-sync-2-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-2-init/container.stderr.log >&2) - /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 -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-2-init -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-2 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -", - yaml_serde::to_string(&git_sync_resources.git_sync_init_containers.get(2)).unwrap() + insta::assert_snapshot!( + "multiple_git_syncs_init_containers", + yaml_serde::to_string(&git_sync_resources.git_sync_init_containers).unwrap() ); assert_eq!(3, git_sync_resources.git_content_volumes.len()); diff --git a/crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs b/crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs index 4fdd76e0e..ea461ecb6 100644 --- a/crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs +++ b/crates/stackable-operator/src/crd/git_sync/v1alpha2_impl.rs @@ -615,334 +615,16 @@ mod tests { assert_eq!(3, git_sync_resources.git_sync_containers.len()); - assert_eq!( - r#"args: -- |- - mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) - - prepare_signal_handlers() - { - unset term_child_pid - unset term_kill_needed - trap 'handle_term_signal' TERM - } - - handle_term_signal() - { - if [ "${term_child_pid}" ]; then - kill -TERM "${term_child_pid}" 2>/dev/null - else - term_kill_needed="yes" - fi - } - - wait_for_termination() - { - set +e - term_child_pid=$1 - if [[ -v term_kill_needed ]]; then - kill -TERM "${term_child_pid}" 2>/dev/null - fi - wait ${term_child_pid} 2>/dev/null - trap - TERM - wait ${term_child_pid} 2>/dev/null - set -e - } - - prepare_signal_handlers - /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git' --link=current --one-time=false --period=20s --ref=main --repo=https://github.com/stackabletech/repo1 --root=/tmp/git & - wait_for_termination $! -command: -- /bin/bash -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-0 -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-0 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -"#, - yaml_serde::to_string(&git_sync_resources.git_sync_containers.first()).unwrap() - ); - - assert_eq!( - r#"args: -- |- - mkdir --parents /stackable/log/git-sync-1 && exec > >(tee /stackable/log/git-sync-1/container.stdout.log) 2> >(tee /stackable/log/git-sync-1/container.stderr.log >&2) - - prepare_signal_handlers() - { - unset term_child_pid - unset term_kill_needed - trap 'handle_term_signal' TERM - } - - handle_term_signal() - { - if [ "${term_child_pid}" ]; then - kill -TERM "${term_child_pid}" 2>/dev/null - else - term_kill_needed="yes" - fi - } - - wait_for_termination() - { - set +e - term_child_pid=$1 - if [[ -v term_kill_needed ]]; then - kill -TERM "${term_child_pid}" 2>/dev/null - fi - wait ${term_child_pid} 2>/dev/null - trap - TERM - wait ${term_child_pid} 2>/dev/null - set -e - } - - prepare_signal_handlers - /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=false --period=60s --ref=trunk --repo=https://github.com/stackabletech/repo2 --rev=HEAD --root=/tmp/git & - wait_for_termination $! -command: -- /bin/bash -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_PASSWORD - valueFrom: - secretKeyRef: - key: password - name: git-credentials -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-1 -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-1 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -"#, - yaml_serde::to_string(&git_sync_resources.git_sync_containers.get(1)).unwrap() - ); - - assert_eq!( - r#"args: -- |- - mkdir --parents /stackable/log/git-sync-2 && exec > >(tee /stackable/log/git-sync-2/container.stdout.log) 2> >(tee /stackable/log/git-sync-2/container.stderr.log >&2) - - prepare_signal_handlers() - { - unset term_child_pid - unset term_kill_needed - trap 'handle_term_signal' TERM - } - - handle_term_signal() - { - if [ "${term_child_pid}" ]; then - kill -TERM "${term_child_pid}" 2>/dev/null - else - term_kill_needed="yes" - fi - } - - wait_for_termination() - { - set +e - term_child_pid=$1 - if [[ -v term_kill_needed ]]; then - kill -TERM "${term_child_pid}" 2>/dev/null - fi - wait ${term_child_pid} 2>/dev/null - trap - TERM - wait ${term_child_pid} 2>/dev/null - set -e - } - - prepare_signal_handlers - /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git,key:value,safe.directory:/safe-dir' --link=current --one-time=false --period=20s --ref=feat/git-sync --repo=https://github.com/stackabletech/repo3 --root=/tmp/git & - wait_for_termination $! -command: -- /bin/bash -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-2 -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-2 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -"#, - yaml_serde::to_string(&git_sync_resources.git_sync_containers.get(2)).unwrap() + insta::assert_snapshot!( + "multiple_git_syncs_containers", + yaml_serde::to_string(&git_sync_resources.git_sync_containers).unwrap() ); assert_eq!(3, git_sync_resources.git_sync_init_containers.len()); - assert_eq!( - r"args: -- |- - mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) - /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 -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-0-init -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-0 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -", - yaml_serde::to_string(&git_sync_resources.git_sync_init_containers.first()).unwrap() - ); - - assert_eq!( - r"args: -- |- - mkdir --parents /stackable/log/git-sync-1-init && exec > >(tee /stackable/log/git-sync-1-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-1-init/container.stderr.log >&2) - /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 -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_PASSWORD - valueFrom: - secretKeyRef: - key: password - name: git-credentials -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-1-init -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-1 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -", - yaml_serde::to_string(&git_sync_resources.git_sync_init_containers.get(1)).unwrap() - ); - - assert_eq!( - r"args: -- |- - mkdir --parents /stackable/log/git-sync-2-init && exec > >(tee /stackable/log/git-sync-2-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-2-init/container.stderr.log >&2) - /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 -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_USERNAME - value: overridden-username -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-2-init -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-2 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -", - yaml_serde::to_string(&git_sync_resources.git_sync_init_containers.get(2)).unwrap() + insta::assert_snapshot!( + "multiple_git_syncs_init_containers", + yaml_serde::to_string(&git_sync_resources.git_sync_init_containers).unwrap() ); assert_eq!(3, git_sync_resources.git_content_volumes.len()); @@ -1019,7 +701,6 @@ name: content-from-git-2 } #[test] - #[expect(clippy::too_many_lines)] fn test_git_sync_ssh() { let git_sync_spec = r#" # GitSync using SSH @@ -1070,120 +751,16 @@ name: content-from-git-2 assert_eq!(1, git_sync_resources.git_sync_containers.len()); - assert_eq!( - r#"args: -- |- - mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) - - prepare_signal_handlers() - { - unset term_child_pid - unset term_kill_needed - trap 'handle_term_signal' TERM - } - - handle_term_signal() - { - if [ "${term_child_pid}" ]; then - kill -TERM "${term_child_pid}" 2>/dev/null - else - term_kill_needed="yes" - fi - } - - wait_for_termination() - { - set +e - term_child_pid=$1 - if [[ -v term_kill_needed ]]; then - kill -TERM "${term_child_pid}" 2>/dev/null - fi - wait ${term_child_pid} 2>/dev/null - trap - TERM - wait ${term_child_pid} 2>/dev/null - set -e - } - - prepare_signal_handlers - /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=false --period=60s --ref=trunk --repo=ssh://git@github.com/stackabletech/repo.git --rev=HEAD --root=/tmp/git & - wait_for_termination $! -command: -- /bin/bash -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_SSH_KEY_FILE - value: /stackable/gitssh-0/key -- name: GITSYNC_SSH_KNOWN_HOSTS_FILE - value: /stackable/gitssh-0/knownHosts -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-0 -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-0 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -- mountPath: /stackable/gitssh-0 - name: ssh-keys-info-0 -"#, - yaml_serde::to_string(&git_sync_resources.git_sync_containers.first()).unwrap() + insta::assert_snapshot!( + "git_sync_ssh_containers", + yaml_serde::to_string(&git_sync_resources.git_sync_containers).unwrap() ); assert_eq!(1, git_sync_resources.git_sync_init_containers.len()); - assert_eq!( - r"args: -- |- - mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) - /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 -- -x -- -euo -- pipefail -- -c -env: -- name: GITSYNC_SSH_KEY_FILE - value: /stackable/gitssh-0/key -- name: GITSYNC_SSH_KNOWN_HOSTS_FILE - value: /stackable/gitssh-0/knownHosts -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-0-init -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-0 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -- mountPath: /stackable/gitssh-0 - name: ssh-keys-info-0 -", - yaml_serde::to_string(&git_sync_resources.git_sync_init_containers.first()).unwrap() + insta::assert_snapshot!( + "git_sync_ssh_init_containers", + yaml_serde::to_string(&git_sync_resources.git_sync_init_containers).unwrap() ); assert_eq!(1, git_sync_resources.git_content_volumes.len()); @@ -1227,7 +804,6 @@ secret: } #[test] - #[allow(clippy::too_many_lines)] fn test_git_sync_ca_cert() { let git_sync_spec = r#" # GitSync using SSH @@ -1280,112 +856,16 @@ secret: assert_eq!(1, git_sync_resources.git_sync_containers.len()); - assert_eq!( - r#"args: -- |- - mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2) - - prepare_signal_handlers() - { - unset term_child_pid - unset term_kill_needed - trap 'handle_term_signal' TERM - } - - handle_term_signal() - { - if [ "${term_child_pid}" ]; then - kill -TERM "${term_child_pid}" 2>/dev/null - else - term_kill_needed="yes" - fi - } - - wait_for_termination() - { - set +e - term_child_pid=$1 - if [[ -v term_kill_needed ]]; then - kill -TERM "${term_child_pid}" 2>/dev/null - fi - wait ${term_child_pid} 2>/dev/null - trap - TERM - wait ${term_child_pid} 2>/dev/null - set -e - } - - prepare_signal_handlers - /stackable/git-sync --depth=3 --git-config='http.sslCAInfo:/stackable/gitca-0/ca.crt,safe.directory:/tmp/git' --link=current --one-time=false --period=60s --ref=trunk --repo=ssh://git@github.com/stackabletech/repo.git --rev=HEAD --root=/tmp/git & - wait_for_termination $! -command: -- /bin/bash -- -x -- -euo -- pipefail -- -c -env: -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-0 -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-0 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -- mountPath: /stackable/gitca-0 - name: ca-cert-0 -"#, - yaml_serde::to_string(&git_sync_resources.git_sync_containers.first()).unwrap() + insta::assert_snapshot!( + "git_sync_ca_cert_containers", + yaml_serde::to_string(&git_sync_resources.git_sync_containers).unwrap() ); assert_eq!(1, git_sync_resources.git_sync_init_containers.len()); - assert_eq!( - r"args: -- |- - mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2) - /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 -- -x -- -euo -- pipefail -- -c -env: -- name: VAR1 - value: value1 -image: oci.stackable.tech/sdp/product:latest -imagePullPolicy: Always -name: git-sync-0-init -resources: - limits: - cpu: 200m - memory: 64Mi - requests: - cpu: 100m - memory: 64Mi -volumeMounts: -- mountPath: /tmp/git - name: content-from-git-0 -- mountPath: /stackable/log - name: log-volume -- mountPath: /mnt/extra-volume - name: extra-volume -- mountPath: /stackable/gitca-0 - name: ca-cert-0 -", - yaml_serde::to_string(&git_sync_resources.git_sync_init_containers.first()).unwrap() + insta::assert_snapshot!( + "git_sync_ca_cert_init_containers", + yaml_serde::to_string(&git_sync_resources.git_sync_init_containers).unwrap() ); assert_eq!(1, git_sync_resources.git_content_volumes.len()); diff --git a/crates/stackable-operator/src/product_logging/framework.rs b/crates/stackable-operator/src/product_logging/framework.rs index 35c7bab02..ecf268af9 100644 --- a/crates/stackable-operator/src/product_logging/framework.rs +++ b/crates/stackable-operator/src/product_logging/framework.rs @@ -1337,7 +1337,7 @@ sinks: /// meta::ObjectMetaBuilder, /// pod::{container::ContainerBuilder, resources::ResourceRequirementsBuilder, PodBuilder}, /// }, -/// product_logging::{self, framework:: {create_vector_shutdown_file_command, remove_vector_shutdown_file_command}}, +/// product_logging::{self, framework::manage_vector_shutdown_file_command}, /// utils::COMMON_BASH_TRAP_FUNCTIONS, /// }; /// use k8s_openapi::apimachinery::pkg::api::resource::Quantity; @@ -1383,16 +1383,13 @@ sinks: /// .args(vec![format!( /// "\ /// {COMMON_BASH_TRAP_FUNCTIONS} -/// {remove_vector_shutdown_file_command} +/// {manage_vector_shutdown_file_command} /// prepare_signal_handlers /// my-application start & /// wait_for_termination $! -/// {create_vector_shutdown_file_command} /// ", -/// remove_vector_shutdown_file_command = -/// remove_vector_shutdown_file_command(STACKABLE_LOG_DIR), -/// create_vector_shutdown_file_command = -/// create_vector_shutdown_file_command(STACKABLE_LOG_DIR), +/// manage_vector_shutdown_file_command = +/// manage_vector_shutdown_file_command(STACKABLE_LOG_DIR), /// )]) /// .build(), /// ); @@ -1469,7 +1466,8 @@ kill $vector_pid } /// Command to create a shutdown file for the vector container. -/// Please delete it before starting your application using [`remove_vector_shutdown_file_command`]. +/// Please delete it before starting your application using [`manage_vector_shutdown_file_command`], +/// which also creates it again when the script exits. /// /// # Example /// @@ -1479,7 +1477,7 @@ kill $vector_pid /// const STACKABLE_LOG_DIR: &str = "/stackable/log"; /// /// let args = vec![ -/// product_logging::framework::remove_vector_shutdown_file_command(STACKABLE_LOG_DIR), +/// product_logging::framework::manage_vector_shutdown_file_command(STACKABLE_LOG_DIR), /// "echo Perform some tasks ...".into(), /// product_logging::framework::create_vector_shutdown_file_command(STACKABLE_LOG_DIR), /// ]; @@ -1500,20 +1498,144 @@ touch {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE}" ) } -/// Use this command to remove the shutdown file (if it exists) created by [`create_vector_shutdown_file_command`]. -/// You should execute this command before starting your application. +/// Use this command to bind the lifetime of the Vector container to the script that runs your +/// application. Execute it before starting your application. +/// +/// The command removes the shutdown file (if it exists) created by +/// [`create_vector_shutdown_file_command`] and installs an `EXIT` trap which creates the shutdown +/// file again, so the Vector container is shut down however the script ends: after a regular +/// exit, after `set -e` aborts the script because `wait_for_termination` returned the non-zero +/// exit status of the application, or after an explicit `exit`. The exit status of the script is +/// not changed by the trap. A trailing [`create_vector_shutdown_file_command`] is therefore not +/// needed. +/// +/// 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. +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", + create_shutdown_file_command = create_vector_shutdown_file_command(stackable_log_dir), + ) +} + +/// Alias of [`manage_vector_shutdown_file_command`], including its `EXIT` trap. +#[deprecated( + note = "renamed to stackable_operator::product_logging::framework::manage_vector_shutdown_file_command", + since = "0.123.0" +)] pub fn remove_vector_shutdown_file_command(stackable_log_dir: &str) -> String { - format!("rm -f {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE}") + manage_vector_shutdown_file_command(stackable_log_dir) } #[cfg(test)] mod tests { use std::collections::BTreeMap; + use indoc::{formatdoc, indoc}; use rstest::rstest; use super::*; - use crate::product_logging::spec::{AppenderConfig, LoggerConfig}; + use crate::{ + product_logging::spec::{AppenderConfig, LoggerConfig}, + utils::{COMMON_BASH_TRAP_FUNCTIONS, bash::run_container_script}, + }; + + /// Runs the given application commands like a product container does and returns the exit + /// status of the script and whether the Vector shutdown file exists afterwards. + /// + /// With `trailing_create_command`, the script ends with an explicit + /// [`create_vector_shutdown_file_command`] like operator scripts written before the `EXIT` + /// trap existed. + fn run_application_script( + application_commands: &str, + trailing_create_command: bool, + ) -> (i32, bool) { + let stackable_log_dir = + tempfile::tempdir().expect("a temporary directory should be creatable"); + let stackable_log_dir_path = stackable_log_dir + .path() + .to_str() + .expect("the temporary directory path should be valid UTF-8"); + + let trailing_commands = if trailing_create_command { + create_vector_shutdown_file_command(stackable_log_dir_path) + } else { + String::new() + }; + let script = formatdoc! {" + {COMMON_BASH_TRAP_FUNCTIONS} + {manage_vector_shutdown_file_command} + prepare_signal_handlers + {application_commands} + {trailing_commands}", + manage_vector_shutdown_file_command = + manage_vector_shutdown_file_command(stackable_log_dir_path), + }; + + let (exit_code, _) = run_container_script(&script); + let shutdown_file_exists = stackable_log_dir + .path() + .join(VECTOR_LOG_DIR) + .join(SHUTDOWN_FILE) + .exists(); + + (exit_code, shutdown_file_exists) + } + + #[rstest] + #[case::application_crashes( + indoc! {" + bash -c 'exit 42' & + wait_for_termination $! + "}, + 42 + )] + #[case::application_exits_successfully( + indoc! {" + bash -c 'exit 0' & + wait_for_termination $! + "}, + 0 + )] + #[case::application_is_killed( + indoc! {" + bash -c 'kill -KILL $$' & + wait_for_termination $! + "}, + 137 + )] + #[case::application_shuts_down_with_failure_on_sigterm( + indoc! {" + bash -c 'trap \"exit 143\" TERM; sleep 10 >/dev/null 2>&1 & wait $!' & + application_pid=$! + (sleep 0.2; kill -TERM $$) & + wait_for_termination $application_pid + "}, + 143 + )] + #[case::application_shuts_down_successfully_on_sigterm( + indoc! {" + bash -c 'trap \"exit 0\" TERM; sleep 10 >/dev/null 2>&1 & wait $!' & + application_pid=$! + (sleep 0.2; kill -TERM $$) & + wait_for_termination $application_pid + "}, + 0 + )] + fn vector_shutdown_file_is_created_on_every_exit( + #[case] application_commands: &str, + #[case] expected_exit_code: i32, + #[values(false, true)] trailing_create_command: bool, + ) { + let (exit_code, shutdown_file_exists) = + run_application_script(application_commands, trailing_create_command); + + assert_eq!(expected_exit_code, exit_code); + assert!(shutdown_file_exists); + } #[rstest] #[case("0Mi", &[])] diff --git a/crates/stackable-operator/src/utils/bash.rs b/crates/stackable-operator/src/utils/bash.rs index b93b77173..02dbd57eb 100644 --- a/crates/stackable-operator/src/utils/bash.rs +++ b/crates/stackable-operator/src/utils/bash.rs @@ -1,7 +1,12 @@ /// This is a bash snippet, which adds two functions out of interest: /// /// 1. `prepare_signal_handlers` call this first to set up the needed traps -/// 2. `wait_for_termination` waits for the PID you passed as the first argument to terminate +/// 2. `wait_for_termination` waits for the PID you passed as the first argument to terminate and +/// returns its exit status +/// +/// A SIGTERM which arrives after `prepare_signal_handlers` but before `wait_for_termination` is +/// called exits the script with status `143`. A product already started in that window is killed +/// with the container instead of shut down gracefully. /// /// An example use could be /// ```text @@ -9,23 +14,36 @@ /// echo "Run before startup" /// prepare_signal_handlers /// {hadoop_home}/bin/hdfs {role} & -/// wait_for_termination $! +/// product_exit_code=0 +/// wait_for_termination $! || product_exit_code=$? /// echo "Run after termination" +/// exit "${product_exit_code}" /// ``` +// A `wait` status above 128 can mean two things: +// 1. bash reports a signal death as `128 + signal` +// 2. an in-progress `wait` aborts with such a status when a trapped signal arrives +// +// We wait a second time to get the actual child status, which immediately returns the +// status again in case 1. In case 2 the child is still running and we do need to wait. +// +// See https://www.gnu.org/software/bash/manual/html_node/Signals.html +// +// A SIGTERM before the child PID is known exits the script instead of being forwarded once the +// child is started: bash 5.1 (UBI 9) regularly loses a SIGTERM sent to a child right after it +// was forked, and the child then runs until the termination grace period expires. pub const COMMON_BASH_TRAP_FUNCTIONS: &str = r#" prepare_signal_handlers() { unset term_child_pid - unset term_kill_needed trap 'handle_term_signal' TERM } handle_term_signal() { - if [ "${term_child_pid}" ]; then + if [ -n "${term_child_pid:-}" ]; then kill -TERM "${term_child_pid}" 2>/dev/null else - term_kill_needed="yes" + exit 143 fi } @@ -33,12 +51,69 @@ wait_for_termination() { set +e term_child_pid=$1 - if [[ -v term_kill_needed ]]; then - kill -TERM "${term_child_pid}" 2>/dev/null - fi wait ${term_child_pid} 2>/dev/null + term_child_status=$? trap - TERM - wait ${term_child_pid} 2>/dev/null + if [ "${term_child_status}" -gt 128 ]; then + wait ${term_child_pid} 2>/dev/null + term_child_status=$? + fi set -e + return ${term_child_status} } "#; + +/// Runs the given script with the shell options of a product container and returns its exit +/// status and stdout. +#[cfg(test)] +pub(crate) fn run_container_script(script: &str) -> (i32, String) { + let output = std::process::Command::new("/bin/bash") + .args(["-euo", "pipefail", "-c", script]) + .output() + .expect("bash should be executable in the test environment"); + let exit_code = output + .status + .code() + .expect("the script should terminate regularly and not by a signal"); + let stdout = String::from_utf8(output.stdout).expect("the script should only print UTF-8"); + + (exit_code, stdout) +} + +#[cfg(test)] +mod tests { + use indoc::formatdoc; + + use super::*; + + #[test] + fn crash_with_command_after_the_call() { + let (exit_code, stdout) = run_container_script(&formatdoc! {" + {COMMON_BASH_TRAP_FUNCTIONS} + prepare_signal_handlers + bash -c 'exit 42' & + product_exit_code=0 + wait_for_termination $! || product_exit_code=$? + echo tail-ran + exit \"${{product_exit_code}}\"" + }); + + assert_eq!(42, exit_code); + assert!(stdout.contains("tail-ran\n")); + } + + #[test] + fn sigterm_before_child_pid_is_known() { + let (exit_code, stdout) = run_container_script(&formatdoc! {" + {COMMON_BASH_TRAP_FUNCTIONS} + prepare_signal_handlers + kill -TERM $$ + echo child-started + sleep 10 & + wait_for_termination $!" + }); + + assert_eq!(143, exit_code); + assert!(!stdout.contains("child-started")); + } +}