Skip to content

feat(chart): keep MongoDB data on uninstall - #427

Merged
uniqueg merged 6 commits into
devfrom
feat/mongodb-keep
Oct 23, 2026
Merged

uniqueg merged 6 commits into
devfrom
feat/mongodb-keep

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #425.

helm uninstall no longer deletes the bundled MongoDB's database volume (maintainer decision on a finding declined in #422):

  • The shared helper foca-common.persistentVolumeClaim gets persistence.resourcePolicy, named as in Bitnami's charts: keep annotates the claim with helm.sh/resource-policy: keep, and empty leaves the annotation off. It is in the shared schema definition, so both mongodb.persistence and broker.persistence have it.
  • Defaults (also if the key or the map is not set, e.g., values written for 0.1.0): keep for MongoDB, empty for the broker, whose queue can be rebuilt. Operators turn it off with mongodb.persistence.resourcePolicy: "".
  • Helm keeps the claim on uninstall, and on an upgrade that removes it from the release (disabling persistence or switching to existingClaim). Upgrades check the live claim, so releases from 0.1.0 are protected only after their first upgrade to 0.2.0. The docs and the migration notes say so.
  • Reinstalling a release of the same name and namespace adopts the kept claim: its ownership annotations still match, and the CI step below checks this. A release with other resource names gets a new claim. One of another release name but the same resource names (e.g., the same fullnameOverride) fails on the kept claim, unless it sets it as its existingClaim.
  • The only snapshot changes are the annotation on MongoDB's claim. The broker's output is unchanged.
  • Docs: MongoDB data note in docs/helm-chart.md (keep, removal by hand, reinstall, upgrade order), the template table row, the MongoDB persistence section of docs/migration-0.17.md, and the petstore values.

CI

  • New kind step "Check that MongoDB's data survives a reinstallation". It installs release petstore-keep with ci/test-values.yaml and writes a document with mongosh. It then uninstalls the release (--wait) and checks that the claim (UID) is kept. After a reinstall, it checks that the claim is the same and that the document is still there, and runs helm test (the app user is still there, as the init script does not run again).
  • ct install runs with --skip-clean-up, so it never uninstalls, and the kind cluster is discarded with the runner, so kept claims leave nothing behind.

Verification

  • pytest charts/foca-common/tests (Helm 3.22.1): 196 passed. Snapshots updated: only the MongoDB claim's annotation.
  • helm lint --strict on the library chart, and on the petstore chart with every ci/*-values.yaml: pass.
  • ruff check, ruff format --check, mypy and mkdocs build --strict: pass.
  • CI: all checks pass, including the new kind step "Check that MongoDB's data survives a reinstallation" (success).

Declined review findings

  • A generic persistence.annotations instead of resourcePolicy: resourcePolicy is the common-chart convention for exactly this. Arbitrary annotations can be added later if needed.

  • Sourcery, "Legacy claims are deleted during upgrade": on upgrade, Helm checks the live claim's annotation (it refreshes each removed resource with info.Get() before checking it), not the old manifest. Annotating the claim by hand before the upgrade therefore protects it. The rollback and empty-resourcePolicy cases Sourcery raised are now documented.

Open questions

None.

🤖 Generated with Claude Code

uniqueg and others added 5 commits October 23, 2026 04:15
Add persistence.resourcePolicy (keep or empty, as in common charts) to
the shared PVC helper: with keep, the claim is annotated with
helm.sh/resource-policy: keep, so that Helm keeps it when the release
is uninstalled or the claim is removed from it. It defaults to keep for
MongoDB (also for values without it, as for 0.1.0) and to empty for
the broker, whose queue can be rebuilt. A new release of the same name
and namespace adopts the kept claim.

Closes #425

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Install a release on kind, write a document to MongoDB, uninstall and
reinstall the release, and check that the claim is the same, the
document is still there and the app user works (helm test).

Refs #425

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Helm checks the keep annotation of the live claim on upgrades, so a
release of 0.1.0 is protected only after its first upgrade to 0.2.0:
say so in the docs, values and schema, and in the migration notes,
which also describe uninstalling and reinstalling. A release of
another name gets a new claim instead of failing. Test the broker's
resourcePolicy in the schema, and make the CI check of MongoDB's data
take only mongosh's last line.

Refs #425

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A release of another name with the same resource names (e.g., the same
fullnameOverride) fails on the kept claim instead of getting a new one.
Set pipefail in the CI check of MongoDB's data, so that failures of
kubectl exec are reported as such.

Refs #425

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR introduces a shared persistence.resourcePolicy mechanism that emits Helm's keep annotation, defaults MongoDB claims to survive uninstall while leaving broker claims unchanged, and validates the behavior through template tests, CI reinstallation checks, schema updates, and operational documentation.

Sequence diagram for MongoDB data surviving Helm reinstallation

sequenceDiagram
    participant CI
    participant Helm
    participant Kubernetes
    participant MongoDB

    CI->>Helm: helm install petstore-keep
    Helm->>Kubernetes: Create PVC with helm.sh/resource-policy: keep
    Kubernetes-->>Helm: PVC ready
    CI->>MongoDB: mongosh db.getSiblingDB(...).insertOne(...)
    MongoDB-->>CI: Document stored
    CI->>Helm: helm uninstall petstore-keep --wait
    Helm->>Kubernetes: Delete release resources
    Kubernetes-->>Helm: Kept MongoDB PVC remains
    CI->>Helm: helm install petstore-keep
    Helm->>Kubernetes: Adopt existing PVC
    Kubernetes-->>Helm: Same PVC and data available
    CI->>MongoDB: mongosh ...countDocuments(...)
    MongoDB-->>CI: Document still present
    CI->>Helm: helm test petstore-keep
Loading

File-Level Changes

Change Details Files
Adds configurable Helm resource-retention behavior for persistent volume claims, defaulting MongoDB to keep its claim and broker storage to the existing delete behavior.
  • Adds persistence.resourcePolicy to the shared schema and persistence helper.
  • Applies helm.sh/resource-policy: keep when the policy is keep, including component-specific defaults.
  • Defaults MongoDB to keep for backward-compatible values and broker to empty; supports explicitly disabling retention.
  • Adds template tests and updates snapshots to verify annotations and schema validation.
charts/foca-common/app-values.schema.json
charts/foca-common/templates/_helpers.tpl
charts/foca-common/templates/_mongodb.tpl
charts/foca-common/templates/_broker.tpl
charts/foca-common/tests/test_templates.py
charts/foca-common/tests/__snapshots__/celery.yaml
charts/foca-common/tests/__snapshots__/default.yaml
charts/foca-common/tests/__snapshots__/http-route.yaml
charts/foca-common/tests/__snapshots__/ingress-https.yaml
charts/foca-common/tests/__snapshots__/route.yaml
Adds end-to-end coverage proving MongoDB storage and data survive uninstall and same-name reinstallation.
  • Installs a test release, writes a MongoDB document, and records the PVC UID.
  • Verifies uninstall preserves the PVC, reinstall re-adopts the same claim, and the document remains available.
  • Runs helm test after reinstall to validate the existing application user and restored database state.
.github/workflows/charts.yml
Documents MongoDB retention, manual deletion, reinstall adoption, resource-name conflicts, and upgrade-order requirements.
  • Explains default retention and the empty policy override for MongoDB and broker claims.
  • Documents that 0.1.0 claims are protected only after an upgrade applies the annotation, unless operators annotate them first.
  • Updates migration guidance, chart resource tables, and example values.
docs/helm-chart.md
docs/migration-0.17.md
examples/petstore/chart/values.yaml
examples/petstore/chart/values.schema.json

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (29af8a6) to head (b983570).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #427   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         2869      2869           
=========================================
  Hits          2869      2869           
Flag Coverage Δ
test_unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review October 23, 2026 02:43

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="docs/helm-chart.md" line_range="515-516" />
<code_context>
+    the claim of a release installed with foca-common 0.1.0 (or with an
+    empty `resourcePolicy`) is annotated only by the next upgrade: disable
+    persistence or switch to an `existingClaim` in a later upgrade, or
+    annotate the claim first (`kubectl annotate pvc my-app-mongodb
+    helm.sh/resource-policy=keep`). With an empty `resourcePolicy`, Helm
+    deletes the claim (and, with the usual reclaim policy `Delete`, the data)
+    with the release or when it is removed from it. The broker's claim is not
</code_context>
<issue_to_address>
**Legacy claims are deleted during upgrade**

When a release installed before this change annotates its live PVC and removes that PVC from the chart in the same upgrade, `helm upgrade` evaluates the old release manifest, which lacks the keep annotation, when deleting the PVC; annotating only the live claim does not protect it, so the claim and, with the usual `Delete` reclaim policy, its data are removed.

Require a separate upgrade that records the keep annotation in the release manifest before removing the PVC, and remove the manual-annotation alternative unless it is verified to protect this upgrade path.

Also at `docs/migration-0.17.md:167-168`.
</issue_to_address>

### Comment 2
<location path="charts/foca-common/templates/_helpers.tpl" line_range="424" />
<code_context>
     {{- include "foca-common.labels" (dict "root" .root "component" .component) | nindent 4 }}
+  {{- if eq (toString $resourcePolicy) "keep" }}
+  annotations:
+    helm.sh/resource-policy: keep
+  {{- end }}
 spec:
</code_context>
<issue_to_address>
**Rollback removes the keep annotation**

When an operator rolls back to a release revision rendered before this annotation and later uninstalls the release, the rollback restores a manifest without the annotation and removes it from the live PVC, so a later uninstall deletes the claim and, with the usual `Delete` reclaim policy, its data.

Document that operators must reapply the keep annotation after such a rollback and before uninstalling.
</issue_to_address>

### Comment 3
<location path="docs/helm-chart.md" line_range="517" />
<code_context>
+    persistence or switch to an `existingClaim` in a later upgrade, or
+    annotate the claim first (`kubectl annotate pvc my-app-mongodb
+    helm.sh/resource-policy=keep`). With an empty `resourcePolicy`, Helm
+    deletes the claim (and, with the usual reclaim policy `Delete`, the data)
+    with the release or when it is removed from it. The broker's claim is not
+    kept by default, as its queue can be rebuilt
</code_context>
<issue_to_address>
**Kept claims survive removal**

When an upgrade removes persistence or switches to an existing claim while changing `resourcePolicy` from `keep` to empty, helm uses the previous release manifest’s keep annotation when removing the PVC, so it retains the claim despite the empty setting and the operator’s attempt to delete its data leaves the PVC behind.

Document that operators must remove the keep annotation in a separate upgrade before removing persistence or switching to an existing claim.

Also at `examples/petstore/chart/values.yaml:206`, `charts/foca-common/templates/_helpers.tpl:415`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 3 findings to address first, and MongoDB claims get helm.sh/resource-policy: keep by default, so uninstalling a release can leave its database data behind instead of deleting it. This changes the default data-retention policy for every affected release, and reverting the chart will not remove claims already kept.

Blocking findings: docs/helm-chart.md:516, charts/foca-common/templates/_helpers.tpl:424, docs/helm-chart.md:517


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread docs/helm-chart.md Resolved
Comment thread docs/helm-chart.md Resolved
A rollback to a revision without the keep annotation removes it from
MongoDB's claim, and emptying resourcePolicy only takes effect on the
claim's removal in a later upgrade.

Refs #425

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit a629254 into dev Oct 23, 2026
22 checks passed
@uniqueg
uniqueg deleted the feat/mongodb-keep branch October 23, 2026 02:56
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.

1 participant