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 appliedSync- with the normal manifests (ordered by wave)PostSync- after everything is applied and healthySyncFail- when the sync failedPreDelete/PostDelete- on app deletion (newer versions)Skip- don’t apply the resource at all- Inside a phase,
sync-waveorders hooks the same way as normal resources
Gotchas:
PreSyncruns before the rest, so it can’t depend on anything from the same sync (new namespace, db, configmap). UseCreateNamespace=true, or put the hook in theSyncphase with a wave- Hook failure fails the sync, a failed
PreSyncstops it before anything is applied
Hook delete policy
argocd.argoproj.io/hook-delete-policy:
HookSucceeded- delete after successHookFailed- delete after failureBeforeHookCreation- delete the previous one before creating a new one (default if unset)- Jobs with a fixed name need
BeforeHookCreationorHookSucceeded, or the next sync can’t recreate it generateNameinstead ofnameis 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-migrateruns and completes, deployment+service applied, thensmoke-test - Hooks show up in the app resource tree with the
HOOKcolumn set - Change
curlto a bad URL and resync to see the sync fail andSyncFailfire - 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