From 9ebe54ed89b31ed1a86286aa43b20ccf9f8084bb Mon Sep 17 00:00:00 2001 From: Wesley Ellis Date: Tue, 7 Jul 2026 13:36:06 -0400 Subject: [PATCH 1/2] Include account_id job variable in pod/configmap/pdb name Jobs that set an account_id variable now get it woven into the generated k8s object name (opslevel-job---), making it easier to identify which account a running job belongs to. Falls back to the existing naming scheme when account_id is absent. Co-Authored-By: Claude Sonnet 5 --- src/pkg/k8s.go | 22 +++++++++++++++++++--- src/pkg/k8s_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/pkg/k8s.go b/src/pkg/k8s.go index dbbf6aa..d34b6db 100644 --- a/src/pkg/k8s.go +++ b/src/pkg/k8s.go @@ -111,6 +111,16 @@ func NewJobRunner(runnerId string, path string) *JobRunner { } } +// getVariable returns the value of the job variable with the given key, if present. +func getVariable(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 := getVariable(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..7b9402b 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 TestGetVariable_ReturnsMatchingValue(t *testing.T) { + // Arrange + vars := []opslevel.RunnerJobVariable{ + {Key: "FOO", Value: "bar"}, + {Key: "account_id", Value: "acct-123"}, + } + + // Act + value := getVariable(vars, "account_id") + + // Assert + autopilot.Equals(t, "acct-123", value) +} + +func TestGetVariable_ReturnsEmptyWhenMissing(t *testing.T) { + // Arrange + vars := []opslevel.RunnerJobVariable{ + {Key: "FOO", Value: "bar"}, + } + + // Act + value := getVariable(vars, "account_id") + + // Assert + autopilot.Equals(t, "", value) +} + func TestGetPodObject_NoInitCommands(t *testing.T) { // Arrange runner := &JobRunner{ From a0cfb4886b1068bc1ec995347547328d32a3a90e Mon Sep 17 00:00:00 2001 From: Wesley Ellis Date: Tue, 7 Jul 2026 15:06:39 -0400 Subject: [PATCH 2/2] Rename getVariable to getRunnerJobVariable for clarity Addresses review feedback: a similar helper will be needed for egress proxies, so the name should be more explicit about what it operates on. Co-Authored-By: Claude Sonnet 5 --- src/pkg/k8s.go | 6 +++--- src/pkg/k8s_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/pkg/k8s.go b/src/pkg/k8s.go index d34b6db..ecfe447 100644 --- a/src/pkg/k8s.go +++ b/src/pkg/k8s.go @@ -111,8 +111,8 @@ func NewJobRunner(runnerId string, path string) *JobRunner { } } -// getVariable returns the value of the job variable with the given key, if present. -func getVariable(configs []opslevel.RunnerJobVariable, key string) string { +// 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 @@ -371,7 +371,7 @@ func (s *JobRunner) Run(ctx context.Context, job opslevel.RunnerJob, stdout, std jobIdentifier = job.Number() } var identifier string - if accountId := getVariable(job.Variables, "account_id"); accountId != "" { + 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()) diff --git a/src/pkg/k8s_test.go b/src/pkg/k8s_test.go index 7b9402b..a3b8722 100644 --- a/src/pkg/k8s_test.go +++ b/src/pkg/k8s_test.go @@ -193,7 +193,7 @@ func TestGetPodEnv_FiltersByScope(t *testing.T) { autopilot.Equals(t, []string{"BOTH", "MAIN_ONLY"}, mainKeys) } -func TestGetVariable_ReturnsMatchingValue(t *testing.T) { +func TestGetRunnerJobVariable_ReturnsMatchingValue(t *testing.T) { // Arrange vars := []opslevel.RunnerJobVariable{ {Key: "FOO", Value: "bar"}, @@ -201,20 +201,20 @@ func TestGetVariable_ReturnsMatchingValue(t *testing.T) { } // Act - value := getVariable(vars, "account_id") + value := getRunnerJobVariable(vars, "account_id") // Assert autopilot.Equals(t, "acct-123", value) } -func TestGetVariable_ReturnsEmptyWhenMissing(t *testing.T) { +func TestGetRunnerJobVariable_ReturnsEmptyWhenMissing(t *testing.T) { // Arrange vars := []opslevel.RunnerJobVariable{ {Key: "FOO", Value: "bar"}, } // Act - value := getVariable(vars, "account_id") + value := getRunnerJobVariable(vars, "account_id") // Assert autopilot.Equals(t, "", value)