Skip to content

Commit 0dc285a

Browse files
committed
Fall back to simpler behavior, if setsid is not installed
1 parent 8642d88 commit 0dc285a

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

pkg/cli/admin/mustgather/mustgather.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,15 @@ while true; do
9898
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
101-
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
106-
exit 1
101+
echo "[disk usage checker] Disk usage exceeds the volume percentage of ${usage_percentage_limit} for mounted directory, terminating..." >&2
102+
if command -v setsid >/dev/null 2>&1; 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
107110
fi
108111
sleep 5
109112
done`
@@ -1325,12 +1328,19 @@ func buildPodCommand(
13251328
cmd.WriteString(volumeCheckerScript)
13261329
cmd.WriteString(` & `)
13271330

1328-
// Start the gather command in a separate session.
1329-
cmd.WriteString("setsid -w bash <<-MUSTGATHER_EOF\n")
1331+
// Start the gather command in a separate session if setsid is available.
1332+
// Fall back to simpler approach if setsid is not present (minimal images).
1333+
cmd.WriteString("if command -v setsid >/dev/null 2>&1; then\n")
1334+
cmd.WriteString(" setsid -w bash <<-MUSTGATHER_EOF\n")
13301335
cmd.WriteString(gatherCommand)
13311336
cmd.WriteString("\nMUSTGATHER_EOF\n")
1337+
cmd.WriteString("else\n")
1338+
cmd.WriteString(" ")
1339+
cmd.WriteString(gatherCommand)
1340+
cmd.WriteString("\nfi; ")
13321341

13331342
// Make sure all changes are written to disk.
13341343
cmd.WriteString(`sync && echo 'Caches written to disk'`)
1344+
13351345
return cmd.String()
13361346
}

pkg/cli/admin/mustgather/mustgather_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,19 +454,25 @@ 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: `sleep infinity & if command -v setsid >/dev/null 2>&1; then
458+
setsid -w bash <<-MUSTGATHER_EOF
458459
/usr/bin/gather
459460
MUSTGATHER_EOF
460-
sync && echo 'Caches written to disk'`,
461+
else
462+
/usr/bin/gather
463+
fi; sync && echo 'Caches written to disk'`,
461464
},
462465
{
463466
name: "custom gather command",
464467
volumeUsageCheckerScript: "sleep infinity",
465468
gatherCommand: "sed -i 's#--rotated-pod-logs# #g' /usr/bin/*gather* && /usr/bin/gather",
466-
expectedCommand: `sleep infinity & setsid -w bash <<-MUSTGATHER_EOF
469+
expectedCommand: `sleep infinity & if command -v setsid >/dev/null 2>&1; then
470+
setsid -w bash <<-MUSTGATHER_EOF
467471
sed -i 's#--rotated-pod-logs# #g' /usr/bin/*gather* && /usr/bin/gather
468472
MUSTGATHER_EOF
469-
sync && echo 'Caches written to disk'`,
473+
else
474+
sed -i 's#--rotated-pod-logs# #g' /usr/bin/*gather* && /usr/bin/gather
475+
fi; sync && echo 'Caches written to disk'`,
470476
},
471477
}
472478

0 commit comments

Comments
 (0)