Sunday, November 21, 2021

Ingress Controller (continue)

On my March blog, I compared the community version ingress-nginx controller and the Nginx supported nginx-ingress controller.  My conclusion was the community version is better.  

Now my conclusion still stays.  But in reality, you do not always have the choice.  Sometimes, you have to depend on some features on Nginx plus or just your company decides to use nginx-ingress.  If that is the case, you will have to deal with some inconvenience from that. 

Today, I will share two things that are very easy in the community version but is hard in Nginx's nginx-ingress.


Rewrite Annotation


When we get the request from the customer, we need to route it to a certain URI (for example, /mycheck) to do something (such as a security check) regardless what its original URI is. 
 
The “rewrite” annotation worked very well for the community version of ingress-nginx controller using the annotation like this:
 
nginx.ingress.kubernetes.io/rewrite-target: /mycheck
 
The new way in Nginx plus ingress controller is something like this (they changed the annotation name and syntax):
 
nginx.org/rewrites: "serviceName=my-service rewrite=/mycheck"
 
On both cases, the original path is:
 
- path: /
 
But this does not work for us in Nginx plus ingress controller. 

After hours and hours working on this, I contacted the developer in Nginx team.  What I got from him is:

http://nginx.org/rewrites annotation doesn’t support a rewrite that you need, unfortunately.

The advice from him is:

1. To support it, you can use a custom template


2. You can also create a custom annotation 


3. Alternatively, you can use a VirtualServer resource that supports a rewrite that you need:

  - action:
      proxy:
        rewritePath: /mycheck
        upstream: tea
    path: ~^/ # this is a regex path. for a rewrite that you need, it is necessary to use regex 



Multiple Ingresses for the same host


When using the community version ingress controller, we can create multiple ingresses (one for each path) for the same host.   But the Nginx plus ingress controller does not.   I have to put all paths into a single Ingress.  But we do not want to do that for multiple reasons (we may have lots of paths or we need to enable cache for some of the paths, but not all of the paths.  

To work around this in Nginx ingress controller, we need to define additional annotation: