ArgoCD 消息通知

 

#参考:https://www.cnblogs.com/evescn/p/17654505.html

简介

有的时候我们可能希望将应用同步的状态发送到指定的渠道,这样方便我们了解部署流水线的结果,最新版本 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-95c6bc58-k2dz6      1/1     Running   0          64s

备份old argocd-notifications-cm config文件

# kubectl get cm argocd-notifications-cm -o yaml -n argocd 
apiVersion: v1
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"ConfigMap","metadata":{"annotations":{},"name":"argocd-notifications-cm","namespace":"argocd"}}
  creationTimestamp: "2024-12-24T03:41:37Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:kubectl.kubernetes.io/last-applied-configuration: {}
    manager: kubectl-client-side-apply
    operation: Update
    time: "2024-12-24T03:41:37Z"
  name: argocd-notifications-cm
  namespace: argocd
  resourceVersion: "20323975"
  uid: 7336b896-b703-4b08-8fc9-38aed641aad4

编写new argocd-notifications-cm config文件

vim install.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.qywx: |
    url: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxx
    headers:
      - name: Content-Type
        value: application/json
  context: |
    argocdUrl: https://10.30.17.161:30881/
  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]

应用部署

# kubectl -n argocd delete cm argocd-notifications-cm
# kubectl -n argocd apply -f install.yaml
# kubectl get pods -n argocd
NAME                                                READY   STATUS    RESTARTS   AGE
argocd-application-controller-0                     1/1     Running   0          15d
argocd-applicationset-controller-59bf4dc99f-p74b2   1/1     Running   0          15d
argocd-dex-server-5549798757-sq6qd                  1/1     Running   0          15d
argocd-notifications-controller-95c6bc58-k2dz6      1/1     Running   0          64s
argocd-redis-6d7c4576-gdvg4                         1/1     Running   0          15d
argocd-repo-server-75cf6c59c4-mfwwm                 1/1     Running   0          15d
argocd-server-84fd9647bc-h2sc9                      1/1     Running   0          15d
# kubectl -n argocd delete pods argocd-notifications-controller-95c6bc58-k2dz6ng
# kubectl get pods -n argocd

posted @ 2025-01-08 16:17  しみずよしだ  阅读(68)  评论(0)    收藏  举报