《OpenShift 4.x HOL教程汇总》 说明:本文已经 在OpenShift 4.15 + OpenShift GitOps 1.11.2 环境中验证

文章目录

ArgoCD 的 Notification 功能简介启动 OpenShift GitOps 的 Notification 功能配置邮件通知验证参考

说明:先根据《OpenShift 4 之 GitOps(1)通过OpenShift GitOps Operator 安装 ArgoCD》完成安装,然后在 ArgoCD 中创建 spring-petclinic 应用。

ArgoCD 的 Notification 功能简介

ArgoCD 在更新完应用后可以根据更新的状态触发相关事件,例如:on-sync-succeeded,on-sync-running,on-sync-failed 等。针对这些事件,可以通过不同的渠道(例如:Email、Slack、Grafna、Webhook)发送给指定通知方。

启动 OpenShift GitOps 的 Notification 功能

执行以下命令,启动 Notification 功能。

$ oc patch argocd openshift-gitops -n openshift-gitops --type merge --patch '{"spec": {"notifications": {"enabled": true}}}'

完成后可以查看 openshift-gitops 拓扑中会新增名为 openshift-gitops-notification-controller 的部署。 执行以下命令,确认 GitOps Operator 已经自动创建 NotificationsConfiguration 对象。而和通知相关的对象还有 argocd-notifications-cm 和 argocd-notifications-secret。

$ oc get notificationsconfiguration -n openshift-gitops

NAME AGE

default-notifications-configuration 21m

$ oc get cm argocd-notifications-cm -n openshift-gitops

NAME DATA AGE

argocd-notifications-cm 16 35m

$ oc get secret argocd-notifications-secret -n openshift-gitops

NAME TYPE DATA AGE

argocd-notifications-secret Opaque 0 37m

argocd-notifications-cm 中是触发通知的 trigger 和通知内的 template。由于 argocd-notifications-cm 是由 NotificationsConfiguration CRD 生成的,因此无法直接修改 argocd-notifications-cm,而只能修改 NotificationsConfiguration。

配置邮件通知

创建将在邮件 serveice 中使用的 Secret。

$ oc apply -n argocd -f - << EOF

apiVersion: v1

kind: Secret

metadata:

name: argocd-notifications-secret

stringData:

email-username: xxx@gmail.com

email-password: xxxxxxxxxxxxx

type: Opaque

EOF

在 default-notifications-configuration 对象中增加以下 services 的内容,通过该邮件 serveice 发送通知邮件。

apiVersion: argoproj.io/v1alpha1

kind: NotificationsConfiguration

name: default-notifications-configuration

namespace: openshift-gitops

spec:

services:

service.email.gmail: |

host: smtp.gmail.com

port: 465

username: $email-username

password: $email-password

from: $email-username

。。。

向 spring-petclinic 的 ArgoCD 应用添加注释,注释内容为 notifications.argoproj.io/subscribe..: ,其定义了该应用接收 ArgoCD 的 trigger 类型(即 subscribe.on-sync-succeeded),使用的发送 service (即 gmail)和邮件的接受者(即 user@sina.com;user@sohu.com)。

$ oc annotate application spring-petclinic -n openshift-gitops notifications.argoproj.io/subscribe.on-sync-succeeded.gmail='user@sina.com;user@sohu.com'

验证

进入 spring-petclinic 应用用到的 Git Repo,调整 openshift-gitops-getting-started/app/deployment.yaml 文件的 replicas。确认 ArgoCD 可以自动更新最新的 deployment。确认可以收到邮件。

参考

https://blog.argoproj.io/notifications-for-argo-bb7338231604 https://github.com/redhat-developer/gitops-operator/blob/master/docs/OpenShift%20GitOps%20Usage%20Guide.md https://developers.redhat.com/articles/2023/01/17/how-openshift-gitops-notifications-can-trigger-pipelines https://argo-cd.readthedocs.io/en/stable/operator-manual/notifications/ https://argo-cd.readthedocs.io/en/latest/operator-manual/notifications/subscriptions/ https://github.com/argoproj/argo-cd/discussions/9655 https://wearenotch.com/email-notifications-and-github-webhooks-with-argo-cd/

精彩文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: