What should I do if I want to do something after syncing the ArgoCD Application?

Media Headquarters Service Reliability Group (SRG)@ishikawa_kumo)is.
#SRG(Service Reliability Group) mainly provides cross-sectional support for the infrastructure of our media services, improving existing services, launching new ones, contributing to OSS, etc.
This article explains how to perform post-synchronization (PostSync) in a GitOps operational flow using ArgoCD, and proposes some practical solutions for post-processing of specific resources only.

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 are possible:
  • Creating certain resources after deployment
  • Various post-deployment notifications and tests
  • Purging the CDN cache after deployment
A common way to address these needs is to use the Resource Hooks feature of ArgoCD, but if you want to manage the 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. To do this, you need to use some special methods.

Common practice

ResourceHook

Resource Hook is a feature for customizing the resource synchronization process. Using hooks such as PreSync, Sync, and PostSync, you can run custom jobs at specific times in the synchronization process. Post-processing of an application corresponds to 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 under the ArgoCD Application.

constraints

  • Resource Hooks operate on an application-wide synchronization basis and cannot target operations to specific resources.
  • Scenarios with complex dependencies can make hooks and waves more cumbersome to configure, especially when they depend on specific resources. The sync order alone may not be able to handle the assumptions of dependencies.

How do I post-process a specific resource?

At the moment there are three practical options:

1. Manage specific resources in a separate application

By separating the ArgoCD Application, you can manage specific resources as separate ArgoCD Applications, making it easier to perform post-processing with 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.
Although this approach is the most efficient, it cannot be used in scenarios with complex dependencies and can lead to additional complexity.

2. How ArgoEvents and CustomResource work

By using ArgoEvents, you can execute processes using specific events (resource updates, updates to specific spec values, etc.) as triggers. ArgoEvents is a powerful tool. It can be configured in detail even for CustomResources 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 instead manage a KubeVela Application.KubeVela Application WorkflowIt is recommended that you use the following.
apply-component
Below is an example of triggering Github Actions via API after applying the KubeVela Application.

3. Using Notification Controller

Originally, the ArgoCD 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 Status configuration of the ArgoCD Application.
status.operationState.syncResult.resources[]
status.operationState.syncResult

Conclusion


In this article, we have 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 are interested, please contact us here.
 
SRG runs a podcast where we chat about the latest hot topics in IT and books. We hope you will enjoy listening to it while you work.