From e60e97768123ce88b2071678b8ecaee240824e43 Mon Sep 17 00:00:00 2001 From: Aarushi Date: Tue, 23 Jul 2024 15:41:51 +0100 Subject: [PATCH] Set up helm and tf for backend --- rnd/infra/helm/autogpt-server/.helmignore | 23 ++++++ rnd/infra/helm/autogpt-server/Chart.yaml | 10 +++ .../helm/autogpt-server/templates/NOTES.txt | 22 ++++++ .../autogpt-server/templates/_helpers.tpl | 62 +++++++++++++++ .../autogpt-server/templates/deployment.yaml | 68 +++++++++++++++++ .../helm/autogpt-server/templates/hpa.yaml | 32 ++++++++ .../autogpt-server/templates/ingress.yaml | 61 +++++++++++++++ .../autogpt-server/templates/managedcert.yaml | 7 ++ .../autogpt-server/templates/service.yaml | 15 ++++ .../templates/serviceaccount.yaml | 13 ++++ .../templates/tests/test-connection.yaml | 15 ++++ rnd/infra/helm/autogpt-server/values.dev.yaml | 40 ++++++++++ rnd/infra/helm/autogpt-server/values.yaml | 76 +++++++++++++++++++ rnd/infra/terraform/environments/dev.tfvars | 28 +++++++ rnd/infra/terraform/main.tf | 11 +++ .../terraform/modules/gke_cluster/main.tf | 9 +++ rnd/infra/terraform/modules/iam/main.tf | 26 +++++++ rnd/infra/terraform/modules/iam/outputs.tf | 4 + rnd/infra/terraform/modules/iam/variables.tf | 29 +++++++ rnd/infra/terraform/variables.tf | 25 ++++++ 20 files changed, 576 insertions(+) create mode 100644 rnd/infra/helm/autogpt-server/.helmignore create mode 100644 rnd/infra/helm/autogpt-server/Chart.yaml create mode 100644 rnd/infra/helm/autogpt-server/templates/NOTES.txt create mode 100644 rnd/infra/helm/autogpt-server/templates/_helpers.tpl create mode 100644 rnd/infra/helm/autogpt-server/templates/deployment.yaml create mode 100644 rnd/infra/helm/autogpt-server/templates/hpa.yaml create mode 100644 rnd/infra/helm/autogpt-server/templates/ingress.yaml create mode 100644 rnd/infra/helm/autogpt-server/templates/managedcert.yaml create mode 100644 rnd/infra/helm/autogpt-server/templates/service.yaml create mode 100644 rnd/infra/helm/autogpt-server/templates/serviceaccount.yaml create mode 100644 rnd/infra/helm/autogpt-server/templates/tests/test-connection.yaml create mode 100644 rnd/infra/helm/autogpt-server/values.dev.yaml create mode 100644 rnd/infra/helm/autogpt-server/values.yaml create mode 100644 rnd/infra/terraform/modules/iam/main.tf create mode 100644 rnd/infra/terraform/modules/iam/outputs.tf create mode 100644 rnd/infra/terraform/modules/iam/variables.tf diff --git a/rnd/infra/helm/autogpt-server/.helmignore b/rnd/infra/helm/autogpt-server/.helmignore new file mode 100644 index 0000000000..0e8a0eb36f --- /dev/null +++ b/rnd/infra/helm/autogpt-server/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/rnd/infra/helm/autogpt-server/Chart.yaml b/rnd/infra/helm/autogpt-server/Chart.yaml new file mode 100644 index 0000000000..57ccbd6efe --- /dev/null +++ b/rnd/infra/helm/autogpt-server/Chart.yaml @@ -0,0 +1,10 @@ +apiVersion: v2 +name: autogpt-server +description: A Helm chart for AutoGPT on Kubernetes + +type: application + + +version: 0.1.0 + +appVersion: "1.16.0" diff --git a/rnd/infra/helm/autogpt-server/templates/NOTES.txt b/rnd/infra/helm/autogpt-server/templates/NOTES.txt new file mode 100644 index 0000000000..01105181f4 --- /dev/null +++ b/rnd/infra/helm/autogpt-server/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "autogpt_server.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch its status by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "autogpt_server.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "autogpt_server.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "autogpt_server.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/rnd/infra/helm/autogpt-server/templates/_helpers.tpl b/rnd/infra/helm/autogpt-server/templates/_helpers.tpl new file mode 100644 index 0000000000..2ff84f3d72 --- /dev/null +++ b/rnd/infra/helm/autogpt-server/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "autogpt-server.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "autogpt-server.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "autogpt-server.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "autogpt-server.labels" -}} +helm.sh/chart: {{ include "autogpt-server.chart" . }} +{{ include "autogpt-server.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "autogpt-server.selectorLabels" -}} +app.kubernetes.io/name: {{ include "autogpt-server.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "autogpt-server.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "autogpt-server.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/rnd/infra/helm/autogpt-server/templates/deployment.yaml b/rnd/infra/helm/autogpt-server/templates/deployment.yaml new file mode 100644 index 0000000000..7cb0a3d5dc --- /dev/null +++ b/rnd/infra/helm/autogpt-server/templates/deployment.yaml @@ -0,0 +1,68 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "autogpt-server.fullname" . }} + labels: + {{- include "autogpt-server.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "autogpt-server.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "autogpt-server.labels" . | nindent 8 }} + {{- with .Values.podLabels }} + {{- toYaml . | nindent 8 }} + {{- end }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "autogpt-server.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: {{ .Values.service.port }} + protocol: TCP + livenessProbe: + {{- toYaml .Values.livenessProbe | nindent 12 }} + readinessProbe: + {{- toYaml .Values.readinessProbe | nindent 12 }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.volumeMounts }} + volumeMounts: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.volumes }} + volumes: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/rnd/infra/helm/autogpt-server/templates/hpa.yaml b/rnd/infra/helm/autogpt-server/templates/hpa.yaml new file mode 100644 index 0000000000..f2e1af78ae --- /dev/null +++ b/rnd/infra/helm/autogpt-server/templates/hpa.yaml @@ -0,0 +1,32 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "autogpt-server.fullname" . }} + labels: + {{- include "autogpt-server.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "autogpt-server.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + target: + type: Utilization + averageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/rnd/infra/helm/autogpt-server/templates/ingress.yaml b/rnd/infra/helm/autogpt-server/templates/ingress.yaml new file mode 100644 index 0000000000..115d67031c --- /dev/null +++ b/rnd/infra/helm/autogpt-server/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "autogpt-server.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "autogpt-server.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/rnd/infra/helm/autogpt-server/templates/managedcert.yaml b/rnd/infra/helm/autogpt-server/templates/managedcert.yaml new file mode 100644 index 0000000000..97fe878f20 --- /dev/null +++ b/rnd/infra/helm/autogpt-server/templates/managedcert.yaml @@ -0,0 +1,7 @@ +apiVersion: networking.gke.io/v1 +kind: ManagedCertificate +metadata: + name: {{ include "autogpt-server.fullname" . }}-cert +spec: + domains: + - {{ .Values.domain }} \ No newline at end of file diff --git a/rnd/infra/helm/autogpt-server/templates/service.yaml b/rnd/infra/helm/autogpt-server/templates/service.yaml new file mode 100644 index 0000000000..fd2f813bc1 --- /dev/null +++ b/rnd/infra/helm/autogpt-server/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "autogpt-server.fullname" . }} + labels: + {{- include "autogpt-server.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "autogpt-server.selectorLabels" . | nindent 4 }} diff --git a/rnd/infra/helm/autogpt-server/templates/serviceaccount.yaml b/rnd/infra/helm/autogpt-server/templates/serviceaccount.yaml new file mode 100644 index 0000000000..2a403f9c74 --- /dev/null +++ b/rnd/infra/helm/autogpt-server/templates/serviceaccount.yaml @@ -0,0 +1,13 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "autogpt-server.serviceAccountName" . }} + labels: + {{- include "autogpt-server.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automount }} +{{- end }} diff --git a/rnd/infra/helm/autogpt-server/templates/tests/test-connection.yaml b/rnd/infra/helm/autogpt-server/templates/tests/test-connection.yaml new file mode 100644 index 0000000000..0f0d4593f2 --- /dev/null +++ b/rnd/infra/helm/autogpt-server/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "autogpt-server.fullname" . }}-test-connection" + labels: + {{- include "autogpt-server.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "autogpt-server.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/rnd/infra/helm/autogpt-server/values.dev.yaml b/rnd/infra/helm/autogpt-server/values.dev.yaml new file mode 100644 index 0000000000..8a7fbda2e2 --- /dev/null +++ b/rnd/infra/helm/autogpt-server/values.dev.yaml @@ -0,0 +1,40 @@ +# dev values, overwrite base values as needed. + +image: + repository: us-east1-docker.pkg.dev/agpt-dev/agpt-server-dev/agpt-server-dev + pullPolicy: IfNotPresent + tag: "fe3d2a9" + +serviceAccount: + annotations: + iam.gke.io/gcp-service-account: "dev-agpt-server-sa@agpt-dev.iam.gserviceaccount.com" + name: "dev-agpt-server-sa" + +service: + type: ClusterIP + port: 80 + targetPort: 8000 + +ingress: + enabled: true + className: "gce" + annotations: + kubernetes.io/ingress.class: gce + kubernetes.io/ingress.global-static-ip-name: "agpt-dev-agpt-server-ip" + networking.gke.io/managed-certificates: "autogpt-server-cert" + hosts: + - host: server.agpt.co + paths: + - path: /* + pathType: ImplementationSpecific + tls: [] + +resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 512Mi + +domain: "server.agpt.co" \ No newline at end of file diff --git a/rnd/infra/helm/autogpt-server/values.yaml b/rnd/infra/helm/autogpt-server/values.yaml new file mode 100644 index 0000000000..80797575df --- /dev/null +++ b/rnd/infra/helm/autogpt-server/values.yaml @@ -0,0 +1,76 @@ +# base values, environment specific variables should be specified/overwritten in environment values + +replicaCount: 2 + +image: + repository: us-east1-docker.pkg.dev/agpt-dev/agpt-server-dev/agpt-server-dev + pullPolicy: IfNotPresent + tag: "latest" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + create: true + automount: true + annotations: {} + name: "" + +podAnnotations: {} +podLabels: {} + +podSecurityContext: {} + +securityContext: {} + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + className: "" + annotations: {} + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + + +resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 512Mi + +livenessProbe: + httpGet: + path: / + port: http +readinessProbe: + httpGet: + path: / + port: http + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetMemoryUtilizationPercentage: 80 + +volumes: [] + +volumeMounts: [] + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +domain: "" diff --git a/rnd/infra/terraform/environments/dev.tfvars b/rnd/infra/terraform/environments/dev.tfvars index cbcc69ccd8..8fbd6bb554 100644 --- a/rnd/infra/terraform/environments/dev.tfvars +++ b/rnd/infra/terraform/environments/dev.tfvars @@ -10,3 +10,31 @@ node_pool_name = "dev-main-pool" machine_type = "e2-medium" disk_size_gb = 100 static_ip_names = ["agpt-server-ip", "agpt-builder-ip", "auth-ip"] + + +service_accounts = { + "dev-agpt-server-sa" = { + display_name = "AutoGPT Dev Server Account" + description = "Service account for agpt dev server" + } +} + +workload_identity_bindings = { + "dev-agpt-server-workload-identity" = { + service_account_name = "dev-agpt-server-sa" + namespace = "dev-agpt" + ksa_name = "dev-agpt-server-sa" + } +} + +role_bindings = { + "roles/container.developer" = [ + "serviceAccount:dev-agpt-server-sa@agpt-dev.iam.gserviceaccount.com" + ], + "roles/cloudsql.client" = [ + "serviceAccount:dev-agpt-server-sa@agpt-dev.iam.gserviceaccount.com" + ], + "roles/cloudsql.editor" = [ + "serviceAccount:dev-agpt-server-sa@agpt-dev.iam.gserviceaccount.com" + ] +} diff --git a/rnd/infra/terraform/main.tf b/rnd/infra/terraform/main.tf index 856372e10c..57de51eb63 100644 --- a/rnd/infra/terraform/main.tf +++ b/rnd/infra/terraform/main.tf @@ -6,10 +6,12 @@ terraform { version = "~> 4.0" } } + backend "gcs" { bucket = "agpt-dev-terraform" prefix = "terraform/state" } + } provider "google" { @@ -49,3 +51,12 @@ module "gke_cluster" { subnetwork = module.networking.subnet_self_link enable_autopilot = var.enable_autopilot } + +module "iam" { + source = "./modules/iam" + + project_id = var.project_id + service_accounts = var.service_accounts + workload_identity_bindings = var.workload_identity_bindings + role_bindings = var.role_bindings +} \ No newline at end of file diff --git a/rnd/infra/terraform/modules/gke_cluster/main.tf b/rnd/infra/terraform/modules/gke_cluster/main.tf index c646a14030..d30946848e 100644 --- a/rnd/infra/terraform/modules/gke_cluster/main.tf +++ b/rnd/infra/terraform/modules/gke_cluster/main.tf @@ -2,6 +2,11 @@ resource "google_container_cluster" "primary" { name = var.cluster_name location = var.zone + workload_identity_config { + workload_pool = "${var.project_id}.svc.id.goog" + } + + dynamic "node_pool" { for_each = var.enable_autopilot ? [] : [1] content { @@ -11,6 +16,10 @@ resource "google_container_cluster" "primary" { node_config { machine_type = var.machine_type disk_size_gb = var.disk_size_gb + + workload_metadata_config { + mode = "GKE_METADATA" + } } } } diff --git a/rnd/infra/terraform/modules/iam/main.tf b/rnd/infra/terraform/modules/iam/main.tf new file mode 100644 index 0000000000..3a07d69264 --- /dev/null +++ b/rnd/infra/terraform/modules/iam/main.tf @@ -0,0 +1,26 @@ +resource "google_service_account" "service_accounts" { + for_each = var.service_accounts + account_id = each.key + display_name = each.value.display_name + description = each.value.description +} + +# IAM policy binding for Workload Identity +resource "google_service_account_iam_binding" "workload_identity_binding" { + for_each = var.workload_identity_bindings + service_account_id = google_service_account.service_accounts[each.value.service_account_name].name + role = "roles/iam.workloadIdentityUser" + + members = [ + "serviceAccount:${var.project_id}.svc.id.goog[${each.value.namespace}/${each.value.ksa_name}]" + ] +} + +# Role bindings grouped by role +resource "google_project_iam_binding" "role_bindings" { + for_each = var.role_bindings + project = var.project_id + role = each.key + + members = each.value +} \ No newline at end of file diff --git a/rnd/infra/terraform/modules/iam/outputs.tf b/rnd/infra/terraform/modules/iam/outputs.tf new file mode 100644 index 0000000000..b503414873 --- /dev/null +++ b/rnd/infra/terraform/modules/iam/outputs.tf @@ -0,0 +1,4 @@ +output "service_account_emails" { + description = "The emails of the created service accounts" + value = { for k, v in google_service_account.service_accounts : k => v.email } +} \ No newline at end of file diff --git a/rnd/infra/terraform/modules/iam/variables.tf b/rnd/infra/terraform/modules/iam/variables.tf new file mode 100644 index 0000000000..61637a7187 --- /dev/null +++ b/rnd/infra/terraform/modules/iam/variables.tf @@ -0,0 +1,29 @@ +variable "project_id" { + description = "The ID of the project" + type = string +} + +variable "service_accounts" { + description = "Map of service accounts to create" + type = map(object({ + display_name = string + description = string + })) + default = {} +} + +variable "workload_identity_bindings" { + description = "Map of Workload Identity bindings to create" + type = map(object({ + service_account_name = string + namespace = string + ksa_name = string + })) + default = {} +} + +variable "role_bindings" { + description = "Map of roles to list of members" + type = map(list(string)) + default = {} +} \ No newline at end of file diff --git a/rnd/infra/terraform/variables.tf b/rnd/infra/terraform/variables.tf index 66c35e7ad6..80a0b77e7d 100644 --- a/rnd/infra/terraform/variables.tf +++ b/rnd/infra/terraform/variables.tf @@ -74,3 +74,28 @@ variable "static_ip_names" { type = list(string) default = ["ip-1", "ip-2", "ip-3"] } + +variable "service_accounts" { + description = "Map of service accounts to create" + type = map(object({ + display_name = string + description = string + })) + default = {} +} + +variable "workload_identity_bindings" { + description = "Map of Workload Identity bindings to create" + type = map(object({ + service_account_name = string + namespace = string + ksa_name = string + })) + default = {} +} + +variable "role_bindings" { + description = "Map of roles to list of members" + type = map(list(string)) + default = {} +}