✨ Include metadata in WorkApplier cache to detect external changes - #224
✨ Include metadata in WorkApplier cache to detect external changes#224mkolesnik wants to merge 1 commit into
Conversation
WalkthroughThe PR extends the WorkApplier cache to detect external metadata modifications (labels, annotations, ownerReferences) that don't bump the ChangesMetadata Drift Detection for WorkApplier Cache
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pkg/apis/work/v1/applier/workapplier_test.go`:
- Around line 178-191: The helper assertExternalMetadataChangeDetected currently
builds tamperedWork from newWork.DeepCopy(), which can reintroduce a
stale/default Generation and make the "patch" assertion pass for the wrong
reason; instead, fetch the existing object from the informer store (via
workInformerFactory.Work().V1().ManifestWorks().Informer().GetStore().Get/ByKey
for the same work), DeepCopy that informer object into tamperedWork, then apply
the tamper mutation so the live Generation/metadata from the informer is
preserved before calling Update and running workApplier.Apply; keep references
to assertExternalMetadataChangeDetected, newWork, tamperedWork,
workInformerFactory.Work().V1().ManifestWorks().Informer().GetStore().Update,
workApplier.Apply and fakeWorkClient.Actions when making this change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2e726a16-1b50-4445-92bd-cad80f59e718
📒 Files selected for processing (3)
pkg/apis/work/v1/applier/workapplier_test.gopkg/apis/work/v1/applier/workcache.gopkg/apis/work/v1/applier/workcache_test.go
The WorkApplier cache uses the desired work hash and the existing work's generation to skip redundant updates. However, metadata-only changes (labels, annotations, owner references) do not bump generation, so the cache misses external modifications to these fields. Store a hash of the desired work's metadata (labels, annotations, owner references) in the cache and compare it against the existing work's metadata on each apply. This ensures the cache invalidates when an external actor modifies metadata fields that the applier owns. Fixes: open-cluster-management-io#223 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Mike Kolesnik <mkolesni@redhat.com>
f95cb17 to
0833fa4
Compare
| value := cachedResource{ | ||
| resourceHash: hashOfResourceStruct(required), | ||
| generation: existing.Generation, | ||
| metadataHash: hashOfMetadata(required), |
There was a problem hiding this comment.
I think resourceHash has included metadata section already. Are you trying to detect if label/annotation of the exisiting mw is changed? If so, I would consider an annotatil/label merge when apply rather than replace.
There was a problem hiding this comment.
Thanks for the quick review!
You're right that resourceHash hashes the desired work, so it catches when we change our labels.
The problem is it doesn't catch when someone else changes labels on the existing work (see #223 which I reported).
When that happens, our desired hash stays the same and generation doesn't bump, so the cache skips and the external change is never corrected.
Re merge vs replace, that's a good point and worth discussing separately.
This PR just makes the cache consistent with what the applier already does when the cache misses.
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: mikeshng, mkolesnik The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
The WorkApplier cache uses the desired work hash and the existing work's generation to skip redundant updates. However, metadata-only changes (labels, annotations, owner references) do not bump generation, so the cache misses external modifications to these fields and
ManifestWorkEqualis never called.This stores a hash of the desired work's metadata in the cache and compares it against the existing work's metadata on each apply, ensuring the cache invalidates when an external actor modifies owned metadata fields.
Fixes: #223
Summary by CodeRabbit
Bug Fixes
Tests