Skip to content

Commit 22c6e82

Browse files
committed
fix(reconciler): Support default value for RUNNER_NAME_PREFIX from myoung34-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). Signed-off-by: Håkon Solbjørg <[email protected]>
1 parent 456a5b5 commit 22c6e82

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

controllers/githubactionrunner_controller.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func (r *GithubActionRunnerReconciler) getPodRunnerPairs(ctx context.Context, cr
417417

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

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

427427
return from(podList, runners), err
428428
}
429+
430+
// getRunnerNamePrefix gets the prefix for the name of a runner
431+
func getRunnerNamePrefix(cr *garov1alpha1.GithubActionRunner) string {
432+
runnerPrefix := cr.Name
433+
434+
// If no value is set for env var RUNNER_NAME_PREFIX it defaults to 'github-runner'
435+
// (https://github.com/myoung34/docker-github-actions-runner#environment-variables)
436+
// so we check if it is set, and if not, we prepend it to the runnerPrefix used
437+
// to check if a GitHub Actions Runner was created by us.
438+
hasRunnerNamePrefix := false
439+
for _, container := range cr.Spec.PodTemplateSpec.Spec.Containers {
440+
for _, env := range container.Env {
441+
if env.Name == "RUNNER_NAME_PREFIX" {
442+
hasRunnerNamePrefix = true
443+
runnerPrefix = fmt.Sprintf("%s-%s", env.Value, runnerPrefix)
444+
break
445+
}
446+
}
447+
if hasRunnerNamePrefix {
448+
break
449+
}
450+
}
451+
452+
if !hasRunnerNamePrefix {
453+
runnerPrefix = fmt.Sprintf("github-runner-%s", runnerPrefix)
454+
}
455+
456+
return runnerPrefix
457+
}

0 commit comments

Comments
 (0)