Transform requests
Alter a request before matching and routing, such as with an Inja header template.
You might transform the request or response to match a different routing destination based on the transformed content. You can also apply special transformations, such as via Inja templates. With Inja, you can write loops, conditional logic, and other functions to transform requests and responses.
For more information, see the following resources.
- Gloo Mesh API docs
- The following video that shows how you might use transformation policies with other Gloo policies such as JWT and rate limiting.
Before you begin
This guide assumes that you use the same names for components like clusters, workspaces, and namespaces as in the getting started. If you have different names, make sure to update the sample configuration files in this guide.
Complete the multicluster getting started guide to set up the following testing environment.
- Three clusters along with environment variables for the clusters and their Kubernetes contexts.
- The Gloo
meshctl
CLI, along with other CLI tools such askubectl
andistioctl
. - The Gloo management server in the management cluster, and the Gloo agents in the workload clusters.
- Istio installed in the workload clusters.
- A simple Gloo workspace setup.
- Install Bookinfo and other sample apps.
Configure transformation policies
You can apply a transformation policy at the route level. For more information, see Apply policies.
When you attempt to apply multiple transformation policies, all policies are first sorted ascending by creation time, and then grouped by stage (preAuthz
and postAuthz
). For each stage, only the oldest policy is applied. All subsequent policies are ignored. Note that this behavior applies even if you specify the prioritizedPhase
field in your transformation policy.
Review the following sample configuration files.
Verify transformation policies
Log in to the reviews app and send a request to the
ratings
app. Make sure that a 200 HTTP response code is returned.Apply a transformation policy to the ratings app that adds the
foo: bar
response header.kubectl apply --context ${REMOTE_CONTEXT1} -f- <<EOF apiVersion: trafficcontrol.policy.gloo.solo.io/v2 kind: TransformationPolicy metadata: annotations: cluster.solo.io/cluster: "" name: basic-auth namespace: bookinfo spec: applyToRoutes: - route: labels: route: ratings config: response: injaTemplate: headers: foo: text: bar EOF
Create a route table for the ratings app.
kubectl apply --context ${REMOTE_CONTEXT1} -f- <<EOF apiVersion: networking.gloo.solo.io/v2 kind: RouteTable metadata: name: ratings-rt namespace: bookinfo spec: hosts: - ratings http: - forwardTo: destinations: - ref: name: ratings namespace: bookinfo cluster: ${REMOTE_CLUSTER1} labels: route: ratings workloadSelectors: - {} EOF
Send another request to the ratings app by using the command in step 1. Verify that you now see the
foo: bar
header in your response.Example output:
< HTTP/1.1 200 OK < content-type: application/json < date: Wed, 17 May 2023 16:48:51 GMT < x-envoy-upstream-service-time: 2 < server: envoy < foo: bar < transfer-encoding: chunked * Connection #0 to host ratings left intact {"id":1,"ratings":{"Reviewer1":5,"Reviewer2":4}}%
Optional: Clean up the resources that you created.
kubectl -n bookinfo --context $REMOTE_CONTEXT1 delete transformationpolicy basic-auth kubectl -n bookinfo --context $REMOTE_CONTEXT1 delete routetable ratings-rt