What should I do if I want to do something after syncing with the ArgoCD Application?
#SRGThe Service Reliability Group primarily provides comprehensive support for the infrastructure surrounding our media services, focusing on improving existing services, launching new ones, and contributing to open-source software (OSS).
This article explains how to perform post-sync processing in a GitOps workflow using ArgoCD, and proposes several practical solutions for post-syncing specific resources.
backgroundCommon methodResourceHookSyncWaveconstraintsHow should I handle post-processing for specific resources?1. Manage specific resources with another application.2. How ArgoEvents and CustomResource work3. Use Notification ControllerIn conclusion
background
In GitOps operations using ArgoCD, synchronization is often performed manually or automatically. ArgoCD applications frequently require post-processing after synchronization. For example, the following scenarios are possible:
- Create specific resources after deployment
- Various notifications and tests after deployment
- CDN cache purging after deployment
A common way to address these needs is to utilize ArgoCD's Resource Hooks feature. If you want to manage the post-processing itself as part of a resource under ArgoCD's management, you can also control it using SyncWaves.
However, Resource Hooks can only operate on an application-by-application basis and cannot target specific resources within an application. In such cases, several special methods are required.
Common method
ResourceHook
Resource Hooks are a feature for customizing the resource synchronization process. Using hooks such as PreSync, Sync, and PostSync, you can execute custom jobs at specific points in the synchronization process. Post-processing for applications falls under the PostSync hook.
For example, when adding post-processing to an ArgoCD Application named "dev," the following configuration is commonly used:
sync-waveSyncWave
SyncWaves is a feature that controls the synchronization order of resources. Lower values are executed first. Normally, a resource's sync-wave is 0, so setting it to a number greater than 0 will allow it to synchronize later than normal resources.
If you want to manage the logs and status of post-processing, you can also manage the post-processing resources themselves as resources under the management of the ArgoCD Application.
constraints
- Resource Hooks operate based on application-wide synchronization, so they cannot target specific resources.
- In scenarios with complex dependencies, configuring hooks and waves can become cumbersome. Especially when relying on specific resources, synchronization order alone may not adequately handle the underlying assumptions of those dependencies.
How should I handle post-processing for specific resources?
At this point, there are three realistic approaches.
1. Manage specific resources with another application.
By separating ArgoCD applications, you can manage specific resources as separate ArgoCD applications, making it easier to perform post-processing with PostSync or SyncWave. For example, you can define a job for clearing the cache as a separate application and execute it after that application has synchronized.
While this method is the most effective, it cannot be used in scenarios with complex dependencies and may even make things more complicated.
2. How ArgoEvents and CustomResource work
ArgoEvents allows you to trigger actions based on specific events (such as resource updates or updates to specific Spec values). ArgoEvents is a powerful tool that allows for granular configuration even with CustomResources in addition to standard resources.
Additionally, if the resources under ArgoCD Application management are specific CustomResources, using the features of that CustomResource is also a good option.
For example, in an ArgoCD application, if you manage a KubeVela application without directly handling standard resources such as Deployment/ConfigMap,KubeVela Application WorkflowIt is recommended to use this.
apply-componentThe following is an example of triggering GitHub Actions via the API after applying a KubeVela Application.
3. Use Notification Controller
Originally, ArgoCD's Notification Controller is intended to send notifications based on application synchronization events. However, by using Webhook's NotificationType, you can incorporate other processes into the deployment process.
The principle is to use part of the ArgoCD Application's Status and process it via a Webhook.
First, let's look at the configuration of the ArgoCD Application's Status.
status.operationState.syncResult.resources[]status.operationState.syncResultIn conclusion
This article explains how to perform post-processing on ArgoCD and provides practical solutions for specific resources.
argocd.argoproj.io/hook-trigger-resource-type: DeploymentSRG is looking for new team members.
If you are interested, please contact us here.
SRG runs a podcast where we chat about the latest hot IT technologies and books. We hope you'll enjoy listening to it while you work.
