【问题标题】:Google Cloud Pub/Sub Trigger on Google ImagesGoogle 图片上的 Google Cloud Pub/Sub 触发器
【发布时间】:2019-12-26 00:38:19
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    要仅针对特定系列触发它,您可以使用下面的其他过滤器,但 protoPayload.request.family 仅在收到 API 请求时出现,而不是在实际完成时出现(也许您可以在处理函数中添加一些延迟,如果需要)

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

    【讨论】:

      【解决方案2】:

      另一种解决方案是使用 --trigger-topic={your pub sub topic} 创建一个云函数,然后根据云函数上的一些环境变量仅过滤您想要操作的图像

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

          gcloud pubsub topics create projects/<project_id>/topics/gcr
      
      1. 现在这将发布与在 repo 中插入/修改/删除的所有图像相对应的所有消息
      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>
      

      希望有帮助

      【讨论】:

        猜你喜欢
        • 2021-02-21
        • 2021-08-22
        • 2022-01-13
        • 2021-12-12
        • 1970-01-01
        • 1970-01-01
        • 2018-09-08
        • 2018-08-09
        • 1970-01-01
        相关资源
        最近更新 更多