ArgoCD notifications 配置

发布时间 2023-12-21 10:05:46作者: 小吉猫

ArgoCD notifications Triggers

Triggers 定义应发送通知的条件。定义包括名称、条件 和通知模板参考。条件是一个谓词表达式,如果通知则返回 true 应该发送。

Triggers 列表

1. on-created                        # Application 创建
2. on-deleted                        # Application 删除
3. on-deployed                       # Application 已经同步完成且正常运行。每次提交触发一次。
4. on-health-degraded                # Application 降级
5. on-sync-failed                    # Application 同步失败
6. on-sync-running                   # Application 正在同步
7. on-sync-status-unknown            # Application 状态为 'Unknown'
8. on-sync-succeeded                 # Application 同步成功

Triggers 示例

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  # Optional 'oncePer' property ensure that notification is sent only once per specified field value
  # E.g. following is triggered once per sync revision
  trigger.on-deployed: |
    when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
    oncePer: app.status.sync.revision
    send: [app-sync-succeeded]

ArgoCD notifications Templates

通知模板用于生成通知内容,并在argocd-notifications-cm ConfigMap中进行配置。

Templates 定义规范

1. app:保存应用程序对象。
2. context:是用户定义的字符串映射,可能包含任何字符串键和值。
3. serviceType:保存通知服务类型名称。该字段可用于有条件地呈现服务特定字段。
4. recipient:保存收件人姓名。

定义 context

定义共享 context 可在Templates引用。
apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  context: |
    region: east
    environmentName: staging

  template.a-slack-template-with-context: |
    message: "Something happened in {{ .context.environmentName }} in the {{ .context.region }} data center!"

Email 配置

配置所需参数

电子邮件通知服务使用 SMTP 协议发送电子邮件通知,并需要指定以下设置:
host: SMTP 服务器主机名
port: SMTP 服务器端口
username: 用户名
password: 密码
from: 发送电子邮件地址
html: 可选布尔值,true 或 false
insecure_skip_verify: 可选布尔值,true 或 false

Secret 配置

apiVersion: v1
kind: Secret
metadata:
  name: argocd-notifications-secret
  namespace: argocd
stringData:
  email-username: email-username
  email-password: email-password
type: Opaque

ConfigMap 配置

# kubectl edit cm argocd-notifications-cm  -n argocd
添加 data 部分内容。
apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
  ...
data:
  service.email: |
    username: $email-username
    password: $email-password
    host: smtp.exmail.qq.com
    port: 465
    from: $email-username

  template.app-deployed: |
    email:
      subject: New version of an application {{.app.metadata.name}} is up and running.
    message: |
      Application {{.app.metadata.name}} is now running new version of deployments manifests.
  template.app-sync-succeeded: |
    email:
      subject: Application {{.app.metadata.name}} has been successfully synced.
    message: |
      Application {{.app.metadata.name}} has been successfully synced at {{.app.status.operationState.finishedAt}}.
      Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .

  trigger.on-deployed: |
    - description: Application is synced and healthy. Triggered once per commit.
      oncePer: app.status.operationState?.syncResult?.revision
      send:
      - app-deployed
      when: app.status.operationState != nil and app.status.operationState.phase in ['Succeeded']
        and app.status.health.status == 'Healthy'
  trigger.on-sync-succeeded: |
    - description: Application syncing has succeeded
      send:
      - app-sync-succeeded
      when: app.status.operationState != nil and app.status.operationState.phase in ['Succeeded']

订阅通知 

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: nginx
  namespace: argocd
  annotations:
    notifications.argoproj.io/subscribe.on-deployed.email: wgs@hard-chain.cn;1304995320@qq.com
...

邮件信息

Application nginx is now running new version of deployments manifests.

notifications 配置示例

apiVersion: v1
data:
  service.email: |
    username: $email-username
    password: $email-password
    host: smtp.exmail.qq.com
    port: 465
    from: $email-username
  template.app-deployed: |
    email:
      subject: New version of an application {{.app.metadata.name}} is up and running.
    message: |
      Application {{.app.metadata.name}} is now running new version of deployments manifests.
  template.app-sync-succeeded: |
    email:
      subject: Application {{.app.metadata.name}} has been successfully synced.
    message: |
      Application {{.app.metadata.name}} has been successfully synced at {{.app.status.operationState.finishedAt}}.
      Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
  trigger.on-deployed: |
    - description: Application is synced and healthy. Triggered once per commit.
      oncePer: app.status.operationState?.syncResult?.revision
      send:
      - app-deployed
      when: app.status.operationState != nil and app.status.operationState.phase in ['Succeeded']
        and app.status.health.status == 'Healthy'
  trigger.on-sync-succeeded: |
    - description: Application syncing has succeeded
      send:
      - app-sync-succeeded
      when: app.status.operationState != nil and app.status.operationState.phase in ['Succeeded']
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"ConfigMap","metadata":{"annotations":{},"labels":{"app.kubernetes.io/component":"notifications-controller","app.kubernetes.io/name":"argocd-notifications-controller","app.kubernetes.io/part-of":"argocd"},"name":"argocd-notifications-cm","namespace":"argocd"}}
  creationTimestamp: "2023-12-08T09:41:38Z"
  labels:
    app.kubernetes.io/component: notifications-controller
    app.kubernetes.io/name: argocd-notifications-controller
    app.kubernetes.io/part-of: argocd
  name: argocd-notifications-cm
  namespace: argocd
  resourceVersion: "9897682"
  uid: 6953cdca-7551-49e2-8e5c-daf16c3fdd9f

参考文档

https://argo-cd.readthedocs.io/en/stable/operator-manual/notifications/