Skip to content

Instantly share code, notes, and snippets.

@alienth

alienth/foo.go Secret

Last active December 9, 2020 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alienth/e2e356c719f7a47b0af0dde829c4601d to your computer and use it in GitHub Desktop.
Save alienth/e2e356c719f7a47b0af0dde829c4601d to your computer and use it in GitHub Desktop.
// Takes an entry point and a file path and returns a script which will execute
// the entry point and kill it once the file path exists. Used for daemon
// sidecars running on transient job workloads.
//
// Requires dumb-init to fully function, but if dumb-init is not present it
// will print an error and fail-open. This is intended to prevent breakage with
// any preexisting CronJob workflows that might not have dumb-init installed.
func sidecarWrapperCommand(sidecarEntryPoint []string, sentinelFilePath string) []string {
return append([]string{
// There is an outer bash here, but it will always call exec one
// way or another; the outer bash will not hold back any signals.
"/bin/bash", "-c",
// exec dumb-init if it's there. If not, print an error and fail-open, execing the cron's original command.
`command -v dumb-init >/dev/null && ` +
`exec dumb-init "${@:0}" || ` +
`>&2 echo "ERROR: dumb-init not found. Running sidecar with no task-completion wrapper!" && ` +
`exec $3 "${@:4}"`, // This skips over the subsequent bash, -c, and wrapper script.
"/bin/bash", "-c",
// Run $0 in background. $0 is passed in as an argument at the end of this slice.
`$0 "${@:1}" & ` +
// While loop waiting for the sentinel to exist, at which point we kill the background job.
`while true; do ` +
`[[ -f "` + sentinelFilePath + `" ]] && break; ` +
`sleep 1; ` +
`done; ` +
`kill %1`,
}, sidecarEntryPoint...) // $0...
}
func defaultJobWrapper(jobEntryPoint []string, sentinelFilePath string) []string {
return append([]string{
"/bin/bash", "-c",
`command -v dumb-init >/dev/null && ` +
`exec dumb-init "${@:0}" || ` +
`>&2 echo "ERROR: dumb-init not found. Running job with no task-completion wrapper!" && ` +
`exec $3 "${@:4}"`, // This skips over the subsequent bash, -c, and wrapper script.
"/bin/bash", "-c",
// Run $0, touching the sentinel upon exit.
`trap 'touch ` + sentinelFilePath + `' EXIT; $0 "${@:1}"`,
}, jobEntryPoint...) // $0...
}
@alienth
Copy link
Author

alienth commented Dec 9, 2020

alienth  31129  0.0  0.0  12888  3352 ?        Ss   22:35   0:00 /bin/bash -c $0 "${@:1}" & while true; do [[ -f "/tmp/die" ]] && break; sleep 1; done; kill %1 dumb-init dumb-init dumb-init dumb-init bash -c tail -f /etc/passwd /etc/lsb-release & sleep 200
alienth  31130  0.0  0.0   4388   768 ?        S    22:35   0:00 dumb-init dumb-init dumb-init dumb-init bash -c tail -f /etc/passwd /etc/lsb-release & sleep 200
alienth  31132  0.0  0.0   4388   872 ?        Ss   22:35   0:00 dumb-init dumb-init dumb-init bash -c tail -f /etc/passwd /etc/lsb-release & sleep 200
alienth  31133  0.0  0.0   4388   836 ?        Ss   22:35   0:00 dumb-init dumb-init bash -c tail -f /etc/passwd /etc/lsb-release & sleep 200
alienth  31134  0.0  0.0   4388   756 ?        Ss   22:35   0:00 dumb-init bash -c tail -f /etc/passwd /etc/lsb-release & sleep 200
alienth  31135  0.0  0.0  12888  3184 ?        Ss   22:35   0:00 bash -c tail -f /etc/passwd /etc/lsb-release & sleep 200
alienth  31136  0.0  0.0   7508   924 ?        S    22:35   0:00 tail -f /etc/passwd /etc/lsb-release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment