Argo CD 消息通知和 webhook

Argo CD 消息通知和 webhook

有的时候我们可能希望将应用同步的状态发送到指定的渠道,这样方便我们了解部署流水线的结果,最新版本 Argo CD 也内置了 ArgoCD Notifications 用于同步状态通知功能,同时我们也可以与第三方的系统进行集成。

  • ArgoCD Notifications - Argo CD 通知系统,持续监控 Argo CD 应用程序,旨在与各种通知服务集成,例如 Slack、SMTP、Telegram、Discord 等。
  • Argo Kube Notifier - 通用 Kubernetes 资源控制器,允许监控任何 Kubernetes 资源并在满足配置的规则时发送通知。
  • Kube Watch - 可以向 Slack/hipchat/mattermost/flock 频道发布通知,它监视集群中的资源变更并通过 webhook 通知它们。

这里我们就以 ArgoCD Notifications 为例来说明如何使用 企业微信 来通知 Argo CD 的同步状态通知。

ArgoCD Notifications 默认已经随着 Argo CD 安装了:

$ kubectl get pods -n argocd
NAME                                                READY   STATUS    RESTARTS       AGE
argocd-notifications-controller-5b56f6f7bb-jqpng    1/1     Running   1 (163m ago)   3d2h

然后我们企业微信群里创建一个机器人。

然后我们需要修改 install.yaml 文件中的 argocd-notifications-cm 添加相关配置才能支持企业微信机器人。

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.qywx: |
    url: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx-xxxx-xxxx-xxxx-xxxxxx
    headers:
      - name: Content-Type
        value: application/json
  context: |
    argocdUrl: https://dev-argocd.evescn.com
  template.app-sync-change: |
    webhook:
      qywx:
        method: POST
        body: |
          {
              "msgtype": "markdown",
              "markdown": {
                  "content": "
                  ### ArgoCD服务发版成功
                  > <font color=\"info\">服务名称</font>: {{.app.metadata.name}}
                  > <font color=\"info\">app同步状态</font>: {{.app.status.operationState.phase}}
                  > <font color=\"info\">app服务状态</font>: {{.app.status.health.status}}
                  > <font color=\"info\">时间</font>: {{.app.status.operationState.startedAt}}
                  > <font color=\"info\">URL</font>: [点击跳转ArgoCD]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true)"
              }
          }
  template.app-sync-degraded: |
    webhook:
      qywx:
        method: POST
        body: |
          {
              "msgtype": "markdown",
              "markdown": {
                  "content": "
                  ### ArgoCD服务发版失败
                  > <font color=\"warning\">服务名称</font>: {{.app.metadata.name}}
                  > <font color=\"warning\">app同步状态</font>: {{.app.status.operationState.phase}}
                  > <font color=\"warning\">app服务状态</font>: {{.app.status.health.status}}
                  > <font color=\"warning\">时间</font>: {{.app.status.operationState.startedAt}}
                  > <font color=\"warning\">URL</font>: [点击跳转ArgoCD]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true)"
              }
          }
  trigger.on-deployed: |
    - description: Application is synced and healthy. Triggered once per commit.
      send: [app-sync-change]  # template names
      # 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-degraded]
      when: app.status.health.status == 'Degraded'
  trigger.on-sync-failed: |
    - description: Application syncing has failed
      send: [app-sync-degraded]  # template names
      when: app.status.operationState.phase in ['Error', 'Failed']
  trigger.on-sync-running: |
    - description: Application is being synced
      send: [app-sync-change]  # template names
      when: app.status.operationState.phase in ['Running']
  trigger.on-sync-status-unknown: |
    - description: Application status is 'Unknown'
      send: [app-sync-degraded]  # template names
      when: app.status.sync.status == 'Unknown'
  trigger.on-sync-succeeded: |
    - description: Application syncing has succeeded
      send: [app-sync-change]  # template names
      when: app.status.operationState.phase in ['Succeeded']
  subscriptions: |
    - recipients: [qywx]  # 可能有bug,正常应该是webhook:qywx
      triggers: [on-deployed, on-sync-failed, on-health-degraded]

其中 argocd-notifications-cm 中添加了一段如下所示的配置:

  subscriptions: |
    - recipients: [qywx]  # 可能有bug,正常应该是webhook:qywx
      triggers: [on-deployed, on-sync-failed, on-health-degraded]

这个是为定义的触发器添加通知订阅,当满足 [on-deployed, on-sync-failed, on-health-degraded] 这些 triggers 时,会使用 triggers 对应的 send 模版,然后使用 service.webhook 定义的信息进行消息发送。

service.webhook 提供给 subscriptions 中的 recipients 调用,用于发送消息到企业微信机器人

  service.webhook.qywx: |
    url: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx-xxxx-xxxx-xxxx-xxxxxx
    headers:
      - name: Content-Type
        value: application/json

然后可以根据不同的状态来配置不同的触发器,如下所示:

  trigger.on-sync-status-unknown: |
    - description: Application status is 'Unknown'
      send: [app-sync-degraded]  # template names
      when: app.status.sync.status == 'Unknown'

该触发器定义包括名称、条件和通知模板引用:

  • send:表示通知内容使用的模板名称
  • description:当前触发器的描述信息
  • when:条件表达式,如果应发送通知,则返回 true

然后下面就是配置发送的消息通知模板:

  template.app-sync-change: |
    webhook:
      qywx:
        method: POST
        body: |
          {
              "msgtype": "markdown",
              "markdown": {
                  "content": "
                  ### ArgoCD服务发版成功
                  > <font color=\"info\">服务名称</font>: {{.app.metadata.name}}
                  > <font color=\"info\">app同步状态</font>: {{.app.status.operationState.phase}}
                  > <font color=\"info\">app服务状态</font>: {{.app.status.health.status}}
                  > <font color=\"info\">时间</font>: {{.app.status.operationState.startedAt}}
                  > <font color=\"info\">URL</font>: [点击跳转ArgoCD]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true)"
              }
          }
  template.app-sync-degraded: |
    webhook:
      qywx:
        method: POST
        body: |
          {
              "msgtype": "markdown",
              "markdown": {
                  "content": "
                  ### ArgoCD服务发版失败
                  > <font color=\"warning\">服务名称</font>: {{.app.metadata.name}}
                  > <font color=\"warning\">app同步状态</font>: {{.app.status.operationState.phase}}
                  > <font color=\"warning\">app服务状态</font>: {{.app.status.health.status}}
                  > <font color=\"warning\">时间</font>: {{.app.status.operationState.startedAt}}
                  > <font color=\"warning\">URL</font>: [点击跳转ArgoCD]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true)"
              }
          }

这里我们定义了2个模版,这2个模板用于生成通知内容,并且可以由多个触发器引用。每个模板默认都可以访问以下字段:

  • app:保存应用程序对象
  • context:是用户定义的字符串映射,可能包含任何字符串键和值
  • notificationType 保留通知服务类型名称,该字段可用于有条件地呈现服务特定字段

context 定义,提供给 template 模版中使用,用于拼接 ArgoCD APP 服务地址,在报警信息中进行返回。

  context: |
    argocdUrl: https://dev-argocd.evescn.com

配置完成后我们更新整个资源清单文件:

➜  ~ kubectl -n argocd delete cm argocd-notifications-cm
➜  ~ kubectl -n argocd apply -f install.yaml
➜  ~ kubectl -n argocd delete pods argocd-notifications-controller-5b56f6f7bb-jqpng
➜  ~ kubectl get pods -n argocd
NAME                                               READY   STATUS    RESTARTS   AGE
argocd-application-controller-0                    1/1     Running   0          5d4h
argocd-dex-server-76ff776f97-ds7mm                 1/1     Running   0          5d4h
argocd-notifications-controller-5c548f8dc9-dx824   1/1     Running   0          9m22s
argocd-redis-747b678f89-w99wf                      1/1     Running   0          5d4h
argocd-repo-server-6fc4456c89-586zl                1/1     Running   0          5d4h
argocd-server-5cc96b75b4-zws2c                     1/1     Running   0          4d22h

安装完成后重新去修改下 APP 服务的镜像版本信息,Sync APP 服务,正常就可以在钉钉中收到如下所示的消息通知了,

企业微信通知

关于 ArgoCD Notification 的更多使用可以参考官方文档了解更多:https://argo-cd.readthedocs.io/en/stable/operator-manual/notifications/

ArgoCD webhook 配置

Argo CD 会自动检查到配置的应用变化,这是因为 Argo CD 会每个三分钟去轮询一次 Git 存储库来检测清单的变化,为了消除这种轮询延迟,我们也可以将 API 服务端配置为接收 webhook 事件的方式,这样就能实时获取到 Git 存储库中的变化了。Argo CD 支持来着 GitHub、GitLab、Bitbucket、Bitbucket Server 和 Gogs 的 Git webhook 事件,这里我们仍然以上面的 GitLab 为例来说明如果配置 Webhook。

进入到 GitLab 项目仓库 https://git.evescn.com/ops/argocd-apps 中配置 Webhooks:

Webhook 的地址填写 Argo CD 的 API 接口地址 https://dev-argocd.evescn.com/api/webhook, 下面的 Secret token 是可选的,建议添加上,任意定义即可。

然后需要将上面配置的 Secret token 添加到 Argo CD 的 Secret 配置中:

## 修改 install.yaml 配置文件
[root@node argocd]# vim install.yaml
---
apiVersion: v1
kind: Secret
metadata:
  labels:
    app.kubernetes.io/name: argocd-secret
    app.kubernetes.io/part-of: argocd
  name: argocd-secret
type: Opaque
stringData:
  # gitlab webhook secret
  webhook.gitlab.secret: evescn

[root@node argocd]# kubectl get secret argocd-secret -n argocd
---
apiVersion: v1
data:
  admin.password: JDJhJxxxxxxxxxxxxxxxxxxxxalBFUQUtwSxxxxxxxxxxxxxRiTxxxxxxxxxSR0xh
  admin.passwordMtime: MjAyMyxxxxxxxxxxxxxxxxDowMjo0NFo=
  server.secretkey: K2tEcnhGxxxxxxxxxxxxxxxxxxxxx02UnQ5dxxxxxxxxxxxxxxx2SmgvZWJEQT0=
kind: Secret
metadata:
  ......
type: Opaque

保存后,更改会自动生效,我们可以在 GitLab 这边测试配置的 Webhook,查看 Argo CD 的 API 服务 Pod 日志,正常就可以收到 Push 事件了:

[root@node argocd]# kubectl -n argocd  logs -f argocd-server-8544dd9f89-cz49l
time="2023-09-06T08:55:08Z" level=info msg="Received push event repo: http://git.evescn.com/ops/argocd-apps, revision: test, touchedHead: false"
time="2023-09-06T08:55:08Z" level=info msg="Requested app 'demo' refresh"
posted @ 2023-08-24 16:43  evescn  阅读(769)  评论(0编辑  收藏  举报