예시

예시 (Examples)

Argo CD 알림 서비스로 할 수 있는 일들을 예시로 보여드려요. sync가 발생했을 때 웹훅으로 알림을 받고 리소스가 어떻게 바뀌었는지 파악하는 방법, 그리고 배포된 이미지 목록을 Slack으로 보내는 방법을 다뤄요.

출처: 문서

본문

Argo CD의 알림 서비스로 할 수 있는 일들의 예시를 여기서 찾을 수 있어요.

sync가 발생할 때 알림을 받고 리소스가 어떻게 바뀌었는지 이해하기

Argo CD로 sync가 발생한 시점과 무엇이 바뀌었는지 알려주는 알림 시스템을 만들 수 있어요. sync가 발생했을 때 웹훅으로 알림을 받으려면 다음 트리거를 추가할 수 있어요.

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.on-deployed-webhook: |
    url: <your-webhook-url>
    headers:
    - name: "Content-Type"
      value: "application/json"

  template.on-deployed-template: |
    webhook:
      on-deployed-webhook:
        method: POST
        body: |
              {{toJson .app.status.operationState.syncResult}}


  trigger.on-deployed-trigger: |
    when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
    oncePer: app.status.sync.revision
    send: [on-deployed-template]

트리거 섹션에서 설명했듯이, 이것은 앱이 sync되고 healthy일 때 알림을 생성해요. 그다음 웹훅 통합을 위한 구독(subscription)을 만들어야 해요.

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  annotations:
    notifications.argoproj.io/subscribe.on-deployed-trigger.on-deployed-webhook: ""

아무 웹훅 사이트를 추가하고 애플리케이션을 sync해서 이것이 동작하는지 테스트하고 응답이 어떻게 생겼는지 볼 수 있어요. 여기서 리소스 목록과 메시지, 그리고 그것들의 일부 속성을 받는 것을 볼 수 있어요. 예를 들어:

{
  "resources": [
    {
      "group": "apps",
      "hookPhase": "Running",
      # The images array follows the same order as in the resource yaml
      "images": [
        "nginx:1.27.1"
      ],
      "kind": "Deployment",
      "message": "deployment.apps/test configured",
      "name": "test",
      "namespace": "argocd",
      "status": "Synced",
      "syncPhase": "Sync",
      "version": "v1"
    },
    {
      "group": "autoscaling",
      "hookPhase": "Running",
      "kind": "HorizontalPodAutoscaler",
      "message": "horizontalpodautoscaler.autoscaling/test-hpa unchanged",
      "name": "test-hpa",
      "namespace": "argocd",
      "status": "Synced",
      "syncPhase": "Sync",
      "version": "v2"
    }
  ],
  "revision": "f3937462080c6946ff5ec4b5fa393e7c22388e4c",
  ...
}

이 정보를 활용해 다음을 알 수 있어요.

  1. 어떤 리소스가 바뀌었는지(Server Side Apply에 대해서는 유효하지 않음)
  2. 어떻게 바뀌었는지

바뀐 리소스를 이해하려면 각 리소스에 연결된 message를 확인할 수 있어요. 'unchanged'로 표시된 것들은 sync 작업 중 영향을 받지 않았어요. 바뀐 리소스 목록으로 images 배열을 살펴보면 어떻게 바뀌었는지 알 수 있어요.

이 정보로 예를 들어 다음을 할 수 있어요.

  1. 배포되는 이미지의 버전 모니터링
  2. 조직 내에서 오류가 알려진 이미지가 있는 배포 롤백
  3. 예상치 못한 이미지 변경 감지: 웹훅 페이로드의 images 배열을 모니터링해 예상된 컨테이너 이미지만 배포되는지 확인

이렇게 하면 배포 상태를 더 고급스럽게 이해할 수 있는 알림 시스템을 구축하는 데 도움이 돼요.

이미지 목록을 Slack으로 보내기

여기서는 위와 비슷한 설정을 사용하되 수신자를 Slack으로 바꿔요.

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.slack: |
    token: <your-slack-bot-token>

  template.on-deployed-template: |
    slack:
      message: |
        *Deployment Notification*
        *Application:* `{{.app.metadata.name}}`
        *Namespace:* `{{.app.spec.destination.namespace}}`
        *Revision:* `{{.app.status.sync.revision}}`
        *Deployed Images:*
          {{- range $resource := .app.status.operationState.syncResult.resources -}}
            {{- range $image := $resource.images -}}
              - "{{$image}}"
            {{- end }}
          {{- end }}
  trigger.on-deployed-trigger: |
    when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
    oncePer: app.status.sync.revision
    send: [on-deployed-template]

이제 위 설정으로 sync하면 이미지 목록이 Slack 애플리케이션으로 전송돼요. Slack 통합에 대한 자세한 내용은 Slack 통합 가이드를 참고하세요.

이미지 중복 제거 (Deduplicating images)

syncResult.resources의 필드는 GitOps 저장소에서 사용자가 선언한 리소스만 포함하지만, 설정에 따라 이미지가 중복될 수 있어요. 중복 이미지를 피하려면 이미지를 중복 제거하는 외부 웹훅 수신기를 만들고, 그다음 메시지를 Slack으로 보내야 해요.

더 알아보기 (Learn more)