Webhook

Webhook

argocd-notifications 로 템플릿화한 요청 본문과 URL을 사용해 일반 HTTP 요청을 보내는 방법입니다. Jenkins 작업을 트리거하거나 GitHub commit status 를 갱신하는 데 쓸 수 있어요.

출처: 문서

본문

webhook 알림 서비스는 템플릿화한 요청 본문과 URL을 사용해 일반 HTTP 요청을 보낼 수 있게 해줍니다. Webhook 을 사용해 Jenkins 작업을 트리거하거나 GitHub commit status 를 갱신할 수 있어요.

파라미터 (Parameters)

Webhook 알림 서비스 구성에는 다음 설정이 포함됩니다:

  • url - 웹훅을 보낼 url

  • headers - 선택 사항, 웹훅과 함께 전달할 헤더

  • basicAuth - 선택 사항, 웹훅과 함께 전달할 기본 인증

  • insecureSkipVerify - 선택 사항 bool, true 또는 false

  • retryWaitMin - 선택 사항, 재시도 사이 최소 대기 시간. 기본값: 1s.

  • retryWaitMax - 선택 사항, 재시도 사이 최대 대기 시간. 기본값: 5s.

  • retryMax - 선택 사항, 최대 재시도 횟수. 기본값: 3.

  • maxIdleConns - 선택 사항, 모든 호스트를 통틀어 유휴(keep-alive) 연결의 최대 수.

  • maxIdleConnsPerHost - 선택 사항, 호스트당 유휴(keep-alive) 연결의 최대 수.

  • maxConnsPerHost - 선택 사항, 호스트당 총 연결의 최대 수.

  • idleConnTimeout - 선택 사항, 유휴(keep-alive) 연결이 닫히기 전에 유지될 최대 시간, 예: '90s'.

재시도 동작 (Retry Behavior)

webhook 서비스는 네트워크 오류나 서버가 5xx 상태 코드를 반환해 요청이 실패하면 자동으로 재시도합니다. 재시도 횟수와 재시도 사이 대기 시간은 retryMax, retryWaitMin, retryWaitMax 파라미터로 구성할 수 있어요.

재시도 사이 대기 시간은 retryWaitMinretryWaitMax 사이입니다. 모든 재시도가 실패하면 Send 메서드가 오류를 반환합니다.

설정 (Configuration)

다음 단계로 webhook 을 구성합니다:

1 argocd-notifications-cm ConfigMap 에 webhook 을 등록합니다:

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.<webhook-name>: |
    url: https://<hostname>/<optional-path>
    headers: #optional headers
    - name: <header-name>
      value: <header-value>
    basicAuth: #optional username password
      username: <username>
      password: <api-key>
    insecureSkipVerify: true #optional bool

2 webhook 요청 메서드, 경로, 본문을 커스터마이즈하는 템플릿을 정의합니다:

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  template.github-commit-status: |
    webhook:
      <webhook-name>:
        method: POST # one of: GET, POST, PUT, PATCH. Default value: GET
        path: <optional-path-template>
        body: |
          <optional-body-template>
  trigger.<trigger-name>: |
    - when: app.status.operationState.phase in ['Succeeded']
      send: [github-commit-status]

3 webhook 통합에 대한 구독을 만듭니다:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  annotations:
    notifications.argoproj.io/subscribe.<trigger-name>.<webhook-name>: ""

  • TLS 설정 (선택 사항)

webhook 서버가 커스텀 TLS 인증서를 사용한다면, 아래처럼 argocd-tls-certs-cm ConfigMap 에 인증서를 추가해 알림 서비스가 이를 신뢰하도록 구성할 수 있어요:

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-tls-certs-cm
data:
  <hostname>: |
    -----BEGIN CERTIFICATE-----
    <TLS DATA>
    -----END CERTIFICATE-----

참고: 커스텀 인증서를 신뢰하지 않으면 다음과 같은 오류가 발생할 수 있습니다:

Put \"https://...\": x509: certificate signed by unknown authority

argocd-tls-certs-cm 에 서버 인증서를 추가하면 이 문제가 해결됩니다.

예시 (Examples)

GitHub commit status 설정

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.github: |
    url: https://api.github.com
    headers: #optional headers
    - name: Authorization
      value: token $github-token

2 webhook 요청 메서드, 경로, 본문을 커스터마이즈하는 템플릿을 정의합니다:

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.github: |
    url: https://api.github.com
    headers: #optional headers
    - name: Authorization
      value: token $github-token

  template.github-commit-status: |
    webhook:
      github:
        method: POST
        path: /repos/{{call .repo.FullNameByRepoURL .app.spec.source.repoURL}}/statuses/{{.app.status.operationState.operation.sync.revision}}
        body: |
          {
            {{if eq .app.status.operationState.phase "Running"}} "state": "pending"{{end}}
            {{if eq .app.status.operationState.phase "Succeeded"}} "state": "success"{{end}}
            {{if eq .app.status.operationState.phase "Error"}} "state": "error"{{end}}
            {{if eq .app.status.operationState.phase "Failed"}} "state": "error"{{end}},
            "description": "ArgoCD",
            "target_url": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
            "context": "continuous-delivery/{{.app.metadata.name}}"
          }

Jenkins 작업 시작

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.jenkins: |
    url: http://<jenkins-host>/job/<job-name>/build?token=<job-secret>
    basicAuth:
      username: <username>
      password: <api-key>

type: Opaque

form-data 보내기

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.form: |
    url: https://form.example.com
    headers:
    - name: Content-Type
      value: application/x-www-form-urlencoded

  template.form-data: |
    webhook:
      form:
        method: POST
        body: key1=value1&key2=value2

Slack 보내기

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.slack_webhook: |
    url: https://hooks.slack.com/services/xxxxx
    headers:
    - name: Content-Type
      value: application/json

  template.send-slack: |
    webhook:
      slack_webhook:
        method: POST
        body: |
          {
            "attachments": [{
              "title": "{{.app.metadata.name}}",
              "title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
              "color": "#18be52",
              "fields": [{
                "title": "Sync Status",
                "value": "{{.app.status.sync.status}}",
                "short": true
              }, {
                "title": "Repository",
                "value": "{{.app.spec.source.repoURL}}",
                "short": true
              }]
            }]
          }

더 알아보기 (Learn more)