Transformation

Alter a request before matching and routing, such as with an Inja header template.

Transformation policies apply to the route. 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.

Before you begin

  1. Complete the demo setup to install Gloo Mesh, Istio, and Bookinfo in your cluster.

  2. Create the Gloo Mesh resources for this policy in the management and workload clusters.

    The following files are examples only for testing purposes. Your actual setup might vary. You can use the files as a reference for creating your own tests.

    1. Download the following Gloo Mesh resources:
    2. Apply the files to your management cluster.
      kubectl apply -f kubernetes-cluster_gloo-mesh_cluster-1.yaml --context ${MGMT_CONTEXT}
      kubectl apply -f kubernetes-cluster_gloo-mesh_cluster-2.yaml --context ${MGMT_CONTEXT}
      kubectl apply -f workspace_gloo-mesh_anything.yaml --context ${MGMT_CONTEXT}
      
    1. Download the following Gloo Mesh resources:
    2. Apply the files to your workload cluster.
      kubectl apply -f route-table_bookinfo_httpbin.yaml --context ${REMOTE_CONTEXT1}
      kubectl apply -f virtual-gateway_bookinfo_north-south-gw.yaml --context ${REMOTE_CONTEXT1}
      kubectl apply -f workspace-settings_bookinfo_anything.yaml --context ${REMOTE_CONTEXT1}
      

Configure transformation policies

You can apply a transformation policy at the route level. For more information, see Applying policies.

Review the following sample configuration files.

The following example is to inject a header with simple Inja template into a request.

apiVersion: trafficcontrol.policy.gloo.solo.io/v2
kind: TransformationPolicy
metadata:
  name: basic-auth
  namespace: bookinfo
spec:
  applyToRoutes:
  - route:
      labels:
        route: httpbin
  config:
    request:
      injaTemplate:
        headers:
          foo:
            text: bar

Review the following table to understand this configuration.

Setting Description
spec.applyToRoutes Configure which routes to apply the policy to, by using labels. The label matches the app and the route from the route table. If omitted, the policy applies to all routes in the workspace.

The following example is to inject a header with simple Inja template into a response.

apiVersion: trafficcontrol.policy.gloo.solo.io/v2
kind: TransformationPolicy
metadata:
  name: basic-auth
  namespace: bookinfo
spec:
  applyToRoutes:
  - route:
      labels:
        route: httpbin
  config:
    response:
      injaTemplate:
        headers:
          foo1:
            text: bar1

Review the following table to understand this configuration.

Setting Description
spec.applyToRoutes Configure which routes to apply the policy to, by using labels. The label matches the app and the route from the route table. If omitted, the policy applies to all routes in the workspace.

Verify transformation policies

  1. Apply the example transformation policy in the workload cluster.
    kubectl apply --context ${REMOTE_CONTEXT1} -f transformation-policy.yaml
    
  2. Send a request to the httpbin app through the ingress gateway.
    curl -vik -H "Host: www.example.com" "http://$INGRESS_GW_IP/status/200/"
    
  3. Verify that you notice the transformed request and response headers.