ez_docker/plugins/module_utils/label/traefik_service.py
2025-01-20 18:13:02 +01:00

65 lines
1.7 KiB
Python

# Copyright: (c) 2025, Luca Bilke <luca@bil.ke>
# MIT License (see LICENSE)
from __future__ import annotations
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from ansible_collections.snailed.ez_docker.plugins.module_utils.models import State
DOCUMENTATION = """
traefik_service:
description:
- Configuration for traefik_service labels.
type: list
elements: dict
suboptions:
name:
description:
- Name of the middleware.
type: string
proxy_type:
description:
- Traefik proxy type.
type: str
choices: [http, tcp, udp]
default: http
port:
description:
- Port to forward to.
type: int
protocol_version:
description:
- Proxy protocol version to use (TCP only).
type: int
"""
EXTRA_ARGS = {
"name": {"type": "str"},
"proxy_type": {"type": "str", "default": "http"},
"port": {"type": "int"},
"protocol_version": {"type": "int"},
}
def helper(state: State, service_name: str, params: dict[str, Any]) -> dict[str, Any]:
project_name: str = state.params["name"]
port: int | None = params.get("port")
protocol_version: int | None = params.get("protocol_version")
proxy_type: str = params["proxy_type"]
name: str = params.get("name", f"{project_name}_{service_name}_{proxy_type}")
prefix = f"traefik.{proxy_type}.services.{name}"
labels: dict[str, Any] = {}
if port:
labels[f"{prefix}.loadbalancer.server.port"] = str(port)
if protocol_version:
labels[f"{prefix}.loadbalancer.proxyProtocol.version"] = str(protocol_version)
return {
"labels": labels,
}