Skip to content

feat(chart): add persistence settings for MongoDB - #422

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

uniqueg merged 6 commits into
devfrom
feat/mongodb-persistence

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #419.

The bundled MongoDB of foca-common gets the persistence settings of the bundled broker (#411):

  • mongodb.persistence with the keys of broker.persistence: enabled, size, accessModes (ReadWriteOnce if not set), storageClass (the cluster's default if empty, - for no dynamic provisioning) and existingClaim.
  • On by default: persistence is enabled also without mongodb.persistence.enabled or the whole map (values written for 0.1.0), as MongoDB always had its claim. With enabled: false, the data is kept in an emptyDir limited to size, if set (e.g., for tests).
  • mongodb.volumeSize is a deprecated alias of mongodb.persistence.size. Rendering fails if both are set, with a message naming the fix (move the size to persistence.size; unset a volumeSize of the app chart's values with --set-json mongodb.volumeSize=null), so that neither value is silently ignored. A silently ignored override would also shrink the claim, which Kubernetes rejects.
  • The claim keeps its name (<app name>-mongodb), so upgrades keep the data. An integer volumeSize (bytes) is now rendered as an integer (1073741824 instead of 1.073741824e+09), the same quantity.
  • One shared helper renders both claims (foca-common.persistentVolumeClaim, with foca-common.claimName); the broker's templates call it, with unchanged output. Both schemas share a persistence definition.
  • The petstore example, the schemas, the tests and docs/helm-chart.md are updated: the MongoDB rows of the tables, and a new "MongoDB data" deployment note. The note warns that disabling persistence or switching to an existingClaim deletes the chart's claim on upgrade unless it is annotated with helm.sh/resource-policy: keep first.

Verification

  • pytest charts/foca-common/tests (Helm 3.22.1): 193 passed. The snapshots are unchanged, so the rendered broker and MongoDB resources of every snapshot case are identical.
  • helm lint --strict charts/foca-common, and of the petstore chart with every ci/*-values.yaml: pass.
  • ruff check, ruff format --check, mypy and mkdocs build --strict: pass.
  • 0.1.0 app values against the new library: I rendered the petstore chart of chart-v0.1.0 (with volumeSize and without mongodb.persistence) with the library of chart-v0.1.0 and with the library of this branch. I used the default values and each of its ci/*-values.yaml. MongoDB's resources are identical in every case; the only differences are the broker's StatefulSet from feat(chart): persist the bundled broker #411.
  • CI: all checks pass, including "Install petstore chart on kind" (with the new default MongoDB persistence) and "Lint and test charts".

Declined review findings

  • Let volumeSize win over (or lose to) persistence.size instead of failing (both reviewers): either order silently ignores one value in a plausible upgrade, an old override against new app chart defaults or an old app chart default against a new override. Failing names the fix, and the docs describe it.
  • Fail when existingClaim is the chart's own claim name (round 1). I added the check, then dropped it after round 2: it blocks the standard way to detach the claim from the release (annotate it with helm.sh/resource-policy: keep, then set it as existingClaim). The docs describe that procedure instead.
  • Add helm.sh/resource-policy: keep to MongoDB's claim by default: it would change the rendered output and the uninstall behaviour of existing releases. That would be a separate decision.
  • Sourcery: "PVC preservation instruction still deletes data". On upgrade, Helm reads the live object of each resource it removes and keeps it if the live object has the keep annotation. Only helm uninstall goes by the release manifest. The documented order (annotate the claim, then switch to existingClaim) is therefore safe.
  • Extract an "is set" helper for the two checks in foca-common.mongodbPersistence (low): there are only two uses, and they sit next to each other.

Open questions

None.

🤖 Generated with Claude Code

uniqueg and others added 5 commits October 23, 2026 02:57
Move the broker's PersistentVolumeClaim and claim name into the
generic helpers foca-common.persistentVolumeClaim and
foca-common.claimName, so that MongoDB can use them, too. The rendered
broker resources are unchanged (snapshot tests).

Refs #419

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add mongodb.persistence with the keys of broker.persistence (enabled,
size, accessModes, storageClass, existingClaim), rendered by the shared
PVC helper. Persistence stays on by default, also without the map (as
in values written for foca-common 0.1.0); without it, the data is kept
in an emptyDir limited to the size. The claim keeps its name.

mongodb.volumeSize is a deprecated alias of mongodb.persistence.size;
rendering fails if both are set, so that neither is ignored silently.
Both schemas share a persistence definition; the petstore example
switches to the new values.

Closes #419

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Name the fix in the error for a deprecated mongodb.volumeSize set
with mongodb.persistence.size, and document it, the emptyDir without
a size, and that disabling persistence or switching to an existing
claim deletes the chart's claim on upgrade, unless it is annotated
with helm.sh/resource-policy: keep first. Split the alias tests and
cover the emptyDir with the alias and without a size.

An integer mongodb.volumeSize (bytes) is now rendered as an integer
(e.g., 1073741824 instead of 1.073741824e+09), the same quantity.

Refs #419

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

Adds MongoDB persistence settings matching the broker’s configuration model, while preserving the default claim and name for existing releases. The implementation introduces shared Helm helpers, supports existing claims and an emptyDir fallback, migrates the deprecated volumeSize setting with explicit conflict errors, and updates schemas, examples, documentation, and tests.

Sequence diagram for MongoDB persistence settings rendering

sequenceDiagram
    participant Helm
    participant MongoDBTemplate
    participant PersistenceHelper
    participant Kubernetes
    Helm->>MongoDBTemplate: mongodbDeployment
    MongoDBTemplate->>PersistenceHelper: mongodbPersistence
    PersistenceHelper->>PersistenceHelper: claimName
    alt persistence enabled
        MongoDBTemplate->>PersistenceHelper: persistentVolumeClaim
        PersistenceHelper-->>Kubernetes: PersistentVolumeClaim and Deployment volume
    else persistence disabled
        MongoDBTemplate->>PersistenceHelper: emptyDir
        PersistenceHelper-->>Kubernetes: Deployment volume using emptyDir
    end
Loading

Flow diagram for MongoDB persistence volume selection

flowchart TD
    A[MongoDB deployment renders persistence settings] --> B{mongodb.persistence.enabled}
    B -->|unset| C[Persistence enabled by default]
    B -->|true| C
    B -->|false| D[Mount emptyDir]
    D --> E[Apply persistence.size as size limit if set]
    C --> F{existingClaim set}
    F -->|yes| G[Mount existingClaim]
    F -->|no| H[Render PersistentVolumeClaim]
    H --> I[Claim name app-mongodb]
    H --> J[Use persistence.size accessModes and storageClass]
Loading

File-Level Changes

Change Details Files
Adds configurable MongoDB persistence with backward-compatible defaults and a nonpersistent emptyDir fallback.
  • Introduces mongodb.persistence settings for enablement, size, access modes, storage class, and existing claims.
  • Preserves persistence by default when settings are absent, including values from chart version 0.1.0.
  • Uses a size-limited emptyDir when persistence is disabled.
  • Centralizes claim-name and PersistentVolumeClaim rendering for MongoDB and the broker.
charts/foca-common/templates/_mongodb.tpl
charts/foca-common/templates/_helpers.tpl
charts/foca-common/templates/_broker.tpl
Provides a guarded migration path from the deprecated MongoDB volumeSize setting.
  • Treats mongodb.volumeSize as an alias for mongodb.persistence.size, including integer byte quantities.
  • Fails rendering when both size settings are provided and reports the required migration steps.
  • Retains the existing <app name>-mongodb claim name for upgrade compatibility.
charts/foca-common/templates/_mongodb.tpl
charts/foca-common/app-values.schema.json
examples/petstore/chart/values.schema.json
Updates chart examples, documentation, and validation coverage for MongoDB persistence behavior.
  • Documents persistence, existing-claim migration, claim deletion risks, and the deprecated alias.
  • Updates petstore defaults and schemas to expose the new persistence configuration.
  • Adds tests for defaults, claim reuse, emptyDir behavior, aliases, validation, and error cases while preserving broker coverage.
charts/foca-common/tests/test_templates.py
examples/petstore/chart/values.yaml
docs/helm-chart.md
charts/foca-common/app-values.schema.json
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 (f74be0a) to head (7d566bb).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #422   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         2777      2777           
=========================================
  Hits          2777      2777           
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 01:33

@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 4 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="472-473" />
<code_context>
+  to an `existingClaim` for an existing release deletes the chart's claim
+  (and, with the usual reclaim policy `Delete`, its data) on upgrade. To
+  keep the claim, e.g., to manage it outside the release under its name,
+  annotate it first (`kubectl annotate pvc my-app-mongodb
+  helm.sh/resource-policy=keep`), then set it as `existingClaim`.
+
+    `mongodb.volumeSize`, the setting up to foca-common 0.1.0, is
</code_context>
<issue_to_address>
**PVC preservation instruction still deletes data**

When an operator follows the documented annotation step and then upgrades an existing release to use `mongodb.persistence.existingClaim`, the operator adds `helm.sh/resource-policy: keep` only to the live PVC, while Helm determines upgrade deletions from the prior release manifest. `mongodbPersistentVolumeClaim` omits the PVC after `existingClaim` is set, so Helm deletes the claim and, with the usual `Delete` reclaim policy, its data.

Ensure the keep annotation is present in a release manifest before removing the PVC, or provide a chart mechanism to add it and document the required preparatory upgrade.
</issue_to_address>

### Comment 2
<location path="charts/foca-common/templates/_mongodb.tpl" line_range="279" />
<code_context>
+{{- if and (not (kindIs "invalid" $size)) (ne (toString $size) "") -}}
+{{- fail "mongodb.persistence.size and the deprecated mongodb.volumeSize are both set: set only mongodb.persistence.size (rename volumeSize in your values; if the app chart's values set volumeSize, unset it with null, e.g., --set-json mongodb.volumeSize=null)" -}}
+{{- end -}}
+{{- $size = ternary $volumeSize (int64 $volumeSize | toString) (kindIs "string" $volumeSize) -}}
+{{- end -}}
+{{- dict "persistence" $persistence "name" (include "foca-common.mongodbFullname" .) "enabledByDefault" true "size" $size "key" "mongodb.persistence" | toJson -}}
</code_context>
<issue_to_address>
**Invalid legacy sizes reach Kubernetes**

When `mongodb.volumeSize` is malformed or nonpositive, `mongodbPersistence` assigns the value to `$size` without validating it, so `persistentVolumeClaim` emits an invalid PVC request or `emptyDir` size limit and Kubernetes rejects the release.

Validate the legacy value’s quantity and positivity before assigning it to `$size`.
</issue_to_address>

### Comment 3
<location path="charts/foca-common/templates/_mongodb.tpl" line_range="277" />
<code_context>
+{{- $volumeSize := get $mongodb "volumeSize" -}}
+{{- if and (not (kindIs "invalid" $volumeSize)) (ne (toString $volumeSize) "") -}}
+{{- if and (not (kindIs "invalid" $size)) (ne (toString $size) "") -}}
+{{- fail "mongodb.persistence.size and the deprecated mongodb.volumeSize are both set: set only mongodb.persistence.size (rename volumeSize in your values; if the app chart's values set volumeSize, unset it with null, e.g., --set-json mongodb.volumeSize=null)" -}}
+{{- end -}}
+{{- $size = ternary $volumeSize (int64 $volumeSize | toString) (kindIs "string" $volumeSize) -}}
</code_context>
<issue_to_address>
**Suggested unset command fails validation**

When an upgrade retains `mongodb.volumeSize` and the caller follows the suggested `--set-json mongodb.volumeSize=null` command, the app-values schema permits only strings and integers for `mongodb.volumeSize`, so Helm rejects `null` before `mongodbPersistence` can clear the legacy value and the documented upgrade command cannot render.

Allow `null` for the deprecated key in the app-values schema so the suggested command can clear it.

Also at `docs/helm-chart.md:481`.
</issue_to_address>

### Comment 4
<location path="charts/foca-common/tests/test_templates.py" line_range="855" />
<code_context>
+        "existingClaim": {
+          "type": "string",
+          "description": "Existing PersistentVolumeClaim to use instead of the component's own"
+        }
+      },
+      "description": "Volume with a component's data: a PersistentVolumeClaim if enabled, otherwise an emptyDir"
</code_context>
<issue_to_address>
**Integer rendering regression passes**

When an integer `mongodb.volumeSize` is rendered as an exactly equivalent floating-point quantity, in `test_mongodb_volume_size_alias`, the dictionary equality compares the parsed storage value with an integer, but Python considers an exactly representable float equal to that integer. A regression rendering the byte quantity in scientific notation therefore passes without verifying the intended integer output.

Assert that the parsed storage value has integer type, or assert the rendered YAML scalar preserves integer formatting.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 4 findings to address first, and setting persistence to false or switching to an existing claim stops rendering the release-managed PVC, so an upgrade can delete it and its MongoDB data under the usual reclaim policy. Reverting the setting cannot recover data already deleted.

Blocking findings: docs/helm-chart.md:473, charts/foca-common/templates/_mongodb.tpl:279, charts/foca-common/templates/_mongodb.tpl:277, charts/foca-common/tests/test_templates.py:855


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

Comment thread docs/helm-chart.md Resolved
Validate mongodb.volumeSize like the other sizes (a positive quantity
or number of bytes, or null to unset it), and check that numbers of
bytes are rendered as integers.

Refs #419

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit 0aae032 into dev Oct 23, 2026
22 checks passed
@uniqueg
uniqueg deleted the feat/mongodb-persistence branch October 23, 2026 01:52
uniqueg added a commit that referenced this pull request Oct 23, 2026
Cover mongodb.persistence of foca-common 0.2.0 (#422): replacing
volumeSize in app charts and deployments, and keeping the claim when
switching to an existing claim (#417).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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