FoxuTech

ArgoCD Resource Hooks – Explained

ArgoCD Resource Hooks

We have seen more about  ArgoCD part of our ArgoCD series, in this will see another interesting feature on ArgoCD called resource hooks.

What is Argo CD Resource Hooks?

In Argo CD, “resource hooks” are a feature that allows you to run additional steps (in the form of Kubernetes jobs or other resource types) during the lifecycle of a synchronization process. This can be particularly useful when you need to perform additional setup, work flow, or other actions as part of deploying your application.

Argo CD Synchronization

Synchronization (or sync) is the process by which the actual state of the Kubernetes cluster is made to match the desired state defined in a Git repository. The process looks like:

The phases of sync are as follows:

These are called resource hooks, which give us the power to run any other operation before, during, or after the sync phase. In Argo CD, the following hooks are defined:

Note:

Argo CD Resource Hooks Use Cases

The resource hooks are applied to the particular Kubernetes manifests and usually defined just like a Kubernetes annotation.

metadata:
 annotations:
  argocd.argoproj.io/hook: PreSync
metadata:
 annotations:
  argocd.argoproj.io/hook: Skip

metadata:
 annotations:
  argocd.argoproj.io/hook: Sync

metadata:
 annotations:
  argocd.argoproj.io/hook: PostSync
metadata:
  annotations:
    argocd.argoproj.io/hook: SyncFail

Argo CD Resource Hook Example

Here is an example of how to use a resource hook: Also you can find more examples on https://github.com/foxutech/kubernetes/tree/main/argocd/sync/phases.

apiVersion: batch/v1
kind: Job
metadata:
  name: postsync
  annotations:
    argocd.argoproj.io/hook: PostSync
    argocd.argoproj.io/hook-delete-policy: HookFailed
spec:
  template:
    spec:
      containers:
      - name: sleep
        image: alpine:latest
        command: ["sleep", "20"]
      restartPolicy: Never
  backoffLimit: 0

In this example, a PostSync hook is defined. This hook will run a Kubernetes job after the application synchronization is complete.

Exit mobile version