文档

使用钉钉机器人通知GitOps应用变更

更新时间:

使用GitOps发布应用时,可以配置多种通知服务(例如Email、钉钉等)用于接收应用的重要变化。例如,当应用发布异常时您可以快速收到通知提醒,并及时排查异常原因。本文以发送钉钉消息为例,介绍如何在GitOps中发送通知。

背景信息

Argo CD Notifications提供了配置应用通知的机制,有以下几个主要概念:

  • Triggers:定义发送通知的条件。

  • Templates:用于生成通知内容。

  • Subscriptions:订阅应用、触发器,并通过某种或者某几种服务进行消息通知。

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

钉钉是通过暴露HTTP Webhook接口的方式接收消息输入,在ConfigMap下的argocd/argocd-notifications-cm中配置Triggers、Templates、Notification Services和Subscriptions,可实现应用发生重要变化时自动推送钉钉通知。

前提条件

步骤一:创建钉钉机器人

创建钉钉机器人后会生成专属的Webhook地址,通过Webhook地址可以关联到其他服务用于接收通知消息,例如Argo工作流。

  1. 打开需要接收事件的钉钉群。

  2. 进入机器人设置页面,添加自定义机器人。

    1. 单击钉钉群右上角的image图标,然后在群设置面板单击机器人

    2. 在机器人管理面板单击添加机器人,并单击自定义卡片,然后在弹出的对话框中单击添加

    3. 设置机器人名字并选择安全设置,然后单击完成。

      您必须至少选择一种安全设置,建议选择加签IP地址(段)中的至少一种,以保证安全性。

      重要

      在工作流中通过钉钉发送消息,要求工作流集群的VPC网络具有公网访问能力。您可以为工作流集群VPC配置公网NAT网关,同时将公网NAT网关的EIP地址配置到机器人的IP地址(段)中,以确保安全性。

  3. 设置完成后复制并保存自定义机器人的Webhook地址。

    image

步骤二:配置应用通知

通过kubectl连接到Fleet实例后,使用以下命令在argocd/argocd-notifications-cm中进行配置。

kubectl edit cm argocd-notifications-cm -nargocd

配置示例如下:

说明
  • service.webhook.dingtalkurl需要替换为您在步骤一:创建钉钉机器人中获取到的钉钉机器人的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]

相关文档

若您需要配置钉钉自定义机器人的消息通知展示样式,可参见钉钉自定义机器人数据类型及格式

  • 本页导读 (1)
文档反馈