Directly respond to requests

Rather than routing an incoming request to an upstream service in your cluster, 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 Gateway API docs for route tables.

Create a direct response route table

  1. Create a virtual gateway that opens up port 80 on your ingress gateway.

    kubectl apply -f- <<EOF
    apiVersion: networking.gloo.solo.io/v2
    kind: VirtualGateway
    metadata:
      name: istio-ingressgateway
      namespace: bookinfo
    spec:
      workloads:
        # Matches on `spec.selector` labels for the ingress gateway service
        - selector:
            labels:
              istio: ingressgateway
            cluster: ${CLUSTER_NAME}
      listeners:
        # The port the ingress gateway listens on for incoming requests to route
        - port:
            number: 80
          http: {}
    EOF
    
  2. Save the external address of the ingress gateway.

    export INGRESS_GW_IP=$(kubectl get svc -n gloo-mesh-gateways istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    echo $INGRESS_GW_IP
    
    export INGRESS_GW_IP=$(kubectl get svc -n gloo-mesh-gateways istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
    echo $INGRESS_GW_IP
    

  3. Create a direct response route table, and specify the status and optional body that is returned. In the following example, the 510 status and no longer supported message are immediately returned in response to any requests to the /unsupported-app path.

    kubectl apply -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: istio-ingressgateway
          namespace: bookinfo
          cluster: ${CLUSTER_NAME}
      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
    
  4. 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://$INGRESS_GW_IP/unsupported-app