Azure Application Gateway equivalent for URL Rewriting in NGINX Ingress

working on an nginx ingress project that I am passing to Azure Application Gateway Ingress Controller, I came across this issue of passing this notation from the Paths to the Application Gateway ingress controller. here the nginx file

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-trackingservice
  namespace: trackingserviceespaceuat
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /trackingserviceapi(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: trackingservice
            port:
              number: 80

then I want to pass this notation to ingress controller Application Gateway

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-trackingservice
  namespace: trackingserviceespace
  annotations:
    kubernetes.io/ingress.class: azure/application-gateway
    appgw.ingress.kubernetes.io/backend-path-prefix: "/"
spec:
  tls:
    - hosts:
      - essoftware.online
      secretName: tlscertn
  rules:
  - host: essoftware.online
    http:
      paths:
      - path: /trackingserviceapi(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: trackingservice
            port:
              number: 80

then I realized two important things about nginx

nginx.ingress.kubernetes.io/rewrite-target: /$2
  - path: /trackingserviceapi(/|$)(.*)    

How can I migrate this to Azure Application Gateway? Because of course the load balancer works for me but this doesn't.

Upvotes: 0

Views: 360

Answers (1)

Arko
Arko

Reputation: 3781

Your ingress is correct. You need to have one Ingress Extension or AzureApplicationGatewayRewrite resource defined and applied.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sample-app-ingress
  annotations:
    kubernetes.io/ingress.class: azure/application-gateway
    appgw.ingress.kubernetes.io/backend-path-prefix: "/"
spec:
  rules:
  - host: essoftware.online
    http:
      paths:
      - path: /trackingserviceapi(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: trackingservice
            port:
              number: 80

Now here also we have two types one is Ingress-extension which is used when you need to extend the functionality of an existing ingress resource, adding more complex routing, rewriting, or header manipulation rules.

apiVersion: appgw.ingress.k8s.io/v1
kind: IngressExtension
metadata:
  name: sample-app-rewrite
  namespace: default
spec:
  ingressRef:
    name: sample-app-ingress
  rules:
    - name: rewrite-rule
      priority: 100
      match:
        pathMatch: 
          type: Exact
          value: /trackingserviceapi(/|$)(.*)
      actions:
        - type: ReplacePrefixMatch
          newPrefix: /

and another one is AzureApplicationGatewayRewrite which is used when you are specifically working with the Azure Application Gateway Ingress Controller and need to define URL rewrite rules that the Application Gateway will enforce. It looks something like this-

apiVersion: appgw.ingress.azure.io/v1
kind: AzureApplicationGatewayRewrite
metadata:
  name: sample-app-rewrite
  namespace: default
spec:
  rewriteRules:
    - name: rewrite-rule
      pathRule:
        pathPattern: /trackingserviceapi(/|$)(.*)
      backendPath: /

enter image description here

enter image description here

As per MS- Azure Application Gateway for Containers allows you to rewrite the URL of a client request, including the requests' hostname and/or path. When Application Gateway for Containers initiates the request to the backend target, the request contains the newly rewritten URL to initiate the request. You can use URL rewrite capabilities for Azure Application Gateway for Containers' Ingress API or Gateway API. For Ingress API, you need to define an IngressExtension resource, defining a rewrite with a ReplacePrefixMatch. For Gateway API, you need to define an HTTPRoute resource that has a filter to URLRewrite the path. To migrate from NGINX Ingress to Azure Application Gateway Ingress Controller, you can replace the nginx.ingress.kubernetes.io/rewrite-target annotation with appgw.ingress.kubernetes.io/backend-path-prefix annotation. For example, if the nginx.ingress.kubernetes.io/rewrite-target annotation is set to /foo/$1, you can replace it with appgw.ingress.kubernetes.io/backend-path-prefix: /foo/.

As discussed, if CRD is not available then you can download

curl -L -o azureapplicationgatewayrewrite-crd.yaml https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/master/deploy/azureapplicationgatewayrewrites.yaml

kubectl apply -f azureapplicationgatewayrewrite-crd.yaml

or create one as below-

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: azureapplicationgatewayrewrites.appgw.ingress.azure.io
spec:
  group: appgw.ingress.azure.io
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                rewriteRules:
                  type: array
                  items:
                    type: object
                    properties:
                      name:
                        type: string
                      pathRule:
                        type: object
                        properties:
                          pathPattern:
                            type: string
                      backendPath:
                        type: string
  names:
    kind: AzureApplicationGatewayRewrite
    listKind: AzureApplicationGatewayRewriteList
    plural: azureapplicationgatewayrewrites
    singular: azureapplicationgatewayrewrite
  scope: Namespaced

and apply

kubectl apply -f azureapplicationgatewayrewrite-crd.yaml

and re-apply your ingress and ingress-extension.

References:

Upvotes: 0

Related Questions