コンテンツにスキップ

ArgoCD Notifications(Helm) の設定

Kubernetes クラスタでアプリケーションのデリバリーを自動化する ArgoCD と、通知ツールとしての Slack を連携させる方法を解説します。

これにより、デプロイや同期の状態を Slack チャンネルへリアルタイムで通知し、運用時の可視性と即応性を向上させることができます。

手順

Slack の設定

Slack Apps の作成

  1. slack apiページへアクセスし、Create New App をクリックします。
    img.png
  2. From scratch を選択し、App Name の入力と Workspace を選択します。
    img2.png
  3. 左上のアプリ名が先ほど入力したものが選択されていることを確認した上で、サイドバーの OAuth & Permissions を選択します。
    img3.png
  4. Scopes で chat:write を選択する
    img4.png
  5. サイドバーの Install App を選択し、Workspace にインストールを行います。
    img5.png
  6. インストールが完了すると Bot User OAuth Token が表示されるのでメモしておきます。
    img5.png

Slack チャンネルのインテグレーション設定

チャンネル設定画面から先ほど作成した Slack アプリを追加します。
img6.png

ArgoCD(Helm) の設定

secret の作成

values.yaml
notifications:
secret:
items:
slack-token: <Bot User OAuth Token>

notifiers 設定

Argo CD Notifications が Secret で登録したトークンを使用するように設定します。

values.yaml
notifications:
notifiers:
service.slack: |
token: $slack-token

subscriptions 設定

通知先のチャンネルとトリガーを設定します。

values.yaml
notifications:
subscriptions:
- recipients:
- slack:<Channel name>
triggers:
- on-deployed
- on-sync-running
- on-sync-status-unknown
- on-sync-succeeded

triggers 設定

トリガーの内容を設定します。

values.yaml
notifications:
triggers:
trigger.on-deployed: |
- description: Application is synced and healthy. Triggered once per commit.
oncePer: app.status.sync.revision
send:
- app-deployed
when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
trigger.on-health-degraded: |
- description: Application has degraded
send:
- app-health-degraded
when: app.status.health.status == 'Degraded'
trigger.on-sync-failed: |
- description: Application syncing has failed
send:
- app-sync-failed
when: app.status.operationState.phase in ['Error', 'Failed']
trigger.on-sync-running: |
- description: Application is being synced
send:
- app-sync-running
when: app.status.operationState.phase in ['Running']
trigger.on-sync-status-unknown: |
- description: Application status is 'Unknown'
send:
- app-sync-status-unknown
when: app.status.sync.status == 'Unknown'
trigger.on-sync-succeeded: |
- description: Application syncing has succeeded
send:
- app-sync-succeeded
when: app.status.operationState.phase in ['Succeeded']
defaultTriggers: |
- on-deployed
- on-sync-running
- on-sync-status-unknown
- on-sync-succeeded

templates 設定

Slack のメッセージ内容のテンプレートを設定します。

Application ソースの指定方法として、単体で指定する方法と複数指定する方法の2種類があります。

values.yaml
spec:
source:
repoURL:
values.yaml
spec:
sources:
- repoURL:

どちらの repoURL も表示するには条件分岐して表示を設定する必要があります。

values.yaml
{{if .app.spec.source.repoURL}}
{
"value": "{{.app.spec.source.repoURL}}",
},
{{else}}
{
"value": "{{ (index .app.spec.sources 0).repoURL }}",
},
{{end}}

そのため、以下のような設定を行います。

values.yaml
notifications:
templates:
template.app-deployed: |
slack:
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
},
{{if .app.spec.source.repoURL}}
{
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
},
{{else}}
{
"title": "Repository",
"value": "{{ (index .app.spec.sources 0).repoURL }}",
"short": true
},
{{end}}
{
"title": "Revision",
"value": "{{.app.status.sync.revision}}",
"short": true
}
{{range $index, $c := .app.status.conditions}}
{{if not $index}},{{end}}
{{if $index}},{{end}}
{
"title": "{{$c.type}}",
"value": "{{$c.message}}",
"short": true
}
{{end}}
]
}]
template.app-health-degraded: |
slack:
attachments: |-
[{
"title": "{{ .app.metadata.name}}",
"title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
"color": "#f4c030",
"fields": [
{
"title": "Sync Status",
"value": "{{.app.status.sync.status}}",
"short": true
},
{{if .app.spec.source.repoURL}}
{
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}
{{else}}
{
"title": "Repository",
"value": "{{ (index .app.spec.sources 0).repoURL }}",
"short": true
}
{{end}}
{{range $index, $c := .app.status.conditions}}
{{if not $index}},{{end}}
{{if $index}},{{end}}
{
"title": "{{$c.type}}",
"value": "{{$c.message}}",
"short": true
}
{{end}}
]
}]
template.app-sync-failed: |
slack:
attachments: |-
[{
"title": "{{ .app.metadata.name}}",
"title_link":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
"color": "#E96D76",
"fields": [
{
"title": "Sync Status",
"value": "{{.app.status.sync.status}}",
"short": true
},
{{if .app.spec.source.repoURL}}
{
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}
{{else}}
{
"title": "Repository",
"value": "{{ (index .app.spec.sources 0).repoURL }}",
"short": true
}
{{end}}
{{range $index, $c := .app.status.conditions}}
{{if not $index}},{{end}}
{{if $index}},{{end}}
{
"title": "{{$c.type}}",
"value": "{{$c.message}}",
"short": true
}
{{end}}
]
}]
template.app-sync-running: |
slack:
attachments: |-
[{
"title": "{{ .app.metadata.name}}",
"title_link":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
"color": "#0DADEA",
"fields": [
{
"title": "Sync Status",
"value": "{{.app.status.sync.status}}",
"short": true
},
{{if .app.spec.source.repoURL}}
{
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}
{{else}}
{
"title": "Repository",
"value": "{{ (index .app.spec.sources 0).repoURL }}",
"short": true
}
{{end}}
{{range $index, $c := .app.status.conditions}}
{{if not $index}},{{end}}
{{if $index}},{{end}}
{
"title": "{{$c.type}}",
"value": "{{$c.message}}",
"short": true
}
{{end}}
]
}]
template.app-sync-status-unknown: |
slack:
attachments: |-
[{
"title": "{{ .app.metadata.name}}",
"title_link":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
"color": "#E96D76",
"fields": [
{
"title": "Sync Status",
"value": "{{.app.status.sync.status}}",
"short": true
},
{{if .app.spec.source.repoURL}}
{
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}
{{else}}
{
"title": "Repository",
"value": "{{ (index .app.spec.sources 0).repoURL }}",
"short": true
}
{{end}}
{{range $index, $c := .app.status.conditions}}
{{if not $index}},{{end}}
{{if $index}},{{end}}
{
"title": "{{$c.type}}",
"value": "{{$c.message}}",
"short": true
}
{{end}}
]
}]
template.app-sync-succeeded: |
slack:
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
},
{{if .app.spec.source.repoURL}}
{
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}
{{else}}
{
"title": "Repository",
"value": "{{ (index .app.spec.sources 0).repoURL }}",
"short": true
}
{{end}}
{{range $index, $c := .app.status.conditions}}
{{if not $index}},{{end}}
{{if $index}},{{end}}
{
"title": "{{$c.type}}",
"value": "{{$c.message}}",
"short": true
}
{{end}}
]
}]

これにより、Helm でインストールした ArgoCD から Slack に通知を行う設定が完了します。

トラブルシューティング

ログの確認

通知に関する問題が発生した場合、argocd-notifications-controller Pod のログを確認します。
img8.png

テスト送信

argocd-notifications-controller Pod 内から argocd admin notifications template notify コマンドを使用して、通知のテスト送信が可能です。

terminal
argocd admin notifications template notify app-sync-succeeded <Application Name> --recipient slack:<Channel Name>

img9.png