-
-
Save alienth/e2e356c719f7a47b0af0dde829c4601d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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... | |
} |
Author
alienth
commented
Dec 9, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment