Search code examples
nginxkubernetesingress-nginx

ingress-nginx: How to insert access_by_lua_block{} only for specific location?


I have multiple location blocks under a single host:, something like this:

apiVersion: networking.k8s.io/v1
kind: ingress
metadata:
  name: ingress-nginx
  annotations:
    kubernetes.io/ingress.calass: nginx
    ngnx.ingress.kubernetes.io/use-regex: "true"
    ngnx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
    - host: ingress.mydomain.org.local
      http:
        paths:
          - path: /app1(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: app1-service
                port:
                  number: 5678
          - path: /api(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: api-service
                port:
                  number: 5678
    

I need to insert access_by_lua_block{...} only for one location, say: /api - how do I do that? I tried with ngnx.ingress.kubernetes.io\configuration-snippet, like this:

annotations:
  ngnx.ingress.kubernetes.io\configuration-snippet: |
    location ~* "^/api(/|$)(.*)" {
      access_by_lua_block {
      ....
      }
    }

but that's just adding a nested location ~* "^/api(/|$)(.*)" block under every other location entries. Is there any way that can be achieved?


Solution

  • As suggested by @mdaniel ,the answer is always to create a 2nd Ingress resource when you need to apply annotations to just one of them.

    The smooth coexistence of multiple Ingress Controllers in one cluster is provided by the Ingress class concept, which mandates the following:

    1. Every Ingress Controller must only handle Ingress resources for its particular class.

    2. Ingress resources should have the ingressClassName field set to the value, which corresponds to the class of the Ingress Controller the user wants to use.

    3. VirtualServer, VirtualServerRoute, Policy and TransportServer resources should have the ingressClassName field set to the value, which corresponds to the class of the Ingress Controller the user wants to use.

    Refer to this documentation for more information on Ingress resources.