将 Certificate Manager 事件路由到 Workflows

Eventarc 触发器声明您对某个事件或一系列事件感兴趣。如需配置事件路由,您可以为触发器指定过滤条件,包括事件来源和目标工作流。

事件通过 HTTP 请求以 CloudEvents 格式传送。Workflows 服务将事件转换为 JSON 对象(遵循 CloudEvents 规范),并将事件作为工作流运行时参数传递给工作流执行。确保事件大小不超过 512 KB。大于 Workflows 参数大小上限的事件不会触发工作流执行。

以下说明介绍如何配置事件路由,以便触发工作流执行来响应直接Certificate Manager 事件。如需了解详情,请参阅支持的直接事件列表。

准备创建触发器

在为目标工作流创建 Eventarc 触发器之前,请完成以下任务。

控制台

  1. 在 Google Cloud 控制台的“项目选择器”页面上,选择或创建 Google Cloud 项目

    转到“项目选择器”

  2. 启用 Eventarc、Eventarc Publishing、Workflows 和 Workflow Executions API。

    启用 API

  3. 如果适用,请启用与直接事件相关的 API。例如,对于 Certificate Manager 事件,请启用Certificate Manager API。

  4. 如果您还没有用户管理的服务账号,请创建一个,并为其授予必要的角色和权限,以便 Eventarc 可以管理目标工作流的事件。

    1. 在 Google Cloud 控制台中,转到服务账号页面。

      转到“服务账号”

    2. 选择您的项目。

    3. 服务账号名称字段中,输入一个名称。Google Cloud 控制台会根据此名称填充服务账号 ID 字段。

      服务账号说明字段中,输入说明。例如 Service account for event trigger

    4. 点击创建并继续

    5. 如需提供适当的访问权限,请在选择角色列表中,选择要向服务账号授予的所需 Identity and Access Management (IAM) 角色。如需了解详情,请参阅 Workflows 目标的角色和权限

      如需添加其他角色,请点击 添加其他角色,然后添加其他各个角色。

    6. 点击继续

    7. 如需完成账号的创建过程,请点击完成

gcloud

  1. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

    Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。

  2. 启用 Eventarc、Eventarc Publishing、Workflows 和 Workflow Executions API:

    gcloud services enable eventarc.googleapis.com \
        eventarcpublishing.googleapis.com \
        workflows.googleapis.com \
        workflowexecutions.googleapis.com
    

  3. 如果适用,请启用与直接事件相关的 API。例如,对于 Certificate Manager 事件,请启用 certificatemanager.googleapis.com

  4. 如果您还没有用户管理的服务账号,请创建一个,并为其授予必要的角色和权限,以便 Eventarc 可以管理目标工作流的事件。

    1. 创建服务账号:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      SERVICE_ACCOUNT_NAME 替换为服务账号的名称。该名称必须介于 6 到 30 个字符之间,且可以包含小写字母数字字符和短划线。在服务账号创建完毕后,无法再更改其名称。

    2. 授予所需的 Identity and Access Management (IAM) 角色或权限。如需了解详情,请参阅 Workflows 目标的角色和权限

创建触发器

您可以使用 Google Cloud CLI(gcloud 或 Terraform)或通过 Google Cloud 控制台创建将已部署工作流作为事件接收器的 Eventarc 触发器。

控制台

  1. 在 Google Cloud 控制台中,进入 Eventarc 触发器页面。

    转到“触发器”

  2. 点击 创建触发器
  3. 输入触发器名称

    这是触发器的 ID,必须以字母开头。最多可包含 63 个小写字母、数字或连字符。

  4. 触发器类型部分,选择 Google 来源
  5. 事件提供方列表中,选择 Certificate Manager

    请注意,关联的 Google Cloud 文档中使用的事件提供方名称可能没有 Cloud 或 Google Cloud 前缀。例如,在控制台中,Memorystore for Redis 称为 Google Cloud Memorystore for Redis

  6. 事件类型列表中,从“直接”事件中选择事件类型。
  7. 如需指定事件载荷的编码,请在事件数据内容类型列表中选择 application/jsonapplication/protobuf

    请注意,采用 JSON 格式的事件载荷大于采用 Protobuf 格式的事件载荷。这可能会影响可靠性,具体取决于您的事件目标及其事件大小限制。如需了解详情,请参阅已知问题

  8. 区域列表中,选择 global(全球)

    如需了解详情,请参阅 Eventarc 位置

  9. 如果适用于事件提供方,请点击添加过滤条件并指定以下内容:
    1. 属性 1 字段中,根据您选择的直接事件,选择可用作事件过滤条件的资源 ID
    2. 选择一个运算符:
    3. 属性值 1 字段中,根据您选择的运算符,输入确切的值或应用路径模式。
    4. 如果有更多属性过滤条件适用,请点击添加过滤条件并指定适当的值。
  10. 选择将调用您的服务或工作流的服务账号

    或者,您可以创建新的服务账号。

    这用于指定与触发器相关联且您之前向其授予 Eventarc 所需的特定角色的 Identity and Access Management (IAM) 服务账号电子邮件。

  11. 事件目的地列表中,选择 Workflows
  12. 选择工作流。

    这是要将事件传递给的工作流的名称。工作流执行的事件会被转换并作为运行时参数传递给工作流。

    如需了解详情,请参阅为工作流创建触发器

  13. 点击创建
  14. 创建触发器之后,您便无法修改事件来源过滤条件。请创建新触发器,并删除旧触发器。 如需了解详情,请参阅管理触发器

  1. 点击创建

gcloud

您可以通过运行包含必需和可选标志的 gcloud eventarc triggers create 命令来创建触发器。

gcloud eventarc triggers create TRIGGER \
  --location=global \
  --destination-workflow=DESTINATION_WORKFLOW  \
  --destination-workflow-location=DESTINATION_WORKFLOW_LOCATION \
  --event-filters="type=EVENT_FILTER_TYPE" \
  --event-filters="COLLECTION_ID=RESOURCE_ID" \
  --event-filters-path-pattern="COLLECTION_ID=PATH_PATTERN" \
  --event-data-content-type="EVENT_DATA_CONTENT_TYPE" \
  --service-account="MY_SERVICE_ACCOUNT@PROJECT_ID.iam.gserviceaccount.com"

Replace the following:

  • TRIGGER: the ID of the trigger or a fully qualified identifier.
  • DESTINATION_WORKFLOW: the ID of the deployed workflow that receives the events from the trigger. The workflow can be in any of the Workflows supported locations and does not need to be in the same location as the trigger. However, the workflow must be in the same project as the trigger.
  • DESTINATION_WORKFLOW_LOCATION (optional): the location in which the destination workflow is deployed. If not specified, it is assumed that the workflow is in the same location as the trigger.
  • EVENT_FILTER_TYPE: the identifier of the event. An event is generated when an API call for the method succeeds. For long-running operations, the event is only generated at the end of the operation, and only if the action is performed successfully. For a list of supported event types, see Event types supported by Eventarc.
  • COLLECTION_ID (optional): the resource component that can act as an event filter, and is one of the following:
    • certificate
    • certificateissuanceconfig
    • certificatemap
    • certificatemapentry
    • dnsauthorization
  • RESOURCE_ID: the identifier of the resource used as the filtering value for the associated collection. For more information, see Resource ID.
  • PATH_PATTERN: the path pattern to apply when filtering for the resource
  • EVENT_DATA_CONTENT_TYPE: (optional) the encoding of the event payload. This can be application/json or application/protobuf. The default encoding is application/json.

    Note that an event payload formatted in JSON is larger than one formatted in Protobuf. This might impact reliability depending on your event destination and its limits on event size. For more information, see Known issues.

  • SERVICE_ACCOUNT_NAME: the name of the IAM service account you created to which you granted specific roles required by Workflows.
  • PROJECT_ID: your Google Cloud project ID

Notes:

  • The --location flag must be global. For more information, see Eventarc locations.
  • The --event-filters="type=EVENT_FILTER_TYPE" flag is required. If no other event filter is set, events for all resources are matched.
  • EVENT_FILTER_TYPE cannot be changed after creation. To change EVENT_FILTER_TYPE, create a new trigger and delete the old one.
  • Each trigger can have multiple event filters, comma delimited in one --event-filters=[ATTRIBUTE=VALUE,...] flag, or you can repeat the flag to add more filters. Only events that match all the filters are sent to the destination. Wildcards and regular expressions are not supported; however, when using the --event-filters-path-pattern flag, you can define a resource path pattern.
  • The --service-account flag is used to specify the Identity and Access Management (IAM) service account email associated with the trigger.

Example:

gcloud eventarc triggers create helloworld-trigger \
  --location=global \
  --destination-workflow=my-workflow \
  --destination-workflow-location=us-central1 \
  --event-filters="type=google.cloud.certificatemanager.certificate.v1.updated" \
  --event-filters-path-pattern="certificate=my-certificate-*" \
  --service-account="${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com"

此命令会为标识为 google.cloud.certificatemanager.certificate.v1.updated 的事件创建一个名为 helloworld-trigger 的触发器,并针对以 my-certificate- 开头的 certificate ID 匹配事件。