2

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?

andoshin11
  • 23
  • 1
  • 4

3 Answers3

3

You can use gcloud run revisions list to get a list of all revisions:

$ gcloud run revisions list --service helloworld

   REVISION          ACTIVE  SERVICE     DEPLOYED                 DEPLOYED BY
✔  helloworld-00009  yes     helloworld  2019-08-17 02:09:01 UTC  email@email.com
✔  helloworld-00008          helloworld  2019-08-17 01:59:38 UTC  email@email.com
✔  helloworld-00007          helloworld  2019-08-13 22:58:18 UTC  email@email.com
✔  helloworld-00006          helloworld  2019-08-13 22:51:18 UTC  email@email.com
✔  helloworld-00005          helloworld  2019-08-13 22:46:14 UTC  email@email.com
✔  helloworld-00004          helloworld  2019-08-13 22:41:44 UTC  email@email.com
✔  helloworld-00003          helloworld  2019-08-13 22:39:16 UTC  email@email.com
✔  helloworld-00002          helloworld  2019-08-13 22:36:06 UTC  email@email.com
✔  helloworld-00001          helloworld  2019-08-13 22:30:03 UTC  email@email.com

You can also use gcloud run revisions describe to get details about a specific revision, which will contain a status field. For example, an active revision:

$ gcloud run revisions describe helloworld-00009
...
status:
  conditions:
  - lastTransitionTime: '2019-08-17T02:09:07.871Z'
    status: 'True'
    type: Ready
  - lastTransitionTime: '2019-08-17T02:09:14.027Z'
    status: 'True'
    type: Active
  - lastTransitionTime: '2019-08-17T02:09:07.871Z'
    status: 'True'
    type: ContainerHealthy
  - lastTransitionTime: '2019-08-17T02:09:05.483Z'
    status: 'True'
    type: ResourcesAvailable

And an inactive revision:

$ gcloud run revisions describe helloworld-00008
...
status:
  conditions:
  - lastTransitionTime: '2019-08-17T01:59:45.713Z'
    status: 'True'
    type: Ready
  - lastTransitionTime: '2019-08-17T02:39:46.975Z'
    message: Revision retired.
    reason: Retired
    status: 'False'
    type: Active
  - lastTransitionTime: '2019-08-17T01:59:45.713Z'
    status: 'True'
    type: ContainerHealthy
  - lastTransitionTime: '2019-08-17T01:59:43.142Z'
    status: 'True'
    type: ResourcesAvailable

You'll specifically want to check the type: Active condition.

This is all available via the Cloud Run REST API as well: https://cloud.google.com/run/docs/reference/rest/v1/namespaces.revisions

Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82
3

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

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
  • Thanks Ahmet! > This is "desired configuration" but it actually might not reflect the status definitively Yes that is the exact problem I worried about when I run `$ gcloud run services describe` command. (just as I do for my k8s services) Inspecting `Route` object might be the perfect solution for this :) – andoshin11 May 07 '20 at 21:49
  • don’t forget to mark the question as complete if it solves your problem. – ahmet alp balkan May 08 '20 at 01:22
1

By default, the traffic is routed to the latest revision. You can see this into the logs.

Deploying container to Cloud Run service [SERVICE_NAME] in project [YOUR_PROJECT] region [YOUR_REGION]
✓ Deploying... Done.                                                           
  ✓ Creating Revision...
  ✓ Routing traffic...
Done.
Service [SERVICE_NAME] revision [SERVICE_NAME-00012-yic] has been deployed and is serving 100 percent of traffic at https://SERVICE_NAME-vqg64v3fcq-uc.a.run.app

If you want to be sure, you can explicitly call the update traffic command

gcloud run services update-traffic --platform=managed --region=YOUR_REGION --to-latest YOUR_SERVICE
guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76