argo-cd/docs/user-guide/resource_hooks.md
David Maciel 84bece53a3
docs: Use official curl image instead of appropriate/curl (#3695)
The appropriate/curl last update is from two years ago:

```
$ docker run -it --entrypoint /bin/ash appropriate/curl:latest
$ curl --version
curl 7.59.0 (x86_64-alpine-linux-musl) libcurl/7.59.0 LibreSSL/2.6.3 zlib/1.2.11 libssh2/1.8.0
Release-Date: 2018-03-14
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile NTLM NTLM_WB SSL libz UnixSockets HTTPS-proxy
```

There is already an official curl image at https://hub.docker.com/r/curlimages/curl (The link to the docker hub can be found in the official curl download page: https://curl.haxx.se/download.html)
$ docker run -it --entrypoint /bin/ash curlimages/curl:latest
$ curl --version
curl 7.70.0-DEV (x86_64-pc-linux-musl) libcurl/7.70.0-DEV OpenSSL/1.1.1d zlib/1.2.11 libssh2/1.9.0 nghttp2/1.40.0
Release-Date: [unreleased]
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: AsynchDNS HTTP2 HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL TLS-SRP UnixSockets
2020-06-02 18:45:15 +02:00

4.7 KiB

Resource Hooks

Overview

Synchronization can be configured using resource hooks. Hooks are ways to run scripts before, during, and after a Sync operation. Hooks can also be run if a Sync operation fails at any point. Some use cases for hooks are:

  • Using a PreSync hook to perform a database schema migration before deploying a new version of the app.
  • Using a Sync hook to orchestrate a complex deployment requiring more sophistication than the Kubernetes rolling update strategy.
  • Using a PostSync hook to run integration and health checks after a deployment.
  • Using a SyncFail hook to run clean-up or finalizer logic if a Sync operation fails. SyncFail hooks are only available starting in v1.2

Usage

Hooks are simply Kubernetes manifests inside your Argo CD Application annotated with argocd.argoproj.io/hook, e.g.:

apiVersion: batch/v1
kind: Job
metadata:
  generateName: schema-migrate-
  annotations:
    argocd.argoproj.io/hook: PreSync

During a Sync operation, Argo CD will apply the resource during the appropriate phase of the deployment. Hooks can be any type of Kubernetes resource kind, but tend to be Pod, Job or Argo Workflows. Multiple hooks can be specified as a comma separated list.

The following hooks are defined:

Hook Description
PreSync Executes prior to the apply of the manifests.
Sync Executes after all PreSync hooks completed and were successful, at the same time as the apply of the manifests.
Skip Indicates to Argo CD to skip the apply of the manifest.
PostSync Executes after all Sync hooks completed and were successful, a successful apply, and all resources in a Healthy state.
SyncFail Executes when the sync operation fails. Available starting in v1.2

Generate Name

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.

Selective Sync

Hooks are not run during selective sync.

Hook Deletion Policies

Hooks can be deleted in an automatic fashion using the annotation: argocd.argoproj.io/hook-delete-policy.

apiVersion: batch/v1
kind: Job
metadata:
  generateName: integration-test-
  annotations:
    argocd.argoproj.io/hook: PostSync
    argocd.argoproj.io/hook-delete-policy: HookSucceeded

The following policies define when the hook will be deleted.

Policy Description
HookSucceeded The hook resource is deleted after the hook succeeded (e.g. Job/Workflow completed successfully).
HookFailed The hook resource is deleted after the hook failed.
BeforeHookCreation Any existing hook resource is deleted before the new one is created (since v1.3).

As an alternative to hook deletion policies, both Jobs and Argo Workflows support the ttlSecondsAfterFinished field in the spec, which let their respective controllers delete the Job/Workflow after it completes.

spec:
  ttlSecondsAfterFinished: 600

Using A Hook To Send A Slack Message

The following example uses the Slack API to send a a Slack message when sync completes or fails:

apiVersion: batch/v1
kind: Job
metadata:
  generateName: app-slack-notification-
  annotations:
    argocd.argoproj.io/hook: PostSync
    argocd.argoproj.io/hook-delete-policy: HookSucceeded
spec:
  template:
    spec:
      containers:
      - name: slack-notification
        image: curlimages/curl
        command:
          - "curl"
          - "-X"
          - "POST"
          - "--data-urlencode"
          - "payload={\"channel\": \"#somechannel\", \"username\": \"hello\", \"text\": \"App Sync succeeded\", \"icon_emoji\": \":ghost:\"}"
          - "https://hooks.slack.com/services/..."
      restartPolicy: Never
  backoffLimit: 2
apiVersion: batch/v1
kind: Job
metadata:
  generateName: app-slack-notification-fail-
  annotations:
    argocd.argoproj.io/hook: SyncFail
    argocd.argoproj.io/hook-delete-policy: HookSucceeded
spec:
  template:
    spec:
      containers:
      - name: slack-notification
        image: curlimages/curl
        command: 
          - "curl"
          - "-X"
          - "POST"
          - "--data-urlencode"
          - "payload={\"channel\": \"#somechannel\", \"username\": \"hello\", \"text\": \"App Sync failed\", \"icon_emoji\": \":ghost:\"}"
          - "https://hooks.slack.com/services/..."
      restartPolicy: Never
  backoffLimit: 2