misterli's Blog.

Argo CD使用ding talk通知同步状态

字数统计: 1.1k阅读时长: 4 min
2020/11/18

Argo CD使用ding talk通知同步状态

实际使用中,我们可能会想把Argo CD中app的同步状态发送到指定媒介。但是Argo CD本身并未内置同步状态通知功能,但是可以与第三方通知系统的集成。

如果需要监视Argo CD的性能或托管应用程序的运行状况,可以将Prometheus MetricsGrafanaAlertmanager结合使用。

如果我们要将应用程序同步状态,比如同步中,同步成功,同步失败,同步状态未知等事件通知给使用Argo CD的最终用户,我们这里使用ArgoCD Notifications这个服务。argocd-notifications是特定于Argo CD的通知系统,可连续监视Argo CD应用程序,并旨在与各种通知服务(例如Slack,SMTP,Telegram,Discord,webhooks等)集成。

项目地址:https://github.com/argoproj-labs/argocd-notifications.git

安装与配置

先下载安装文件

1
wget https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/stable/manifests/install.yaml

这里我们需要修改install.yaml中的argocd-notifications-cm argocd-notifications-secret 添加相关配置才能支持ding talk.

修改后的argocd-notifications-cm和argocd-notifications-secret

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-notifications-cm
data:
config.yaml: |
context:
argocdUrl: https://argocd.lishuai.fun
triggers:
- name: sync-failed
template: sync-change
condition: app.status.operationState.phase in ['Error', 'Failed']
enabled: true
- name: sync-running
template: sync-change
condition: app.status.operationState.phase in ['Running']
enabled: true
- name: sync-succeeded
template: sync-change
condition: app.status.operationState.phase in ['Succeeded']
enabled: true
- name: health-degraded
condition: app.status.health.status == 'Degraded'
enabled: true
template: sync-change
- name: sync-unknown
condition: app.status.sync.status == 'Unknown'
enabled: true
template: sync-change
subscriptions:
- recipients:
- webhook:dingtalk
templates:
- name: sync-change
webhook:
dingtalk:
method: POST
body: |
{
"msgtype": "markdown",
"markdown": {
"title":"ArgoCD同步状态",
"text": "### ArgoCD同步状态\n> - app名称: {{.app.metadata.name}}\n> - app同步状态: {{ .app.status.operationState.phase}}\n> - 时间:{{.app.status.operationState.startedAt}}\n> - 地址: [点我访问]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true) \n"
}
}
---
apiVersion: v1
kind: Secret
metadata:
name: argocd-notifications-secret
stringData:
notifiers.yaml: |
webhook:
- name: dingtalk
url: https://oapi.dingtalk.com/robot/send?access_token=xxxxx
headers:
- name: Content-Type
value: application/json
type: Opaque

这里简单解释一下:

context:
  argocdUrl: https://argocd.lishuai.fun

这个参数是修改argocd的地址,否则后面发送时argocdurl 就变为https://localhost:4000/xxxx

subscriptions:
  - recipients:
      - webhook:dingtalk

这个是为所有app 添加通知订阅,我们还可以通过为app 添加注解recipients.argocd-notifications.argoproj.io":"webhook:dingtalk"来开启通知订阅。

1
kubectl patch app <my-app> -n argocd -p '{"metadata": {"annotations": {"recipients.argocd-notifications.argoproj.io":"webhokk:dingtalk"}}}' --type merge

这个还可以加一些条件判断,参考如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
subscriptions:
# global subscription for all type of notifications
- recipients:
- slack:test1
- webhook:github
# subscription for on-sync-status-unknown trigger notifications
- recipients:
- slack:test2
- email:test@gmail.com
trigger: on-sync-status-unknown
# global subscription restricted to applications with matching labels only
- recipients:
- slack:test3
selector: test=true
triggers:
  - name: sync-failed
    template: sync-change
    condition: app.status.operationState.phase in ['Error', 'Failed']
    enabled: true

这个是定义触发器即定义何时发送通知的条件。该定义包括名称,条件和通知模板引用。

  • 名称-唯一的触发器标识符。
  • **template-**定义通知内容的模板的名称。
  • **condition-**谓词表达式,如果应发送通知,则返回true。条件语言语法在Language-Definition.md中进行了描述。
  • **enabled-**指示是否启用触发器的标志。默认情况下启用触发器。

templates:

1
2
3
4
5
6
7
- name: sync-change
webhook:
dingtalk:
method: POST
body: |
Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.

通知模板用于生成通知内容。该模板利用 html / template golang包,并允许定义通知标题和正文。该模板旨在可重用,并且可以由多个触发器引用。

每个模板都可以访问以下字段:

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

效果

使用前注意钉钉机器人添加关键字,可以根据template body中内容随便添加

在argocd ui上点击sync,我们就能收到消息如下:

image-20201118160526253

CATALOG
  1. 1. Argo CD使用ding talk通知同步状态
    1. 1.1. 安装与配置
    2. 1.2. 效果