-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add GitHub workflow and job metadata labels to runner pods #4256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,8 @@ rules: | |
| - create | ||
| - delete | ||
| - get | ||
| - update | ||
| - patch | ||
| - apiGroups: | ||
| - "" | ||
| resources: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,9 @@ import ( | |||||||||||||
| "context" | ||||||||||||||
| "encoding/json" | ||||||||||||||
| "fmt" | ||||||||||||||
| "regexp" | ||||||||||||||
| "strconv" | ||||||||||||||
| "strings" | ||||||||||||||
|
|
||||||||||||||
| "github.com/actions/actions-runner-controller/apis/actions.github.com/v1alpha1" | ||||||||||||||
| "github.com/actions/actions-runner-controller/cmd/ghalistener/listener" | ||||||||||||||
|
|
@@ -12,13 +15,46 @@ import ( | |||||||||||||
| jsonpatch "github.com/evanphx/json-patch" | ||||||||||||||
| "github.com/go-logr/logr" | ||||||||||||||
| kerrors "k8s.io/apimachinery/pkg/api/errors" | ||||||||||||||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||||||||||
| "k8s.io/apimachinery/pkg/types" | ||||||||||||||
| "k8s.io/client-go/kubernetes" | ||||||||||||||
| "k8s.io/client-go/rest" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| const workerName = "kubernetesworker" | ||||||||||||||
|
|
||||||||||||||
| // sanitizeLabelValue sanitizes a string to be a valid Kubernetes label value | ||||||||||||||
| // Kubernetes label values must be alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character | ||||||||||||||
| func sanitizeLabelValue(value string) string { | ||||||||||||||
| if value == "" { | ||||||||||||||
| return "" | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Replace invalid characters with hyphens | ||||||||||||||
| reg := regexp.MustCompile(`[^a-zA-Z0-9._-]`) | ||||||||||||||
| sanitized := reg.ReplaceAllString(value, "-") | ||||||||||||||
|
|
||||||||||||||
| // Remove consecutive hyphens | ||||||||||||||
| reg = regexp.MustCompile(`-+`) | ||||||||||||||
| sanitized = reg.ReplaceAllString(sanitized, "-") | ||||||||||||||
|
|
||||||||||||||
| // Ensure it starts and ends with alphanumeric character | ||||||||||||||
| sanitized = strings.Trim(sanitized, "-._") | ||||||||||||||
|
||||||||||||||
|
|
||||||||||||||
| // If empty after sanitization, use a default value | ||||||||||||||
| if sanitized == "" { | ||||||||||||||
| sanitized = "unknown" | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Truncate to max length (63 characters for Kubernetes labels) | ||||||||||||||
| if len(sanitized) > 63 { | ||||||||||||||
| sanitized = sanitized[:63] | ||||||||||||||
| sanitized = strings.Trim(sanitized, "-._") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| // If empty after truncation and trimming, use a default value | |
| if sanitized == "" { | |
| sanitized = "unknown" | |
| } |
Copilot
AI
Sep 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Update() without checking resource version could lead to conflicts if the pod is modified by another process between Get() and Update(). Consider using Patch() instead or implementing retry logic with exponential backoff to handle update conflicts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider compiling the regular expressions once as package-level variables instead of recompiling them on every function call. This will improve performance when the function is called frequently.