feat(terraform): Improved dispatcher 33/43733/1
authorPeter Mikus <[email protected]>
Tue, 23 Sep 2025 06:26:05 +0000 (08:26 +0200)
committerPeter Mikus <[email protected]>
Tue, 23 Sep 2025 06:26:05 +0000 (08:26 +0200)
Signed-off-by: Peter Mikus <[email protected]>
Change-Id: Ic9804865fba42d1a455407e15c03b48121795d7d

fdio.infra.terraform/terraform-nomad-gha-dispatcher/README.md [new file with mode: 0644]
fdio.infra.terraform/terraform-nomad-gha-dispatcher/main.tf
fdio.infra.terraform/terraform-nomad-gha-dispatcher/nomad-gha-dispatcher-sandbox.hcl.tftpl [deleted file]
fdio.infra.terraform/terraform-nomad-gha-dispatcher/nomad-gha-dispatcher.hcl.tftpl [moved from fdio.infra.terraform/terraform-nomad-gha-dispatcher/nomad-gha-dispatcher-prod.hcl.tftpl with 78% similarity]
fdio.infra.terraform/terraform-nomad-gha-dispatcher/variables.tf

diff --git a/fdio.infra.terraform/terraform-nomad-gha-dispatcher/README.md b/fdio.infra.terraform/terraform-nomad-gha-dispatcher/README.md
new file mode 100644 (file)
index 0000000..914f08a
--- /dev/null
@@ -0,0 +1,42 @@
+<!-- BEGIN_TF_DOCS -->
+## Requirements
+
+| Name | Version |
+|------|---------|
+| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.12.1 |
+| <a name="requirement_nomad"></a> [nomad](#requirement\_nomad) | >= 2.5.0 |
+
+## Providers
+
+| Name | Version |
+|------|---------|
+| <a name="provider_nomad"></a> [nomad](#provider\_nomad) | 2.5.0 |
+
+## Modules
+
+No modules.
+
+## Resources
+
+| Name | Type |
+|------|------|
+| [nomad_job.gha-dispatcher](https://registry.terraform.io/providers/hashicorp/nomad/latest/docs/resources/job) | resource |
+
+## Inputs
+
+| Name | Description | Type | Default | Required |
+|------|-------------|------|---------|:--------:|
+| <a name="input_cpu"></a> [cpu](#input\_cpu) | Specifies the CPU required to run this task in MHz. | `number` | `12000` | no |
+| <a name="input_datacenters"></a> [datacenters](#input\_datacenters) | Specifies the list of DCs to be considered placing this task. | `list(string)` | <pre>[<br/>  "yul1"<br/>]</pre> | no |
+| <a name="input_dispatchers"></a> [dispatchers](#input\_dispatchers) | n/a | <pre>list(object({<br/>    namespace  = string<br/>    repository = string<br/>  }))</pre> | <pre>[<br/>  {<br/>    "namespace": "sandbox",<br/>    "repository": "fdio-csit"<br/>  },<br/>  {<br/>    "namespace": "prod",<br/>    "repository": "fdio-csit"<br/>  }<br/>]</pre> | no |
+| <a name="input_image"></a> [image](#input\_image) | Specifies the Docker image to run. | `string` | `"pmikus/docker-gha-dispatcher"` | no |
+| <a name="input_job_name"></a> [job\_name](#input\_job\_name) | Specifies a name for the job. | `string` | `"gha-dispatcher"` | no |
+| <a name="input_memory"></a> [memory](#input\_memory) | Specifies the memory required in MB. | `number` | `8000` | no |
+| <a name="input_node_pool"></a> [node\_pool](#input\_node\_pool) | Specifies the node pool to place the job in. | `string` | `"default"` | no |
+| <a name="input_region"></a> [region](#input\_region) | The region in which to execute the job. | `string` | `"global"` | no |
+| <a name="input_type"></a> [type](#input\_type) | Specifies the Nomad scheduler to use. | `string` | `"service"` | no |
+
+## Outputs
+
+No outputs.
+<!-- END_TF_DOCS -->
\ No newline at end of file
index 635317a..9395bde 100644 (file)
@@ -1,18 +1,21 @@
 locals {
   datacenters = join(",", var.datacenters)
+  flat_dispatchers = {
+    for dispatcher in var.dispatchers : dispatcher.namespace => dispatcher
+  }
 }
 
 resource "nomad_job" "gha-dispatcher" {
-  for_each = toset(var.namespace)
+  for_each = local.flat_dispatchers
   jobspec = templatefile(
-    "${path.cwd}/nomad-${var.job_name}-${each.key}.hcl.tftpl",
+    "${path.cwd}/nomad-${var.job_name}.hcl.tftpl",
     {
       cpu         = var.cpu,
       datacenters = local.datacenters,
-      image       = "${var.image}-${each.key}:latest",
-      job_name    = "${var.job_name}-${each.key}",
+      image       = "${var.image}-${each.value.namespace}:latest",
+      job_name    = "${var.job_name}-${each.value.namespace}-${each.value.repository}",
       memory      = var.memory,
-      namespace   = each.key,
+      namespace   = each.value.namespace,
       node_pool   = var.node_pool,
       region      = var.region,
       type        = var.type
diff --git a/fdio.infra.terraform/terraform-nomad-gha-dispatcher/nomad-gha-dispatcher-sandbox.hcl.tftpl b/fdio.infra.terraform/terraform-nomad-gha-dispatcher/nomad-gha-dispatcher-sandbox.hcl.tftpl
deleted file mode 100644 (file)
index ea17f9c..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-job "${job_name}" {
-  datacenters = ["${datacenters}"]
-  type        = "${type}"
-  node_pool   = "${node_pool}"
-  region      = "${region}"
-  namespace   = "${namespace}"
-
-  group "${job_name}" {
-    count = 1
-    constraint {
-      attribute = "$${attr.cpu.arch}"
-      value     = "amd64"
-    }
-    constraint {
-      attribute = "$${node.class}"
-      value     = "builder"
-    }
-    ephemeral_disk {
-      migrate = false
-      size    = 3000
-      sticky  = false
-    }
-    task "${job_name}" {
-      driver = "docker"
-      config {
-        image = "${image}"
-      }
-      template {
-        destination = "$${NOMAD_SECRETS_DIR}/.env"
-        env         = true
-        data        = <<EOT
-{{- with nomadVar "nomad/jobs" -}}
-{{- range $k, $v := . }}
-{{ $k }}={{ $v }}
-{{- end }}
-{{- end }}
-EOT
-      }
-      resources {
-        cpu    = ${cpu}
-        memory = ${memory}
-      }
-    }
-  }
-}
\ No newline at end of file
@@ -34,6 +34,17 @@ job "${job_name}" {
 {{ $k }}={{ $v }}
 {{- end }}
 {{- end }}
+EOT
+      }
+      template {
+        destination = "$${NOMAD_SECRETS_DIR}/.job"
+        env         = true
+        data        = <<EOT
+{{- with nomadVar "nomad/jobs/${job_name}" -}}
+{{- range $k, $v := . }}
+{{ $k }}={{ $v }}
+{{- end }}
+{{- end }}
 EOT
       }
       resources {
index 5e95285..dd4b6b6 100644 (file)
@@ -28,13 +28,6 @@ variable "memory" {
   type        = number
   default     = 8000
 }
-
-variable "namespace" {
-  description = "The namespace in which to execute the job."
-  type        = set(string)
-  default     = ["prod","sandbox"]
-}
-
 variable "node_pool" {
   description = "Specifies the node pool to place the job in."
   type        = string
@@ -51,4 +44,21 @@ variable "type" {
   description = "Specifies the Nomad scheduler to use."
   type        = string
   default     = "service"
+}
+
+variable "dispatchers" {
+  type = list(object({
+    namespace  = string
+    repository = string
+  }))
+  default = [
+    {
+      namespace  = "sandbox"
+      repository = "fdio-csit"
+    },
+    {
+      namespace  = "prod"
+      repository = "fdio-csit"
+    },
+  ]
 }
\ No newline at end of file