argocd image-updater and notification

# argocd-image-updater 설치
# 이미지가 업데이트되면 해당 이미지를 사용하는 argocd app 에 반영할 수 있다.
# 0.14.0 버전이 남아 있어 삭제할때
kubectl delete -n kave-argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-image-updater/v0.14.0/manifests/install.yaml
# 최신 1.2.2 버전 설치
kubectl apply -n kave-argocd -f https://raw.githubusercontent.com/argoproj-labs/argocd-image-updater/v1.2.2/config/install.yaml

# 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-controller

# 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 필드를 사용중인 경우
# v0.x 에서는 app 에 어노테이션
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=ysoftmanApp.image.tag

# v1.x 에부터는 아래처럼 ImageUpdater CR 을 사용한다.
apiVersion: argocd-image-updater.argoproj.io/v1alpha1
kind: ImageUpdater
metadata:
  name: myapp
  namespace: argocd            # 대상 Application 과 같은 네임스페이스
spec:
  applicationRefs:
    - namePattern: "myapp" # 대상 Argo CD Application 이름 (glob 가능)
      images:
        - alias: "myapp"
          imageName: "myapp:dev"
          commonUpdateSettings:
            updateStrategy: "digest"
          manifestTargets:
            helm:
              tag: "ysoftmanApp.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"

# argocd-image-updater-manager-rolebinding 의 subject 에 namespace추가
kubectl -n kave-argocd patch rolebinding argocd-image-updater-manager-rolebinding \
  --type=json -p='[{"op":"add","path":"/subjects/0/namespace","value":"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 부분 설명이 없어 아래 예시에 추가했다.
kind: ConfigMap
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"
          }

  # 기본 템플릿에서 참조할 argocd url 명시
  context: |
    argocdUrl: https://ysoftman.argocd

  # 슬랙 사용시 위에서 저장한 시크릿을 토큰으로 참조하도록 한다.
  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:

댓글 쓰기