What should I do if I want to do something after syncing the ArgoCD Application?
#SRG(Service Reliability Group) is a group that mainly provides cross-sectional support for the infrastructure of our media services, improving existing services, launching new ones, and contributing to OSS.
This article explains how to perform post-synchronization (PostSync) in a GitOps workflow using ArgoCD, and proposes some practical solutions for post-processing specific resources only.
backgroundGeneral methodResourceHookSyncWaveconstraintsHow do I post-process a specific resource?1. Manage specific resources in a separate application2. How ArgoEvents and CustomResource work3. Using a Notification ControllerConclusion
background
In GitOps operations using ArgoCD, synchronization is often performed manually or automatically. ArgoCD applications often require post-processing after synchronization. For example, the following scenarios can be considered:
- Creating certain resources after deployment
- Various notifications and tests after deployment
- Purging the CDN cache after deployment
A common way to address these needs is to use ArgoCD's Resource Hooks feature. If you want to manage post-processing itself as part of the resources managed by ArgoCD, you can also control it using SyncWaves.
However, Resource Hooks only work at the application level, and cannot target specific resources within an application. In that case, some special methods are required.
General method
ResourceHook
Resource Hook is a feature that allows you to customize the resource synchronization process. Using hooks such as PreSync, Sync, and PostSync, you can run custom jobs at specific times in the synchronization process. Application post-processing falls under the PostSync hook.
For example, when adding post-processing to an ArgoCD Application named dev, it is common to use the following configuration:
sync-wave
SyncWave
SyncWaves is a feature for controlling the synchronization order of resources. Lower values are executed first. Since the sync-wave of normal resources is 0, if you set it to a number greater than 0, you can synchronize resources in a later order than normal resources.
If you want to manage the post-processing logs and status, you can also manage the post-processing resources themselves as resources managed by the ArgoCD Application.
constraints
- Resource Hooks operate on an application-wide synchronization basis, so they cannot operate on a specific resource.
- In scenarios with complex dependencies, configuring hooks and waves can be tedious, especially when relying on specific resources. Synchronization order alone may not be able to handle dependency assumptions.
How do I post-process a specific resource?
At the moment, there are three practical options:
1. Manage specific resources in a separate application
Separating ArgoCD Applications allows you to manage specific resources as separate ArgoCD Applications, making it easier to perform post-processing using PostSync, SyncWave, etc. For example, you can define a job for clearing the cache as a separate application and run it after synchronizing that application.
This approach is the most efficient, but it can be unusable and can add complexity to scenarios with complex dependencies.
2. How ArgoEvents and CustomResource work
By using ArgoEvents, you can execute processing triggered by specific events (resource update, update of specific spec value, etc.). ArgoEvents is a powerful tool. It can be configured in detail even for CustomResource other than standard resources.
Also, if the resource managed by the ArgoCD Application is a specific CustomResource, it is a good choice to use the functions of that CustomResource.
For example, in an ArgoCD Application, you do not directly handle standard resources such as Deployment/ConfigMap, but manage a KubeVela Application.KubeVela Application WorkflowIt is recommended to use the following.
apply-component
Below is an example of triggering Github Actions via API after applying the KubeVela Application.
3. Using a Notification Controller
Originally, ArgoCD's Notification Controller was intended to send notifications based on Application synchronous events. The Webhook NotificationType allows you to incorporate other processing into the deployment process.
The principle is to use some of the ArgoCD Application Status and process it with a Webhook.
First, let's look at the configuration of the ArgoCD Application's Status.
status.operationState.syncResult.resources[]
status.operationState.syncResult
Conclusion
This article has described how to post-process ArgoCD and provided practical solutions for specific resources.
argocd.argoproj.io/hook-trigger-resource-type: Deployment
SRG is looking for people to work with us.
If you're interested, please contact us here.
SRG runs a podcast where we chat about the latest hot topics in IT technology and books. We hope you'll listen to it while you work.