Kubernetes Ingress Validation Fails When Using Regex Path Matching Without Wildcard Hostnames

Viewed 4482

I have deployed my application in a baremetal kubernetes cluster using nginx as ingress controller. I have deployed several ui applications with <my-hostname>/<module-name> (I cannot change this due to client's requirements). I have written following ingress rules to access my APIs and modules.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: 100m
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/ssl-passthrough: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.org/proxy-connect-timeout: 60s
    nginx.org/proxy-read-timeout: 60s
  name: test-ingress
  namespace: csi-dev
spec:
  rules:
  - host: 'my.host.name'
    http:
      paths:
      - backend:
          serviceName: security-servicename
          servicePort: 80
        path: (/<api-pattern>/iam/)(.*)
      - backend:
          serviceName: api-gateway-servicename
          servicePort: 80
        path: (/<api-pattern>/)(.*)
      - backend:
          serviceName: ui-config-server-servicename
          servicePort: 80
        path: (/<ui-config-server-pattern>/)(.*)
      - backend:
          serviceName: ui-module1-servicename
          servicePort: 80
        path: /(ui-module1)/?(.*)
      - backend:
          serviceName: ui-module1-servicename
          servicePort: 80
        path: /(ui-module2)/?(.*)
      - backend:
          serviceName: ui-module1-servicename
          servicePort: 80
        path: /(ui-module3)/?(.*)

When I apply this Ingress controller, Kubernetes give me following errors.

* spec.rules[0].http.paths[0].path: Invalid value: "(.*/<api-pattern>/iam/)(.*)": must be an absolute path
* spec.rules[0].http.paths[1].path: Invalid value: "(.*/<api-pattern>/)(.*)": must be an absolute path
* spec.rules[0].http.paths[2].path: Invalid value: "(.*/<ui-config-server>/)(.*)": must be an absolute path

But when I use *.host.name instead of my.host.name this works without error. I need to restrict my hostname also.

Do anyone have a solution?

1 Answers

Found an answer on this

Kubernetes API validates whether spec.rules[n].http.paths[m].path has a / when hostname does not have a wildcard. Hence rewrote Path as follows.

/(.*<api-pattern>/iam/)(.*)

Related