Skip to content

feat(chart): persist the bundled broker - #411

Merged
uniqueg merged 8 commits into
devfrom
feat/chart-broker-persistence
Oct 23, 2026
Merged

uniqueg merged 8 commits into
devfrom
feat/chart-broker-persistence

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Status

Ready to merge except for one step. Sourcery's single finding (broker pods could share queue data) is fixed in a2227d7 (StatefulSet), and I replied in its thread. I could not resolve the thread: GraphQL calls are not permitted for the agent. Please resolve it (or confirm the fix), then mark the PR ready and squash-merge. CI is green on a2227d7, including the kind check that a queued task survives a broker restart.

Summary

Closes #408.

The bundled RabbitMQ of the foca-common library chart (unreleased 0.2.0) now keeps its data in a PersistentVolumeClaim, so queued tasks survive restarts of its pod (upgrades, node drains).

  • New values broker.persistence: enabled (true in the petstore example), size, accessModes (default ReadWriteOnce), storageClass (empty means the cluster's default; - means no dynamic provisioning) and existingClaim.
  • New template foca-common.brokerPersistentVolumeClaim and helper foca-common.brokerClaimName. The petstore example adds templates/broker-pvc.yaml.
  • The claim is mounted at /var/lib/rabbitmq/mnesia only. RabbitMQ's home, which holds the Erlang cookie, stays in the emptyDir. The kubelet makes files on claims group-readable for the pod's fsGroup when it mounts them, and Erlang refuses to use a cookie that anyone other than its owner can read.
  • The broker is now a StatefulSet with one replica. A Deployment can start a replacement while a deleted or evicted pod is still terminating, and a ReadWriteOnce claim can be mounted by two pods on the same node; a StatefulSet starts the new pod only once the old one is gone.
    • New template foca-common.brokerStatefulSet; the petstore template file is renamed to broker-statefulset.yaml.
    • foca-common.brokerDeployment remains as a deprecated alias for app charts written for 0.1.0, and a test covers it.
    • Upgrading from 0.1.0 replaces the broker's Deployment with the StatefulSet.
    • This came from Sourcery's review.
  • RABBITMQ_NODENAME=rabbit@localhost is now fixed. RabbitMQ keeps its data in a directory named after the node, and the default name follows the pod's host name, which changes with every pod.
  • Backwards compatible: values without a broker.persistence map (written for 0.1.0), or with enabled: false, keep the previous emptyDir.
  • Both schemas (library and example) get the persistence object. They reject zero or invalid sizes, unknown access modes and unknown keys.
  • CI (charts.yml), in the Celery installation:
    1. stops the worker and queues a task;
    2. restarts the broker's pod;
    3. checks that the task is still queued;
    4. starts the worker again and checks that the worker receives the task.
  • Docs (docs/helm-chart.md):
    • persistence, durability (Celery's default queue is durable and its messages are persistent; rpc:// results are transient);
    • password rotation with rabbitmqctl change_password, since the user is created only on first start;
    • the PVC template file that app charts must add.

How it was verified

  • pytest charts/foca-common/tests: 182 passed (snapshots updated).
  • ruff check and ruff format --check, mypy charts/foca-common/tests: clean.
  • helm lint examples/petstore/chart: clean.
  • mkdocs build --strict: clean.
  • actionlint: clean.
  • Review: adversarial-reviewer and quality-reviewer in round 1, then adversarial-reviewer again in round 2, which found nothing.
    • Fixed: the Erlang cookie on the claim (high); the persistence helper simplified to return only the claim name; zero sizes rejected; more tests (existing claim with persistence disabled, no claim without the broker); docs split up and the OpenShift sentence corrected; a comment saying the node name must not change.
  • CI: all checks pass. On kind, the log shows "Queued before the restart: 1", "Queued after the restart: 1" and "The worker received the queued task".
    • The first run failed: importing the example's worker module in the app's pod loads the whole app, which exceeded the container's 128Mi limit (exit code 137).
    • Fixed: the step now sends the task with a bare Celery app, and the CI Celery values raise the app's memory limit to 256Mi.
    • CI is green on a2227d7 (StatefulSet). The kind log shows the same three lines, plus "statefulset.apps/petstore-celery-broker condition met".
  • Sourcery: one high finding (a Deployment can run two broker pods on one claim). Fixed with the StatefulSet. The fix came after the two internal review rounds, so the reviewer subagents did not see it.

Declined review findings

  • Render the PVC from the Deployment template, so that app charts cannot forget it (low). Declined for consistency with MongoDB, whose claim is also a separate template. The docs say app charts need the file, and the 0.2.0 release notes list the new template files.
  • Move MongoDB to a mongodb.persistence block (medium, quality). Out of scope here, and a breaking change of app values. Better as a follow-up issue, to be decided by the maintainer.
  • Move the CI step into a script with a shared wait_for function (medium, quality). The other steps of charts.yml are inline too. I added a comment naming the example's worker entry point instead.
  • Use a shared PVC helper for MongoDB and the broker (low). Waits for the MongoDB follow-up above. The quoting of the size now matches MongoDB's.

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Persist the bundled RabbitMQ broker's task data across pod restarts while maintaining backward-compatible chart configuration.

New Features:

  • Add configurable persistence for the bundled RabbitMQ broker, including storage size, access modes, storage class, and existing claims.
  • Preserve queued Celery tasks across broker pod restarts when persistence is enabled.

Bug Fixes:

  • Prevent concurrent broker pods from accessing the same persistent volume during replacement.
  • Keep RabbitMQ data associated with a stable node name across pod replacements.

Enhancements:

  • Run the bundled broker as a single-replica StatefulSet while retaining a deprecated Deployment template alias for compatibility.
  • Document broker persistence behavior, password rotation, result durability, and required application chart templates.

CI:

  • Add a kind-based CI check that queues a task, restarts the broker, and verifies the task is subsequently delivered to the worker.

Documentation:

  • Update Helm chart documentation and example values for RabbitMQ persistence and operational procedures.

Tests:

  • Extend chart rendering and schema tests for persistent claims, existing claims, disabled persistence, StatefulSet behavior, compatibility, and validation.

uniqueg and others added 4 commits October 22, 2026 23:01
Keep the data of the bundled RabbitMQ in a PersistentVolumeClaim
(broker.persistence: size, accessModes, storageClass, existingClaim),
so that queued tasks survive restarts of its pod. The node name is
fixed, as RabbitMQ keeps its data in a directory named after it.
Values without a broker.persistence map keep the emptyDir.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The kubelet makes the files of claims group-readable for the pod's
fsGroup on mounting them, and Erlang refuses a cookie that others than
its owner can read, so the broker would not start again. Mount the claim
at /var/lib/rabbitmq/mnesia only and keep the home (with the cookie) in
the emptyDir. Also simplify the persistence helper (claim name only),
reject zero sizes in the schema, cover more cases in tests and split the
docs on the broker's persistence.

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

sourcery-ai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR adds configurable PVC-backed persistence for the bundled RabbitMQ, migrates the broker to a single-replica StatefulSet with a stable node name to prevent concurrent claim use, preserves backward-compatible emptyDir and helper behavior, and validates queue survival through CI, tests, schemas, and documentation.

Sequence diagram for queued task survival across broker restart

sequenceDiagram
    participant CI
    participant AppPod
    participant Broker as RabbitMQ StatefulSet
    participant PVC
    participant Worker

    CI->>Worker: scale deployment to zero
    CI->>AppPod: Celery.send_task('ci.persistence_check')
    AppPod->>Broker: publish persistent task to celery queue
    Broker->>PVC: store queued task
    CI->>Broker: delete broker pod
    Broker->>PVC: remount persistent data
    Broker-->>CI: StatefulSet becomes ready
    CI->>Broker: list_queues
    Broker-->>CI: celery queue contains task
    CI->>Worker: scale deployment to one
    Worker->>Broker: consume celery queue
    Broker-->>Worker: deliver queued task
Loading

File-Level Changes

Change Details Files
Persist bundled RabbitMQ data and make broker replacement safe for a single ReadWriteOnce volume.
  • Added configurable broker persistence with generated or existing PVCs, storage class, size, and access modes.
  • Mounted only RabbitMQ mnesia data on the claim while retaining the Erlang cookie in emptyDir.
  • Changed the broker from a Deployment to a one-replica StatefulSet and fixed its node name to preserve data across pod replacement.
  • Kept the former brokerDeployment helper as a compatibility alias and documented the required PVC template for app charts.
charts/foca-common/templates/_broker.tpl
examples/petstore/chart/templates/broker-pvc.yaml
examples/petstore/chart/templates/broker-statefulset.yaml
examples/petstore/chart/templates/broker-deployment.yaml
charts/foca-common/tests/test_templates.py
charts/foca-common/tests/__snapshots__/celery.yaml
Extend chart configuration and validation for persistence while preserving 0.1.0 and explicitly disabled behavior.
  • Added persistence values to the library and petstore schemas and example defaults.
  • Validated storage sizes, access modes, and unknown persistence keys.
  • Retained emptyDir behavior when persistence is disabled or absent and tested existing-claim handling.
charts/foca-common/app-values.schema.json
examples/petstore/chart/values.schema.json
examples/petstore/chart/values.yaml
charts/foca-common/tests/test_templates.py
Verify queued-task durability in kind CI and update operational documentation.
  • Added an end-to-end check that queues a task with workers stopped, restarts the broker, confirms the queue remains, and verifies later delivery.
  • Adjusted CI app memory for the task-sending step.
  • Documented durability semantics, password rotation, persistence settings, and required app-chart templates.
.github/workflows/charts.yml
examples/petstore/chart/ci/celery-values.yaml
docs/helm-chart.md

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 22, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #411   +/-   ##
=========================================
  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.

Importing the example's worker entry point loads the whole app, which
exceeded the memory limit of the app's container (exit code 137). Send
the task with a bare Celery app and give the app more memory in the
Celery installation of CI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg marked this pull request as ready for review October 22, 2026 21:55

@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 1 issue

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

## Individual Comments

### Comment 1
<location path="charts/foca-common/templates/_broker.tpl" line_range="111-113" />
<code_context>
           name: data
+        {{- if $claimName }}
+        # RabbitMQ's data, on the claim
+        - mountPath: /var/lib/rabbitmq/mnesia
+          name: persistent-data
+        {{- end }}
         # Writable, as the root file system is read-only by default
         - mountPath: /tmp
</code_context>
<issue_to_address>
**Broker pods can share queue data**

When a broker pod is deleted or evicted and its RabbitMQ process is still shutting down when its replacement starts on the same node, the Deployment can create a replacement while a deleted pod is still terminating; because `ReadWriteOnce` allows multiple pods on the same node to mount the claim, both RabbitMQ processes can access the same Mnesia files and corrupt the broker state.

Use a controller or replacement mechanism that waits for the old broker process to terminate before another process can access the Mnesia claim.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and with persistence enabled, queued Celery tasks remain on the PVC across broker pod replacements and can later be delivered to the worker, where task side effects may already have happened before a revert. Reverting removes the persistent mount but cannot undo tasks that were delivered or their effects.

Blocking findings: charts/foca-common/templates/_broker.tpl:113


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

A Deployment replaces a deleted or evicted pod while the old one still
terminates, and a ReadWriteOnce claim can be mounted by both pods on one
node, so two RabbitMQ processes could use the same data. A StatefulSet
starts the new pod only once the old one is gone. The template is now
foca-common.brokerStatefulSet; foca-common.brokerDeployment stays as a
deprecated alias for app charts written for 0.1.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg marked this pull request as draft October 22, 2026 22:17
@uniqueg
uniqueg marked this pull request as ready for review October 22, 2026 23:45

@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 1 issue

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

## Individual Comments

### Comment 1
<location path="charts/foca-common/templates/_broker.tpl" line_range="188" />
<code_context>
+  labels:
+    {{- include "foca-common.labels" (dict "root" $root "component" "broker") | nindent 4 }}
+spec:
+  accessModes:
+    {{- toYaml ($persistence.accessModes | default (list "ReadWriteOnce")) | nindent 2 }}
+  resources:
</code_context>
<issue_to_address>
**Read-only storage prevents broker startup**

When `broker.persistence.accessModes` is set to `ReadOnlyMany` and the provisioner honors its read-only semantics, the claim receives a read-only volume, so RabbitMQ cannot write its Mnesia data and broker operations fail.

Reject read-only access modes for the broker data claim.

Also at `examples/petstore/chart/values.yaml:321`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and on upgrade, replacing the broker Deployment with a StatefulSet does not copy queued messages out of the old pod’s emptyDir, so tasks still queued during the rollout can be lost. Reverting cannot restore those messages.

Blocking findings: charts/foca-common/templates/_broker.tpl:188


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

The broker writes its data to the claim, so ReadOnlyMany would leave
it unable to start. The schema now accepts writable access modes only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit 86a2471 into dev Oct 23, 2026
20 checks passed
@uniqueg
uniqueg deleted the feat/chart-broker-persistence branch October 23, 2026 00:16
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