Google图片上的Google Cloud Pub / Sub触发器

时间:2019-08-20 18:32:57

标签: google-cloud-platform google-cloud-functions google-cloud-storage google-cloud-pubsub

我们需要一种在新的计算映像上(最好在特定映像系列上触发)自动创建发布/订阅触发器的方法。另外,我们知道GCS存储桶上的发布/订阅,但是我们还没有找到一种自动将图像传输到GCS存储桶的方法。

对于某些背景:我们正在通过打包程序自动进行图像烘焙,我们需要这一段来触发地形创建。我们知道可以创建cron作业来在创建图像时简单地轮询图像,但是我们想知道GCP中是否已经支持这种触发器。

2 个答案:

答案 0 :(得分:2)

您可以有一个Stackdriver Logging导出接收器,该接收器发布到Pub / Sub,并由特定的过滤器(docs)触发。例如:

resource.type="gce_image"
jsonPayload.event_subtype="compute.images.insert"
jsonPayload.event_type="GCE_OPERATION_DONE"

enter image description here

要仅针对特定家庭触发它,您可以在下面使用此其他过滤器,但是protoPayload.request.family仅在收到API请求时才出现,而不是在实际上得到满足时才出现(也许您可以在处理中添加一些延迟)功能)

resource.type="gce_image"
protoPayload.request."@type"="type.googleapis.com/compute.images.insert"
protoPayload.request.family="FAMILY-NAME"

答案 1 :(得分:0)

另一种解决方案是使用--trigger-topic = {your pub子主题}创建云功能,然后根据云功能上的某些环境变量仅过滤要操作的图像

伪代码 1.为要插入GCR中的图像创建pub子主题

    gcloud pubsub topics create projects/<project_id>/topics/gcr
  1. 这现在将发布与在回购中插入/修改/删除的所有图像相对应的所有消息
  2. 创建一个具有函数签名的云函数
// contents of index.js
// use the Storage function from google-coud node js api to work on storages
// https://www.npmjs.com/package/@google-cloud/storage

const Storage = require(@google-cloud/storage).Storage;

function moveToStorageBucket(pubSubEvents, context, callback) {
 /* this is how the pubsub comes from GCR
{"data":{"@type":"... .v1.PuSubMessage", "attribute":null, "data": "<base 64 encoded>"},
 "context":{..other details}}
data that is base 64 encoded in in this format
{ "action":"INSERT","digest":"<image name>","tag":<"tag name>"}
*/
    const data = JSON.parse(Buffer.from(pubSubEvents.data, 'base64').toString())
    // get image name from the environment variable passed
    const IMAGE_NAME = process.env.IMAGE_NAME;
    if (data.digest.indexOf(IMAGE_NAME) !== -1) {
     // your action here...
    }
}
module.exports.moveToStorageBucket = moveToStorageBucket;
  1. 部署云功能
gcloud functions deploy <function_name> --region <region> --runtime=nodejs8 --trigger-topic=<topic created> --entry-point=moveToStorageBucket --set-env-vars=^--^IMAGE_NAME=<your image name>

希望有帮助

相关问题