Skip to content

Fix failed rollback after canceled health check - #1605

Merged
matheuscscp merged 1 commit into
fluxcd:mainfrom
ruslan-shaydullin:fix/1557-skip-remediation-when-enqueued
Oct 11, 2026
Merged

matheuscscp merged 1 commit into
fluxcd:mainfrom
ruslan-shaydullin:fix/1557-skip-remediation-when-enqueued

Conversation

@ruslan-shaydullin

@ruslan-shaydullin ruslan-shaydullin commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes: #1557

With the CancelHealthCheckOnNewRevision feature gate enabled and rollback remediation configured, the rollback starts in the same reconciliation in which the health check was canceled, applies the previous release, waits on the already canceled interrupt context, fails with context canceled and leaves a second failed release in Helm storage. After a real timeout the rollback completes.

I added a check before the rollback: if the object is enqueued again, the reconciler requeues instead, regardless of why it was enqueued or why the upgrade failed, as it already does with IsObjectEnqueued when AtomicRelease returns an error. The canceled upgrade is still recorded as a failed release. The next reconciliation upgrades if the chart or values changed, otherwise it rolls back on a fresh interrupt context. An uninstall remediation is not interrupted by the cancellation and keeps running in the same reconciliation. Resources only in the canceled release stay after that upgrade, where the failed rollback used to remove them; with RetryOnFailure they stay on main too.

Comment thread internal/reconcile/atomic_release.go Resolved
Comment thread internal/controller_test/wait_test.go Resolved
Comment thread internal/controller_test/wait_test.go Resolved
@ruslan-shaydullin
ruslan-shaydullin force-pushed the fix/1557-skip-remediation-when-enqueued branch from 1748e27 to d94b6c5 Compare October 9, 2026 07:55
@matheuscscp

matheuscscp commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

@ruslan-shaydullin Rollback tests are good. Now lets please add full coverage for the uninstall remediation too. There are two positive-negative test case pairs for the remediation being the uninstall action. One positive-negative pair for uninstall as the (only option for) remediation of install, and one positive-negative pair for uninstall as the configured remediation of upgrade. By positive-negative I mean always the question "did release inputs change or not?", which we achieve by performing the stimulation steps we did differently in the two current tests (apply a fixed revision versus just a flux CLI-like reconcile).

I'm curious in seeing what will be the expected revision history and latest.Action for those four tests. @artem-nefedov please keep an eye on these too, these things will tell us the story so we can decide if the behavior is good/fixed or not.

@ruslan-shaydullin

Copy link
Copy Markdown
Contributor Author

Added the uninstall tests in a9073ec, one table test with the four cases. As in the rollback tests, the two cases of a pair differ only in the stimulus, a fixed chart revision or a reconcile request annotation. Both pairs set remediateLastFailure: true, otherwise the uninstall never runs with 0 retries.

Expected history and latest.Action, with main next to it:

Remediation Inputs changed With the guard: Helm history / latest On main: Helm history / latest
install → uninstall yes (fixed 2.0.0) 1 superseded, 2 deployed / upgrade 2.0.0 1 deployed / install 2.0.0
install → uninstall no empty / uninstall-remediation 1.0.0 same
upgrade → uninstall yes (fixed 3.0.0) 1 superseded, 2 failed, 3 deployed / upgrade 3.0.0 1 deployed / install 3.0.0
upgrade → uninstall no 1 failed / install 2.0.0 same

Unlike the rollback, the uninstall does not wait on the interrupt context (action.Uninstall drops the ctx, WaitForDelete has its own timeout), so on main it completes inside the canceled reconcile. With changed inputs main therefore tears the release down and installs the fixed chart from scratch; in the upgrade case the running 1.0.0 release is gone until 3.0.0 is up. With the guard it is an upgrade on top of the failed release, so covering the uninstall in the guard matters for exactly this case. With unchanged inputs the end state is the same, the uninstall just moves to the next reconcile, and those two tests pass on main too.

In the last case the controller installs the same failing chart again after the uninstall; that install fails and the release stalls. This is the existing state machine, the test only sets spec.install.timeout: 10s so that it happens within the test.

Since the "wait would fail" part of the guard comment only holds for the rollback, I reworded it in 7bc2777: for the uninstall the point is not to tear the release down when the new request may have changed the inputs.

@matheuscscp

Copy link
Copy Markdown
Member

Cool, I think uninstall behavior is maybe not ideal but it's due to how Helm works.

@matheuscscp matheuscscp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! 🚀

Thanks @ruslan-shaydullin!

@matheuscscp

Copy link
Copy Markdown
Member

@ruslan-shaydullin Please squash all commits into one and force-push so I can merge 🙏

@artem-nefedov

Copy link
Copy Markdown

I tested this build on live cluster, and the behavior seems correct. When revision gets canceled due to new revision, only one failed revision is recorded in history. No rollbacks in between. If revision times out normally, it still rolls back as expected. Retry count still works correctly, e.g. with upgrade retries: 1 in scenario A(success) -> B(canceled) -> C(failure), C will rollback to A, then retry upgrade once and rollback to A again.

To be clear: I tested by changing values. I still don't fully understand the "release inputs not changed" scenario and how it can occur.

@matheuscscp

Copy link
Copy Markdown
Member

Changing values is perfectly a way to "change release inputs" 👍 Thanks @artem-nefedov!

@artem-nefedov

Copy link
Copy Markdown

Also, if last revision C is deployed / Rollback to A, changing helmrelease values to match those of A does not create new revision in the history, but changes helmrelease state from ready:false / Rolled back to A to ready:true / Upgraded to C. I think this is OK. Just FYI.

@ruslan-shaydullin
ruslan-shaydullin force-pushed the fix/1557-skip-remediation-when-enqueued branch from 7bc2777 to a37ae57 Compare October 10, 2026 15:51
@artem-nefedov

Copy link
Copy Markdown

I also tested scenario where many revisions are canceled in a row (more than revision history limit). Since each cancel creates a failed revision in history, I was concerned that there will be nothing to rollback to. Thankfully, that's not the case. The last successful revision is always preserved in history, so rollback works fine.

While the actual behavior is already correct, I don't think there's a test for this scenario. Would be nice to add it. The closest I found is TestSnapshots_Truncate from api/v2/snapshot_types_test.go, but it doesn't look like it tests this specific scenario.

@matheuscscp

Copy link
Copy Markdown
Member

Good call @artem-nefedov! Please add this test @ruslan-shaydullin 🙏

@artem-nefedov

Copy link
Copy Markdown

One potentially concerning scenario that may need consideration with CancelHealthCheckOnNewRevision and remediation strategy:

  1. Create new helmrelease
  2. Before first install finishes, interrupt it with a bad revision that times out naturally

Here, we have nothing to rollback. Current behavior is: second revision is treated as an upgrade, and is only attempted once. After that, helmrelease is stuck in failed state until new revision appears. upgrade.remediation.retries value is ignored! (I assume because it can't retry from upgrade without rollback)

Is this correct behavior? I would argue, it would've been better if it behaved same as fresh install if retries are defined (uninstall + try again from scratch, which would reset revisions/retries). That's how it works without CancelHealthCheckOnNewRevision, and it is one scenario where this feature gate might be detrimental. But then again, it's somewhat of a niche scenario.

@matheuscscp

matheuscscp commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

@artem-nefedov Once again, great catch 🥇 🥇 🥇

This means we have to go back to what this PR was doing initially and bail only for rollbacks. But not the way it was being done before with .(*Rollback). Like this instead:

if next.Type() == ReconcilerTypeRemediate &&
    req.Object.GetActiveRemediation().GetStrategy() == v2.RollbackRemediationStrategy {
    //...
}

@ruslan-shaydullin pls 🙏

Important note here: when we introduced health check cancellation, we deliberately decided to opt uninstalls out from the cancellable context. Interrupting an uninstall is not something we need to do ever. So bailing only for rollback is the right solution, indeed!

@artem-nefedov Very important question for you stemming from all these developments: Do you explicitly opt into RemediateOnFailure, or do you use the defaults? What does .spec.install and .spec.upgrade look like in your HelmReleases? Reason for the questions: I'm considering flipping the default value of these two feature gates to true in Flux 2.11 next year:

  • CancelHealthCheckOnNewRevision
  • DefaultToRetryOnFailure

I understand this only makes you half happy as you are using the cancellation one but you want the opposite of the other one. However, we have reached the conclusion that RetryOnFailure is the best default behavior for Flux, and it's a breaking change we need to do in coordination with the community. I'm considering printing a warning in helm-controller's main.go to say those gates will be flipped in Flux 2.11 next year in case the user has not set them to true, encouraging them to do it in preparation for the upcoming breaking change.

Why is RetryOnFailure the best default: It's the simpler one, that matches kustomize-controller behavior. It assumes that simply applying the release with either new values or a new chart solves all the problems, remediations are unneeded. As you can see, remediation is a difficult framework to deal with, and it's therefore a feature for advanced users like you. So it should be opt-in.

With CancelHealthCheckOnNewRevision enabled, a new reconcile request
cancels the health check of a running upgrade and the upgrade fails.
If rollback remediation is configured, the rollback starts in the
same reconcile: it applies the previous manifests, waits on the
already canceled context, fails as well and leaves a second failed
revision behind.

Requeue instead of starting the rollback when the object has been
enqueued again. The next reconcile upgrades if the chart or values
changed, otherwise it rolls back on a fresh context. An uninstall
remediation is not interrupted and keeps running in the same
reconcile.

Cover rollback and uninstall remediation after a canceled health
check, each with a fixed chart revision and with a reconcile request
that leaves the chart and values unchanged, and a rollback after
more canceled upgrades than the history limit keeps.

Signed-off-by: Ruslan Shaydullin <[email protected]>
@ruslan-shaydullin
ruslan-shaydullin force-pushed the fix/1557-skip-remediation-when-enqueued branch from a37ae57 to dcf0cae Compare October 11, 2026 07:18
@ruslan-shaydullin

Copy link
Copy Markdown
Contributor Author

Done in dcf0cae: the check now bails only for the rollback strategy, written the way you suggested, and the comment says so. The uninstall tests expect the uninstall to run in the canceled reconcile: with a fixed revision the fixed chart is then installed as a new release, with a reconcile request the end state is the same as before.

@artem-nefedov thanks for testing. I added TestHelmReleaseReconciler_CancelHealthCheckWithRemediationBeyondMaxHistory for your scenario, with maxHistory: 2 to keep it short: three failing revisions canceled one after another, then a reconcile request. Before the request the storage holds 1 deployed, 4 pending-upgrade, since Helm never prunes the deployed release. The rollback record then prunes the failed 4, so the end state is 1 superseded, 5 deployed with latest.Action rollback to 1.0.0.

@artem-nefedov

Copy link
Copy Markdown

I'm not sure this would fix the problem I described in #1605 (comment), because uninstall and reinstall retries already worked properly as long as history has only 1 ("install") revision. The difference is that without CancelHealthCheckOnNewRevision its always only one revision in history in this scenario, while with CancelHealthCheckOnNewRevision a second "upgrade" revision is added, which doesn't follow uninstall path.

I'm not able to test the new change right now, but might be able to later.

@artem-nefedov

Copy link
Copy Markdown

@matheuscscp We do explicitly define remediation strategy. You can see how .spec.install and .spec.upgrade look in the description of #1557. Some apps also have upgrade retries set to 0 with remediateLastFailure: true. And there's also upgrade.cleanupOnFail=true sometimes set and sometimes not (I think it should be set everywhere, we just forgot).

We do also have apps with RetryStrategy explicitly enabled, but those are not in the scope of this issue.

@matheuscscp

Copy link
Copy Markdown
Member

Sounds good @artem-nefedov, thanks for the info! It already helps.

@artem-nefedov

Copy link
Copy Markdown

I tested the latest build. Turns out, I was wrong in my assumptions in #1605 (comment). The change did help. Now, if new revision is added before install has finished, it immediately triggers uninstall and reinstall from scratch. I think this is good.

Overall, I have no remaining concerns and I believe it's good to merge.

@matheuscscp matheuscscp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! 🚀

Thanks very much for making Flux better @ruslan-shaydullin @artem-nefedov!

@matheuscscp
matheuscscp merged commit 6424a09 into fluxcd:main Oct 11, 2026
6 checks passed
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.

CancelHealthCheckOnNewRevision starts and interrupts extra rollbacks

3 participants