Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed /bin/sh and robustified entrypoint/command translation k8s #34

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/slurm/Create.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,17 @@ func (h *SidecarHandler) SubmitHandler(w http.ResponseWriter, r *http.Request) {
singularityOptions = singOpts
}

commstr1 := []string{"singularity", "exec", "--containall", "--nv", singularityMounts, singularityOptions}
// See https://github.com/interTwin-eu/interlink-slurm-plugin/issues/32#issuecomment-2416031030
// singularity run will honor the entrypoint/command (if exist) in container image, while exec will override entrypoint.
// Thus if pod command (equivalent to container entrypoint) exist, we do exec, and other case we do run
singularityCommand := ""
if container.Command != nil && len(container.Command) != 0 {
singularityCommand = "exec"
} else {
singularityCommand = "run"
}

commstr1 := []string{"singularity", singularityCommand, "--containall", "--nv", singularityMounts, singularityOptions}

image := ""

Expand Down
90 changes: 30 additions & 60 deletions pkg/slurm/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,6 @@ func prepareMounts(

}

mountedData += "--bind " + config.DataRootFolder + podData.Pod.Namespace + "-" + string(podData.Pod.UID) + "/" + "command_" + container.Name + ".sh" +
":" + "/tmp/" + "command_" + container.Name + ".sh "
mountedData += "--bind " + config.DataRootFolder + podData.Pod.Namespace + "-" + string(podData.Pod.UID) + "/" + "args_" + container.Name + ".sh" +
":" + "/tmp/" + "args_" + container.Name + ".sh"

if last := len(mountedData) - 1; last >= 0 && mountedData[last] == ',' {
mountedData = mountedData[:last]
}
Expand Down Expand Up @@ -482,74 +477,49 @@ func produceSLURMScript(

log.G(Ctx).Debug("--- Writing file")

var stringToBeWritten string
var stringToBeWritten strings.Builder

stringToBeWritten += sbatch_macros
stringToBeWritten.WriteString(sbatch_macros)

for _, singularityCommand := range commands {

cmdString := ""

f3, err := os.Create(path + "/" + "command_" + singularityCommand.containerName + ".sh")
if err != nil {
log.G(Ctx).Error(err)
return "", err
}
defer f3.Close()

f2, err := os.Create(path + "/" + "args_" + singularityCommand.containerName + ".sh")
if err != nil {
log.G(Ctx).Error(err)
return "", err
}
defer f2.Close()
stringToBeWritten.WriteString("\n")
stringToBeWritten.WriteString(strings.Join(singularityCommand.singularityCommand[:], " "))

if len(singularityCommand.containerArgs) != 0 {
if singularityCommand.containerCommand != nil {
cmdString = strings.Join(singularityCommand.containerCommand, " ") + " \"$(cat /tmp/args_" + singularityCommand.containerName + ".sh)\""
} else {
cmdString = "/bin/sh -c \"$(cat /tmp/args_" + singularityCommand.containerName + ".sh)\""
if singularityCommand.containerCommand != nil {
// Case the pod specified a container entrypoint array to override.
for _, commandEntry := range singularityCommand.containerCommand {
stringToBeWritten.WriteString(" ")
// We convert from GO array to shell command, so escaping is important to avoid space, quote issues and injection vulnerabilities.
stringToBeWritten.WriteString(shellescape.Quote(commandEntry))
}
} else {
cmdString = strings.Join(singularityCommand.containerCommand, " ")
}

_, err = f3.WriteString(cmdString)
if err != nil {
log.G(Ctx).Error(err)
return "", err
} else {
log.G(Ctx).Debug("---- Written command file " + f3.Name())
}

argString := strings.Join(singularityCommand.containerArgs, " ")
_, err = f2.WriteString(argString)
if err != nil {
log.G(Ctx).Error(err)
return "", err
} else {
log.G(Ctx).Debug("---- Written arguments file " + f2.Name())
if singularityCommand.containerArgs != nil {
// Case the pod specified a container command array to override.
for _, argsEntry := range singularityCommand.containerArgs {
stringToBeWritten.WriteString(" ")
// We convert from GO array to shell command, so escaping is important to avoid space, quote issues and injection vulnerabilities.
stringToBeWritten.WriteString(shellescape.Quote(argsEntry))
}
}

os.Chmod(f2.Name(), 0777|os.ModePerm)
os.Chmod(f3.Name(), 0777|os.ModePerm)

if singularityCommand.isInitContainer {
stringToBeWritten += "\n" + strings.Join(singularityCommand.singularityCommand[:], " ") + " " +
"/bin/sh" + " /tmp/" + "command_" + singularityCommand.containerName + ".sh" +
" &> " + path + "/" + singularityCommand.containerName + ".out; " +
"echo $? > " + path + "/" + singularityCommand.containerName + ".status"
} else {
stringToBeWritten += "\n" + strings.Join(singularityCommand.singularityCommand[:], " ") + " " +
"/bin/sh" + " /tmp/" + "command_" + singularityCommand.containerName + ".sh" +
" &> " + path + "/" + singularityCommand.containerName + ".out; " +
"echo $? > " + path + "/" + singularityCommand.containerName + ".status; sleep 30 &"
stringToBeWritten.WriteString(" &> ")
stringToBeWritten.WriteString(path)
stringToBeWritten.WriteString("/")
stringToBeWritten.WriteString(singularityCommand.containerName)
stringToBeWritten.WriteString(".out; ")
stringToBeWritten.WriteString("echo $? > " + path + "/" + singularityCommand.containerName + ".status")

if ! singularityCommand.isInitContainer {
// Not init containers are run in parallel.
stringToBeWritten.WriteString("; sleep 30 &")
}
}

stringToBeWritten += "\n" + postfix
stringToBeWritten.WriteString("\n")
stringToBeWritten.WriteString(postfix)

_, err = f.WriteString(stringToBeWritten)
_, err = f.WriteString(stringToBeWritten.String())

if err != nil {
log.G(Ctx).Error(err)
Expand Down
Loading