Skip to content

Commit

Permalink
fix(reconciler): Support default value for RUNNER_NAME_PREFIX from my…
Browse files Browse the repository at this point in the history
…oung34-derivate

Fixes not-in-sync between GitHub API and running Pods when the default
value for `RUNNER_NAME_PREFIX` is used (and likely also if
`RUNNER_NAME_PREFIX` is set).
  • Loading branch information
sklirg committed Feb 20, 2023
1 parent 456a5b5 commit 38016dd
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion controllers/githubactionrunner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (r *GithubActionRunnerReconciler) getPodRunnerPairs(ctx context.Context, cr

allRunners, err := r.GithubAPI.GetRunners(ctx, cr.Spec.Organization, cr.Spec.Repository, token)
runners := funk.Filter(allRunners, func(r *github.Runner) bool {
return strings.HasPrefix(r.GetName(), cr.Name)
return strings.HasPrefix(r.GetName(), getRunnerNamePrefix(cr))
}).([]*github.Runner)

if err != nil {
Expand All @@ -426,3 +426,32 @@ func (r *GithubActionRunnerReconciler) getPodRunnerPairs(ctx context.Context, cr

return from(podList, runners), err
}

// getRunnerNamePrefix gets the prefix for the name of a runner
func getRunnerNamePrefix(cr *garov1alpha1.GithubActionRunner) string {
runnerPrefix := cr.Name

// If no value is set for env var RUNNER_NAME_PREFIX it defaults to 'github-runner'
// (https://github.com/myoung34/docker-github-actions-runner#environment-variables)
// so we check if it is set, and if not, we prepend it to the runnerPrefix used
// to check if a GitHub Actions Runner was created by us.
hasRunnerNamePrefix := false
for _, container := range cr.Spec.PodTemplateSpec.Spec.Containers {
for _, env := range container.Env {
if env.Name == "RUNNER_NAME_PREFIX" {
hasRunnerNamePrefix = true
runnerPrefix = fmt.Sprintf("%s-%s", env.Value, runnerPrefix)
break
}
}
if hasRunnerNamePrefix {
break
}
}

if !hasRunnerNamePrefix {
runnerPrefix = fmt.Sprintf("github-runner-%s", runnerPrefix)
}

return runnerPrefix
}

0 comments on commit 38016dd

Please sign in to comment.