|
| 1 | +Inject artificial latency into requests with an {{< reuse "agw-docs/snippets/policy.md" >}} to test how your clients and upstream services handle slow responses. Fault injection is useful for verifying that timeouts, retries, and client-side deadlines behave as expected. To learn more, you can use fault injection alongside [Timeouts]({{< link-hextra path="/resiliency/timeouts/" >}}) and [Retries]({{< link-hextra path="/resiliency/retry/" >}}). |
| 2 | + |
| 3 | +The injected delay counts against the request timeout. If the delay is longer than the configured [request timeout]({{< link-hextra path="/resiliency/timeouts/" >}}), the request times out. |
| 4 | + |
| 5 | +{{< reuse "agw-docs/snippets/agentgateway/prereq.md" >}} |
| 6 | + |
| 7 | +## Inject a delay {#inject-a-delay} |
| 8 | + |
| 9 | +1. Create an HTTPRoute for the httpbin app. |
| 10 | + ```yaml {paths="delay-in-trafficpolicy"} |
| 11 | + kubectl apply -n httpbin -f- <<EOF |
| 12 | + apiVersion: gateway.networking.k8s.io/v1 |
| 13 | + kind: HTTPRoute |
| 14 | + metadata: |
| 15 | + name: httpbin-delay |
| 16 | + namespace: httpbin |
| 17 | + spec: |
| 18 | + hostnames: |
| 19 | + - faultinjection.example |
| 20 | + parentRefs: |
| 21 | + - name: agentgateway-proxy |
| 22 | + namespace: {{< reuse "agw-docs/snippets/namespace.md" >}} |
| 23 | + rules: |
| 24 | + - backendRefs: |
| 25 | + - kind: Service |
| 26 | + name: httpbin |
| 27 | + port: 8000 |
| 28 | + EOF |
| 29 | + ``` |
| 30 | + |
| 31 | +2. Create an {{< reuse "agw-docs/snippets/policy.md" >}} that injects a fixed 2-second delay into the requests that the HTTPRoute handles. |
| 32 | + ```yaml {paths="delay-in-trafficpolicy"} |
| 33 | + kubectl apply -f- <<EOF |
| 34 | + apiVersion: {{< reuse "agw-docs/snippets/api-version.md" >}} |
| 35 | + kind: {{< reuse "agw-docs/snippets/policy.md" >}} |
| 36 | + metadata: |
| 37 | + name: httpbin-delay |
| 38 | + namespace: httpbin |
| 39 | + spec: |
| 40 | + targetRefs: |
| 41 | + - group: gateway.networking.k8s.io |
| 42 | + kind: HTTPRoute |
| 43 | + name: httpbin-delay |
| 44 | + traffic: |
| 45 | + delay: |
| 46 | + duration: 2s |
| 47 | + EOF |
| 48 | + ``` |
| 49 | + |
| 50 | + {{< reuse "agw-docs/snippets/review-table.md" >}} |
| 51 | + |
| 52 | + | Field | Description | |
| 53 | + | -- | -- | |
| 54 | + | `targetRefs` | The route, gateway, or listener to apply the delay to. In this example, the policy targets the `httpbin-delay` HTTPRoute. | |
| 55 | + | `traffic.delay.duration` | The latency to inject before the request is forwarded to the backend. Set either a duration string, such as `2s` or `500ms`, or a CEL expression that is evaluated against the request. For more information, see [Inject a probabilistic or random delay](#inject-a-probabilistic-or-random-delay). | |
| 56 | + |
| 57 | +3. Send a request along the route and verify that the response is delayed by about 2 seconds. |
| 58 | + {{< tabs >}} |
| 59 | + {{% tab name="Cloud Provider LoadBalancer" %}} |
| 60 | + ```sh |
| 61 | + curl -s -o /dev/null -w "%{time_total}s\n" http://$INGRESS_GW_ADDRESS/get -H "host: faultinjection.example" |
| 62 | + ``` |
| 63 | + {{% /tab %}} |
| 64 | + {{% tab name="Port-forward for local testing" %}} |
| 65 | + ```sh |
| 66 | + curl -s -o /dev/null -w "%{time_total}s\n" localhost:8080/get -H "host: faultinjection.example" |
| 67 | + ``` |
| 68 | + {{% /tab %}} |
| 69 | + {{< /tabs >}} |
| 70 | + |
| 71 | + Example output: |
| 72 | + ```console |
| 73 | + 2.01s |
| 74 | + ``` |
| 75 | + |
| 76 | +4. Optional: Verify that the delay policy is applied in the proxy configuration. |
| 77 | + |
| 78 | + 1. Port-forward the gateway proxy on port 15000. |
| 79 | + ```sh |
| 80 | + kubectl port-forward deploy/agentgateway-proxy -n {{< reuse "agw-docs/snippets/namespace.md" >}} 15000 |
| 81 | + ``` |
| 82 | + |
| 83 | + 2. Find the delay policy in the config dump. |
| 84 | + ```sh |
| 85 | + curl -s http://localhost:15000/config_dump | jq '[.policies[] | select(.policy.traffic.delay?)] | .[0]' |
| 86 | + ``` |
| 87 | + |
| 88 | +## Inject a probabilistic or random delay {#inject-a-probabilistic-or-random-delay} |
| 89 | + |
| 90 | +Because `duration` accepts a CEL expression, you can inject latency into only a subset of requests, or add jitter. The expression is evaluated against each request and returns either a duration or a number that is interpreted as milliseconds. A non-positive result injects no delay. |
| 91 | + |
| 92 | +| Expression | Effect | |
| 93 | +| -- | -- | |
| 94 | +| `duration("500ms")` | A fixed 500ms delay, expressed as a CEL duration. | |
| 95 | +| `random() < 0.1 ? 500 : 0` | A 500ms delay on approximately 10% of requests, and no delay otherwise. | |
| 96 | +| `int(random() * 500.0)` | A random delay between 0 and 500ms (jitter) on every request. | |
| 97 | +
|
| 98 | +For example, the following policy delays approximately 10% of requests by 500ms. |
| 99 | +
|
| 100 | +```yaml |
| 101 | +kubectl apply -f- <<EOF |
| 102 | +apiVersion: {{< reuse "agw-docs/snippets/api-version.md" >}} |
| 103 | +kind: {{< reuse "agw-docs/snippets/policy.md" >}} |
| 104 | +metadata: |
| 105 | + name: httpbin-delay |
| 106 | + namespace: httpbin |
| 107 | +spec: |
| 108 | + targetRefs: |
| 109 | + - group: gateway.networking.k8s.io |
| 110 | + kind: HTTPRoute |
| 111 | + name: httpbin-delay |
| 112 | + traffic: |
| 113 | + delay: |
| 114 | + duration: "random() < 0.1 ? 500 : 0" |
| 115 | +EOF |
| 116 | +``` |
| 117 | + |
| 118 | +{{< doc-test paths="delay-in-trafficpolicy" >}} |
| 119 | +# WHAT THIS TEST VALIDATES: |
| 120 | +# * The httpbin-delay HTTPRoute (host faultinjection.example) is Accepted. |
| 121 | +# * The httpbin-delay AgentgatewayPolicy (traffic.delay.duration: 2s) is Accepted. |
| 122 | +# * A request to /get with host faultinjection.example returns 200 (the injected |
| 123 | +# delay does not break the request). |
| 124 | +# * The delay policy is present in the proxy config dump. |
| 125 | +# WHAT THIS TEST DOES NOT VALIDATE (and why): |
| 126 | +# * That the response is actually delayed by ~2s — Different layer: YAMLTest has |
| 127 | +# no response-latency assertion, so timing is not measured. |
| 128 | +# * The probabilistic/random CEL delay example — Display-only block, not tagged. |
| 129 | +# Warm up the new faultinjection.example hostname before asserting (two-phase proxy warmup). |
| 130 | +for i in $(seq 1 60); do |
| 131 | + curl -s --max-time 5 -o /dev/null "http://${INGRESS_GW_ADDRESS}:80/get" -H "host: faultinjection.example" && break |
| 132 | + sleep 2 |
| 133 | +done |
| 134 | +{{< /doc-test >}} |
| 135 | + |
| 136 | +{{< doc-test paths="delay-in-trafficpolicy" >}} |
| 137 | +YAMLTest -f - <<'EOF' |
| 138 | +- name: wait for httpbin-delay HTTPRoute to be accepted |
| 139 | + wait: |
| 140 | + target: |
| 141 | + kind: HTTPRoute |
| 142 | + metadata: |
| 143 | + namespace: httpbin |
| 144 | + name: httpbin-delay |
| 145 | + jsonPath: "$.status.parents[0].conditions[?(@.type=='Accepted')].status" |
| 146 | + jsonPathExpectation: |
| 147 | + comparator: equals |
| 148 | + value: "True" |
| 149 | + polling: |
| 150 | + timeoutSeconds: 300 |
| 151 | + intervalSeconds: 5 |
| 152 | +- name: wait for httpbin-delay AgentgatewayPolicy to be accepted |
| 153 | + wait: |
| 154 | + target: |
| 155 | + kind: AgentgatewayPolicy |
| 156 | + metadata: |
| 157 | + namespace: httpbin |
| 158 | + name: httpbin-delay |
| 159 | + jsonPath: "$.status.ancestors[0].conditions[?(@.type=='Accepted')].status" |
| 160 | + jsonPathExpectation: |
| 161 | + comparator: equals |
| 162 | + value: "True" |
| 163 | + polling: |
| 164 | + timeoutSeconds: 300 |
| 165 | + intervalSeconds: 5 |
| 166 | +- name: verify request through the delayed route returns 200 |
| 167 | + retries: 1 |
| 168 | + http: |
| 169 | + url: "http://${INGRESS_GW_ADDRESS}:80" |
| 170 | + path: /get |
| 171 | + method: GET |
| 172 | + headers: |
| 173 | + host: "faultinjection.example" |
| 174 | + source: |
| 175 | + type: local |
| 176 | + expect: |
| 177 | + statusCode: 200 |
| 178 | +- name: verify delay policy in config dump |
| 179 | + http: |
| 180 | + url: http://localhost:15000 |
| 181 | + skipSslVerification: true |
| 182 | + method: GET |
| 183 | + path: /config_dump |
| 184 | + source: |
| 185 | + type: pod |
| 186 | + usePortForward: true |
| 187 | + selector: |
| 188 | + kind: Deployment |
| 189 | + metadata: |
| 190 | + namespace: agentgateway-system |
| 191 | + name: agentgateway-proxy |
| 192 | + expect: |
| 193 | + bodyContains: |
| 194 | + - '"duration"' |
| 195 | +EOF |
| 196 | +{{< /doc-test >}} |
| 197 | + |
| 198 | +## Cleanup |
| 199 | + |
| 200 | +{{< reuse "agw-docs/snippets/cleanup.md" >}} Run the following commands. |
| 201 | + |
| 202 | +```sh |
| 203 | +kubectl delete httproute httpbin-delay -n httpbin |
| 204 | +kubectl delete {{< reuse "agw-docs/snippets/policy.md" >}} httpbin-delay -n httpbin |
| 205 | +``` |
0 commit comments