AutoKuma icon indicating copy to clipboard operation
AutoKuma copied to clipboard

Use traefik labels as label prefix

Open Disane87 opened this issue 1 year ago • 12 comments

Hi and thank you for this awesome project!

I have several services running with one or more available webservers. Since I have around 30 docker containers running, I don't want to edit all of them manually and I tried to link autokuma with the traefik labels, because all available service have traefik labels.

Here are some of my labels

- traefik.enable=true
- traefik.subdomain=status
- traefik.http.services.uptimekuma.loadbalancer.server.port=3001

traefik.subdomain is a custom tag to get rid of the boilerplate I have to set on each service for Traefik.

But when I set AUTOKUMA__DOCKER__LABEL_PREFIX to traefik autokuma throws exceptions like

WARN [kuma_client::util] Error while parsing http: unknown variant services, expected one of group, http, port, ping, keyword, json-query, grpc-keyword, dns, docker, real-browser, push, steam, gamedig, mqtt, kafka-producer, sqlserver, postgres, mysql, mongodb, radius, redis, tailscale-ping! WARN [autokuma::sync] Encountered error during sync: Error while trying to parse labels: unknown variant services, expected one of group, http, port, ping, keyword, json-query, grpc-keyword, dns, docker, real-browser, push, steam, gamedig, mqtt, kafka-producer, sqlserver, postgres, mysql, mongodb, radius, redis, tailscale-ping

Which I understand, because the traefik labels doesn't match your kuma.xyz syntax.

Do you have any idea how I can automatically create entries in UptimeKuma with these treafik labels?

Disane87 avatar Nov 27 '24 15:11 Disane87

Not possible, you could create a snippet to "translate" traefik labels to autokuma ones to decrease duplication, but you'd still need to add a label to include the snippet in the given containers.

BigBoot avatar Nov 28 '24 06:11 BigBoot

Too bad!

Is there a chance to get such a "mode" or is this a thing you don't want to support?

Disane87 avatar Nov 28 '24 14:11 Disane87

I loved the idea

LukanRocks avatar Jan 21 '25 00:01 LukanRocks

A "traefik mode" is not possible since traefik uses multiple sources of information, i.e. the config file/env variables etc, the labels just don't contain all of the information. However I've added the ability to create snippets without the "kuma." prefix, this will allow creating a snippet tailored to whatever setup you have, see !Snippets

BigBoot avatar Jan 21 '25 19:01 BigBoot

A "traefik mode" is not possible since traefik uses multiple sources of information, i.e. the config file/env variables etc, the labels just don't contain all of the information. However I've added the ability to create snippets without the "kuma." prefix, this will allow creating a snippet tailored to whatever setup you have, see !Snippets

That looks awesome @BigBoot will test it soon! Thank you!

Disane87 avatar Jan 21 '25 19:01 Disane87

Was this just added? I was trying to get it to work and it didnt appear to be doing anything, I guess its not in the 0.8 release :)

ryanwinter avatar Jan 28 '25 23:01 ryanwinter

Just updated and configured this, works awesomely! Now I just need to enable the traefik label and it automatically created the autokuma using my customer snippet. Thanks :)

ryanwinter avatar Jan 29 '25 06:01 ryanwinter

This sounds like a super interesting and handy feature but its not really clear to me from the small section in the docs how one would actually use it... At least not for me and my pea sized brain :)

johntdyer avatar Jan 29 '25 14:01 johntdyer

@johntdyer Unfortunately it's not possible to create a one size fits all solution here, because autokuma just doesn't have all the same information as your traefik instance, so you might need to think a bit outside the box here and/or work with conventions on your setup to get everything you need. In the end you can access any label on your container using container["Labels"]["<label name>"] and you'll have to figure out a way to produce your monitor from this, the templating engine allows you to iterate/filter through lists and do various text manipulations and in the worst case you might need to hardcode some things into the template.

BigBoot avatar Jan 29 '25 15:01 BigBoot

@BigBoot - thanks for getting back to me.... I guess what I am looking for here is a most complete practical example of how this feature might be used. I felt that was kind of lacking with whats currently provided.... Hope that makes sense

johntdyer avatar Jan 29 '25 16:01 johntdyer

Here is an example of what I have done for homepage.

Create a traefik snippet

The following snippet overrides the default rule for a docker provider. It lets me override host and domain if I want, but otherwise falls back to defaults.

providers:
  docker:
    endpoint: unix:///var/run/docker.sock
    defaultRule: "Host(`\
      {{ if (index .Labels \"service.host\") }}\
        {{ index .Labels \"service.host\" }}\
      {{ else }}\
        {{ .ContainerName }}\
      {{ end }}\
      {{ if (index .Labels \"service.domain\") }}\
        .{{ index .Labels \"service.domain\" }}\
      {{ else }}\
        .example.com\
      {{ end }}`)"
    exposedByDefault: false

Create autokuma snippet

Then in my autokuma's config.yaml I have the following snippet which to create the monitor and group (optional)

snippets:
  "!traefik.enable": |
    {% set label = container.Labels %}
    {{ label["service.host"] }}.http.name: {{ label["service.host"] }}
    {{ label["service.host"] }}.http.url: https://{{ label["service.host"] }}
    {%- if label["service.domain"] %}.{{ label["service.domain"] }}{% else %}.example.com{% endif %}
    {%- if label["service.path"] %}/{{ label["service.path"] }}{% endif %}
    {% if label["service.group"] %}
      {{ label["service.group"] }}.group.name: {{ label["service.group"] }}
      {{ label["service.host"] }}.http.parent_name: {{ label["service.group"] }}
    {% endif %}

Define my service

Now in my compose file for a service, I can define service.host, service.domain and service.group. This is only triggered if traefik.enable is defined. The is because I only want uptime kuma to run on enabled traefik services.

services:
  homepage:
    image: ghcr.io/gethomepage/homepage:latest
    restart: unless-stopped
    volumes:
      - config:/app/config
    labels:
      service.host: "home"
      service.group: "apps"
      traefik.enable: "true"

volumes:
  config:

ryanwinter avatar Jan 29 '25 18:01 ryanwinter

@ryanwinter I don't get it working, it seems my autokuma ignores my config file. Could you send me your compose file or commands to execute the container? Want to doublecheck my docker against yours. Thank you in advance!

Disane87 avatar Mar 06 '25 15:03 Disane87