Skip to content
Draft
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
22 changes: 19 additions & 3 deletions src/pkg/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the addition of this jobIdentifier 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the diff makes it look weirder than it is. This block now just gets the job identifier, then line 373 constructs identifier with account id if present

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{
Expand Down
27 changes: 27 additions & 0 deletions src/pkg/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading