Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
trana authored and trana committed Oct 8, 2024
1 parent 238b48f commit cf92d12
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions pkg/slurm/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,11 @@ func (h *SidecarHandler) LoadJIDs() error {
return nil
}

// prepareEnvs reads all Environment variables from a container and append them to a envfile.properties. The values are bash-escaped.
// It returns the slice containing, if there are Environment variables, the arguments for envfile and its path, or else an empty array.
func prepareEnvs(Ctx context.Context, config SlurmConfig, podData commonIL.RetrievedPodData, container v1.Container) []string {
start := time.Now().UnixMicro()
span := trace.SpanFromContext(Ctx)
span.AddEvent("Preparing ENVs for container " + container.Name)
var envs []string = []string{}
// For debugging purpose only
envs_data := []string{}

if len(container.Env) > 0 {
func createEnvFile(Ctx context.Context, config SlurmConfig, podData commonIL.RetrievedPodData, container v1.Container) ([]string, []string, error) {
envs := []string{}
// For debugging purpose only
envs_data := []string{}

envfilePath := (config.DataRootFolder + podData.Pod.Namespace + "-" + string(podData.Pod.UID) + "/" + "envfile.properties")
log.G(Ctx).Info("-- Appending envs using envfile " + envfilePath)
envs = append(envs, "--env-file")
Expand All @@ -204,7 +198,7 @@ func prepareEnvs(Ctx context.Context, config SlurmConfig, podData commonIL.Retri
envfile, err := os.Create(envfilePath)
if err != nil {
log.G(Ctx).Error(err)
return "", err
return "", "", err
}
defer envfile.Close()

Expand All @@ -219,7 +213,7 @@ func prepareEnvs(Ctx context.Context, config SlurmConfig, podData commonIL.Retri
_, err = envfile.WriteString(tmp + "\n")
if err != nil {
log.G(Ctx).Error(err)
return "", err
return "", "", err
} else {
log.G(Ctx).Debug("---- Written envfile file " + envfilePath + " key " + envVar.Name + " value " + tmpValue)
}
Expand All @@ -229,11 +223,31 @@ func prepareEnvs(Ctx context.Context, config SlurmConfig, podData commonIL.Retri
err = envfile.Sync()
if err != nil {
log.G(Ctx).Error(err)
return "", err
return "", "", err
}

// Calling Close() in case of error. If not error, the defer will close it again but it should be idempotent.
envfile.Close()

return envs, envs_data, nil
}

// prepareEnvs reads all Environment variables from a container and append them to a envfile.properties. The values are bash-escaped.
// It returns the slice containing, if there are Environment variables, the arguments for envfile and its path, or else an empty array.
func prepareEnvs(Ctx context.Context, config SlurmConfig, podData commonIL.RetrievedPodData, container v1.Container) []string {
start := time.Now().UnixMicro()
span := trace.SpanFromContext(Ctx)
span.AddEvent("Preparing ENVs for container " + container.Name)
var envs []string = []string{}
// For debugging purpose only
envs_data := []string{}

if len(container.Env) > 0 {
envs, envs_data, err = createEnvFile(Ctx, config, podData, container)
if err != nil {
log.G(Ctx).Error(err)
return "", err
}
}

duration := time.Now().UnixMicro() - start
Expand Down

0 comments on commit cf92d12

Please sign in to comment.