Skip to main content

Generate traefik labels easily

Project description

Laebelmaker

Tool for generating Traefik labels. Written in Python.

Installation

Laebelmaker is published on PyPI. You can use pip to install it:

python3 -m pip install --user laebelmaker

It is recommended that you also install the docker module. You can install both Laebelmaker and docker as its dependency with:

python3 -m pip install --user laebelmaker[docker]

This will allow Laebelmaker to use metadata of Docker images to prevent redundant prompts from the user, e.g. when an image exposes a single port.

Usage

Laebelmaker can be used to automatically generate Traefik labels from various sources, such as docker-compose.yml files, running Docker containers and user-given options.

You can always consult the help menu for all features with short (and hopefully clear) explanations:

$ laebelmaker --help
usage: laebelmaker [-h] [-i] [-c NAME] [-f FORMAT] [FILE ...]

Generate Traefik labels

positional arguments:
  FILE                  Compose file to generate labels for

options:
  -h, --help            show this help message and exit
  -i, --interactive     use interactive mode
  -c NAME, --container NAME
                        generate labels for a given container on the system
  -f FORMAT, --format FORMAT
                        set output format, one of: [docker, none, yaml]

Examples

CLI Interactive Mode

$ laebelmaker -i
Enter value for 'deploy name': myapp
Enter value for 'url': myapp.example.com
Enter value for 'port' (integer): 25565
Enter value for 'https redirection' (yes/No): no
--START GENERATED LABELS FOR 'myapp'--
traefik.enable=true
traefik.http.routers.myapp.rule=Host(`myapp.example.com`)
traefik.http.services.myapp.loadbalancer.server.port=25565
--END GENERATED LABELS FOR 'myapp'--

With Compose YAML file

Invoking laebelmaker on a Docker Compose YAML file, the program will prompt the user for different options, with the defaults given in parentheses. This example also modifies the output format with -f yaml, which means the labels are immediately ready to be used in a YAML file.

$ laebelmaker -f yaml examples/docker-compose-testapp.yml
Found multiple services.
 1. testapp
 2. testapp-db
Service number to use (default 1): 1
Enter value for 'url': testapp.example.com/api
Enter value for 'https redirection' (yes/No): yes
Enter value for 'web entrypoint': http
Enter value for 'websecure entrypoint': https
Enter value for 'tls resolver': letsencrypt
--START GENERATED LABELS FOR 'testapp'--
  - traefik.enable=true
  - traefik.http.routers.testapp.rule=(Host(`testapp.example.com`) && PathPrefix(`/api`))
  - traefik.http.routers.testapp.entrypoints=http
  - traefik.http.routers.testapp-https.rule=(Host(`testapp.example.com`) && PathPrefix(`/api`))
  - traefik.http.routers.testapp-https.entrypoints=https
  - traefik.http.routers.testapp.middlewares=testapp-redir
  - traefik.http.middlewares.testapp-redir.redirectscheme.scheme=https
  - traefik.http.routers.testapp-https.tls=true
  - traefik.http.routers.testapp-https.tls.certresolver=letsencrypt
  - traefik.http.services.testapp.loadbalancer.server.port=80
--END GENERATED LABELS FOR 'testapp'--

If an invalid file is given, Laebelmaker should hopefully print a sensible error message.

$ laebelmaker examples/invalid-image-tag.yml
Pulling image:
 ⠿ ubuntu:latestest Failed
Invalid image tag: 'ubuntu:latestest' in 'examples/invalid-image-tag.yml'.
Failed to produce output.
Try running: laebelmaker --help

To-do

  • Generate Traefik labels using an interactive CLI
  • Generate Traefik labels using command-line options
  • Generate labels from existing service definitions (e.g. Docker Compose YAML files)
  • Learn how to and publish this project to PyPi
  • Add combined Rule types (with logical operators)
  • Add automated tests
  • Increase code coverage for tests
  • Add support for TCP/UDP routers and services
  • Remove pyyaml as a hard dependency
  • Add local Traefik config as data source (e.g. for entrypoint and TLS resolver names)
  • Add Dockerfile as a data source
  • Add K8s YAML as a data source
  • Add Docker Swarm YAML as a data source
  • Add more sophisticated Rule parsing (e.g. from a given URL)

Something to think about:

  • Expand out of Traefik into a more general use-case
  • Compatibility for Windows machines

Guidelines for development

  • Ease of use is a priority
  • Use sensible defaults without asking, when possible
  • When defaults are not possible, offer the user a sensible prefilled value
  • The code should be as Pythonic as possible
  • Use type hints as much as possible to catch logical errors

Local development setup

For local development, a virtual environment is highly recommended. The following section will assume you installed a virtual environment with the venv module:

python3 -m venv venv

All requirements for development are given in requirements-dev.txt. Install all of them before contributing. This is required for pre-commit. You can use the following command to install the requirements:

python3 -m pip --require-virtualenv install -r requirements-dev.txt

Before commiting, install pre-commit hooks for Git by running:

pre-commit install

This will run the following programs to verify a commit:

You can install the Laebelmaker project locally in Development Mode:

python3 -m pip install -e .

The project will be installed in the virtual environment but will remain editable.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

laebelmaker-0.4.1.tar.gz (16.2 kB view hashes)

Uploaded Source

Built Distribution

laebelmaker-0.4.1-py3-none-any.whl (15.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page