Score:1

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

ru flag

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 supports 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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.