How to adapt to a kubernetes path change in Python Flask routes

2/15/2020

I have a Python Flask app I've deployed on IBM Kubernetes Service. My deployment YAML specifies path: /, which is handled in my code with @app.route('/'). That works fine. I then attempted to move the app by changing path: / to path: /foo in my deployment YAML. I was expecting the request coming into my app to still come in as /, but it's coming in as /foo. Ultimately what I'm trying to do is to be flexible in the deployment of the app without having the change source code. I don't see a way in either Kubernetes or Flask to create this level of indirection. Am I missing something?

Original YAML:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  tls:
  - hosts:
      - my....us-east.containers.appdomain.cloud
    secretName: my...
  rules:
  - host: my....us-east.containers.appdomain.cloud
    http:
      paths:
       - path: /
         backend:
           serviceName: my-service
           servicePort: 5000
-- user2085050
flask
ibm-cloud
kubernetes
python-3.x

1 Answer

2/17/2020

Need to check the content of your ingress yaml definition.

Here's an example yaml definition with rewrites

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*) 

For example, the ingress definition above will result in the following rewrites:

rewrite.bar.com/something rewrites to rewrite.bar.com/
rewrite.bar.com/something/ rewrites to rewrite.bar.com/
rewrite.bar.com/something/new rewrites to rewrite.bar.com/new

You can check Nginx Ingress controller Rewrite annotations here. You can also customize Ingress routing with annotations on IBM Cloud following the documentation here

-- Vidyasagar Machupalli
Source: StackOverflow