Slack
incoming webhook을 사용해서 메시지를 보내고 싶다면, webhook을 사용할 수 있어요. Slack 알림 서비스의 파라미터와 설정 방법, 템플릿 커스터마이즈 방법을 설명하는 문서예요.
출처: 문서
본문
incoming webhook을 사용해서 메시지를 보내고 싶다면, webhook을 사용할 수 있어요.
Parameters
Slack 알림 서비스 설정에는 다음 항목들이 포함돼요:
| | Option | Required | Type | Description | Example |
| | apiURL | False | string | 서버 URL이에요. | https://example.com/api |
| | channels | False | list[string] | | ["my-channel-1", "my-channel-2"] |
| | icon | False | string | 앱 아이콘이에요. | :robot_face: or https://example.com/image.png |
| | insecureSkipVerify | False | bool | | true |
| | signingSecret | False | string | | 8f742231b10e8888abcd99yyyzzz85a5 |
| | token | True | string | 앱의 OAuth 액세스 토큰이에요. | «redacted:xox…» |
| | username | False | string | 앱 사용자 이름이에요. | argocd |
| | disableUnfurl | False | bool | 메시지에서 Slack 링크 unfurling을 비활성화해요. | true |
| | maxIdleConns | False | int | 모든 호스트에 걸친 유휴(keep-alive) 연결의 최대 개수예요. | — |
| | maxIdleConnsPerHost | False | int | 호스트당 유휴(keep-alive) 연결의 최대 개수예요. | — |
| | maxConnsPerHost | False | int | 호스트당 총 연결의 최대 개수예요. | — |
| | idleConnTimeout | False | string | 유휴(keep-alive) 연결이 닫히기 전에 유지되는 최대 시간이에요 (예: 90s). | — |
Configuration
-
https://api.slack.com/apps?new_app=1 에서 Slack 애플리케이션을 만들어요
-
애플리케이션이 생성되면
OAuth & Permissions로 이동해요 -
Scopes>Bot Token Scopes>Add an OAuth Scope로 이동해chat:write스코프를 추가해요. Slack 알림 서비스에서 선택적 username과 icon 오버라이드를 사용하려면chat:write.customize스코프도 추가해요. -
OAuth & Permission>OAuth Tokens for Your Workspace>Install to Workspace
설치가 완료되면 OAuth 토큰을 복사해요.
이 예제의 my_channel처럼 공개 또는 비공개 채널을 만들어요
-
Slack 봇을 이 채널에 초대해요. 그렇지 않으면 Slack 봇이 이 채널에 알림을 전달할 수 없어요
OAuth 액세스 토큰을 argo-rollouts-notification-secret secret에 저장해요
apiVersion: v1
kind: Secret
metadata:
name: <secret-name>
stringData:
slack-token: <OAuth-access-token>
argo-rollouts-notification-configmap configmap의 data 섹션에서 service type slack을 정의해요:
apiVersion: v1
kind: ConfigMap
metadata:
name: argo-rollouts-notification-configmap
data:
service.slack: |
token: $slack-token
특정 argocd 앱에 알림을 활성화하려면 application yaml 파일에 annotation을 추가해요. 다음 예제는 on-sync-succeeded 트리거를 사용해요:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
annotations:
notifications.argoproj.io/subscribe.on-sync-succeeded.slack: my_channel
여러 트리거와 여러 대상·수신자를 가진 annotation
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
annotations:
notifications.argoproj.io/subscriptions: |
- trigger: [on-scaling-replica-set, on-rollout-updated, on-rollout-step-completed]
destinations:
- service: slack
recipients: [my-channel-1, my-channel-2]
- service: email
recipients: [recipient-1, recipient-2, recipient-3 ]
- trigger: [on-rollout-aborted, on-analysis-run-failed, on-analysis-run-error]
destinations:
- service: slack
recipients: [my-channel-21, my-channel-22]
Templates
알림 템플릿은 slack의 message blocks와 attachments 기능을 활용하도록 커스터마이즈할 수 있어요.
message blocks와 attachments는 slack 필드 아래의 blocks와 attachments 문자열 필드에 지정할 수 있어요:
template.app-sync-status: |
message: |
Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
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
}, {
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}]
}]
각 메시지에 아이콘과 사용자 이름을 지정하고 싶다면, slack 필드에서 username과 icon 값을 지정할 수 있어요. 서비스 정의에서처럼 icon에는 이모지와 이미지 URL을 지정할 수 있어요. 템플릿에서 username과 icon을 설정하면, 서비스 정의에 값이 지정되어 있더라도 템플릿에 설정된 값이 사용돼요.
template.app-sync-status: |
message: |
Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
slack:
username: "testbot"
icon: https://example.com/image.png
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
}]
}]
메시지는 slack 필드 아래의 groupingKey 문자열 필드에 지정할 수 있는 그룹 키를 사용해 slack 스레드로 묶을 수 있어요. groupingKey는 각 템플릿에 걸쳐 사용되며 각 slack 채널에서 독립적으로 동작해요. 여러 애플리케이션이 동시에 또는 자주 업데이트될 때, git 커밋 해시나 애플리케이션 이름 등으로 묶으면 slack 채널의 메시지를 쉽게 읽을 수 있어요. 또한 notifyBroadcast 필드로 특정 템플릿의 메시지를 채널로 브로드캐스트할 수 있어요.
template.app-sync-status: |
message: |
Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
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
}, {
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}]
}]
# Aggregate the messages to the thread by git commit hash
groupingKey: "{{.app.status.sync.revision}}"
notifyBroadcast: false
template.app-sync-failed: |
message: |
Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
slack:
attachments: |
[{
"title": "{{.app.metadata.name}}",
"title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
"color": "#ff0000",
"fields": [{
"title": "Sync Status",
"value": "{{.app.status.sync.status}}",
"short": true
}, {
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}]
}]
# Aggregate the messages to the thread by git commit hash
groupingKey: "{{.app.status.sync.revision}}"
notifyBroadcast: true
메시지는 slack 필드 아래의 deliveryPolicy 문자열 필드에 따라 전송돼요. 사용 가능한 모드는 Post(기본값), PostAndUpdate, Update이에요. PostAndUpdate와 Update 설정에는 groupingKey가 설정되어 있어야 해요.