全部產品
Search
文件中心

Container Service for Kubernetes:使用DingTalk機器人通知GitOps應用變更

更新時間:Jun 19, 2024

使用GitOps發布應用時,可以配置多種通知服務(例如Email、DingTalk等)用於接收應用的重要變化。例如,當應用發布異常時您可以快速收到通知提醒,並及時排查異常原因。本文以發送DingTalk訊息為例,介紹如何在GitOps中發送通知。

背景資訊

Argo CD Notifications提供了配置應用通知的機制,有以下幾個主要概念:

  • Triggers:定義發送通知的條件。

  • Templates:用於組建通知內容。

  • Subscriptions:訂閱應用、觸發器,並通過某種或者某幾種服務進行訊息通知。

  • Notification Services:通知服務,如Email、Slack、GitHub、Webhook等。

DingTalk是通過暴露HTTP Webhook介面的方式接收訊息輸入,在ConfigMap下的argocd/argocd-notifications-cm中配置Triggers、Templates、Notification Services和Subscriptions,可實現應用發生重要變化時自動推送DingTalk通知。

前提條件

步驟一:建立DingTalk機器人

建立DingTalk機器人後會產生專屬的Webhook地址,通過Webhook地址可以關聯到其他服務用於接收通知訊息,例如Argo工作流程。

  1. 開啟需要接收事件的DingTalk群。

  2. 進入機器人設定頁面,添加自訂機器人。

    1. 單擊DingTalk群右上方的image表徵圖,然後在群設定面板單擊機器人

    2. 在機器人管理面板單擊添加機器人,並單擊自訂卡片,然後在彈出的對話方塊中單擊添加

    3. 設定機器人名字並選擇安全設定,然後單擊完成。

      您必須至少選擇一種安全設定,建議選擇加簽IP地址(段)中的至少一種,以保證安全性。

      重要

      在工作流程中通過DingTalk發送訊息,要求工作流程叢集的VPC網路具有公網訪問能力。您可以為工作流程叢集VPC配置公網NAT Gateway,同時將公網NAT Gateway的EIP地址配置到機器人的IP地址(段)中,以確保安全性。

  3. 設定完成後複製並儲存自訂機器人的Webhook地址。

    image

步驟二:配置應用通知

通過kubectl串連到Fleet執行個體後,使用以下命令在argocd/argocd-notifications-cm中進行配置。

kubectl edit cm argocd-notifications-cm -nargocd

配置樣本如下:

說明
  • service.webhook.dingtalkurl需要替換為您在步驟一:建立DingTalk機器人中擷取到的DingTalk機器人的Webhook地址。

  • contextargocdUrl需要替換為實際的ArgoCD Server網域名稱。擷取方式,請參見GitOps ArgoCD Server網域名稱介紹

  • 如果每個trigger需要定製不同資訊,可以為每個trigger配置一個對應的templates。

  • 如果需要改變通知內容的顏色,可以使用<font color=>xxxx</font>配置通知內容。

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
  namespace: argocd
data:
  service.webhook.dingtalk: |
    url: https://oapi.dingtalk.com/robot/send?access_token=535a56d**********
    headers:
      - name: Content-Type
        value: application/json
  context: |
    argocdUrl: https://argocd.<your-fleet-id>.<region>.alicontainer.com
  template.app-sync-change: |
    webhook:
      dingtalk:
        method: POST
        body: |
          {
                "msgtype": "markdown",
                "markdown": {
                    "title":"ArgoCD應用狀態",
                    "text": "### ArgoCD應用狀態\n> - 應用程式名稱: {{.app.metadata.name}}\n> - 同步狀態: {{ .app.status.operationState.phase}}\n> - 時間:{{.app.status.operationState.finishedAt}}\n> - 應用URL: [點擊跳轉]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true) \n"
                }
          }
  template.app-sync-status-unknown: |
    webhook:
      dingtalk:
        method: POST
        body: |
          {
                "msgtype": "markdown",
                "markdown": {
                    "title":"ArgoCD應用Unknown",
                    "text": "### ArgoCD應用Unknown\n> - <font color=\"warning\">應用程式名稱</font>: {{.app.metadata.name}}\n> - <font color=\"warning\">應用同步狀態</font>: {{.app.status.sync.status}}\n> - <font color=\"warning\">應用健康狀態</font>: {{.app.status.health.status}}\n> - <font color=\"warning\">時間</font>: {{.app.status.operationState.startedAt}}\n> - <font color=\"warning\">應用URL</font>: [點擊跳轉ArgoCD UI]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true)"
                }
          }
  template.app-sync-failed: |
    webhook:
      dingtalk:
        method: POST
        body: |
          {
                "msgtype": "markdown",
                "markdown": {
                    "title":"ArgoCD應用發布失敗",
                    "text": "### ArgoCD應用發布失敗\n> - <font color=\"danger\">應用程式名稱</font>: {{.app.metadata.name}}\n> - <font color=\"danger\">應用同步狀態</font>: {{.app.status.operationState.phase}}\n> - <font color=\"danger\">應用健康狀態</font>: {{.app.status.health.status}}\n> - <font color=\"danger\">時間</font>: {{.app.status.operationState.startedAt}}\n> - <font color=\"danger\">應用URL</font>: [點擊跳轉ArgoCD UI]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true)"
                }
          }
  trigger.on-deployed: |
    - description: Application is synced and healthy. Triggered once per commit.
      oncePer: app.status.sync.revision
      send: [app-sync-change]
      # trigger condition
      when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
  trigger.on-health-degraded: |
    - description: Application has degraded
      send: [app-sync-change]
      when: app.status.health.status == 'Degraded'
  trigger.on-sync-failed: |
    - description: Application syncing has failed
      send: [app-sync-failed]
      when: app.status.operationState != nil and app.status.operationState.phase in ['Error',
        'Failed']
  trigger.on-sync-status-unknown: |
    - description: Application status is 'Unknown'
      send: [app-sync-status-unknown]
      when: app.status.sync.status == 'Unknown'
  trigger.on-sync-running: |
    - description: Application is being synced
      send: [app-sync-change]
      when: app.status.operationState != nil and app.status.operationState.phase in ['Running']
  trigger.on-sync-succeeded: |
    - description: Application syncing has succeeded
      send: [app-sync-change]
      when: app.status.operationState != nil and app.status.operationState.phase in ['Succeeded']
  subscriptions: |
    - recipients: [dingtalk]
      triggers: [on-sync-failed, on-sync-succeeded, on-sync-status-unknown,on-deployed]