Google Cloud Pub/Sub

Spinnaker can be configured to trigger pipelines based on Google Pub/Sub messages.

Spinnaker pipelines can be triggered using a Google Pub/Sub subscription .

Prerequisites

You need a Google Cloud Platform (GCP) project to with the Pub/Sub API enabled , and <code>gcloud</code> installed locally. You can check that gcloud is installed and authenticated by running:

  1. gcloud info

A Pub/Sub subscription

The Pub/Sub integration can be used to either:

Configuration and setup for all cases are described below.

Sending your own Pub/Sub messages

First, record the fact that your $MESSAGE_FORMAT is CUSTOM, this will be needed later.

  1. MESSAGE_FORMAT=CUSTOM

You need a topic with name $TOPIC to publish messages to:

  1. gcloud beta pubsub topics create $TOPIC

This topic needs a a pull subscription named $SUBSCRIPTION to let Spinnaker read messages from. It is important that Spinnaker is the only system reading from this single subscription. You can always create more subscriptions for this topic if you want multiple systems to recieve the same messages.

  1. gcloud beta pubsub subscriptions create $SUBSCRIPTION --topic $TOPIC

At this point, you can use the publisher guide to learn how to publish messages programmatically to the topic you have created.

Note that your topic messages need to be valid JSON, otherwise an exception will be raised in the echo service and your pipeline will not be triggered.

Receiving messages from Google Cloud Storage (GCS)

First, record the fact that your $MESSAGE_FORMAT is GCS, this will be needed later.

  1. MESSAGE_FORMAT=GCS

Given that you’ll be listening to changes in a GCS bucket ($BUCKET), the following command will create (or use an existing) topic with name $TOPIC to publish messages to:

  1. gsutil notification create -t $TOPIC -f json gs://${BUCKET}

Finally, create a pull subscription named $SUBSCRIPTION to listen to changes to this topic:

  1. gcloud beta pubsub subscriptions create $SUBSCRIPTION --topic $TOPIC

To understand the format that GCS will publish messages in, please read the reference material .

Receiving messages from Google Container Registry (GCR)

Warning

The GCR message type extracts the image digest by default, but Google App Engine doesn’t support deploying from a digest. So if you’re using artifacts from GCR messages to trigger a GAE deployment, you need to use a custom message template. The template must copy the image’s tag into the artifact’s reference and version fields.

First, record the fact that your $MESSAGE_FORMAT is GCR, this will be needed later.

  1. MESSAGE_FORMAT=GCR

Given a project name $PROJECT, GCR will always try to publish messages to a topic named projects/${PROJECT}/topics/gcr for any repositories in $PROJECT. To ensure that GCR has a valid topic to publish to, try to create the following topic:

  1. gcloud beta pubsub topics create projects/${PROJECT}/topics/gcr

If the command fails with “Resource already exists in the project”, then the topic already exists (which is OK).

Finally, create a pull subscription named $SUBSCRIPTION to listen to changes to this topic:

  1. gcloud beta pubsub subscriptions create $SUBSCRIPTION \
  2. --topic projects/${PROJECT}/topics/gcr

To understand the format that GCR will publish messages in, please read the reference material .

Credentials

Spinnaker needs a service account to authenticate as against GCP, with the roles/pubsub.subscriber role enabled. If you don’t already have such a service account with the corresponding JSON key downloaded, you can run the following commands to do so:

  1. SERVICE_ACCOUNT_NAME=spinnaker-pubsub-account
  2. SERVICE_ACCOUNT_DEST=~/.gcp/pubsub-account.json
  3. gcloud iam service-accounts create \
  4. $SERVICE_ACCOUNT_NAME \
  5. --display-name $SERVICE_ACCOUNT_NAME
  6. SA_EMAIL=$(gcloud iam service-accounts list \
  7. --filter="displayName:$SERVICE_ACCOUNT_NAME" \
  8. --format='value(email)')
  9. PROJECT=$(gcloud config get-value project)
  10. gcloud projects add-iam-policy-binding $PROJECT \
  11. --role roles/pubsub.subscriber --member serviceAccount:$SA_EMAIL
  12. mkdir -p $(dirname $SERVICE_ACCOUNT_DEST)
  13. gcloud iam service-accounts keys create $SERVICE_ACCOUNT_DEST \
  14. --iam-account $SA_EMAIL

Warning

It’s possible to restrict access to a subscription by service account in GCP. If this is how your subscription is configured, you may need to grant $SA_EMAIL additional permissions following the instructions on the

Pub/Sub IAM page

Once you have run these commands, your GCS JSON key is sitting in a file called $SERVICE_ACCOUNT_DEST.

Editing your Pub/Sub settings

All that’s required are the following values:

  1. # See 'Credentials' section above
  2. SERVICE_ACCOUNT_DEST=
  3. # See 'A Pub/Sub Subscription' section above
  4. MESSAGE_FORMAT=
  5. PROJECT=
  6. SUBSCRIPTION=
  7. # You can pick this name, it's meant to be human-readable
  8. PUBSUB_NAME=my-google-pubsub

First, make sure that Google Pub/Sub support is enabled:

  1. hal config pubsub google enable

Next, add your subscription

  1. hal config pubsub google subscription add $PUBSUB_NAME \
  2. --subscription-name $SUBSCRIPTION \
  3. --json-path $SERVICE_ACCOUNT_DEST \
  4. --project $PROJECT \
  5. --message-format $MESSAGE_FORMAT

There are more options described here if you need more control over your configuration.

Last modified May 4, 2021: rest of migration (700781a)