Search code examples
google-cloud-platformcdngoogle-cloud-pubsubgoogle-cloud-buildgoogle-cloud-run

How to check if the latest Cloud Run revision is ready to serve


I've been using Cloud Run for a while and the entire user experience is simply amazing!

Currently I'm using Cloud Build to deploy the container image, push the image to GCR, then create a new Cloud Run revision. Now I want to call a script to purge caches from CDN after the latest revision is successfully deployed to Cloud Run, however $ gcloud run deploy command can't tell you if the traffic is started to pointing to the latest revision.

Is there any command or the event that I can subscribe to to make sure no traffic is pointing to the old revision, so that I can safely purge all caches?


Solution

  • @Dustin’s answer is correct, however "status" messages are an indirect result of Route configuration, as those things are updated separately (and you might see a few seconds of delay between them). The status message will still be able to tell you the Revision has been taken out of rotation if you don't mind this.

    To answer this specific question (emphasis mine) using API objects directly:

    Is there any command or the event that I can subscribe to to make sure no traffic is pointing to the old revision?

    You need to look at Route objects on the API. This is a Knative API (it's available on Cloud Run) but it doesn't have a gcloud command: https://cloud.google.com/run/docs/reference/rest/v1/namespaces.routes

    For example, assume you did 50%-50% traffic split on your Cloud Run service. When you do this, you’ll find your Service object (which you can see on Cloud Console → Cloud Run → YAML tab) has the following spec.traffic field:

    spec:
      traffic:
      - revisionName: hello-00002-mob
        percent: 50
      - revisionName: hello-00001-vat
        percent: 50
    

    This is "desired configuration" but it actually might not reflect the status definitively. Changing this field will go and update Route object –which decides how the traffic is splitted.

    To see the Route object under the covers (sadly I'll have to use curl here because no gcloud command for this:)

    TOKEN="$(gcloud auth print-access-token)"
    
    curl -vH "Authorization: Bearer $TOKEN" \
        https://us-central1-run.googleapis.com/apis/serving.knative.dev/v1/namespaces/GCP_PROJECT/routes/SERVICE_NAME
    

    This command will show you the output:

      "spec": {
        "traffic": [
          {
            "revisionName": "hello-00002-mob",
            "percent": 50
          },
          {
            "revisionName": "hello-00001-vat",
            "percent": 50
          }
        ]
      },
    

    (which you might notice is identical with Service’s spec.traffic –because it's copied from there) that can tell you definitively which revisions are currently serving traffic for that particular Service.