How to use tags in traefik front-end template?

871 Views Asked by At

I have services declared with tags in Consul Catalog, and these services are visible in traefik through the correct configuration of Traefik (consider it works, I've already tested that).

These services have tags declared in Consul, through environment variables on the service layer. I typically have a version tag on some services, and a unit tag on others.

I want to have my services proxied by traefik on URL of the following form

http://{version}.{unit].{service}.{domain}/

I already have the default working template Host:{{.ServiceName}}.{{.Domain}} So I guess I can add my unit tag by changing it to Host:{{getTag("BU", tags, "white")}}.{{.ServiceName}}.{{.Domain}}. But when I do that, I get tons of error messages in the form

"Error creating route for frontend a-frontend: error parsing rule: Empty rule"

even for frontends mapping with services which have the unit tag defined.

So, how can I have it working, considering I'm like Jon Snow (I know nothing about go templates) ?

1

There are 1 best solutions below

0
On BEST ANSWER

Suppose I have defined the version tag on my service. This version tag is visible in Consul. In traefik, this tag will be visible in the Attributes "map".

As a consequence, I can use in the frontEndRule the following code

frontEndRule = Host:{{if hasTag "version" .Attributes}}{{getTag "version" .Attributes ""}}.{{end}}{{.ServiceName}}.{{.Domain}}

Notice that method calls do not use C-like text formatting.