Skip to content

Commit dd141b0

Browse files
authored
Add job pod work dir configuration (#256)
1 parent f302b65 commit dd141b0

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Feature
2+
body: Add ability to configure the job pod working directory using `--job-pod-workdir` or `OPSLEVEL_JOB_POD_WORKDIR` or the configuration file.
3+
time: 2025-08-08T11:11:06.018244-04:00

src/cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func init() {
4747
rootCmd.PersistentFlags().Int64("job-pod-limits-cpu", 1000, "The job pod resource limits cpu millicores.")
4848
rootCmd.PersistentFlags().Int64("job-pod-limits-memory", 1024, "The job pod resource limits in MB.")
4949
rootCmd.PersistentFlags().String("job-pod-shell", "/bin/sh", "The job pod shell to use for commands run inside the pod.")
50+
rootCmd.PersistentFlags().String("job-pod-workdir", "/jobs", "The job pod working directory.")
5051
rootCmd.PersistentFlags().Int("job-pod-log-max-interval", 30, "The max amount of time between when pod logs are shipped to OpsLevel. Works in tandem with 'job-pod-log-max-size'")
5152
rootCmd.PersistentFlags().Int("job-pod-log-max-size", 1000000, "The max amount in bytes to buffer before pod logs are shipped to OpsLevel. Works in tandem with 'job-pod-log-max-interval'")
5253

@@ -67,6 +68,7 @@ func init() {
6768
viper.BindEnv("job-pod-max-lifetime", "OPSLEVEL_JOB_POD_MAX_LIFETIME")
6869
viper.BindEnv("job-pod-namespace", "OPSLEVEL_JOB_POD_NAMESPACE")
6970
viper.BindEnv("job-pod-shell", "OPSLEVEL_JOB_POD_SHELL")
71+
viper.BindEnv("job-pod-workdir", "OPSLEVEL_JOB_POD_WORKDIR")
7072
viper.BindEnv("job-pod-log-max-interval", "OPSLEVEL_JOB_POD_LOG_MAX_INTERVAL")
7173
viper.BindEnv("job-pod-log-max-size", "OPSLEVEL_JOB_POD_LOG_MAX_SIZE")
7274

src/pkg/k8s.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"path"
78
"strings"
89
"time"
910

@@ -295,9 +296,9 @@ func (s *JobRunner) Run(ctx context.Context, job opslevel.RunnerJob, stdout, std
295296
}
296297
}
297298

298-
workingDirectory := fmt.Sprintf("/jobs/%s/", id)
299+
workingDirectory := path.Join(s.podConfig.WorkingDir, id)
299300
commands := append([]string{fmt.Sprintf("mkdir -p %s", workingDirectory), fmt.Sprintf("cd %s", workingDirectory), "set -xv"}, job.Commands...)
300-
runErr := s.Exec(ctx, stdout, stderr, pod, pod.Spec.Containers[0].Name, viper.GetString("job-pod-shell"), "-e", "-c", strings.Join(commands, ";\n"))
301+
runErr := s.Exec(ctx, stdout, stderr, pod, pod.Spec.Containers[0].Name, s.podConfig.Shell, "-e", "-c", strings.Join(commands, ";\n"))
301302
if runErr != nil {
302303
return JobOutcome{
303304
Message: fmt.Sprintf("pod execution failed REASON: %s %s", strings.TrimSuffix(stderr.String(), "\n"), runErr),

src/pkg/k8s_config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type K8SPodConfig struct {
1717
Namespace string `yaml:"namespace"`
1818
Lifetime int `yaml:"lifetime"` // in seconds
1919
Shell string `yaml:"shell"`
20+
WorkingDir string `yaml:"workingDir"`
2021
Annotations map[string]string `yaml:"annotations"`
2122
Resources corev1.ResourceRequirements `yaml:"resources"`
2223
ServiceAccountName string `yaml:"serviceAccountName"`
@@ -30,9 +31,10 @@ type K8SPodConfig struct {
3031
func ReadPodConfig(path string) (*K8SPodConfig, error) {
3132
config := Config{
3233
Kubernetes: K8SPodConfig{
33-
Namespace: viper.GetString("job-pod-namespace"),
34-
Lifetime: viper.GetInt("job-pod-max-lifetime"),
35-
Shell: viper.GetString("job-pod-shell"),
34+
Namespace: viper.GetString("job-pod-namespace"),
35+
Lifetime: viper.GetInt("job-pod-max-lifetime"),
36+
Shell: viper.GetString("job-pod-shell"),
37+
WorkingDir: viper.GetString("job-pod-workdir"),
3638
Resources: corev1.ResourceRequirements{
3739
Requests: corev1.ResourceList{
3840
corev1.ResourceCPU: *resource.NewMilliQuantity(viper.GetInt64("job-pod-requests-cpu"), resource.DecimalSI),

0 commit comments

Comments
 (0)