Can we set priority for the middlewares in v2?

Hi, I've asked the same question on stackoverflow some time ago, but I got no relevant answers: https://stackoverflow.com/questions/56644462/can-we-set-priority-for-the-middlewares-in-traefik-v2

The question is the following:

Using the v1.7.9 in kubernetes I'm facing this issue:

if I set a rate limit (traefik.ingress.kubernetes.io/rate-limit) and custom response headers (traefik.ingress.kubernetes.io/custom-response-headers) then when a request gets rate limited, the custom headers won't be set. I guess it's because of some ordering/priority among these plugins. And I totally agree that reaching the rate-limit should return the response as soon as is possible, but it would be nice, if we could modify the priorities if we need.

The question therefore is: will we be able to set priorities for the middlewares?

I couldn't find any clue of it in the docs nor among the github issues.

Concrete use-case:

I want CORS-policy headers to always be set, even if the rate-limiting kicked in. I want this because my SPA won't get the response object otherwise, because the browser won't allow it:

Access to XMLHttpRequest at 'https://api.example.com/api/v1/resource' from origin 'https://cors.exmaple.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

In this case it would be a fine solution if i could just set the priority of the headers middleware higher than the rate limit middleware.

Hello,

In the v2, the middlewares can ordered in the order you want, you can put the same type of middleware several times with different configurations on the same route.

https://docs.traefik.io/v2.0/middlewares/overview/

Hi, thanks for your reply.

I've read that doc earlier, but didn't give v2 a try yet.
I didn't find any example for ordering middlewares, that's why I asked.

So I'm just guessing that something like this will work?

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: ratelimit
spec:
  rateLimit:
      average: 100
      burst: 50
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: response-header
spec:
  headers:
    customResponseHeaders:
      X-Custom-Response-Header: "value"
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: ingressroute
spec:
# more fields...
  routes:
    # more fields...
    middlewares: # the middlewares will be called in this order
    - name: response-header
    - name: ratelimit

So the ordering can be defined in the IngressRoute resource, right?

# ...
    middlewares: # the middlewares will be called in this order
    - name: response-header
    - name: ratelimit

Yes the definition order is the calling order.

1 Like

awesome, thank you! :slight_smile:

Does this apply to definition order in a labels-only v2 setup?

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.