Skip to content

feat: PreDelete hooks support (Issue #13975) #22288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: master
Choose a base branch
from

Conversation

pedro-cs-ribeiro
Copy link

Closes #13975

This PR introduces PreDelete hooks for Argo CD. PreDelete hooks allow users to execute tasks before an application's resources are deleted, providing a way to perform graceful shutdowns, backup operations, or other cleanup tasks that should happen before resource deletion.

Implementation details:

The implementation follows the same pattern established for PostDelete hooks:

  • Adds PreDeleteFinalizerName constant
  • Implements Pre/UnSet/HasPreDeleteFinalizer methods
  • Adds hook detection logic via isPreDeleteHook()
  • Implements hook execution methods via executePreDeleteHooks() and cleanupPreDeleteHooks()
  • Updates application controller to respect the proper deletion order

Checklist:

  • Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this does not need to be in the release notes.
  • The title of the PR states what changed and the related issues number (used for the release note).
  • The title of the PR conforms to the Toolchain Guide
  • I've included "Closes [ISSUE #]" or "Fixes [ISSUE #]" in the description to automatically close the associated issue.
  • I've updated both the CLI and UI to expose my feature, or I plan to submit a second PR with them.
  • Does this PR require documentation updates?
  • I've updated documentation as required by this PR.
  • I have signed off all my commits as required by DCO
  • I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
  • My build is green (troubleshooting builds).
  • My new feature complies with the feature status guidelines.
  • I have added a brief description of why this PR is necessary and/or what this PR solves.
  • Optional. My organization is added to USERS.md.
  • Optional. For bug fixes, I've indicated what older releases this fix should be cherry-picked into (this may or may not happen depending on risk/complexity).
ArgoCD_PreDelete_Hook.mov

todaywasawesome and others added 8 commits March 10, 2025 17:03
Signed-off-by: Dan Garfield <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
…#22211)

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: Pedro Ribeiro <[email protected]>
argoproj#22117)

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ishita Sequeira <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
…delete-hooks

# Conflicts:
#	go.mod
#	go.sum
#	ui-test/package.json
@pedro-cs-ribeiro pedro-cs-ribeiro requested review from a team as code owners March 10, 2025 17:15
Copy link

bunnyshell bot commented Mar 10, 2025

🔴 Preview Environment stopped on Bunnyshell

See: Environment Details | Pipeline Logs

Available commands (reply to this comment):

  • 🔵 /bns:start to start the environment
  • 🚀 /bns:deploy to redeploy the environment
  • /bns:delete to remove the environment

Signed-off-by: Pedro Ribeiro <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
Copy link

codecov bot commented Mar 10, 2025

Codecov Report

Attention: Patch coverage is 71.00000% with 29 lines in your changes missing coverage. Please review.

Project coverage is 56.09%. Comparing base (127eef9) to head (46cc5e0).
Report is 19 commits behind head on master.

Files with missing lines Patch % Lines
controller/appcontroller.go 48.97% 17 Missing and 8 partials ⚠️
controller/hook.go 89.74% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #22288      +/-   ##
==========================================
+ Coverage   56.06%   56.09%   +0.03%     
==========================================
  Files         343      343              
  Lines       57536    57615      +79     
==========================================
+ Hits        32257    32319      +62     
- Misses      22634    22640       +6     
- Partials     2645     2656      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

pedro-ribeiro-rci and others added 7 commits March 10, 2025 20:36
Signed-off-by: Pedro Ribeiro <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
Signed-off-by: Pedro Ribeiro <[email protected]>
@xrow
Copy link

xrow commented Mar 25, 2025

Yesterday I was implementing a workaround because of this missing feature. Now I think holy sh*t cow there it is.

@pedro-cs-ribeiro
Copy link
Author

@andrii-korotkov-verkada Thanks for the Review! I rebased the code correctly now and implemented some changes according to your review, please check.

Copy link
Collaborator

@leoluz leoluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution!
Please check my comments.

@@ -9,6 +9,7 @@ and after a Sync operation. Hooks can also be run if a Sync operation fails at a
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.
* Using a `PreDelete` hook to run tasks before an Application's resources are deleted. This can be useful for graceful shutdowns, backup operations, or other pre-deletion tasks. Similar to `PostDelete` hooks, `PreDelete` hooks are executed when the application is being deleted, and are managed separately from the application lifecycle.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I remove a resource from git and sync with prune option enabled? Will the PreDelete hook be executed? It is better to clarify in the docs in which exact conditions the hook gets triggered.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I updated the docs dcc55ec (#22288) to clarify the conditions where the hook gets triggered

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have unit-tests validating this logic

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the hook_test.go tests here: 33c2007 (#22288)

if app.HasPreDeleteFinalizer() {
objsMap, err := ctrl.getPermittedAppLiveObjects(destCluster, app, proj, projectClusters)
if err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please return the context alongside with errors.
Example:

return fmt.Errorf("error getting permitted app live objects: %w", err)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code to return the context now with the error: 6a864c8 (#22288)


done, err := ctrl.executePreDeleteHooks(app, proj, objsMap, config, logCtx)
if err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please return the context alongside with errors.
Example:

return fmt.Errorf("error getting permitted app live objects: %w", err)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code to return the context now with the error: 6a864c8 (#22288)

// PreDelete hooks are done - remove the finalizer so we can continue with deletion
app.UnSetPreDeleteFinalizer()
if err := ctrl.updateFinalizers(app); err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please return the context alongside with errors.
Example:

return fmt.Errorf("error getting permitted app live objects: %w", err)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code to return the context now with the error: 6a864c8 (#22288)

if app.HasPreDeleteFinalizer("cleanup") {
objsMap, err := ctrl.getPermittedAppLiveObjects(destCluster, app, proj, projectClusters)
if err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please return the context alongside with errors.
Example:

return fmt.Errorf("error getting permitted app live objects: %w", err)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code to return the context now with the error: 6a864c8 (#22288)


done, err := ctrl.cleanupPreDeleteHooks(objsMap, config, logCtx)
if err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please return the context alongside with errors.
Example:

return fmt.Errorf("error getting permitted app live objects: %w", err)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code to return the context now with the error: 6a864c8 (#22288)

@pedro-cs-ribeiro
Copy link
Author

Thanks for the review @leoluz ! I did some changes regarding your comments, PTAL

Signed-off-by: Pedro Ribeiro <[email protected]>
@pedro-cs-ribeiro pedro-cs-ribeiro requested a review from leoluz April 18, 2025 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pre-deletion resource hooks
6 participants