argocd image-updater and notification

# argocd-image-updater 설치
# 이미지가 업데이트되면 해당 이미지를 사용하는 argocd app 에 반영할 수 있다.
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-image-updater/stable/manifests/install.yaml

# image 확인 간격은 기본 2m 인데 변경하려면 deployment 에 아래처럼 --interval 옵션으로 변경할 수 있다.
spec:
  template:
    spec:
      containers:
      - command:
        - /usr/local/bin/argocd-image-updater
        - run
        - --interval
        - 300s
      # 또는 args 로 설정
      - args:
        - run
        - --interval
        - 300s

# docker.io quay.io, ghcr.io 등의 image registry 외
# 커스텀 image 저장소인 경우 configmap 에 추가
data:
  log.level: debug
  registries.conf: |
    registries:
    - name: ysoftman_images
      api_url: https://ysoftman.image.io
      prefix: ysoftman.image.io

# argocd-image-updater 재시작
kubectl -n argocd rollout restart deployment argocd-image-updater

# k8s > argocd-image-updater pod 에 ssh 접속해서 다음 명령으로 작동여부를 확인할 수 있다.
argocd-image-updater test <이미지명>

# argocd app 에 annotation 추가
# latest: 최근 이미지 태그가 생성된것으로 업데이트
# digest: 이미 이미지 태그가 있는 상태에서 태그의 이미지가 변경되면 업데이트(dev, stage, prod, latest 태그 처럼 계속 이미지가 변경되는 경우)
# helm 설정에서 image.tag 가 아닌 ysoftmanApp.image.tag 필드를 사용중인 경우
kubectl annotate app <argocd 앱이름> -n argocd \
argocd-image-updater.argoproj.io/image-list="myapp=<이미지경로:태그>" \
argocd-image-updater.argoproj.io/myapp.update-strategy=digest \
argocd-image-updater.argoproj.io/myapp.helm.image-tag=ysoftmnaApp.image.tag

# 이제 태그 이미지가 변경되면 변경된 이미지 layer(digest)를 받고
# deployment > image > 이미지경로:태그@sha256:xxxxx 로 변경돼 pod 가 재시작된다.
 
# image updater pod 로그에서 다음과 같은 에러 발생시
error listing applications: applications.argoproj.io is forbidden: User "system:serviceaccount:argocd:argocd-image-updater" cannot list resource "applications" in API group "argoproj.io" at the cluster scope"

# ClusterRole 에 get, list, watch 등이 있는지 확인
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: argocd-image-updater-role
rules:
  - apiGroups: ["argoproj.io"]
    resources: ["applications"]
    verbs: ["get", "list", "watch"]

# ClusterRoleBinding 에 설정이 잘 되어 있는지 확인
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: argocd-image-updater-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: argocd-image-updater-role
subjects:
  - kind: ServiceAccount
    name: argocd-image-updater
    namespace: argocd

# 권한 적용 확인, 정상이면 yes 출력된다.
kubectl auth can-i list applications.argoproj.io --as=system:serviceaccount:argocd:argocd-image-updater --all-namespaces

#####

# argocd-notification 로 argocd 상태를 slack, github, webhook 등으로 노티를 보내 보자.
# argocd-notification 은 argocd 설치시 기본으로 포함되어 있다.

# 슬랙으로 알람 보내는 경우
# argocd-notifications-secret 에 슬랙 토크늘 추가한다.
# 참고로 stringData 로 저장하면 data 로 바뀌고 값은 base64 인코딩된다.
kind: Secret
stringData:
  slack-token: <slack oauth access token>

# 현재 설정된 configmap 을 가져오자.
kubectl get cm argocd-notifications-cm -o yaml -n argocd > argocd-notification-cm.yaml

# app sync 성공시 특정 URL 로 노티 보내기 위해 다음을 내용을 추가한다.
# https://argocd-notifications.readthedocs.io/en/stable/services/webhook/ 가 문서가 있는데 데 subscriptions.recipients 부분 설명이 없어 아래 예시에 추가했다.

data:
  subscriptions: |
    - recipients:
      - ysoftman

  # x-www-form-urlencoded 인 경우
  service.webhook.ysoftman: |
    url: https://ysoftman.test.com
    headers:
    - name: Content-Type
      value: application/x-www-form-urlencoded
  template.app-sync-succeeded: |
    webhook:
      ysoftman:
        method: POST
        body: key1=value1&key2=value2

  # json 인 경우
  service.webhook.ysoftman: |
    url: https://ysoftman.test.com
    headers:
    - name: Content-Type
      value: application/json
  template.app-sync-succeeded: |
    webhook:
      ysoftman:
        method: POST
        body: |
          {
            "key1":123,
            "key2":"aaa"
          }

  # 슬랙 사용시 위에서 저장한 시크릿을 토큰으로 참조하도록 한다.
  service.slack: |
    token: $slack-token

# 기본 트리거 설정을 다음과 같이 설치하면 configmap 에 추가된다.
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/notifications_catalog/install.yaml

# (kubectl.kubernetes.io/last-applied-configuration 는 삭제후) 적용
kubectl apply -f ./argocd-notification-cm.yaml

# 노티를 사용할 application 에 다음 annotations 를 추가한다.
# 또는 arogcd ui > app > summary > edit > notification subscriptions 
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  annotations:
    notifications.argoproj.io/subscribe.on-deployed.ysoftman: ""
    notifications.argoproj.io/subscribe.on-sync-succeeded.ysoftman: ""
    notifications.argoproj.io/subscribe.on-sync-failed.ysoftman: ""
    notifications.argoproj.io/subscribe.on-sync-running.slack: my_channel
    notifications.argoproj.io/subscribe.on-sync-succeeded.slack: my_channel

comments:

댓글 쓰기