@@ -24,16 +24,53 @@ locals {
2424 arc_private_key = data. aws_secretsmanager_secret_version . arc_secrets_private_key . secret_string
2525}
2626
27+ # ClusterRole with secret read permissions
28+ # This is never used cluster-wide, only in specific namespaces
29+ resource "kubernetes_cluster_role" "secret_reader" {
30+ metadata {
31+ name = " secret-reader"
32+ }
33+
34+ rule {
35+ api_groups = [" " ]
36+ resources = [" secrets" ]
37+ verbs = [" get" , " list" , " watch" ]
38+ }
39+ }
40+
41+ /*
42+ * A few resources need to be provisioned for each RunnerScaleSet
43+ * i.e. for each folder under ${var.provider_path}/${var.cluster}
44+ *
45+ * - A namespace
46+ * - The GitHub secret
47+ * - A RoleBinding the allows access to the secret
48+ */
49+
50+ // Find folders directly under argocd/${var.provider_path}/${var.cluster}
51+ // and create a JSON map of results for next resources to loop on
52+ data "external" "runner_scale_sets" {
53+ program = [" bash" , " -c" , <<- EOT
54+ REPO_ROOT=$(git rev-parse --show-toplevel)
55+ find "$REPO_ROOT/${ var . provider_path } /${ var . cluster } " -mindepth 1 -maxdepth 1 -type d -type d -exec basename {} \; | jq -R -s -c 'split("\n")[:-1] | map({key: ., value: .}) | from_entries'
56+ EOT
57+ ]
58+ }
59+
2760resource "kubernetes_namespace" "arc_runners" {
61+ for_each = data. external . runner_scale_sets . result
62+
2863 metadata {
29- name = var. namespace
64+ name = " ${ var . organization } - ${ each . value } "
3065 }
3166}
3267
3368resource "kubernetes_secret" "github_app" {
69+ for_each = data. external . runner_scale_sets . result
70+
3471 metadata {
3572 name = " github-config"
36- namespace = var. namespace
73+ namespace = " ${ var . organization } - ${ each . value } "
3774 }
3875
3976 data = {
@@ -45,6 +82,27 @@ resource "kubernetes_secret" "github_app" {
4582 type = " Opaque"
4683}
4784
85+ resource "kubernetes_role_binding" "secret_access" {
86+ for_each = data. external . runner_scale_sets . result
87+
88+ metadata {
89+ name = " secret-reader-binding"
90+ namespace = " ${ var . organization } -${ each . value } "
91+ }
92+
93+ role_ref {
94+ api_group = " rbac.authorization.k8s.io"
95+ kind = " ClusterRole"
96+ name = kubernetes_cluster_role. secret_reader . metadata [0 ]. name
97+ }
98+
99+ subject {
100+ kind = " ServiceAccount"
101+ name = var. arc_controller_sa
102+ namespace = var. arc_controller_sa_namespace
103+ }
104+ }
105+
48106// This secret is required by ArgoCD to enable using an OCI repo
49107// for helm charts. It's deployed once in the argocd namespace.
50108resource "kubernetes_secret" "arc_runner_scale_set_oci_repo" {
@@ -123,18 +181,17 @@ resource "argocd_project" "arc_rss_project" {
123181 * - the helm chart
124182 * - the value files
125183 *
126- * Value files can be found under argocd/cloud/tenant/region/cluster/namespace/ <runner-scale-set-name>.
127- * Each cloud/tenant/region/cluster/namespace combination has its ApplicationSet.
128- * Various RunnerSets in the same namespace belong to the same ApplicationSet, with one application each.
184+ * Value files can be found under argocd/cloud/tenant/region/cluster/<runner-scale-set-name>.
185+ * Each cloud/tenant/region/cluster combination has its ApplicationSet.
186+ * Each RunnerSets runs in its own namespace belong to the ApplicationSet, with one application each.
129187 *
130188 * The cluster folder is used to select the cluster name in ArgoCD
131- * The namespace folder is used to define the target namespace in ArgoCD
132189 * Each resource in the folder is applied as a dedicated Application
133190 */
134191resource "argocd_application_set" "arc_runner_scale_set" {
135192
136193 metadata {
137- name = " arc-rss-${ var . cluster } -${ var . namespace } "
194+ name = " arc-rss-${ var . organization } -${ var . cluster } "
138195 namespace = " argocd"
139196 }
140197
@@ -147,7 +204,7 @@ resource "argocd_application_set" "arc_runner_scale_set" {
147204 repo_url = " https://github.com/pytorch/ci-infra"
148205 revision = var. git_revision
149206 directory {
150- path = " ${ var . provider_path } /${ var . cluster } /${ var . namespace } / *"
207+ path = " ${ var . provider_path } /${ var . cluster } /*"
151208 }
152209 }
153210 }
@@ -170,6 +227,11 @@ resource "argocd_application_set" "arc_runner_scale_set" {
170227 value_files = [
171228 " $values/{{.path.path}}/values.yaml"
172229 ]
230+ values = <<- EOT
231+ controllerServiceAccount:
232+ name: ${ var . arc_controller_sa }
233+ namespace: ${ var . arc_controller_sa_namespace }
234+ EOT
173235 }
174236 }
175237 source {
@@ -180,7 +242,7 @@ resource "argocd_application_set" "arc_runner_scale_set" {
180242
181243 destination {
182244 name = " {{index .path.segments 4}}"
183- namespace = " {{index .path.segments 5}}"
245+ namespace = " ${ var . organization } - {{index .path.segments 5}}"
184246 }
185247
186248 sync_policy {
0 commit comments