Using labels

This page shows how to set, modify, and delete labels on your Cloud Run service and revisions. For Cloud Run, labels are key/value pairs that are applied to the service and revisions. When you set a label on a Cloud Run service, a new revision with this label is created for this service; the label is not applied to older revisions.

For Cloud Run (fully managed), labels are propagated to Google Cloud and so be used directly to filter billing, logs, metrics, and so forth. Possible uses of this include:

  • Use for cost allocation and billing breakdowns.
  • Identify resources used by individual teams or cost centers
  • Distinguish deployment environments (prod, staging, qa, or test)
  • Identify owners, state labels.
  • Monitor resource groups via Logging or Monitoring, which use labels accessible in the resource metadata

For Cloud Run for Anthos on Google Cloud, labels are Kubernetes resource labels, and are not propagated to Google Cloud.

Labels usage rules

The following rules apply to the use of labels set on a Cloud Run service:

Setting or modifying labels

You can use the console or the command line to set or modify labels.

Console

Using the console, you can set or modify a label on a service any time after you create the service:

  1. Go to Cloud Run

  2. Check the checkbox at the left of the service you are setting the label on.

  3. Make sure the Info Panel at the far right is toggled on (Show Info Panel).

  4. Click Labels to display the labels pane.

  5. To edit an existing label key value, locate the label and change the Value as desired.

  6. To set a new label on the service, click Add Label and supply the key and the value.

  7. Click Save

    Set label

Command line

You can update labels for a service using the command:

gcloud run services update SERVICE --update-labels KEY=VALUE

To update more than one label, supply a comma delimited list of key/value pairs. Note that the update-labels command sets a new value for the label key if the label already exists, otherwise it creates a new label.

Replace

  • SERVICE with name of your Cloud Run service
  • KEY with the name of your label key
  • VALUE with the value for the key

You can also set labels during deployment:

gcloud run deploy SERVICE --image IMAGE --labels KEY=VALUE

To create more than one label during deployment, supply a comma delimited list of key/value pairs.

YAML

You can download and view existing service configuration using the gcloud beta run services describe --format yaml command, which yields results in YAML format. You can then modify the fields described below and upload the modified YAML using the gcloud beta run services replace command. Make sure you only modify fields as documented.

To view and download configuration:

  1. gcloud beta run services describe SERVICE --format yaml > service.yaml
  2. Update the labels attribute:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      annotations:
        client.knative.dev/user-image: gcr.io/cloudrun/hello
        run.googleapis.com/client-name: cloud-console
        serving.knative.dev/creator: douga@google.com
        serving.knative.dev/lastModifier: douga@google.com
      creationTimestamp: '2019-12-11T23:32:14.924684Z'
      generation: 9
      labels:
        cloud.googleapis.com/location: us-central1
        LABEL: 'VALUE'

    Replace

    • LABEL with the name of the label
    • VALUE with the desired value
  3. Replace the service with its new configuration using the following command:

    gcloud beta run services replace service.yaml

Deleting a label

You can use the console or the command line delete labels.

Console

  1. Go to Cloud Run

  2. Check the checkbox at the left of the service you are deleting the label from.

  3. Make sure the Info Panel at the far right is toggled on (Show Info Panel).

  4. Click Labels to display the labels pane.

  5. Locate the label you want to delete.

  6. Hover your cursor to the right of the Value textbox for the label to display the trash icon, and click the trash icon.

  7. Click Save

Command line

To clear all labels from a service:

gcloud run services update SERVICE --clear-labels

To delete specific labels from a service, supply a comma delimited list of keys:

gcloud run services update SERVICE --remove-labels KEY

Replace

  • SERVICE with name of your Cloud Run service
  • KEY with the name of your label key