2024-11-21 19:13:26 +00:00
|
|
|
locals {
|
|
|
|
port_names = concat(
|
|
|
|
var.service_port != null ? ["main"] : [],
|
|
|
|
[for port in var.ports : port.name if port.task_config],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-11 21:34:08 +00:00
|
|
|
resource "nomad_job" "service" {
|
|
|
|
jobspec = templatefile("${path.module}/service_template.nomad", {
|
2023-02-27 19:48:24 +00:00
|
|
|
name = var.name
|
2023-07-07 22:51:19 +00:00
|
|
|
count = var.instance_count
|
2023-07-07 00:23:20 +00:00
|
|
|
priority = var.priority
|
2023-02-27 19:48:24 +00:00
|
|
|
image = var.image
|
|
|
|
image_pull_timeout = var.image_pull_timeout
|
|
|
|
args = var.args
|
|
|
|
env = var.env
|
2023-08-24 22:41:18 +00:00
|
|
|
task_meta = var.task_meta
|
2024-05-01 17:18:24 +00:00
|
|
|
task_identity = var.task_identity
|
2023-02-27 19:48:24 +00:00
|
|
|
group_meta = var.group_meta
|
2023-08-24 22:41:18 +00:00
|
|
|
job_meta = var.job_meta
|
2023-04-20 23:47:07 +00:00
|
|
|
constraints = var.constraints
|
|
|
|
docker_devices = var.docker_devices
|
2024-06-26 20:30:50 +00:00
|
|
|
user = var.user
|
2024-10-02 18:26:57 +00:00
|
|
|
actions = var.actions
|
2022-11-11 21:34:08 +00:00
|
|
|
|
2023-07-07 23:33:36 +00:00
|
|
|
service_port = var.service_port
|
|
|
|
service_port_static = var.service_port_static
|
2024-01-23 20:08:47 +00:00
|
|
|
service_check = var.service_check
|
2023-07-07 23:33:36 +00:00
|
|
|
ports = var.ports
|
2024-11-21 19:13:26 +00:00
|
|
|
port_names = local.port_names
|
2023-07-07 23:33:36 +00:00
|
|
|
sticky_disk = var.sticky_disk
|
|
|
|
resources = var.resources
|
2023-08-07 18:31:35 +00:00
|
|
|
stunnel_resources = var.stunnel_resources
|
2023-07-07 23:33:36 +00:00
|
|
|
service_tags = var.service_tags
|
2023-07-24 22:21:20 +00:00
|
|
|
custom_services = var.custom_services
|
2023-08-24 19:36:47 +00:00
|
|
|
use_wesher = var.use_wesher
|
2022-11-11 21:34:08 +00:00
|
|
|
|
|
|
|
ingress = var.ingress
|
|
|
|
ingress_rule = var.ingress_rule
|
|
|
|
ingress_middlewares = var.ingress_middlewares
|
2023-03-25 05:58:44 +00:00
|
|
|
prometheus = var.prometheus
|
2022-11-11 21:34:08 +00:00
|
|
|
|
|
|
|
templates = var.templates
|
|
|
|
host_volumes = var.host_volumes
|
|
|
|
|
2023-07-25 17:59:33 +00:00
|
|
|
use_mysql = var.use_mysql || var.mysql_bootstrap != null
|
2023-08-29 19:48:48 +00:00
|
|
|
use_postgres = var.use_postgres || var.postgres_bootstrap != null
|
2023-07-25 17:59:33 +00:00
|
|
|
use_redis = var.use_redis
|
|
|
|
use_ldap = var.use_ldap
|
2022-11-11 21:34:08 +00:00
|
|
|
|
2023-07-25 17:59:33 +00:00
|
|
|
mysql_bootstrap = var.mysql_bootstrap
|
|
|
|
postgres_bootstrap = var.postgres_bootstrap
|
2022-11-11 21:34:08 +00:00
|
|
|
})
|
2023-09-28 04:30:22 +00:00
|
|
|
|
|
|
|
detach = var.detach
|
2022-11-11 21:34:08 +00:00
|
|
|
}
|
2023-08-29 19:48:48 +00:00
|
|
|
|
|
|
|
resource "nomad_acl_policy" "secrets_mysql" {
|
|
|
|
count = var.use_mysql || var.mysql_bootstrap != null ? 1 : 0
|
|
|
|
|
|
|
|
name = "${var.name}-secrets-mysql"
|
|
|
|
description = "Give access to MySQL secrets"
|
|
|
|
rules_hcl = <<EOH
|
|
|
|
namespace "default" {
|
|
|
|
variables {
|
|
|
|
path "secrets/mysql" {
|
|
|
|
capabilities = ["read"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOH
|
|
|
|
|
|
|
|
job_acl {
|
2024-01-16 22:16:21 +00:00
|
|
|
job_id = resource.nomad_job.service.id
|
2023-08-29 19:48:48 +00:00
|
|
|
group = var.name
|
|
|
|
task = "mysql-bootstrap"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_password" "mysql_psk" {
|
|
|
|
count = var.use_mysql || var.mysql_bootstrap != null ? 1 : 0
|
|
|
|
|
|
|
|
length = 32
|
|
|
|
override_special = "!@#%&*-_="
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "nomad_variable" "mysql_psk" {
|
|
|
|
count = var.use_mysql || var.mysql_bootstrap != null ? 1 : 0
|
|
|
|
|
|
|
|
path = "secrets/mysql/allowed_psks/${var.name}"
|
|
|
|
items = {
|
|
|
|
psk = "${var.name}:${resource.random_password.mysql_psk[0].result}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "nomad_acl_policy" "mysql_psk" {
|
|
|
|
count = var.use_mysql || var.mysql_bootstrap != null ? 1 : 0
|
|
|
|
|
|
|
|
name = "${var.name}-secrets-mysql-psk"
|
|
|
|
description = "Give access to MySQL PSK secrets"
|
|
|
|
rules_hcl = <<EOH
|
|
|
|
namespace "default" {
|
|
|
|
variables {
|
|
|
|
path "secrets/mysql/allowed_psks/${var.name}" {
|
|
|
|
capabilities = ["read"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOH
|
|
|
|
|
|
|
|
job_acl {
|
2024-01-16 22:16:21 +00:00
|
|
|
job_id = resource.nomad_job.service.id
|
2023-08-29 19:48:48 +00:00
|
|
|
group = var.name
|
|
|
|
task = "stunnel"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "nomad_acl_policy" "secrets_postgres" {
|
|
|
|
count = var.use_postgres || var.postgres_bootstrap != null ? 1 : 0
|
|
|
|
|
|
|
|
name = "${var.name}-secrets-postgres"
|
|
|
|
description = "Give access to Postgres secrets"
|
|
|
|
rules_hcl = <<EOH
|
|
|
|
namespace "default" {
|
|
|
|
variables {
|
|
|
|
path "secrets/postgres" {
|
|
|
|
capabilities = ["read"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOH
|
|
|
|
|
|
|
|
job_acl {
|
2024-01-16 22:16:21 +00:00
|
|
|
job_id = resource.nomad_job.service.id
|
2023-08-29 19:48:48 +00:00
|
|
|
group = var.name
|
|
|
|
task = "postgres-bootstrap"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_password" "postgres_psk" {
|
|
|
|
count = var.use_postgres || var.postgres_bootstrap != null ? 1 : 0
|
|
|
|
|
|
|
|
length = 32
|
|
|
|
override_special = "!@#%&*-_="
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "nomad_variable" "postgres_psk" {
|
|
|
|
count = var.use_postgres || var.postgres_bootstrap != null ? 1 : 0
|
|
|
|
|
|
|
|
path = "secrets/postgres/allowed_psks/${var.name}"
|
|
|
|
items = {
|
|
|
|
psk = "${var.name}:${resource.random_password.postgres_psk[0].result}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "nomad_acl_policy" "postgres_psk" {
|
|
|
|
count = var.use_postgres || var.postgres_bootstrap != null ? 1 : 0
|
|
|
|
|
|
|
|
name = "${var.name}-secrets-postgres-psk"
|
|
|
|
description = "Give access to Postgres PSK secrets"
|
|
|
|
rules_hcl = <<EOH
|
|
|
|
namespace "default" {
|
|
|
|
variables {
|
|
|
|
path "secrets/postgres/allowed_psks/${var.name}" {
|
|
|
|
capabilities = ["read"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOH
|
|
|
|
|
|
|
|
job_acl {
|
2024-01-16 22:16:21 +00:00
|
|
|
job_id = resource.nomad_job.service.id
|
2023-08-29 19:48:48 +00:00
|
|
|
group = var.name
|
|
|
|
task = "stunnel"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_password" "ldap_psk" {
|
|
|
|
count = var.use_ldap ? 1 : 0
|
|
|
|
|
|
|
|
length = 32
|
|
|
|
override_special = "!@#%&*-_="
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "nomad_variable" "ldap_psk" {
|
|
|
|
count = var.use_ldap ? 1 : 0
|
|
|
|
|
|
|
|
path = "secrets/ldap/allowed_psks/${var.name}"
|
|
|
|
items = {
|
|
|
|
psk = "${var.name}:${resource.random_password.ldap_psk[0].result}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "nomad_acl_policy" "ldap_psk" {
|
|
|
|
count = var.use_ldap ? 1 : 0
|
|
|
|
|
|
|
|
name = "${var.name}-secrets-ldap-psk"
|
|
|
|
description = "Give access to ldap PSK secrets"
|
|
|
|
rules_hcl = <<EOH
|
|
|
|
namespace "default" {
|
|
|
|
variables {
|
|
|
|
path "secrets/ldap/allowed_psks/${var.name}" {
|
|
|
|
capabilities = ["read"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOH
|
|
|
|
|
|
|
|
job_acl {
|
2024-01-16 22:16:21 +00:00
|
|
|
job_id = resource.nomad_job.service.id
|
2023-08-29 19:48:48 +00:00
|
|
|
group = var.name
|
|
|
|
task = "stunnel"
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 22:11:40 +00:00
|
|
|
|
|
|
|
resource "nomad_acl_policy" "secrets_smtp" {
|
|
|
|
count = var.use_smtp ? 1 : 0
|
|
|
|
|
|
|
|
name = "${var.name}-secrets-smtp"
|
|
|
|
description = "Give access to SMTP secrets"
|
|
|
|
rules_hcl = <<EOH
|
|
|
|
namespace "default" {
|
|
|
|
variables {
|
|
|
|
path "secrets/smtp" {
|
|
|
|
capabilities = ["read"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOH
|
|
|
|
|
|
|
|
job_acl {
|
2024-01-16 22:16:21 +00:00
|
|
|
job_id = resource.nomad_job.service.id
|
2023-08-29 22:11:40 +00:00
|
|
|
group = var.name
|
|
|
|
task = var.name
|
|
|
|
}
|
|
|
|
}
|
2024-08-29 21:07:49 +00:00
|
|
|
|
|
|
|
module "oidc_client" {
|
|
|
|
count = var.oidc_client_config != null ? 1 : 0
|
|
|
|
|
|
|
|
source = "../../core/oidc_client"
|
|
|
|
name = var.name
|
|
|
|
|
|
|
|
oidc_client_config = {
|
|
|
|
description = var.oidc_client_config.description
|
|
|
|
authorization_policy = var.oidc_client_config.authorization_policy
|
|
|
|
redirect_uris = var.oidc_client_config.redirect_uris
|
|
|
|
scopes = var.oidc_client_config.scopes
|
|
|
|
}
|
|
|
|
|
|
|
|
job_acl = {
|
|
|
|
job_id = resource.nomad_job.service.id
|
|
|
|
group = var.name
|
|
|
|
task = var.name
|
|
|
|
}
|
|
|
|
}
|
2024-10-02 18:26:57 +00:00
|
|
|
|
|
|
|
# Action cron jobs
|
|
|
|
resource "nomad_job" "action_cron" {
|
|
|
|
for_each = tomap({ for action in var.actions : action.name => action if action.cron != null })
|
|
|
|
jobspec = templatefile("${path.module}/service_scheduled.nomad", {
|
|
|
|
name = var.name
|
|
|
|
action_name = each.value.name
|
|
|
|
action_cron = each.value.cron
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "nomad_acl_policy" "action_cron_workload_policy" {
|
|
|
|
for_each = resource.nomad_job.action_cron
|
|
|
|
|
|
|
|
name = "service-action-${each.value.id}"
|
|
|
|
description = "Give custom service cron actions access to execute actions."
|
|
|
|
rules_hcl = <<EOH
|
|
|
|
namespace "default" {
|
|
|
|
capabilities = [
|
|
|
|
"list-jobs",
|
|
|
|
"read-job",
|
|
|
|
"alloc-exec",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOH
|
|
|
|
|
|
|
|
job_acl {
|
|
|
|
job_id = each.value.id
|
|
|
|
}
|
|
|
|
}
|