105 lines
2.9 KiB
Python
105 lines
2.9 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_router:
|
|
description:
|
|
- Configuration for traefik_router labels.
|
|
type: list
|
|
elements: dict
|
|
suboptions:
|
|
name:
|
|
description:
|
|
- Name of the middleware.
|
|
type: string
|
|
rule:
|
|
description:
|
|
- Routing rule to match.
|
|
type: str
|
|
required: true
|
|
proxy_type:
|
|
description:
|
|
- Traefik proxy type.
|
|
type: str
|
|
choices: [http, tcp, udp]
|
|
default: http
|
|
service:
|
|
description:
|
|
- Traefik service to point at.
|
|
type: str
|
|
certresolver:
|
|
description:
|
|
- Certresolver to use.
|
|
type: str
|
|
tls_passthrough:
|
|
description:
|
|
- Enable TLS passthrough.
|
|
type: bool
|
|
default: false
|
|
entrypoints:
|
|
description:
|
|
- Entrypoints to listen on.
|
|
type: list
|
|
elements: str
|
|
middlewares:
|
|
description:
|
|
- Middlewares to use.
|
|
type: list
|
|
elements: str
|
|
"""
|
|
|
|
EXTRA_ARGS = {
|
|
"name": {"type": "str"},
|
|
"rule": {"type": "str", "required": True},
|
|
"proxy_type": {"type": "str", "default": "http"},
|
|
"service": {"type": "str"},
|
|
"certresolver": {"type": "str"},
|
|
"tls_passthrough": {"type": "bool", "default": False},
|
|
"entrypoints": {"type": "list"},
|
|
"middlewares": {"type": "list"},
|
|
}
|
|
|
|
|
|
def helper(state: State, service_name: str, params: dict[str, Any]) -> dict[str, Any]:
|
|
project_name: str = state.params["name"]
|
|
rule: str = params["rule"]
|
|
traefik_service: str | None = params.get("service")
|
|
entrypoints: list[str] | None = params.get("entrypoints")
|
|
middlewares: list[str] | None = params.get("middlewares")
|
|
certresolver: str | None = params.get("certresolver")
|
|
tls_passthrough: bool = params["tls_passthrough"]
|
|
proxy_type: str = params["proxy_type"]
|
|
name: str = params.get("name", f"{project_name}_{service_name}_{proxy_type}")
|
|
|
|
prefix = f"traefik.{proxy_type}.routers.{name}"
|
|
|
|
labels = {
|
|
"traefik.enable": True,
|
|
f"{prefix}.rule": rule,
|
|
}
|
|
|
|
if certresolver:
|
|
labels[f"{prefix}.tls.certresolver"] = certresolver
|
|
|
|
if tls_passthrough:
|
|
labels[f"{prefix}.tls.passthrough"] = "true"
|
|
|
|
if entrypoints:
|
|
labels[f"{prefix}.entrypoints"] = ",".join(entrypoints)
|
|
|
|
if traefik_service:
|
|
labels[f"{prefix}.service"] = traefik_service
|
|
|
|
if middlewares:
|
|
labels[f"{prefix}.middlewares"] = ",".join(middlewares)
|
|
|
|
return {
|
|
"labels": labels,
|
|
}
|