TheDirectResponse plug-in supports HTTP requests sent to specified paths of designated services by not forwarding the requests to the services but immediately returning the fixed responses. This plug-in can be enabled on the gateway or injected into the workload of a sidecar. When attached to the gateway, this plug-in matches the domain name, port, and path of the request. When injected into the workload of a sidecar, this plug-in matches the port and path to which the request is sent.
Prerequisites
An ingress gateway is deployed. For more information, see Create an ingress gateway.
The httpbin service is deployed in the data plane cluster. For more information, see deploy the httpbin application.
Configuration fields
Name | Data type | Required | Default value | Description |
patch_context | string | Yes | - | Specifies the effective scenario for the plug-in. The field has two valid values:
|
host | string | No | - | This parameter specifies a domain that requires the direct response.
Important The domain name specified by this parameter must also be specified in the |
port | string | Yes | - | This parameter specifies the service port to which the request is sent.
|
path | string | Yes | - | This parameter specifies the path to which the request is sent, such as |
response | string | Yes | - | This parameter specifies the response body content returned by the direct response. |
response_status | string | Yes | - | This parameter specifies the response status code returned by the direct response. |
Configuration examples
Configure a direct response for the gateway
You can directly enable the DirectResponse plug-in on the gateway and configure a direct response. When the gateway matches the content sent to the specified host and path, it will directly return the specified response content instead of forwarding the request to a specific service.
For the httpbin application in the example, the VirtualService applied to the gateway is as follows:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
"*"
gateways:
httpbin-gateway
http:
route:
destination:
host: httpbin
port:
number: 8000
VirtualService httpbin specifies the matching domain name as * in the hosts field, so the plug-in parameters can be configured as follows:
patch_context: GATEWAY #In this example, the plugin is effective in the gateway, so patch_context must be specified as GATEWAY
host: "*" #The VirtualService httpbin only specifies host "*", so the matching host in the plugin can only be "*"
port: "80" #Fill in the gateway listening port 80
path: /get #Match a specified path /get for direct response
response: test #Any string can be filled in as the direct response content, "test" is used in the example
response_status: "200" #A valid status code can be filled in as the response status code for the direct response
Set the plug-in effective scope to Gateway Scope, and select the gateway ingressgateway to take effect. The test is as follows:
$ curl {gateway IP address}/get
test
When accessing the gateway and the path is /get, you can see that the gateway directly returns the specified response content "test" without further forwarding the request to the httpbin service.
Configure a direct response for the httpbin service
You can also enable the DirectResponse plug-in for the httpbin service and configure a direct response. When a request is sent to the specified path of the httpbin service, the specified response content is directly returned and the request is no longer sent to the workload for processing.
The httpbin service used in the example is defined as follows, and its service port is 8000:
apiVersion: v1
kind: Service
metadata:
name: httpbin
labels:
app: httpbin
service: httpbin
spec:
ports:
name: http
port: 8000
targetPort: 80
selector:
app: httpbin
Since the plug-in is effective in a specific workload, it is no longer necessary to configure the host parameter to specify the matching domain name for the plug-in. The plug-in parameters are configured as follows:
patch_context: SIDECAR_INBOUND #In this example, the plug-in is effective in the httpbin service, so patch_context must be specified as SIDECAR_INBOUND.
port: "8000" #Fill in the service port of httpbin: 8000.
path: /get #Match a specified path /get is for direct response.
response: test #Any string can be filled in as the direct response content, "test" is used in the example.
response_status: "200" #A valid status code can be filled in as the response status code for the direct response.
Set the plug-in effective scope to Workload Scope, and select the httpbin service to take effect. The test is as follows:
$ curl {gateway IP address}/get
testThe test effect is the same as the direct response of the gateway. However, in this example, the direct response is initiated by the sidecar after the gateway forwards the request to the sidecar of the httpbin service.