DirectResponse plug-in
DirectResponse
plug-in immediately returns a fixed response to HTTP requests sent to a specified path of a specified service, instead of forwarding the requests. It can be applied to a gateway or to a workload that has a sidecar injected.
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 instructions, see Deploy the HTTPBin application.
Configuration fields
On a gateway, the plug-in matches the domain name, port, and path of the request. On a sidecar-injected workload, it matches the port and path that the request is sent to.
|
Name |
Data type |
Required |
Default value |
Description |
|
patch_context |
string |
Yes |
- |
Specifies where the plug-in takes effect. Set this parameter based on the effective scope of the plug-in. Two values are available:
|
|
host |
string |
No |
- |
This parameter specifies the domain name for which a direct response is required.
Important
For the VirtualService applied to the gateway, the domain name that you specify for 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 a gateway
Enable the DirectResponse
plug-in directly on a gateway to configure a direct response.
When the gateway matches content sent to the specified host and path, it directly returns the specified response content instead of forwarding the request to a specific service. For the httpbin application in this 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
The VirtualService httpbin uses the hosts field to specify the matching domain name *. Therefore, configure the plug-in parameters as follows:
patch_context: GATEWAY #In this example, the plug-in takes effect on a gateway, so set patch_context to GATEWAY
host: "*" #VirtualService httpbin specifies only the host "*", so the matching host in the plug-in can also be set only to "*"
port: "80" #Specify the listening port of the gateway: 80
path: /get #Match the specified path /get to return a direct response
response: test #You can specify any string as the content of the direct response. This example uses "test"
response_status: "200" #You can specify a valid status code as the response status code of the direct response
Set the effective scope of the plug-in to Gateway Scope, and then select the gateway ingressgateway on which the plug-in takes effect. The test is as follows:
$ curl {gateway_ip_address}/get
test
When you access the gateway and the path is /get, the gateway directly returns the specified response content "test" instead of further forwarding the request to the httpbin service.
Configure a direct response for the httpbin service
The DirectResponse plug-in can also be enabled on the httpbin service to configure a direct response. When a request is sent to the specified path of the httpbin service, the specified response content is returned directly. The request is no longer handed over to the workload for processing. The httpbin service used in this 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
Because the plug-in takes effect on a specific workload, you no longer need to configure the host parameter of the plug-in to specify the matching domain name. Configure the plug-in parameters as follows:
patch_context: SIDECAR_INBOUND #In this example, the plug-in takes effect on the httpbin service, so set patch_context to SIDECAR_INBOUND
port: "8000" #Specify the service port of httpbin: 8000
path: /get #Match the specified path /get to return a direct response
response: test #You can specify any string as the content of the direct response. This example uses "test"
response_status: "200" #You can specify a valid status code as the response status code of the direct response
Set the effective scope of the plug-in to Workload Scope, and then select the httpbin Service on which the plug-in takes effect. The test is as follows:
$ curl {gateway_ip_address}/get
test
The test result is the same as that of the direct response on the gateway. In this example, however, the gateway forwards the request to the sidecar of the httpbin service, and the sidecar returns the direct response.