Auto add instance and consul suffix to router name

This commit is contained in:
Daniel Berteaud 2024-01-29 11:23:16 +01:00
parent e58b691593
commit 5903329a6d

View File

@ -4,44 +4,48 @@
*/ -]]
[[- if .traefik.enabled ]]
[[- $proto := regexp.Replace "^https$" "http" .traefik.proto ]]
[[- $router := (.traefik.router | strings.HasPrefix .instance) | ternary .traefik.router (printf "%s-%s%s" .instance .traefik.router .consul.suffix) ]]
"[[ .traefik.instance ]].enable=true",
[[- if has .traefik "connect" ]]
[[- /* There's no Consul Connect support for UDP */]]
[[- if eq $proto "udp" ]]
"[[ .traefik.instance ]].consulcatalog.connect=false",
[[- else if has .traefik "connect" ]]
"[[ .traefik.instance ]].consulcatalog.connect=[[ .traefik.connect ]]",
[[- end ]]
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ .traefik.router ]].entrypoints=[[ join .traefik.entrypoints "," ]]",
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ $router ]].entrypoints=[[ join .traefik.entrypoints "," ]]",
[[- if has .traefik "priority" ]]
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ .traefik.router ]].priority=[[ .traefik.priority ]]",
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ $router ]].priority=[[ .traefik.priority ]]",
[[- end ]]
[[- if has .traefik "tls" ]]
[[- if has .traefik.tls "enabled" ]]
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ .traefik.router ]].tls=[[ .traefik.tls.enabled ]]",
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ $router ]].tls=[[ .traefik.tls.enabled ]]",
[[- end ]]
[[- if has .traefik.tls "options" ]]
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ .traefik.router ]].tls.options=[[ .traefik.tls.options ]]",
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ $router ]].tls.options=[[ .traefik.tls.options ]]",
[[- end ]]
[[- end ]]
[[- $middlewares := coll.Slice ]]
[[- if and (has . "public_url") (eq $proto "http") ]]
[[- if has .traefik "rule" ]]
"[[ .traefik.instance ]].http.routers.[[ .traefik.router ]].rule=[[ .traefik.rule ]]",
"[[ .traefik.instance ]].http.routers.[[ $router ]].rule=[[ .traefik.rule ]]",
[[- else if .traefik.auto_rule ]]
"[[ .traefik.instance ]].http.routers.[[ .traefik.router ]].rule=Host(`[[ (urlParse .public_url).Hostname ]]`)
"[[ .traefik.instance ]].http.routers.[[ $router ]].rule=Host(`[[ (urlParse .public_url).Hostname ]]`)
[[- if not (regexp.Match "^/?$" (urlParse .public_url).Path) ]] && PathPrefix(`[[ (urlParse .public_url).Path | regexp.Replace "^//" "/" ]]`)[[ end ]]",
[[- end ]]
[[- if and (has .traefik "csp") (isKind "map" .traefik.csp) ]]
"[[ .traefik.instance ]].http.middlewares.csp-[[ .traefik.router ]].headers.contentsecuritypolicy=[[ range $k, $v := .traefik.csp ]][[ $k ]] [[ $v ]];[[ end ]]",
[[- $middlewares = prepend (printf "csp-%s" .traefik.router) $middlewares ]]
"[[ .traefik.instance ]].http.middlewares.csp-[[ $router ]].headers.contentsecuritypolicy=[[ range $k, $v := .traefik.csp ]][[ $k ]] [[ $v ]];[[ end ]]",
[[- $middlewares = prepend (printf "csp-%s" $router) $middlewares ]]
[[- end ]]
[[- if and (.traefik.strip_prefix) (has . "public_url") (not (regexp.Match "^/?$" (urlParse .public_url).Path)) ]]
"[[ .traefik.instance ]].http.middlewares.stripprefix-[[ .traefik.router ]].stripprefix.prefixes=[[ (urlParse .public_url).Path ]]",
[[- $middlewares = prepend (printf "stripprefix-%s" .traefik.router) $middlewares ]]
"[[ .traefik.instance ]].http.middlewares.stripprefix-[[ $router ]].stripprefix.prefixes=[[ (urlParse .public_url).Path ]]",
[[- $middlewares = prepend (printf "stripprefix-%s" $router) $middlewares ]]
[[- end ]]
[[- else if eq $proto "tcp" ]]
[[- if has .traefik "rule" ]]
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ .traefik.router ]].rule=[[ .traefik.rule ]]",
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ $router ]].rule=[[ .traefik.rule ]]",
[[- else if .traefik.auto_rule ]]
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ .traefik.router ]].rule=HostSNI(`*`)",
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ $router ]].rule=HostSNI(`*`)",
[[- end ]]
[[- end ]]
@ -66,6 +70,6 @@
[[- end ]]
[[- end ]]
[[- if gt (len $middlewares) 0 ]]
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ .traefik.router ]].middlewares=[[ join $middlewares "," -]]",
"[[ .traefik.instance ]].[[ $proto ]].routers.[[ $router ]].middlewares=[[ join $middlewares "," -]]",
[[- end ]]
[[- end ]]