diff --git a/src/pkg/k8s.go b/src/pkg/k8s.go index dbbf6aa..ecfe447 100644 --- a/src/pkg/k8s.go +++ b/src/pkg/k8s.go @@ -111,6 +111,16 @@ func NewJobRunner(runnerId string, path string) *JobRunner { } } +// getRunnerJobVariable returns the value of the job variable with the given key, if present. +func getRunnerJobVariable(configs []opslevel.RunnerJobVariable, key string) string { + for _, config := range configs { + if config.Key == key { + return config.Value + } + } + return "" +} + // getPodEnv returns the env vars to inject into a container for the given // scope. Variables with no Scope set are visible to every container; variables // with a Scope are only visible to containers running in that scope. @@ -353,12 +363,18 @@ func (s *JobRunner) Run(ctx context.Context, job opslevel.RunnerJob, stdout, std id := string(job.Id) // Once we get off "the old API" method of runner we can circle back around to this // and fix it to generate safe pod names since k8s has limitations. - var identifier string + var jobIdentifier string switch viper.GetString("mode") { case "faktory": - identifier = fmt.Sprintf("opslevel-job-%s-%d", job.Id, time.Now().Unix()) + jobIdentifier = string(job.Id) case "api": - identifier = fmt.Sprintf("opslevel-job-%s-%d", job.Number(), time.Now().Unix()) + jobIdentifier = job.Number() + } + var identifier string + if accountId := getRunnerJobVariable(job.Variables, "account_id"); accountId != "" { + identifier = fmt.Sprintf("opslevel-job-%s-%s-%d", accountId, jobIdentifier, time.Now().Unix()) + } else { + identifier = fmt.Sprintf("opslevel-job-%s-%d", jobIdentifier, time.Now().Unix()) } runnerIdentifier := fmt.Sprintf("runner-%s", s.runnerId) labels := map[string]string{ diff --git a/src/pkg/k8s_test.go b/src/pkg/k8s_test.go index f7322e5..a3b8722 100644 --- a/src/pkg/k8s_test.go +++ b/src/pkg/k8s_test.go @@ -193,6 +193,33 @@ func TestGetPodEnv_FiltersByScope(t *testing.T) { autopilot.Equals(t, []string{"BOTH", "MAIN_ONLY"}, mainKeys) } +func TestGetRunnerJobVariable_ReturnsMatchingValue(t *testing.T) { + // Arrange + vars := []opslevel.RunnerJobVariable{ + {Key: "FOO", Value: "bar"}, + {Key: "account_id", Value: "acct-123"}, + } + + // Act + value := getRunnerJobVariable(vars, "account_id") + + // Assert + autopilot.Equals(t, "acct-123", value) +} + +func TestGetRunnerJobVariable_ReturnsEmptyWhenMissing(t *testing.T) { + // Arrange + vars := []opslevel.RunnerJobVariable{ + {Key: "FOO", Value: "bar"}, + } + + // Act + value := getRunnerJobVariable(vars, "account_id") + + // Assert + autopilot.Equals(t, "", value) +} + func TestGetPodObject_NoInitCommands(t *testing.T) { // Arrange runner := &JobRunner{