Skip to content

fix: persist lastAppliedSpec only after successful reconcile - #2826

Open
vrutkovs wants to merge 2 commits into
masterfrom
lastAppliedSpec-after-success
Open

vrutkovs wants to merge 2 commits into
masterfrom
lastAppliedSpec-after-success

Conversation

@vrutkovs

Copy link
Copy Markdown
Contributor

Describe your changes

object.LastSpecUpdated() rewrites lastApliedSpec, but it needs to be saved only after successful reconciliation. When reconcile fails the object spec would be saved unconditionally and later reconcile attempts would not, for example, remove extra labels etc.

Checklist

  • Tests are added or updated.
  • The checks from CONTRIBUTING.md pass locally, and generated files are committed.
  • I follow the AI policy: I understand every change and can explain it. Pull requests that look like unreviewed AI output may be closed without review.
  • User-visible changes are described in docs/CHANGELOG.md under ## tip.

e2e tests

e2e tests don't run on every push. Comment /run-e2e on this pull request to run them,
or to run them again after new commits.
See CONTRIBUTING.md for details.

object.LastSpecUpdated() rewrites lastApliedSpec, but it needs to be saved only after successful
reconciliation
@vrutkovs
vrutkovs force-pushed the lastAppliedSpec-after-success branch from db05053 to 2d5341f Compare November 18, 2026 18:04

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

}
specChanged := object.LastSpecUpdated()
// keep previous lastAppliedSpec until success, next reconcile diffs against it to clean up
specChanged := object.DeepCopy().LastSpecUpdated()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The alternative is to have GetLastSpecUpdated and SetLastSpecUpdated, which would be cleaner, but more invasive

@Haleygo Haleygo 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.

From what I can see, the persisting lastAplliedSpec asap was introduced on purpose in 471f183:

Previously, operator update lastAppliedSpec only when object was successfully reconciled.
It made impossible to properly detect changes between prev and current object state. And diff was calculated incorrectly.
Since operator executes Update requests only on object diff, failed changes cannot be removed and rollback was not possible.

But the code seems to have changed since then: spec updates now diff desired against live and no longer depend on prev. The only remaining use of prev is the 3-way merge of labels and annotations, where a key is removed only if prev has it and the new spec doesn't. So the trade-off is limited to metadata:

Case Old: stored before reconcile New: stored after success
A → B fails → fixed or retried A's dropped labels and annotations remain indefinitely ❌ Removed ✅
A → B fails partway → reverted to A B's partially applied labels and annotations are removed ✅ Remain ❌

I'm OK with changing the behavior, since case 1 seems to be the more common path: a failed change usually gets fixed forward.
Please add the detailed reason for this change to the description so the context is complete to track.

cc @f41gh7 in case you have other reason for the current behavior.

@f41gh7

f41gh7 commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

There is no good way to fix. Both options have own problems. And yes, changes seems to be always go forward.

Currently, if update process interrupted in a middle, if it will continue from the last successful step.

For example, if there is a condition like - prevCR.Spec.RequestsLoadBalancer.Enabled -> do some logic. It will work correctly, if previous step failed ( with timeout).

In opposite, rollback of the changes could potentially fail, if it's interrupted. But with explicit oprhaned objects check it's not a problem most likely.

This branch has not been deployed

No deployments
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.

3 participants