Search code examples
google-cloud-platformgcloud

How to escape slash in gcloud format / filter command?


I would like to filter Cloud Run revisions by its container image.

When I run this gcloud run revisions command,

gcloud beta run revisions list --service sample-service --region=asia-northeast1 --limit=5 --sort-by="~DEPLOYED" --format="json"

it will output following json

[
  {
    "apiVersion": "serving.knative.dev/v1",
    "kind": "Revision",
    "metadata": {
      "annotations": {
        "autoscaling.knative.dev/maxScale": "1",
        "client.knative.dev/user-image": "asia.gcr.io/sample-gcp-project/sample-app:e88597bcfb346aa1",
        "run.googleapis.com/client-name": "gcloud",
        "run.googleapis.com/client-version": "383.0.1", # 

I tried to filter revisions by --filter options, but it raises an error.

gcloud beta run revisions list --service it-sys-watch --region=asia-northeast1 --limit=1 --sort-by="~DEPLOYED" --filter='metadata.annotations.client.knative.dev/user-image=asia.gcr.io/sample-gcp-project/sample-app:e88597bcfb346aa1'
ERROR: (gcloud.beta.run.revisions.list) Non-empty key name expected [metadata.annotations.client.knative.dev *HERE* /user-image=asia.gcr.io/sample-gcp-project/sample-app:e88597bcfb346aa1].

Neither adding backslash nor double slashes won't work

gcloud beta run revisions list --service it-sys-watch --region=asia-northeast1 --limit=1 --sort-by="~DEPLOYED" --filter='metadata.annotations.client.knative.dev\/user-image=asia.gcr.io/sample-gcp-project/sample-app:e88597bcfb346aa1'
WARNING: The following filter keys were not present in any resource : metadata.annotations.client.knative.dev\/user-image
Listed 0 items.

gcloud beta run revisions list --service it-sys-watch --region=asia-northeast1 --limit=1 --sort-by="~DEPLOYED" --filter='metadata.annotations.client.knative.dev//user-image=asia.gcr.io/sample-gcp-project/sample-app:e88597bcfb346aa1'
ERROR: (gcloud.beta.run.revisions.list) Non-empty key name expected [metadata.annotations.client.knative.dev *HERE* //user-image=asia.gcr.io/sample-gcp-project/sample-app:e88597bcfb346aa1].

gcloud --format options also does not work with backslash keys.

Is there any idea to help filtering key with slashes?


Solution

  • Try:

    gcloud beta run revisions list \
    --service=it-sys-watch \
    --region=asia-northeast1 \
    --sort-by="~DEPLOYED" \
    --filter='metadata.annotations["client.knative.dev/user-image"]="asia.gcr.io/sample-gcp-project/sample-app:e88597bcfb346aa1"'
    

    NOTE You need to drop the --limit=1 too though this conflicts with the documentation that suggests that limit is applied after filter

    gcloud ... --filter=... --limit=1 | jq 'length' yields 0

    gcloud ... --filter=... | jq 'length' yields 1

    Let's see what Google Engineering says: 231192444