# Route traffic from Cloud Run Services to Cloud Service Mesh workloads on GKE

> [!WARNING]
>
> **Preview**
>
>
> This product or feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section
> of the [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA products and features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

> [!NOTE]
> **Note:** This guide only supports Cloud Service Mesh with Istio APIs and does not support Google Cloud APIs. For more information see, [Cloud Service Mesh overview](https://docs.cloud.google.com/service-mesh/v1.24/docs/overview.md.txt).

This page shows you how to securely route network traffic from
Cloud Run Services to Cloud Service Mesh workloads on
GKE to use Istio APIs and make use of a fully-managed Envoy
sidecar.

## Before you begin

The following sections assume that you have a
[GKE cluster with Cloud Service Mesh enabled](https://docs.cloud.google.com/service-mesh/v1.24/docs/onboarding/provision-control-plane.md.txt).

If you don't have a GKE Service deployed, use the following
command to deploy a sample service:

```bash
cat <<EOF > /tmp/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: ads
spec:
  ports:
  - port: 9999
    targetPort: 8000
  selector:
    run: ads
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ads
spec:
  replicas: 1
  selector:
    matchLabels:
      run: ads
  template:
    metadata:
      labels:
        run: ads
    spec:
      containers:
      - image: docker.io/waip/simple-http:v1.0.1
        name: my-http2-svc
        ports:
        - protocol: TCP
          containerPort: 8000
      securityContext:
        fsGroup: 1337
EOF
kubectl apply -f /tmp/service.yaml
```

## Configure a Custom Domain for `VirtualService` hosts

A [virtual service](https://istio.io/latest/docs/reference/config/networking/virtual-service/)
defines traffic routing rules. Any matched traffic is then sent to a named
destination service

1. Create a new managed zone:

   ```bash
   gcloud dns managed-zones create ZONE_NAME \
     --description="zone for service mesh routes" \
     --dns-name=DNS_SUFFIX. \
     --networks=default \
     --visibility=private
   ```

   where:
   - <var translate="no">ZONE_NAME</var> is a name for your zone (example: 'prod').
   - <var translate="no">DNS_SUFFIX</var> is any valid DNS host (example: 'mesh.private').
2. Create a resource record set:

   ```bash
   IP=10.0.0.1
   gcloud dns record-sets create '*.'"DNS_SUFFIX." --type=A --zone="ZONE_NAME" \
     --rrdatas=10.0.0.1 --ttl 3600
   ```

   Ensure the IP (RFC 1918 required) is unused. Alternatively,
   [reserve a static internal IP](https://docs.cloud.google.com/vpc/docs/reserve-static-internal-ip-address.md.txt#reservenewip).
3. Export a `VirtualService` for External Cloud Run Clients:

   ```bash
   cat <<EOF > virtual-service.yaml
   apiVersion: networking.istio.io/v1alpha3
   kind: VirtualService
   metadata:
     name: VIRTUAL_SERVICE_NAME
     namespace: NAMESPACE
   spec:
     hosts:
     - GKE_SERVICE_NAME.DNS_SUFFIX
     gateways:
     - external-mesh
     http:
     - route:
       - destination:
           host: GKE_SERVICE_NAME
   EOF
   kubectl apply -f virtual-service.yaml
   ```

   where:
   - <var translate="no">VIRTUAL_SERVICE_NAME</var> is a name for your `VirtualService`.
   - <var translate="no">NAMESPACE</var> is `default` if you're using the provided example service; otherwise, replace <var translate="no">NAMESPACE</var> with your namespace name.
   - <var translate="no">GKE_SERVICE_NAME</var> is `ads` if you're using the provided example service; otherwise, replace <var translate="no">GKE_SERVICE_NAME</var> with a name for your GKE service.

While it is feasible to add an `external-mesh` gateway as a target to a
pre-existing `VirtualService`, you should establish a distinct `VirtualService`
to export a Kubernetes service to external Cloud Run clients. Having a
separate `VirtualService` facilitates the management of exported services and
their configurations without affecting existing GKE clients.
Additionally, some fields in `VirtualServices` are disregarded for mesh external
`VirtualServices` but continue to function as anticipated for GKE
services. So managing and troubleshooting `VirtualServices` separately may be
advantageous.

For GKE clients to also receive the `VirtualService` configuration,
the `mesh` or `mesh/default` gateway must be added.

The mesh external `VirtualService` must be defined in the same namespace as the
Kubernetes Service in the `VirtualService` destination.

## Configure a Cloud Run Service to join a service mesh

To join a Cloud Run Service to a service mesh, perform the following steps:

1. Determine the mesh ID backing the Cloud Service Mesh GKE
   cluster:

   ```bash
   MESH=$(kubectl get controlplanerevision --namespace istio-system -o json | jq -r '.items[0].metadata.annotations["mesh.cloud.google.com/external-mesh"]')
   ```
2. Deploy a Cloud Run Service using the mesh ID, making sure to also connect to
   the cluster's VPC network:

   ```bash
   gcloud alpha run deploy --mesh "$MESH" --network default \
     mesh-svc --image=fortio/fortio \
     --region=REGION --project=PROJECT_ID --no-allow-unauthenticated
   ```
3. Verify that the Cloud Run service is able to send a request to the
   GKE workload:

   ```bash
   TEST_SERVICE_URL=$(gcloud run services describe mesh-svc --region REGION --format="value(status.url)" --project=PROJECT_ID)

   curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" "$TEST_SERVICE_URL/fortio/fetch/GKE_SERVICE_NAME.DNS_SUFFIX"
   ```

   The output should be a valid HTTP 200 response.

## Troubleshooting

This section shows you how to troubleshoot common errors with
Cloud Service Mesh and Cloud Run.

### Cloud Run Sidecar Logs

Envoy errors are logged in Cloud Logging.

For example an error such as the following will be logged if the
Cloud Run service account is not given the trafficdirector client role
in the mesh project:

```bash
StreamAggregatedResources gRPC config stream to trafficdirector.googleapis.com:443 closed: 7, Permission 'trafficdirector.networks.getConfigs' denied on resource '//trafficdirector.googleapis.com/projects/525300120045/networks/mesh:test-mesh/nodes/003fb3e0c8927482de85f052444d5e1cd4b3956e82b00f255fbea1e114e1c0208dbd6a19cc41694d2a271d1ab04b63ce7439492672de4499a92bb979853935b03d0ad0' (or it may not exist).
```

### CSDS

The trafficdirector client state can be retrieved using CSDS:

```bash
gcloud alpha container fleet mesh debug proxy-status --membership=<CLUSTER_MEMBERSHIP> --location=<CLUSTER_LOCATION>
External Clients:
....
```

## What's next

- [Route traffic from Cloud Service Mesh workloads to Cloud Run Services](https://docs.cloud.google.com/service-mesh/v1.24/docs/route-to-cloud-run.md.txt)