Skip to content
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

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

Open
wants to merge 20 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 <dan@codefresh.io>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
…#22211)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
…rgoproj#22104) (argoproj#22208)

Signed-off-by: Andrii Korotkov <andrii.korotkov@verkada.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
argoproj#22117)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ishita Sequeira <46771830+ishitasequeira@users.noreply.github.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
…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 deployed on Bunnyshell

Component Endpoints
argocd https://argocd-4blfmh.bunnyenv.com/
argocd-ttyd https://argocd-web-cli-4blfmh.bunnyenv.com/

See: Environment Details | Pipeline Logs

Available commands (reply to this comment):

  • 🔴 /bns:stop to stop the environment
  • 🚀 /bns:deploy to redeploy the environment
  • /bns:delete to remove the environment

pedro-ribeiro-rci and others added 4 commits March 10, 2025 17:28
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
Copy link

codecov bot commented Mar 10, 2025

Codecov Report

Attention: Patch coverage is 53.64583% with 89 lines in your changes missing coverage. Please review.

Project coverage is 55.87%. Comparing base (111cf2c) to head (0071ca3).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
controller/hook.go 52.34% 51 Missing and 10 partials ⚠️
controller/appcontroller.go 46.15% 19 Missing and 9 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #22288      +/-   ##
==========================================
- Coverage   55.91%   55.87%   -0.04%     
==========================================
  Files         343      343              
  Lines       57305    57487     +182     
==========================================
+ Hits        32042    32121      +79     
- Misses      22623    22705      +82     
- Partials     2640     2661      +21     

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

hookHealth = &health.HealthStatus{
Status: health.HealthStatusHealthy,
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary? I don't know if assuming healthy for when there's no health would not have any undesired side effects.

Copy link
Author

Choose a reason for hiding this comment

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

For pre-delete hooks, defaulting to "Healthy" when no health check is defined prevents the process from getting stuck waiting for an undefined condition.
This approach is consistent with how we handle other hook types on the code.

@@ -86,7 +85,7 @@ func Test_deepCopyAppProjectClient_List(t *testing.T) {
{name: "Error listing app project", fields: fields{
AppProjectInterface: func() clientset.AppProjectInterface {
appProject := mocks.AppProjectInterface{}
appProject.On("List", context.Background(), metav1.ListOptions{}).Return(nil, errors.New("error"))
appProject.On("List", t.Context(), metav1.ListOptions{}).Return(nil, errors.New("error"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to rebase, this was fixed in another PR

Copy link
Author

Choose a reason for hiding this comment

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

Rebased to latest upstream master

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like rebase wasn't fully done or wasn't to the latest master, since these changes are still here.

aali309 and others added 8 commits March 10, 2025 20:16
)

Signed-off-by: Atif Ali <atali@redhat.com>
Co-authored-by: Dan Garfield <dan@codefresh.io>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
Signed-off-by: Pedro Ribeiro <pedro.ribeiro@cross-join.com>
destCluster, err := argo.GetDestinationCluster(context.Background(), app.Spec.Destination, ctrl.db)
if err != nil {
logCtx.Warnf("Unable to get destination cluster: %v", err)
logCtx.Warnf("Unable to locate cluster URL for Application being deleted: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this accurate? What if we can locate a URL, but there's some other error getting the cluster?

app.UnSetCascadedDeletion()
app.UnSetPostDeleteFinalizer()
if app.HasPreDeleteFinalizer() {
app.UnSetPreDeleteFinalizer()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why doesn't app.UnSetPostDeleteFinalizer() require such a check?

postDeleteHooks = map[string]string{
"argocd.argoproj.io/hook": postDeleteHook,
"helm.sh/hook": "post-delete",
}
)

func isHook(obj *unstructured.Unstructured) bool {
return hook.IsHook(obj) || isPostDeleteHook(obj)
return hook.IsHook(obj) || isPostDeleteHook(obj) || isPreDeleteHook(obj)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are pre- and post- delete hooks are handled separately?

return true, nil
}

func (ctrl *ApplicationController) executePostDeleteHooks(app *appv1.Application, proj *appv1.AppProject, liveObjs map[kube.ResourceKey]*unstructured.Unstructured, config *rest.Config, logCtx *log.Entry) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we unify some code with post-detele hooks?

@@ -86,7 +85,7 @@ func Test_deepCopyAppProjectClient_List(t *testing.T) {
{name: "Error listing app project", fields: fields{
AppProjectInterface: func() clientset.AppProjectInterface {
appProject := mocks.AppProjectInterface{}
appProject.On("List", context.Background(), metav1.ListOptions{}).Return(nil, errors.New("error"))
appProject.On("List", t.Context(), metav1.ListOptions{}).Return(nil, errors.New("error"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like rebase wasn't fully done or wasn't to the latest master, since these changes are still here.

(httpsValues.type != 'git' && 'Bearer token is only supported for Git BitBucket Data Center repositories'),
tlsClientCertKey: !httpsValues.tlsClientCertKey && httpsValues.tlsClientCertData && 'TLS client cert key is required if TLS client cert is given.'
(validURLValues.password && validURLValues.bearerToken && 'Either the password or the bearer token must be set, but not both.') ||
(validURLValues.type != 'git' && 'Bearer token is only supported for Git BitBucket Data Center repositories.')
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are these changes needed?

private isHTTPSUrl(url: string) {
if (url.match(/^https:\/\/.*$/gi)) {
// Whether url is a http or https url
private isHTTPOrHTTPSUrl(url: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we separate these changes into a different PR, please?

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