Merge profiles recorded before 1.0 when their recording is deleted #3552
<a href="https://easycla.lfx.linuxfoundation.org/#/?version=2"><img src="https://s3.amazonaws.com/cla-project-logo-prod/cla-signed.svg?v=2" alt="CLA Signed" align="left" height="28" width="328" ></a><br >The committers listed above are authorized under a signed CLA.<ul><li>:white_check_mark: login: Vincent056 / name: Vincent Shen (9a4da598334abe2f44da49458ccf346a26219e68)</li></ul>
Codecov Report
:x: Patch coverage is
78.44311% with 36 lines in your changes missing coverage. Please review.:white_check_mark: Project coverage is 83.04%. Comparing base (
470f2d8) to head (7c194a2).:x: Your patch status has failed because the patch coverage (78.44%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## main #3552 +/- ##
==========================================
- Coverage 83.15% 83.04% -0.11%
==========================================
Files 183 184 +1
Lines 16399 16562 +163
==========================================
+ Hits 13636 13754 +118
- Misses 2763 2808 +45
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
This selector doesn't require spo.x-k8s.io/partial=true, so it also matches installed profiles that 0.x merged (they carry recording-id too). If the 0.x recording ns-b/foo produced foo-nginx and was deleted, then creating and deleting ns-a/foo after the upgrade labels foo-nginx with recording-namespace=ns-a. A later ns-b/foo then fails CheckRecordingOwner with ErrProfileOwnedByOtherRecording, although it passes today. Could the selector be derived from partialProfileLabels (keep name and partial, swap the namespace for a DoesNotExist requirement), so the two selectors can't drift?
sameName only counts recordings that still exist. Suppose ns-a/foo and ns-b/foo both have unmerged 0.x partial profiles. Deleting ns-b/foo first sees 2, so it warns and gets released, but its partial profiles stay. Deleting ns-a/foo later sees 1 and adopts and merges the partial profiles of both namespaces into ns-a's profile, which then allows another workload's syscalls.
After returning 0 here, Reconcile goes on to mergeProfiles, which finds no namespaced partial profiles, emits NoPartialProfiles and releases the recording at :225. The legacy partial profiles are then orphaned, which is the problem this PR is fixing, and the second event is misleading. A same-named recording created after the legacy profiles can't own them. Could the count skip recordings whose creationTimestamp is later than the profiles'? That would settle the most likely post-upgrade case.
This relies on the cache showing the new labels within adoptRequeueDelay. If it doesn't, the next reconcile lists the same unlabeled profiles, patches them again and requeues. Did you consider a one-time adoption at manager startup instead? Every recording would still exist at that point, which would also settle the ordering problem from :347, where a recording deleted earlier no longer counts toward the ambiguity check.
This lists from the cache, which only covers the watched namespaces when WATCH_NAMESPACE or the namespace restriction is set. Profiles are cluster-scoped and are still listed in full, so a same-named recording outside the watched namespaces isn't counted and its legacy profiles can be adopted.
Could you add cases for (a) a non-partial legacy profile with the same recording-id, which shouldn't be relabeled; (b) stale legacy partial profiles from a same-named recording that is already deleted; and (c) the ambiguous case, checking that the partial profiles aren't orphaned after the release?
Approved these changes.
[APPROVALNOTIFIER] This PR is APPROVED
This pull-request has been approved by: <a href="https://github.com/kubernetes-sigs/security-profiles-operator/pull/3552#pullrequestreview-5438047409" title="Approved">saschagrunert</a>, <a href="https://github.com/kubernetes-sigs/security-profiles-operator/pull/3552#" title="Author self-approved">Vincent056</a>
The full list of commands accepted by this bot can be found here.
The pull request process is described here
<details >
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
</details>
<!-- META={"approvers":[]} -->
If this times out, Start retries adoptLegacyPartialProfiles, which lists from the API server and finds no unlabeled profiles anymore. adopted is then empty, the wait is skipped and legacyAdoptionPending gets cleared while the cache may still not show the labels. A merge in that window misses the profiles, releaseRecording still sees them through the API reader and keeps the finalizer, and nothing reconciles the recording again until the next resync. Unlikely with a two minute timeout, but keeping the adopted profiles across retries (or waiting on them in Start) would close it.
A held recording emits this warning every legacyHoldWait for as long as it is held, which can be indefinitely. Event aggregation limits the noise, but nit: could it be emitted only when the set of held profiles changes, or once per hold?
Not a blocker, but worth a note in the docs: stale partial profiles left by a failed 0.x merge of a deleted recording are still adopted by an older recording with the same name, because the creation time can't tell them apart. Mentioning that such profiles can be labeled or deleted before the upgrade would help.
Thanks for the rework, the startup adoption settles the ordering and cache issues nicely. Could you update the PR description to the new design? It still describes labeling on deletion, the requeue after labeling and the "exactly one recording has that name" rule, which will end up in the merge record. The 1.0.x note probably needs a second look as well.
saschagrunert
Vincent056
What type of PR is this?
/kind bug
What this PR does / why we need it:
Since 1.0, recorded profiles carry the
spo.x-k8s.io/recording-namespacelabel, and the recording merger selects a recording's partial profiles byrecording-idandrecording-namespace(listPartialProfiles,hasPartialProfiles). Profiles recorded before 1.0 only carryrecording-id. So after an upgrade from 0.x, the partial profiles of a recording that was started before the upgrade are invisible to the merge:main, deleting the recording releases it without merging them, so the partial profiles are left behind unmerged;handleRecordingFinalizeralso returns early without the namespace label), the recording stays in deletion forever with thespo.x-k8s.io/has-unmerged-profilesfinalizer.This PR adopts such profiles when the recording is being deleted. It labels the recording's profiles that have no
recording-namespacewith the recording's namespace, and merges them on the next reconcile, once the cache shows the labels. Profiles are cluster-scoped, so the name alone can't tell recordings in different namespaces apart. The profiles are only adopted when exactly one recording has that name; otherwise a warning event explains why they aren't merged.Which issue(s) this PR fixes:
None
Does this PR have test?
Yes. There are unit tests for the merger: profiles recorded before 1.0 get labeled on the first reconcile and merged on the second, profiles are left alone when the recording name is ambiguous, and profiles of other recordings are not touched. The main test fails without the fix.
The scenario was also reproduced on OpenShift 4.21 with an OLM upgrade from 0.10.1 to 1.0.1. A 0.10 recording with
mergeStrategy: containersleft two partial profiles, and after the upgrade deleting the recording hung. Adding therecording-namespacelabel to the partial profiles, as this change does, made 1.0.1 merge them within seconds into an installed profile and release the recording.Special notes for your reviewer:
main, it would then release the recording and orphan them.Does this PR introduce a user-facing change?
``
release-note
``Partial profiles recorded by a ProfileRecording before an upgrade to 1.0 are now merged when the recording is deleted, instead of being left unmerged or keeping the recording from being deleted.
🤖 Generated with Claude Code