From 22c6e828f229f877ef69240bc5eebd421b40ca5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Solbj=C3=B8rg?= Date: Mon, 20 Feb 2023 16:51:09 +0100 Subject: [PATCH] fix(reconciler): Support default value for RUNNER_NAME_PREFIX from myoung34-derivate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- controllers/githubactionrunner_controller.go | 31 +++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/controllers/githubactionrunner_controller.go b/controllers/githubactionrunner_controller.go index c00cb025..dc84385c 100644 --- a/controllers/githubactionrunner_controller.go +++ b/controllers/githubactionrunner_controller.go @@ -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 { @@ -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 +}