Cloud/Kubernetes

Argo CD Notification with Mattermost

Jacob_baek 2021. 7. 6. 20:20

Argo CD Notification 은

Argo CD Notifications는 지속적인 Argo CD applications들을 모니터하고 Application 상태의 중요변경사항을 사용자에게 알리는 유연한 방법을 제공한다.

참고
아마 Argo CD 를 사용해본 사람이라면 대략 Application이 무엇인지는 알거라 판단된다.
그래도 간단히 알아보면, 다음과 특징이 있고 이를 기반으로 Argo CD의 sync 되어지는 Application을 관리한다.

  • Application Custom Resource Definition(applications.argoproj.io) 이다.
  • Argo CD에 의해 배포되어질 Application에 대한 Argo CD에 의해 배포된 이력 관리를 한다.
  • Argo CD에 의해 배포될때 사용될 git repo 정보 / 배포될 cluster 정보 등이 존재한다.

다음과 같은 구성요소를 사용하여 notification을 발생시킬수 있다.

  • trigger : 이름대로 특정 시점에 어떤 template을 가지고 trigger 될지를 결정하는 정보를 가지게 된다.
  • template : 전달될 message를 좀더 가시성있게 만들 format을 적용시켜준다.
  • subscription : Message를 전달할 channel을 등록하는 곳으로 slack, mattermost, email 등에 대한 연결정보를 가지게 된다.

Argo CD Notification installation

설치관련하여 아래 두가지의 manifest를 제공한다.

  • manifests/install.yaml : argocd-notifications-controller 설치에 대한 manifest
  • catalog/install.yaml : argocd-notification-cm (configmap) 배포 (default trigger/template 등을 생성) 해주는 manifest

다음 명령으로 현시점(2021.07) 최신버전인 1.1.1 설치를 진행할 수 있다.

[root@localhost ~]# kubectl -n argocd apply -f https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/v1.1.1/manifests/install.yaml
[root@localhost ~]# kubectl -n argocd apply -f https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/v1.1.1/catalog/install.yaml

다음과 같이 배포된 argocd resource들을 확인할 수 있다.

[root@localhost ~]# kubectl get deploy,cm,secret -n argocd
NAME                                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/argocd-dex-server                 1/1     1            1           47d
deployment.apps/argocd-notifications-controller   1/1     1            1           5d19h
deployment.apps/argocd-redis                      1/1     1            1           47d
deployment.apps/argocd-repo-server                1/1     1            1           47d
deployment.apps/argocd-server                     1/1     1            1           47d

NAME                                  DATA   AGE
configmap/argocd-cm                   1      47d
configmap/argocd-gpg-keys-cm          0      47d
configmap/argocd-notifications-cm     4      5d19h
configmap/argocd-rbac-cm              0      47d
configmap/argocd-ssh-known-hosts-cm   1      47d
configmap/argocd-tls-certs-cm         2      47d
configmap/kube-root-ca.crt            1      3d13h

NAME                                                 TYPE                                  DATA   AGE
secret/argocd-application-controller-token-xxx8x     kubernetes.io/service-account-token   3      47d
secret/argocd-dex-server-token-2x9ff                 kubernetes.io/service-account-token   3      47d
secret/argocd-initial-admin-secret                   Opaque                                1      47d
secret/argocd-notifications-controller-token-1xx3f   kubernetes.io/service-account-token   3      5d19h
secret/argocd-notifications-secret                   Opaque                                0      5d19h
secret/argocd-redis-token-xx1xx                      kubernetes.io/service-account-token   3      47d
secret/argocd-secret                                 Opaque                                5      47d
secret/argocd-server-token-xx8gg                     kubernetes.io/service-account-token   3      47d
secret/cluster-kubernetes.default.svc-9876543210     Opaque                                3      36d
secret/default-token-xxx1x                           kubernetes.io/service-account-token   3      47d

How to use Argo CD Notification

먼저 Bot 생성 및 channel에 추가 / channel_id 확인이 필요하다.
아래 링크를 참고하여 bot과 함께 token 생성을 진행하고 channel_id도 사전에 확인해두자.

앞서 구성으로 이야기했던 trigger, template, subscription 을 argocd-notifications-cm내에 추가하여 mattermost로 알람을 발생시킬수 있다. argocd namespace내에 argocd-notifications-cm configmap을 아래와 같이 구성하자.

  context: |
    mattermostChannelId: xxxxxxxxxxxxxxx
  service.webhook.mattermost: |
    url: http://192.168.56.20/api/v4/posts
    headers: #optional headers
    - name: Authorization
      value: token xx1xxxxx11x1xxxx11x1x1xxxx
    - name: Content-Type
      value: application/json
  subscriptions: |
    - recipients:
      - mattermost:alarm_channel
      triggers:
      - on-sync-running
  template.mattermost-app-sync-running: |
    webhook:
      mattermost:
        method: POST
        body: |
              {
                "channel_id":"{{ .context.mattermostChannelId }}",
                "message":":exclamation: doing argocd sync",
                "props":{
                  "attachments":
                    [{
                      "title": "argocd syncing",
                      "color": "#f4c030"
                    }]
                }
              }
  trigger.on-sync-running: |
    - description: Application is being synced
      send:
      - mattermost-app-sync-running
      when: app.status.operationState.phase in ['Running']

간단하게 설명하자면 trigger 조건이 on-sync-running 인 상황(즉, argocd app sync가 동작되는 조건)에서 지정한 template.mattermost-app-sync-running의 webhook 조건에 맞게 알람을 발생시키게 된다.
여기서 subscriptions에 정의된 recipients에 해당 notification이 전달되게 되고 recipients는 service.webhook.mattermost에 정의되어 있다.

참고

알람이 발생되게 되면 다음과 같은 log를 notification controller에서 확인할 수 있다.

time="2021-07-06T01:12:05Z" level=info msg="Start processing" app=argocd/argo-testapp
time="2021-07-06T01:12:05Z" level=info msg="Trigger on-sync-running result: [{[0].xGlbhcF_taGYrI3UsrQsINn2hl0 [mattermost-app-sync-running] true}]" app=argocd/argo-testapp
time="2021-07-06T01:12:05Z" level=info msg="Notification about condition 'on-sync-running.[0].xGlbhcF_taGYrI3UsrQsINn2hl0' already sent to '{mattermost alarm_channel}'" app=argocd/argo-testapp

References