Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

Lab 07: Sync Hooks

Hooks are resources (usually Jobs) that run at a point in the sync, set with annotations. Needs lab 01.

Phases

  • PreSync - before any manifests are applied
  • Sync - with the normal manifests (ordered by wave)
  • PostSync - after everything is applied and healthy
  • SyncFail - when the sync failed
  • PreDelete / PostDelete - on app deletion (newer versions)
  • Skip - don’t apply the resource at all
  • Inside a phase, sync-wave orders hooks the same way as normal resources

Gotchas:

  • PreSync runs before the rest, so it can’t depend on anything from the same sync (new namespace, db, configmap). Use CreateNamespace=true, or put the hook in the Sync phase with a wave
  • Hook failure fails the sync, a failed PreSync stops it before anything is applied

Hook delete policy

argocd.argoproj.io/hook-delete-policy:

  • HookSucceeded - delete after success
  • HookFailed - delete after failure
  • BeforeHookCreation - delete the previous one before creating a new one (default if unset)
  • Jobs with a fixed name need BeforeHookCreation or HookSucceeded, or the next sync can’t recreate it
  • generateName instead of name is the alternative

PreSync: migration

presync-job.yaml in a repo under hooks-demo/:

apiVersion: batch/v1
kind: Job
metadata:
  name: db-migrate
  annotations:
    argocd.argoproj.io/hook: PreSync
    argocd.argoproj.io/hook-delete-policy: BeforeHookCreation
spec:
  backoffLimit: 1
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: migrate
        image: busybox:1.36
        command: ["sh", "-c", "echo migrating; sleep 5; echo done"]

PostSync: smoke test

apiVersion: batch/v1
kind: Job
metadata:
  name: smoke-test
  annotations:
    argocd.argoproj.io/hook: PostSync
    argocd.argoproj.io/hook-delete-policy: HookSucceeded
spec:
  backoffLimit: 2
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: test
        image: curlimages/curl:8.8.0
        command: ["sh", "-c", "curl -fsS http://web-app/"]

SyncFail: notify

apiVersion: batch/v1
kind: Job
metadata:
  generateName: sync-failed-
  annotations:
    argocd.argoproj.io/hook: SyncFail
    argocd.argoproj.io/hook-delete-policy: HookSucceeded
spec:
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: notify
        image: busybox:1.36
        command: ["sh", "-c", "echo sync failed"]

Run it

Plus a Deployment/Service called web-app (nginx) in the same dir:

argocd app create hooks-demo \
  --repo https://github.com/<user>/hooks-demo.git \
  --path . \
  --dest-server https://kubernetes.default.svc \
  --dest-namespace hooks-demo \
  --sync-option CreateNamespace=true
argocd app sync hooks-demo
kubectl get jobs -n hooks-demo
argocd app get hooks-demo
  • Order: db-migrate runs and completes, deployment+service applied, then smoke-test
  • Hooks show up in the app resource tree with the HOOK column set
  • Change curl to a bad URL and resync to see the sync fail and SyncFail fire
  • Hooks aren’t part of the desired state: they don’t make the app OutOfSync
  • Sync waves apply to hooks too, argocd.argoproj.io/sync-wave: "-1" runs a hook earlier in its phase

Cleanup

argocd app delete hooks-demo --yes
kubectl delete namespace hooks-demo --ignore-not-found