Adding a unique request header to hundreds of Ingresses with Traefik 2?

Hi, we're (finally) migrating from Traefik 1 to 2. We run 400+ apps on GKE. Our one remaining challenge is this:

In Traefik 1 we set an annotation on each app's Ingress that adds a header to the request to the backend. The value of that header is unique to each Ingress. Here's an example with the value . Each app has another value in its Ingress.:

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: testapp
  namespace: test
  annotations:
    ingress.kubernetes.io/custom-request-headers: X-Backend-Hint:<appname>
spec:
...

Traefik 2 no longer uses this header. It seems that the only way to add such a request header to each app is by creating a unique customRequestHeaders middleware for each unique value, then adding that middleware to the matching Ingress. Like so:

---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: backend-hint-header
  namespace: test
spec:
  headers:
    customRequestHeaders:
      X-Backend-Hint: <appname>
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: testapp
  namespace: test
  annotations:
    traefik.ingress.kubernetes.io/router.middlewares: test-backend-hint-header@kubernetescrd
spec:
...

Automating this is of course possible, but it does mean deploying 400+ almost identical middlewares. Won't that cause problems for Traefik?

Is this really the best way? Or can we somehow use a single middleware with a variable passed in via an Ingress annotation? Maybe something else I haven't thought of?

At this point, due to compatibility with other tools that reference Ingresses such as cert-manager and external-dns, we can't use the IngressRoute CRD yet but have to use classic Ingress.

1 Like

Anyone? Any advice on how to easily add many different request headers is welcome.

1 Like

I have exactly the same question.

1 Like