Allow binding ports directly to service container

This commit is contained in:
IamTheFij 2024-11-21 11:13:26 -08:00
parent 36fa152553
commit ee6f7bd937
3 changed files with 23 additions and 14 deletions

View File

@ -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" { resource "nomad_job" "service" {
jobspec = templatefile("${path.module}/service_template.nomad", { jobspec = templatefile("${path.module}/service_template.nomad", {
name = var.name name = var.name
@ -20,6 +27,7 @@ resource "nomad_job" "service" {
service_port_static = var.service_port_static service_port_static = var.service_port_static
service_check = var.service_check service_check = var.service_check
ports = var.ports ports = var.ports
port_names = local.port_names
sticky_disk = var.sticky_disk sticky_disk = var.sticky_disk
resources = var.resources resources = var.resources
stunnel_resources = var.stunnel_resources stunnel_resources = var.stunnel_resources

View File

@ -152,8 +152,8 @@ job "${name}" {
%{~if image_pull_timeout != null ~} %{~if image_pull_timeout != null ~}
image_pull_timeout = "${image_pull_timeout}" image_pull_timeout = "${image_pull_timeout}"
%{~ endif ~} %{~ endif ~}
%{~ if service_port != null ~} %{~ if length(try(port_names, [])) > 0 ~}
ports = ["main"] ports = ${jsonencode(port_names)}
%{~ endif ~} %{~ endif ~}
%{~ if length(try(args, [])) > 0 ~} %{~ if length(try(args, [])) > 0 ~}
args = ${jsonencode(args)} args = ${jsonencode(args)}

View File

@ -77,6 +77,19 @@ variable "service_port_static" {
description = "Should the port assigned be 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" { variable "prometheus" {
type = bool type = bool
default = false default = false
@ -155,18 +168,6 @@ variable "service_tags" {
description = "Additional tags to be added to the service." 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" { variable "templates" {
type = list(object({ type = list(object({
data = string data = string