How can we rewrite the response of APIs in OpenShift

138 Views Asked by At

We have application that is running inside a pod, to access that I have created the service and exposed it. The route-url is for example : example-city.apps.cluster.com to access my application say server There is any Rest-API calls where, I get json data in which one the field is URL which is mapped to pod-name:80 through my application.

There is client application which get's connected to these server using route-URL, once it is connected there are Rest-API calls, that give me back the json data. As, in json data I don't have route-url but pod-name:80, my application does get connected to it.

So, what I am looking for is, my json data when it does in response, I need to change that pod-name:80 to route-url, in short I want to replace my pod-name with route-url ie. I want to rewrite-response.

Please suggest, how can we do these and what all changes are required in service or route yaml files.

1

There are 1 best solutions below

5
Huong On

What about deploying an additional reverse proxy like nginx for the purpose of rewriting?

Firstly, you need to deploy Nginx using a ConfigMap.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  template:
    spec:
      containers:
        - name: nginx
          image: nginxinc/nginx-unprivileged:1.22
          ports:
          - containerPort: 8080
            name: http-web
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/conf.d
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-config
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
     app: nginx
  ports:
  - name: nginx
    protocol: TCP
    port: 8080
    targetPort: http-web

---
kind: Route
apiVersion: route.openshift.io/v1
metadata:
  name: nginx
spec:
  host: awsome-app.example.com
  to:
    kind: Service
    name: nginx

Then, you can do rewriting in ConfigMap

kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-config
data:
  nginx.conf: |
    server {
      listen 8080;
      server_name awsome-app.example.com

      # your rewriting rules here
      # for example
      location / {
         sub_filter '<your-pod-name>:80' '<your-desired-route-url>';
      }
    }