From ee6f7bd937284692c234e3ae0d0df056e0dec32d Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Thu, 21 Nov 2024 11:13:26 -0800 Subject: [PATCH] Allow binding ports directly to service container --- services/service/main.tf | 8 ++++++++ services/service/service_template.nomad | 4 ++-- services/service/vars.tf | 25 +++++++++++++------------ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/services/service/main.tf b/services/service/main.tf index e0971e2..1d8ec0a 100644 --- a/services/service/main.tf +++ b/services/service/main.tf @@ -1,3 +1,10 @@ +locals { + port_names = concat( + var.service_port != null ? ["main"] : [], + [for port in var.ports : port.name if port.task_config], + ) +} + resource "nomad_job" "service" { jobspec = templatefile("${path.module}/service_template.nomad", { name = var.name @@ -20,6 +27,7 @@ resource "nomad_job" "service" { service_port_static = var.service_port_static service_check = var.service_check ports = var.ports + port_names = local.port_names sticky_disk = var.sticky_disk resources = var.resources stunnel_resources = var.stunnel_resources diff --git a/services/service/service_template.nomad b/services/service/service_template.nomad index e7ce628..1dba45d 100644 --- a/services/service/service_template.nomad +++ b/services/service/service_template.nomad @@ -152,8 +152,8 @@ job "${name}" { %{~if image_pull_timeout != null ~} image_pull_timeout = "${image_pull_timeout}" %{~ endif ~} - %{~ if service_port != null ~} - ports = ["main"] + %{~ if length(try(port_names, [])) > 0 ~} + ports = ${jsonencode(port_names)} %{~ endif ~} %{~ if length(try(args, [])) > 0 ~} args = ${jsonencode(args)} diff --git a/services/service/vars.tf b/services/service/vars.tf index e76fb54..72c867b 100644 --- a/services/service/vars.tf +++ b/services/service/vars.tf @@ -77,6 +77,19 @@ variable "service_port_static" { description = "Should the port assigned be static" } +variable "ports" { + type = list(object({ + name = string + host_network = optional(string) + from = optional(number) + to = optional(number) + static = optional(number) + task_config = optional(bool, false) + })) + default = [] + description = "Additional ports (not service_port) to be bound." +} + variable "prometheus" { type = bool default = false @@ -155,18 +168,6 @@ variable "service_tags" { description = "Additional tags to be added to the service." } -variable "ports" { - type = list(object({ - name = string - host_network = optional(string) - from = optional(number) - to = optional(number) - static = optional(number) - })) - default = [] - description = "Additional ports (not service_port) to be bound." -} - variable "templates" { type = list(object({ data = string