Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use traefik labels as label prefix #105

Open
Disane87 opened this issue Nov 27, 2024 · 12 comments
Open

Use traefik labels as label prefix #105

Disane87 opened this issue Nov 27, 2024 · 12 comments

Comments

@Disane87
Copy link

Disane87 commented Nov 27, 2024

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?

@BigBoot
Copy link
Owner

BigBoot commented Nov 28, 2024

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.

@Disane87
Copy link
Author

Too bad!

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

@LukanRocks
Copy link

I loved the idea

@BigBoot
Copy link
Owner

BigBoot commented Jan 21, 2025

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

@Disane87
Copy link
Author

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!

@ryanwinter
Copy link

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
Copy link

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 :)

@johntdyer
Copy link

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 :)

@BigBoot
Copy link
Owner

BigBoot commented Jan 29, 2025

@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.

@johntdyer
Copy link

@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

@ryanwinter
Copy link

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:

@Disane87
Copy link
Author

Disane87 commented Mar 6, 2025

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants