Skip to content

Commit 201abbf

Browse files
committed
Fall back to simpler behavior, if setsid,ps,pkill are not installed
1 parent 02aba63 commit 201abbf

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

pkg/cli/admin/mustgather/mustgather.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,14 @@ usage_percentage=$(df -P "$target_dir" | awk 'NR==2 {print $5}' | sed 's/%%//')
9999
echo "[disk usage checker] Volume usage percentage: current = ${usage_percentage} ; allowed = ${usage_percentage_limit}"
100100
if [ "$usage_percentage" -gt "$usage_percentage_limit" ]; then
101101
echo "[disk usage checker] Disk usage exceeds the volume percentage of ${usage_percentage_limit} for mounted directory, terminating..."
102-
ps -o sess --no-headers | sort -u | while read sid; do
103-
[[ "$sid" -eq "${$}" ]] && continue
104-
pkill --signal SIGKILL --session "$sid"
105-
done
102+
if [ "$HAVE_SESSION_TOOLS" = "true" ]; then
103+
ps -o sess --no-headers | sort -u | while read sid; do
104+
[[ "$sid" -eq "${$}" ]] && continue
105+
pkill --signal SIGKILL --session "$sid"
106+
done
107+
else
108+
kill 0
109+
fi
106110
exit 1
107111
fi
108112
sleep 5
@@ -1321,16 +1325,30 @@ func buildPodCommand(
13211325
) string {
13221326
var cmd strings.Builder
13231327

1328+
// Check if session management tools are available once and store in a variable.
1329+
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")
1330+
cmd.WriteString(" HAVE_SESSION_TOOLS=true\n")
1331+
cmd.WriteString("else\n")
1332+
cmd.WriteString(" HAVE_SESSION_TOOLS=false\n")
1333+
cmd.WriteString("fi\n\n")
1334+
13241335
// Start the checker in the background.
13251336
cmd.WriteString(volumeCheckerScript)
13261337
cmd.WriteString(` & `)
13271338

1328-
// Start the gather command in a separate session.
1329-
cmd.WriteString("setsid -w bash <<-MUSTGATHER_EOF\n")
1339+
// Start the gather command in a separate session if setsid, ps, and pkill are available.
1340+
// Fall back to simpler approach if any of these tools are not present (minimal images).
1341+
cmd.WriteString(`if [ "$HAVE_SESSION_TOOLS" = "true" ]; then`)
1342+
cmd.WriteString("\n setsid -w bash <<-MUSTGATHER_EOF\n")
13301343
cmd.WriteString(gatherCommand)
13311344
cmd.WriteString("\nMUSTGATHER_EOF\n")
1345+
cmd.WriteString("else\n")
1346+
cmd.WriteString(" ")
1347+
cmd.WriteString(gatherCommand)
1348+
cmd.WriteString("\nfi; ")
13321349

13331350
// Make sure all changes are written to disk.
13341351
cmd.WriteString(`sync && echo 'Caches written to disk'`)
1352+
13351353
return cmd.String()
13361354
}

pkg/cli/admin/mustgather/mustgather_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,19 +454,37 @@ func TestBuildPodCommand(t *testing.T) {
454454
name: "default gather command",
455455
volumeUsageCheckerScript: "sleep infinity",
456456
gatherCommand: "/usr/bin/gather",
457-
expectedCommand: `sleep infinity & setsid -w bash <<-MUSTGATHER_EOF
457+
expectedCommand: `if command -v setsid >/dev/null 2>&1 && command -v ps >/dev/null 2>&1 && command -v pkill >/dev/null 2>&1; then
458+
HAVE_SESSION_TOOLS=true
459+
else
460+
HAVE_SESSION_TOOLS=false
461+
fi
462+
463+
sleep infinity & if [ "$HAVE_SESSION_TOOLS" = "true" ]; then
464+
setsid -w bash <<-MUSTGATHER_EOF
458465
/usr/bin/gather
459466
MUSTGATHER_EOF
460-
sync && echo 'Caches written to disk'`,
467+
else
468+
/usr/bin/gather
469+
fi; sync && echo 'Caches written to disk'`,
461470
},
462471
{
463472
name: "custom gather command",
464473
volumeUsageCheckerScript: "sleep infinity",
465474
gatherCommand: "sed -i 's#--rotated-pod-logs# #g' /usr/bin/*gather* && /usr/bin/gather",
466-
expectedCommand: `sleep infinity & setsid -w bash <<-MUSTGATHER_EOF
475+
expectedCommand: `if command -v setsid >/dev/null 2>&1 && command -v ps >/dev/null 2>&1 && command -v pkill >/dev/null 2>&1; then
476+
HAVE_SESSION_TOOLS=true
477+
else
478+
HAVE_SESSION_TOOLS=false
479+
fi
480+
481+
sleep infinity & if [ "$HAVE_SESSION_TOOLS" = "true" ]; then
482+
setsid -w bash <<-MUSTGATHER_EOF
467483
sed -i 's#--rotated-pod-logs# #g' /usr/bin/*gather* && /usr/bin/gather
468484
MUSTGATHER_EOF
469-
sync && echo 'Caches written to disk'`,
485+
else
486+
sed -i 's#--rotated-pod-logs# #g' /usr/bin/*gather* && /usr/bin/gather
487+
fi; sync && echo 'Caches written to disk'`,
470488
},
471489
}
472490

0 commit comments

Comments
 (0)