Hey all,
when using traefik + nomad auto service discovery, one has to rely on the nomad service tags defined in a job file.
More complex traefik routing rules can get pretty long. So was wondering if there is a way to do this more neatly with multiline (instead of 1 line).
As an example:
tags = [
"traefik.enable=true",
"traefik.http.routers.myroute.rule=(Host(`some.custom-domain.com`) || Host(`some.other-domain.com`) || HostRegexp(`generic-domain.com`)) && !PathPrefix(`/.well-known/acme-challenge/`)",
"traefik.http.routers.myroute.entrypoints=web"
]
In my case I need to support multiple custom domains. Having this in one line isn’t ideal.
Any solutions for this?
It should be possible to use Terraform native concat(). Not perfect, but maybe slightly better?
Thanks a lot! Your answer pointed me into the right direction.
I keep forgetting I have the HCL builtin functions available in nomad jobs =)
I opted to go for following solution:
Define the rules ahead of time in a local var.
locals {
custom_domain_list = [
"domain1",
"domain2",
"domain3",
"domain4",
"domain5"
]
joined_custom_domains = join("`) || Host(`", local.custom_domain_list)
traefik_custom_domain_rules = "Host(`${local.joined_custom_domains}`)"
traefik_router_rules = join("", [
"(",
"HostRegexp(`genericdomain`) || ",
local.traefik_custom_domain_rules,
")",
" && !PathPrefix(`/.well-known/acme-challenge/`)"
])
}
Which allows me to re-use the rules later on for the web and websecure entrypoints:
tags = [
"traefik.enable=true",
"traefik.http.middlewares.myrouter-redirect.redirectscheme.scheme=https",
"traefik.http.routers.myrouter.middlewares=myrouter-redirect",
"traefik.http.routers.myrouter.rule=${local.traefik_router_rules}",
"traefik.http.routers.myrouter.entrypoints=web",
"traefik.http.routers.myrouter_secure.rule=${local.traefik_router_rules}",
"traefik.http.routers.myrouter_secure.tls=true",
"traefik.http.routers.myrouter_secure.entrypoints=websecure"
]
1 Like