Skip to content
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

feat: allow namespace overrides #3797

Merged
merged 20 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 additions & 2 deletions charts/gha-runner-scale-set/templates/kube_mode_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ include "gha-runner-scale-set.kubeModeRoleName" . }}
namespace: {{ include "gha-runner-scale-set.namespace" . }}
finalizers:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- if $hasCustomResourceMeta }}
{{- with .Values.resourceMeta.kubernetesModeRole.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
{{- include "gha-runner-scale-set.labels" . | nindent 4 }}
annotations:
{{- with .Values.annotations }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- if $hasCustomResourceMeta }}
{{- with .Values.resourceMeta.kubernetesModeRole.annotations }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
- actions.github.com/cleanup-protection
rules:
- apiGroups: [""]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ kind: RoleBinding
metadata:
name: {{ include "gha-runner-scale-set.kubeModeRoleBindingName" . }}
namespace: {{ include "gha-runner-scale-set.namespace" . }}
labels:
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- if $hasCustomResourceMeta }}
{{- with .Values.resourceMeta.kubernetesModeRoleBinding.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
{{- include "gha-runner-scale-set.labels" . | nindent 4 }}

annotations:
{{- with .Values.annotations }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- if $hasCustomResourceMeta }}
{{- with .Values.resourceMeta.kubernetesModeRoleBinding.annotations }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
finalizers:
- actions.github.com/cleanup-protection
roleRef:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "gha-runner-scale-set.kubeModeServiceAccountName" . }}
namespace: {{ include "gha-runner-scale-set.namespace" . }}
{{- if .Values.containerMode.kubernetesModeServiceAccount }}
{{- with .Values.containerMode.kubernetesModeServiceAccount.annotations }}
{{- if or .Values.annotations $hasCustomResourceMeta }}
annotations:
{{- with .Values.annotations }}
{{- toYaml . | nindent 4 }}
Expand Down
203 changes: 203 additions & 0 deletions charts/gha-runner-scale-set/tests/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,209 @@ func TestAutoscalingRunnerSetAnnotationValuesHash(t *testing.T) {
assert.LessOrEqual(t, len(secondHash), 63)
}

func TestCustomLabels(t *testing.T) {
t.Parallel()

// Path to the helm chart we will test
helmChartPath, err := filepath.Abs("../../gha-runner-scale-set")
require.NoError(t, err)

releaseName := "test-runners"
namespaceName := "test-" + strings.ToLower(random.UniqueId())

options := &helm.Options{
Logger: logger.Discard,
SetValues: map[string]string{
"githubConfigUrl": "https://github.com/actions",
"githubConfigSecret.github_token": "gh_token12345",
"controllerServiceAccount.name": "arc",
"containerMode.type": "kubernetes",
"controllerServiceAccount.namespace": "arc-system",
`labels.argocd\.argoproj\.io/sync-wave`: `"1"`,
`labels.app\.kubernetes\.io/part-of`: "no-override", // this shouldn't be overwritten
"resourceMeta.autoscalingRunnerSet.labels.ars-custom": "ars-custom-value",
"resourceMeta.githubConfigSecret.labels.gh-custom": "gh-custom-value",
"resourceMeta.kubernetesModeRole.labels.kmr-custom": "kmr-custom-value",
"resourceMeta.kubernetesModeRoleBinding.labels.kmrb-custom": "kmrb-custom-value",
"resourceMeta.kubernetesModeServiceAccount.labels.kmsa-custom": "kmsa-custom-value",
"resourceMeta.managerRole.labels.mr-custom": "mr-custom-value",
"resourceMeta.managerRoleBinding.labels.mrb-custom": "mrb-custom-value",
},
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
}

output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/githubsecret.yaml"})

const targetLabel = "argocd.argoproj.io/sync-wave"
const wantCustomValue = `"1"`
const reservedLabel = "app.kubernetes.io/part-of"
const wantReservedValue = "gha-rs"

var githubSecret corev1.Secret
helm.UnmarshalK8SYaml(t, output, &githubSecret)
assert.Equal(t, wantCustomValue, githubSecret.Labels[targetLabel])
assert.Equal(t, wantReservedValue, githubSecret.Labels[reservedLabel])
assert.Equal(t, "gh-custom-value", githubSecret.Labels["gh-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/kube_mode_role.yaml"})
var role rbacv1.Role
helm.UnmarshalK8SYaml(t, output, &role)
assert.Equal(t, wantCustomValue, role.Labels[targetLabel])
assert.Equal(t, wantReservedValue, role.Labels[reservedLabel])
assert.Equal(t, "kmr-custom-value", role.Labels["kmr-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/kube_mode_role_binding.yaml"})
var roleBinding rbacv1.RoleBinding
helm.UnmarshalK8SYaml(t, output, &roleBinding)
assert.Equal(t, wantCustomValue, roleBinding.Labels[targetLabel])
assert.Equal(t, wantReservedValue, roleBinding.Labels[reservedLabel])
assert.Equal(t, "kmrb-custom-value", roleBinding.Labels["kmrb-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/autoscalingrunnerset.yaml"})
var ars v1alpha1.AutoscalingRunnerSet
helm.UnmarshalK8SYaml(t, output, &ars)
assert.Equal(t, wantCustomValue, ars.Labels[targetLabel])
assert.Equal(t, wantReservedValue, ars.Labels[reservedLabel])
assert.Equal(t, "ars-custom-value", ars.Labels["ars-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/kube_mode_serviceaccount.yaml"})
var serviceAccount corev1.ServiceAccount
helm.UnmarshalK8SYaml(t, output, &serviceAccount)
assert.Equal(t, wantCustomValue, serviceAccount.Labels[targetLabel])
assert.Equal(t, wantReservedValue, serviceAccount.Labels[reservedLabel])
assert.Equal(t, "kmsa-custom-value", serviceAccount.Labels["kmsa-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/manager_role.yaml"})
var managerRole rbacv1.Role
helm.UnmarshalK8SYaml(t, output, &managerRole)
assert.Equal(t, wantCustomValue, managerRole.Labels[targetLabel])
assert.Equal(t, wantReservedValue, managerRole.Labels[reservedLabel])
assert.Equal(t, "mr-custom-value", managerRole.Labels["mr-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/manager_role_binding.yaml"})
var managerRoleBinding rbacv1.RoleBinding
helm.UnmarshalK8SYaml(t, output, &managerRoleBinding)
assert.Equal(t, wantCustomValue, managerRoleBinding.Labels[targetLabel])
assert.Equal(t, wantReservedValue, managerRoleBinding.Labels[reservedLabel])
assert.Equal(t, "mrb-custom-value", managerRoleBinding.Labels["mrb-custom"])

options = &helm.Options{
Logger: logger.Discard,
SetValues: map[string]string{
"githubConfigUrl": "https://github.com/actions",
"githubConfigSecret.github_token": "gh_token12345",
"controllerServiceAccount.name": "arc",
"controllerServiceAccount.namespace": "arc-system",
`labels.argocd\.argoproj\.io/sync-wave`: `"1"`,
"resourceMeta.noPermissionServiceAccount.labels.npsa-custom": "npsa-custom-value",
},
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
}

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/no_permission_serviceaccount.yaml"})
var noPermissionServiceAccount corev1.ServiceAccount
helm.UnmarshalK8SYaml(t, output, &noPermissionServiceAccount)
assert.Equal(t, wantCustomValue, noPermissionServiceAccount.Labels[targetLabel])
assert.Equal(t, wantReservedValue, noPermissionServiceAccount.Labels[reservedLabel])
assert.Equal(t, "npsa-custom-value", noPermissionServiceAccount.Labels["npsa-custom"])
}

func TestCustomAnnotations(t *testing.T) {
t.Parallel()

// Path to the helm chart we will test
helmChartPath, err := filepath.Abs("../../gha-runner-scale-set")
require.NoError(t, err)

releaseName := "test-runners"
namespaceName := "test-" + strings.ToLower(random.UniqueId())

options := &helm.Options{
Logger: logger.Discard,
SetValues: map[string]string{
"githubConfigUrl": "https://github.com/actions",
"githubConfigSecret.github_token": "gh_token12345",
"containerMode.type": "kubernetes",
"controllerServiceAccount.name": "arc",
"controllerServiceAccount.namespace": "arc-system",
`annotations.argocd\.argoproj\.io/sync-wave`: `"1"`,
"resourceMeta.autoscalingRunnerSet.annotations.ars-custom": "ars-custom-value",
"resourceMeta.githubConfigSecret.annotations.gh-custom": "gh-custom-value",
"resourceMeta.kubernetesModeRole.annotations.kmr-custom": "kmr-custom-value",
"resourceMeta.kubernetesModeRoleBinding.annotations.kmrb-custom": "kmrb-custom-value",
"resourceMeta.kubernetesModeServiceAccount.annotations.kmsa-custom": "kmsa-custom-value",
"resourceMeta.managerRole.annotations.mr-custom": "mr-custom-value",
"resourceMeta.managerRoleBinding.annotations.mrb-custom": "mrb-custom-value",
},
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
}

const targetAnnotations = "argocd.argoproj.io/sync-wave"
const wantCustomValue = `"1"`

output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/githubsecret.yaml"})

var githubSecret corev1.Secret
helm.UnmarshalK8SYaml(t, output, &githubSecret)
assert.Equal(t, wantCustomValue, githubSecret.Annotations[targetAnnotations])
assert.Equal(t, "gh-custom-value", githubSecret.Annotations["gh-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/kube_mode_role.yaml"})
var role rbacv1.Role
helm.UnmarshalK8SYaml(t, output, &role)
assert.Equal(t, wantCustomValue, role.Annotations[targetAnnotations])
assert.Equal(t, "kmr-custom-value", role.Annotations["kmr-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/kube_mode_role_binding.yaml"})
var roleBinding rbacv1.RoleBinding
helm.UnmarshalK8SYaml(t, output, &roleBinding)
assert.Equal(t, wantCustomValue, roleBinding.Annotations[targetAnnotations])
assert.Equal(t, "kmrb-custom-value", roleBinding.Annotations["kmrb-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/autoscalingrunnerset.yaml"})
var ars v1alpha1.AutoscalingRunnerSet
helm.UnmarshalK8SYaml(t, output, &ars)
assert.Equal(t, wantCustomValue, ars.Annotations[targetAnnotations])
assert.Equal(t, "ars-custom-value", ars.Annotations["ars-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/kube_mode_serviceaccount.yaml"})
var serviceAccount corev1.ServiceAccount
helm.UnmarshalK8SYaml(t, output, &serviceAccount)
assert.Equal(t, wantCustomValue, serviceAccount.Annotations[targetAnnotations])
assert.Equal(t, "kmsa-custom-value", serviceAccount.Annotations["kmsa-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/manager_role.yaml"})
var managerRole rbacv1.Role
helm.UnmarshalK8SYaml(t, output, &managerRole)
assert.Equal(t, wantCustomValue, managerRole.Annotations[targetAnnotations])
assert.Equal(t, "mr-custom-value", managerRole.Annotations["mr-custom"])

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/manager_role_binding.yaml"})
var managerRoleBinding rbacv1.RoleBinding
helm.UnmarshalK8SYaml(t, output, &managerRoleBinding)
assert.Equal(t, wantCustomValue, managerRoleBinding.Annotations[targetAnnotations])
assert.Equal(t, "mrb-custom-value", managerRoleBinding.Annotations["mrb-custom"])

options = &helm.Options{
Logger: logger.Discard,
SetValues: map[string]string{
"githubConfigUrl": "https://github.com/actions",
"githubConfigSecret.github_token": "gh_token12345",
"controllerServiceAccount.name": "arc",
"controllerServiceAccount.namespace": "arc-system",
`annotations.argocd\.argoproj\.io/sync-wave`: `"1"`,
"resourceMeta.noPermissionServiceAccount.annotations.npsa-custom": "npsa-custom-value",
},
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
}

output = helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/no_permission_serviceaccount.yaml"})
var noPermissionServiceAccount corev1.ServiceAccount
helm.UnmarshalK8SYaml(t, output, &noPermissionServiceAccount)
assert.Equal(t, wantCustomValue, noPermissionServiceAccount.Annotations[targetAnnotations])
assert.Equal(t, "npsa-custom-value", noPermissionServiceAccount.Annotations["npsa-custom"])
}

func TestNamespaceOverride(t *testing.T) {
t.Parallel()

Expand Down
60 changes: 60 additions & 0 deletions charts/gha-runner-scale-set/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,63 @@ template:

# Overrides the default `.Release.Namespace` for all resources in this chart.
namespaceOverride: ""

## Optional annotations and labels applied to all resources created by helm installation
##
## Annotations applied to all resources created by this helm chart. Annotations will not override the default ones, so make sure
## the custom annotation is not reserved.
# annotations:
# key: value
##
## Labels applied to all resources created by this helm chart. Labels will not override the default ones, so make sure
## the custom label is not reserved.
# labels:
# key: value

## If you want more fine-grained control over annotations applied to particular resource created by this chart,
## you can use `resourceMeta`.
## Order of applying labels and annotations is:
## 1. Apply labels/annotations globally, using `annotations` and `labels` field
## 2. Apply `resourceMeta` labels/annotations
## 3. Apply reserved labels/annotations
# resourceMeta:
# autoscalingRunnerSet:
# labels:
# key: value
# annotations:
# key: value
# githubConfigSecret:
# labels:
# key: value
# annotations:
# key: value
# kubernetesModeRole:
# labels:
# key: value
# annotations:
# key: value
# kubernetesModeRoleBinding:
# labels:
# key: value
# annotations:
# key: value
# kubernetesModeServiceAccount:
# labels:
# key: value
# annotations:
# key: value
# managerRole:
# labels:
# key: value
# annotations:
# key: value
# managerRoleBinding:
# labels:
# key: value
# annotations:
# key: value
# noPermissionServiceAccount:
# labels:
# key: value
# annotations:
# key: value
You are viewing a condensed version of this merge commit. You can view the full changes here.