Inject response headers
Use an Inja template to extract a value from a request header and add it as a header to your responses.
Before you begin
-
Follow the Get started guide to install kgateway.
-
Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
-
Get the external address of the gateway and save it in an environment variable.
export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}") echo $INGRESS_GW_ADDRESS
kubectl port-forward deployment/http -n kgateway-system 8080:8080
Inject response headers
-
Create a TrafficPolicy resource with the folloing transformation rules:
x-gateway-response
: Use the value from thex-gateway-request
request header and populate the value of that header into anx-gateway-response
response header.x-podname
: Retrieve the value of thePOD_NAME
environment variable and add the value to thex-podname
response header. Because the transformation is processed in the gateway proxy, these environment variables refer to the variables that are set on the proxy. You can view supported environment variables when you runkubectl get deployment http -n kgateway-system -o yaml and look at the
spec.containers.env` section.x-season
: Adds a static string value ofsummer
to thex-season
response header.x-response-raw
: Adds a static string values ofhello
with all escape characters intact.x-replace
: Replaces the pattern-to-replace text in thebaz
header with a random string.
kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: TrafficPolicy metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin transformation: response: set: - name: x-gateway-response value: '{{ request_header("x-gateway-request") }}' - name: x-podname value: '{{ env("POD_NAME") }}' - name: x-season value: 'summer' - name: x-response-raw value: '{{ raw_string("hello") }}' - name: x-replace value: '{{ replace_with_random(request_header("baz"), "pattern-to-replace") }}' EOF
-
Send a request to the httpbin app and include the
x-gateway-request
andbaz
request headers. Verify that you get back a 200 HTTP response code and that the following response headers are included:x-podname
that is set to the name of the gateway proxy pod.x-season
that is set tosummer
.x-gateway-response
that is set to the value of thex-gateway-request
request header.x-response-raw
that is set tohello
.x-replace
that is set to a random string.
curl -vi http://$INGRESS_GW_ADDRESS:8080/response-headers \ -H "host: www.example.com:8080" \ -H "x-gateway-request: my custom request header" \ -H "baz: pattern-to-replace"
curl -vi localhost:8080/response-headers \ -H "host: www.example.com" \ -H "x-gateway-request: my custom request header" \ -H "baz: pattern-to-replace"
Example output:
... * Request completely sent off < HTTP/1.1 200 OK HTTP/1.1 200 OK < access-control-allow-credentials: true access-control-allow-credentials: true < access-control-allow-origin: * access-control-allow-origin: * < content-type: application/json; encoding=utf-8 content-type: application/json; encoding=utf-8 < content-length: 3 content-length: 3 < x-envoy-upstream-service-time: 2 x-envoy-upstream-service-time: 2 < server: envoy server: envoy < x-envoy-decorator-operation: httpbin.httpbin.svc.cluster.local:8000/* x-envoy-decorator-operation: httpbin.httpbin.svc.cluster.local:8000/* < x-envoy-upstream-service-time: 1 < x-podname: http-85d5775587-tkxmt x-podname: http-85d5775587-tkxmt < x-replace: zljPMhO86gJCFc69jZ0+kQ x-replace: zljPMhO86gJCFc69jZ0+kQ < x-response-raw: hello x-response-raw: hello < x-gateway-response: my custom request header x-gateway-response: my custom request header < x-season: summer x-season: summer
Cleanup
You can remove the resources that you created in this guide.kubectl delete TrafficPolicy transformation -n httpbin