Sunday, December 22, 2024
HomeKubernetesArgoCDArgoCD Resource Hooks - Explained

ArgoCD Resource Hooks – Explained

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:

  • Argo CD continuously monitors a Git repository that describes the desired state of the Kubernetes applications.
  • When the content of the Git repository changes (for example, when a new commit is pushed), Argo CD detects the difference between the desired state (as per the Git repository) and the actual state (the current state of the Kubernetes cluster).
  • It then synchronizes the actual state with the desired state by applying the differences. This can involve creating, updating, or deleting Kubernetes resources.

The phases of sync are as follows:

  • Pre-sync: Run before all other application resources are created/updated.
  • Sync: Run after all other application resources have been created/updated, but before the status is refreshed.
  • Post-sync: Run after all other application resources have been created/updated and the status is refreshed.

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:

  • Named hooks (i.e. ones with /metadata/name) will only be created once. If you want a hook to be re-created each time either use BeforeHookCreation policy (see below) or /metadata/generateName.
  • Hooks are not run during selective sync.

Argo CD Resource Hooks Use Cases

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

  • PreSync hook: Perform any action that needs to be done before the sync phase.
metadata:
 annotations:
  argocd.argoproj.io/hook: PreSync
  • Skip hook: Inform Argo CD to skip the application of the manifest.
metadata:
 annotations:
  argocd.argoproj.io/hook: Skip
  • Sync hook: Orchestrate a complex deployment that requires more sophistication than Kubernetes’ rolling update strategy.

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

  • PostSync hook: Run integration and health checks after a deployment or tweet that the new release is out and any other integration with other systems.
metadata:
 annotations:
  argocd.argoproj.io/hook: PostSync
  • SyncFail hook: to run cleanup or finalizer logic if a Sync operation fails.
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.

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments