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

  1. 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 as kubectl and istioctl.
    • 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.
  2. 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.

Review the following sample configuration files.

Verify transformation policies

  1. Log in to the reviews app and send a request to the ratings app. Make sure that a 200 HTTP response code is returned.

  2. 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
      
  3. 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
      
  4. 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}}% 
       

  5. 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