ArgoCD 消息通知
随着项目的增多,频繁的迭代......主要是非运维测说同步很慢,xxxxxxx,其实是服务有问题...,相比于反驳不如事实来说话,上通知!
# 一 . 消息通知
这里我们就以 ArgoCD Notifications
为例来说明如何使用 企业微信
来通知 Argo CD
的同步状态通知。
# 二. ArgoCD Notifications
默认已经随着 Argo CD
安装了
[root@k8s-uat-manager ~]# kubectl get pod -n argocd
NAME READY STATUS RESTARTS AGE
argocd-application-controller-0 1/1 Running 0 162d
argocd-applicationset-controller-59bf7b4f84-mtn7c 1/1 Running 0 162d
argocd-dex-server-7788568745-9c7fc 1/1 Running 0 121d
argocd-notifications-controller-779c5f97bb-kf8fz 1/1 Running 0 17h
argocd-redis-cfb94f77-vkbcs 1/1 Running 0 121d
argocd-repo-server-7d5c6fcfb-mfksj 1/1 Running 0 121d
argocd-server-6559b8c4f6-z9lcx 1/1 Running 0 121d
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 三. 企业微信群里创建一个机器人,注:企业微信的markdown格式不支持@所有人
# 四. 修改argocd-notifications-cm
,官网 (opens new window)
[root@k8s-uat-manager ~]# cat argocd-notifications-cm.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-notifications-cm
namespace: argocd
data:
service.webhook.qywx: |
url: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=06a0xxxx # 企业微信的webhook地址
headers:
- name: Content-Type
value: application/json
context: |
argocdUrl: http://agrocd.xxx.com # argocd的ui地址
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>: {{(call .repo.GetCommitMetadata .app.status.sync.revision).Author}}
> <font color=\"info\">提交信息</font>: {{(call .repo.GetCommitMetadata .app.status.sync.revision).Message}}
> <font color=\"info\">时间</font>: {{ (call .time.Parse .app.status.operationState.startedAt).Local.Format "2006-01-02 15:04:05" }}
> <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=\"info\">时间</font>: {{ (call .time.Parse .app.status.operationState.startedAt).Local.Format "2006-01-02 15:04:05" }}
> <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]
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]
when: app.status.operationState.phase in ['Error', 'Failed']
trigger.on-sync-status-unknown: |
- description: Application status is 'Unknown'
send: [app-sync-degraded]
when: app.status.sync.status == 'Unknown'
subscriptions: |
- recipients: [qywx] # 可能有bug,正常应该是webhook:qywx
triggers: [on-deployed, on-sync-failed, on-health-degraded,on-sync-status-unknown
----------------------------------------------------------------------------------------------------------------
该触发器定义包括名称、条件和通知模板引用:
send:表示通知内容使用的模板名称
description:当前触发器的描述信息
when:条件表达式,如果应发送通知,则返回 true
----------------------------------------------------------------------------------------------------------------
该模板用于生成通知内容,该模板利用 Golang 中的 html/template 包定义,允许定义通知标题和正文,可以重用,并且可以由多个触发器引用。每个模板默认都可以访问以下字段:
app:保存应用程序对象
context:是用户定义的字符串映射,可能包含任何字符串键和值,这里用于拼接argocd的web UI地址
notificationType 保留通知服务类型名称,该字段可用于有条件地呈现服务特定字段
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 五. 修改argocd-notifications-controller
时区,webhook的时间是根据argocd-notifications-controller来取
apiVersion: apps/v1
kind: Deployment
metadata:
name: argocd-notifications-controller
spec:
template:
spec:
containers:
- name: argocd-notifications-controller
env:
- name: TZ
Values:Asia/Shanghai
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
六. 更新资源文件,重启并查看有无异常,参考 (opens new window)
[root@k8s-uat-manager ~]# kubectl apply -f argocd-notifications-cm.yaml
[root@k8s-uat-manager ~]# kubectl delete pod -n argocd argocd-notifications-controller-779c5f97bb-kf8fz
[root@k8s-uat-manager ~]# kubectl logs -f -n argocd argocd-notifications-controller-779c5f97bb-8nbc8
1
2
3
2
3
上次更新: 2025/04/25, 03:40:17