From 201abbf9ae99557519050abe9ba0acc58c588a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Tue, 25 Nov 2025 15:06:36 +0300 Subject: [PATCH] Fall back to simpler behavior, if setsid,ps,pkill are not installed --- pkg/cli/admin/mustgather/mustgather.go | 30 ++++++++++++++++----- pkg/cli/admin/mustgather/mustgather_test.go | 26 +++++++++++++++--- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/pkg/cli/admin/mustgather/mustgather.go b/pkg/cli/admin/mustgather/mustgather.go index ec3afe52f3..486255a32f 100644 --- a/pkg/cli/admin/mustgather/mustgather.go +++ b/pkg/cli/admin/mustgather/mustgather.go @@ -99,10 +99,14 @@ usage_percentage=$(df -P "$target_dir" | awk 'NR==2 {print $5}' | sed 's/%%//') echo "[disk usage checker] Volume usage percentage: current = ${usage_percentage} ; allowed = ${usage_percentage_limit}" if [ "$usage_percentage" -gt "$usage_percentage_limit" ]; then echo "[disk usage checker] Disk usage exceeds the volume percentage of ${usage_percentage_limit} for mounted directory, terminating..." - ps -o sess --no-headers | sort -u | while read sid; do - [[ "$sid" -eq "${$}" ]] && continue - pkill --signal SIGKILL --session "$sid" - done + if [ "$HAVE_SESSION_TOOLS" = "true" ]; then + ps -o sess --no-headers | sort -u | while read sid; do + [[ "$sid" -eq "${$}" ]] && continue + pkill --signal SIGKILL --session "$sid" + done + else + kill 0 + fi exit 1 fi sleep 5 @@ -1321,16 +1325,30 @@ func buildPodCommand( ) string { var cmd strings.Builder + // Check if session management tools are available once and store in a variable. + cmd.WriteString("if command -v setsid >/dev/null 2>&1 && command -v ps >/dev/null 2>&1 && command -v pkill >/dev/null 2>&1; then\n") + cmd.WriteString(" HAVE_SESSION_TOOLS=true\n") + cmd.WriteString("else\n") + cmd.WriteString(" HAVE_SESSION_TOOLS=false\n") + cmd.WriteString("fi\n\n") + // Start the checker in the background. cmd.WriteString(volumeCheckerScript) cmd.WriteString(` & `) - // Start the gather command in a separate session. - cmd.WriteString("setsid -w bash <<-MUSTGATHER_EOF\n") + // Start the gather command in a separate session if setsid, ps, and pkill are available. + // Fall back to simpler approach if any of these tools are not present (minimal images). + cmd.WriteString(`if [ "$HAVE_SESSION_TOOLS" = "true" ]; then`) + cmd.WriteString("\n setsid -w bash <<-MUSTGATHER_EOF\n") cmd.WriteString(gatherCommand) cmd.WriteString("\nMUSTGATHER_EOF\n") + cmd.WriteString("else\n") + cmd.WriteString(" ") + cmd.WriteString(gatherCommand) + cmd.WriteString("\nfi; ") // Make sure all changes are written to disk. cmd.WriteString(`sync && echo 'Caches written to disk'`) + return cmd.String() } diff --git a/pkg/cli/admin/mustgather/mustgather_test.go b/pkg/cli/admin/mustgather/mustgather_test.go index b881e569e2..358ae03c4a 100644 --- a/pkg/cli/admin/mustgather/mustgather_test.go +++ b/pkg/cli/admin/mustgather/mustgather_test.go @@ -454,19 +454,37 @@ func TestBuildPodCommand(t *testing.T) { name: "default gather command", volumeUsageCheckerScript: "sleep infinity", gatherCommand: "/usr/bin/gather", - expectedCommand: `sleep infinity & setsid -w bash <<-MUSTGATHER_EOF + expectedCommand: `if command -v setsid >/dev/null 2>&1 && command -v ps >/dev/null 2>&1 && command -v pkill >/dev/null 2>&1; then + HAVE_SESSION_TOOLS=true +else + HAVE_SESSION_TOOLS=false +fi + +sleep infinity & if [ "$HAVE_SESSION_TOOLS" = "true" ]; then + setsid -w bash <<-MUSTGATHER_EOF /usr/bin/gather MUSTGATHER_EOF -sync && echo 'Caches written to disk'`, +else + /usr/bin/gather +fi; sync && echo 'Caches written to disk'`, }, { name: "custom gather command", volumeUsageCheckerScript: "sleep infinity", gatherCommand: "sed -i 's#--rotated-pod-logs# #g' /usr/bin/*gather* && /usr/bin/gather", - expectedCommand: `sleep infinity & setsid -w bash <<-MUSTGATHER_EOF + expectedCommand: `if command -v setsid >/dev/null 2>&1 && command -v ps >/dev/null 2>&1 && command -v pkill >/dev/null 2>&1; then + HAVE_SESSION_TOOLS=true +else + HAVE_SESSION_TOOLS=false +fi + +sleep infinity & if [ "$HAVE_SESSION_TOOLS" = "true" ]; then + setsid -w bash <<-MUSTGATHER_EOF sed -i 's#--rotated-pod-logs# #g' /usr/bin/*gather* && /usr/bin/gather MUSTGATHER_EOF -sync && echo 'Caches written to disk'`, +else + sed -i 's#--rotated-pod-logs# #g' /usr/bin/*gather* && /usr/bin/gather +fi; sync && echo 'Caches written to disk'`, }, }