Directly respond to requests
Rather than routing an incoming request to an upstream service within your service mesh, you can send back a pre-defined body and HTTP status response to the client. If necessary, you can also specify headers by using the Header Modification feature in the enclosing route.
For more information, see the Gloo Mesh API docs for route tables.
Create a direct response route table
-
Create a virtual gateway, which selects the default Istio ingress gateway to route incoming traffic (north-south) to your service mesh.
kubectl apply --context $REMOTE_CONTEXT1 -f- <<EOF apiVersion: networking.gloo.solo.io/v2 kind: VirtualGateway metadata: name: ingress-gateway namespace: gloo-mesh spec: workloads: # Selects the istio ingress gateway in workload cluster 1 - selector: labels: istio: ingressgateway cluster: ${REMOTE_CLUSTER1} listeners: # The port the ingress gateway listens on for incoming requests to route - port: number: 80 http: {} EOF
-
Save the external address of the Istio ingress gateway.
export CLUSTER_1_INGRESS_ADDRESS=$(kubectl --context $REMOTE_CONTEXT1 get svc -n istio-system istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}') echo $CLUSTER_1_INGRESS_ADDRESS
export CLUSTER_1_INGRESS_ADDRESS=$(kubectl --context $REMOTE_CONTEXT1 get svc -n istio-system istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].hostname}') echo $CLUSTER_1_INGRESS_ADDRESS
-
Create a direct response route table, and specify the status and optional body that is returned. In the following example, the
510
status andno longer supported
message are immediately returned in response to any requests to the/unsupported-app
path.kubectl apply --context $REMOTE_CONTEXT1 -n global -f- <<EOF apiVersion: networking.gloo.solo.io/v2 kind: RouteTable metadata: name: direct-response namespace: global spec: hosts: - '*' # Selects the virtual gateway you previously created virtualGateways: - name: ingress-gateway namespace: gloo-mesh cluster: ${REMOTE_CLUSTER1} http: - name: unsupported-app-route # Prefix matching matchers: - uri: prefix: /unsupported-app # Immediately returns this 510 response directResponse: body: '{"message": "no longer supported"}' status: 510 EOF
-
Verify that you receive the expected direct response by curling the ingress gateway address and, if applicable, the path. For example, the following command appends
/unsupported-app
.curl http://$CLUSTER_1_INGRESS_ADDRESS/unsupported-app
Add headers to the direct response
You can optionally add headers to the direct responses. In the route table in step 3 of the previous section, you can add the following directResponse.options.headerManipulation
section:
...
directResponse:
body: '{"message": "no longer supported"}'
status: 510
options:
headerManipulation:
appendResponseHeaders:
X-my-response-header: "unsupported-app"