將 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 控制台的「Service Accounts」(服務帳戶) 頁面。

      前往「Service Accounts」(服務帳戶)

    2. 選取專案。

    3. 在「Service account name」(服務帳戶名稱) 欄位中輸入名稱。 Google Cloud 控制台會根據這個名稱填入「服務帳戶 ID」欄位。

      在「服務帳戶說明」欄位中輸入說明。例如:Service account for event trigger

    4. 按一下「建立並繼續」

    5. 如要提供適當的存取權,請在「Select a role」(請選擇角色) 清單中,選取要授予服務帳戶的必要 Identity and Access Management (IAM) 角色。詳情請參閱「工作流程目標的角色和權限」。

      如要新增其他角色,請按一下「新增其他角色」,然後新增每個其他角色。

    6. 按一下「繼續」

    7. 按一下「完成」,即可完成帳戶建立程序。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  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) 角色或權限。詳情請參閱「工作流程目標的角色和權限」。

建立觸發條件

您可以透過 Google Cloud CLI (gcloud 或 Terraform) 或 Google Cloud 控制台,建立以已部署工作流程做為事件接收端的 Eventarc 觸發程序。

主控台

  1. 在 Google Cloud 控制台中,前往 Eventarc「Triggers」(觸發條件) 頁面。

    前往「Triggers」(觸發條件)

  2. 按一下「建立觸發條件」
  3. 輸入觸發條件名稱

    這是觸發條件的 ID,開頭必須是英文字母。最多可包含 63 個小寫英文字母、數字或連字號。

  4. 在「Trigger type」(觸發條件類型) 中,選取「Google sources」(Google 來源)
  5. 在「Event provider」(事件提供者) 清單中,選取「Certificate Manager」

    請注意,相關Google Cloud 文件中使用的事件提供者名稱可能沒有 CloudGoogle Cloud 前置字元。舉例來說,在控制台中,「Memorystore for Redis」稱為「Memorystore for Redis」Google Cloud

  6. 在「Event type」(事件類型) 清單中,從「Direct」(直接) 事件選取事件類型。
  7. 如要指定事件酬載的編碼,請在「事件資料內容類型」清單中,選取「application/json」或「application/protobuf」

    請注意,以 JSON 格式設定的事件酬載會比以 Protobuf 格式設定的酬載大。視事件目的地和事件大小限制而定,這可能會影響可靠性。詳情請參閱「已知問題」。

  8. 在「Region」(區域) 清單中選取「global (Global)」(全域 (全域))

    詳情請參閱「Eventarc 位置」。

  9. 如果適用於活動供應商,請按一下「新增篩選器」,然後指定下列項目:
    1. 在「屬性 1」欄位中,根據您選擇的直接事件,選取可做為事件篩選器的資源 ID
    2. 選取運算子:
    3. 在「屬性值 1」欄位中,根據所選運算子輸入確切值或套用路徑模式。
    4. 如要套用更多屬性篩選器,請按一下「新增篩選器」,然後指定適當的值。
  10. 選取要叫用服務或工作流程的服務帳戶

    或者,您也可以建立新的服務帳戶。

    這會指定與觸發程序相關聯的 Identity and Access Management (IAM) 服務帳戶電子郵件,您先前已授予該帳戶 Eventarc 所需的特定角色

  11. 在「Event destination」(事件目的地) 清單中,選取「Workflows」
  12. 選取工作流程。

    這是要將事件傳送至的工作流程名稱。工作流程執行的事件會經過轉換,並以執行階段引數的形式傳遞至工作流程。

    詳情請參閱 建立 Workflows 的觸發條件

  13. 如要新增標籤,請按一下 「新增標籤」。標籤是鍵/值組合,可協助您整理Google Cloud 資源。詳情請參閱「什麼是標籤?
  14. 點選「建立」
  15. 觸發條件建立後,事件來源篩選器就無法修改。 請改為建立新的觸發條件,並刪除舊的觸發條件。詳情請參閱「管理觸發條件」。

  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 Google 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 事件。