traefik: Rate-limit requests per client IP.
Attach a rateLimit middleware (10 r/s average, burst 100, per IPv4
address or IPv6 /64) to the web and websecure entrypoints, so it covers
every route. Traefik keeps a separate bucket per router.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Change-Id: I34d73eee3dd7746d202c2226c19d912d5a48c297
diff --git a/traefik/middlewares.yaml b/traefik/middlewares.yaml
new file mode 100644
index 0000000..c1390e0
--- /dev/null
+++ b/traefik/middlewares.yaml
@@ -0,0 +1,29 @@
+# Middlewares attached to Traefik's entrypoints (see ports.*.http.middlewares
+# in traefik/values.yaml). Apply this file before upgrading the Helm release:
+# a missing entrypoint middleware breaks every router.
+#
+# kubectl apply -f traefik/middlewares.yaml
+
+# Per-client-IP rate limit for all routes (IngressRoutes as well as the
+# nginx-class Ingresses). Traefik instantiates entrypoint middlewares per
+# router, so the bucket is per IP *and* router, not global per IP. Clients
+# over the limit get 429. The cgit per-host cap (mulk/git-cgit-ratelimit in
+# git/git.yaml) applies on top of this.
+#
+# Traefik sits on hostPorts without a proxy in front, so the remote address is
+# the real client; X-Forwarded-For is never trusted (depth 0).
+apiVersion: traefik.io/v1alpha1
+kind: Middleware
+metadata:
+ name: ratelimit-per-ip
+ namespace: traefik
+spec:
+ rateLimit:
+ average: 10
+ period: 1s
+ burst: 100
+ sourceCriterion:
+ ipStrategy:
+ depth: 0
+ # One bucket per IPv6 /64 rather than per address.
+ ipv6Subnet: 64
diff --git a/traefik/values.yaml b/traefik/values.yaml
index 52c1a5f..908e9c1 100644
--- a/traefik/values.yaml
+++ b/traefik/values.yaml
@@ -7,6 +7,9 @@
# ingress-nginx compatibility provider, so the Ingresses need no changes.
# The CRD provider is enabled for routes that need Traefik middlewares
# (e.g., the cgit rate limit in git/git.yaml).
+#
+# Both entrypoints run the per-client-IP rate limit from
+# traefik/middlewares.yaml, which must be applied before this release.
deployment:
replicas: 1
@@ -61,6 +64,8 @@
hostPort: 80
http:
aliasHeadersStrategy: delete
+ middlewares: &middlewares
+ - traefik-ratelimit-per-ip@kubernetescrd
encodedCharacters: &encodedCharacters
allowEncodedBackSlash: true
allowEncodedHash: true
@@ -72,4 +77,5 @@
hostPort: 443
http:
aliasHeadersStrategy: delete
+ middlewares: *middlewares
encodedCharacters: *encodedCharacters