Skip to content

feat(chart): add app replicas and a PDB - #585

Merged
uniqueg merged 8 commits into
devfrom
feat/chart-replicas
Nov 1, 2026
Merged

uniqueg merged 8 commits into
devfrom
feat/chart-replicas

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Summary

The library chart foca-common rendered the app's Deployment with a fixed replicas: 1. This adds:

  • <valuesKey>.replicas (integer >= 1, 1 if not set; named like the existing worker.replicas, the chart's convention, rather than Helm's scaffold replicaCount);
  • <valuesKey>.podDisruptionBudget (enabled, true if not set; minAvailable or maxUnavailable, at most one, a number or a percentage up to 100%; maxUnavailable: 1 if neither) and a new template foca-common.podDisruptionBudget, rendered only with more than one replica;
  • template-side validation for app charts that still carry an older schema (replicas, enabled, the budget map, both bounds set);
  • schema (app-values.schema.json and the petstore copy), petstore values and template file, template tests, a replicas snapshot case, and the kind installation of ci/access-control-values.yaml with 2 replicas (two instances storing the configured policies at the same time on startup);
  • docs: docs/helm-chart.md ("Several replicas": rolling updates, budget semantics, upgrade steps for app charts moving to 0.3.0, caveats: rules changed in the database directly, per-process caches and memory, app startup tasks, single-pod MongoDB/broker, scaling to 0), and a pointer in docs/deployment.md;
  • chart version 0.3.0 (new values, backward compatible: app charts without them keep one pod and no budget).

Multi-replica safety

FOCA apps already run as several instances (one per Gunicorn worker, two pods during rolling updates). Access control checks the change record before each use of the enforcer and applies changed rules (foca/security/access_control/enforcer_cache.py, docs/access-control.md "Scaling"), and configured policies are replaced in generations, safe for instances starting at once (FocaCasbinAdapter.set_configured_policies()).

Closes #584

Verification

  • pytest charts/foca-common/tests: 229 passed (Helm 3.18.5 locally; CI uses 3.22.1)
  • helm lint --strict charts/foca-common and the petstore chart with each ci/*-values.yaml: passed
  • ruff check, ruff format --check, mypy, ast-grep scan, mkdocs build --strict: passed
  • Full unit suite locally except the MongoDB-backed plugin-store tests (no local MongoDB); CI runs them.
  • CI: all checks pass, including Chart and Chart install (kind, with 2 replicas in ci/access-control-values.yaml); CI passed green.

Review

Two rounds of adversarial and quality review; all high/medium findings fixed (none were high; the medium ones were docs placement and structure), plus the low ones affecting behaviour (template-side validation, percentages up to 100%, budget map, null enabled).

Unrelated fixes

None.

Declined review findings

  • Drop the 460-line replicas snapshot (low): kept, as every other snapshot case is a full render too, and it pins the rendered budget.
  • Move the multi-replica caveats out of the chart docs (medium, partly): trimmed to short bullets that link to access-control.md#scaling and configuration/plugins.md#plugin-store; operators read the chart docs, so the caveats stay listed there.

Release

After merge, the maintainer tags chart-v0.3.0 on the merge commit (see docs/helm-chart.md, Releases). Cloud Registry's chart then pins it (elixir-cloud-aai/cloud-registry#307).

🤖 Generated with Claude Code

Summary by Sourcery

Support safely scaling FOCA application deployments beyond one replica with configurable disruption protection and operator guidance.

New Features:

  • Add configurable application replica counts and PodDisruptionBudgets to the common Helm chart, with budgets rendered for multi-replica deployments.

Bug Fixes:

  • Preserve single-replica, no-budget behavior for existing app charts that do not define the new values.

Enhancements:

  • Add chart- and template-side validation for replica and disruption-budget settings, including backward-compatible handling of older app schemas.
  • Update the petstore example and CI installation to exercise multi-replica access-control startup and budget rendering.

Build:

  • Bump the foca-common chart version to 0.3.0 and update the petstore dependency.

Documentation:

  • Document multi-replica deployments, PodDisruptionBudget behavior, migration steps, operational caveats, and link the guidance from deployment documentation.

Tests:

  • Add schema, rendering, validation, and snapshot coverage for replicas and PodDisruptionBudgets.

uniqueg and others added 4 commits November 1, 2026 09:55
The library chart foca-common rendered the app's Deployment with a fixed
single replica. `<valuesKey>.replicas` (1 if not set, named like
`worker.replicas`) now sets the number of pods, and the new template
`foca-common.podDisruptionBudget` renders a PodDisruptionBudget with more
than one replica (`<valuesKey>.podDisruptionBudget`: `enabled`, true if
not set, and `minAvailable` or `maxUnavailable`, 1 if neither is set).
Adds the values to the schema and the petstore example, template and
snapshot tests, documents several replicas and their caveats, and raises
the chart's version to 0.3.0.

Closes #584

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The kind installation of ci/access-control-values.yaml now runs two
replicas, which store the configured policies of access control at the
same time on startup, and gets a PodDisruptionBudget.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Validate replicas and podDisruptionBudget.enabled also without the
schema, limit percentages to 100%, move the replicas helper to
_helpers.tpl, correct why one replica gets no budget, name caches per
worker process, add upgrade steps for app charts and a pointer in
docs/deployment.md, and split the tests of missing values.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fail with the chart's message if podDisruptionBudget is not a map, and
treat a null podDisruptionBudget.enabled as not set (true).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg uniqueg added this to the v0.20.0 milestone Nov 1, 2026
@uniqueg uniqueg added the type: feature New feature or request label Nov 1, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR upgrades foca-common to 0.3.0 with configurable application replicas and a validated, multi-replica-only PodDisruptionBudget, while preserving backward-compatible single-pod behavior. It wires the feature through library and petstore schemas/templates, adds extensive rendering and legacy-schema tests plus a two-replica kind scenario, and documents operational caveats and upgrade guidance.

Flow diagram for replica-aware chart rendering

flowchart TD
    V[App values] --> R[foca-common.replicas]
    R --> D[Deployment replicas]
    V --> P[foca-common.podDisruptionBudget]
    R --> P
    P -->|replicas greater than 1 and enabled| B[PodDisruptionBudget]
    P -->|one replica or disabled| N[No PodDisruptionBudget]
Loading

File-Level Changes

Change Details Files
Made application replica count configurable while preserving a one-pod default and validating values at render time.
  • Added <valuesKey>.replicas handling to Deployment rendering.
  • Added helper validation for whole numbers at least 1, including charts using older schemas.
  • Updated app value schemas and example values.
charts/foca-common/templates/_helpers.tpl
charts/foca-common/templates/_deployment.tpl
charts/foca-common/app-values.schema.json
examples/petstore/chart/values.schema.json
examples/petstore/chart/values.yaml
Added optional PodDisruptionBudget rendering for multi-replica applications.
  • Added the foca-common.podDisruptionBudget library template and petstore wrapper.
  • Defaulted enabled budgets to maxUnavailable: 1; supported either numeric or percentage minAvailable/maxUnavailable.
  • Skipped budgets for single-replica apps and added validation for malformed budgets, invalid bounds, and both bounds being set.
charts/foca-common/templates/_poddisruptionbudget.tpl
examples/petstore/chart/templates/poddisruptionbudget.yaml
charts/foca-common/app-values.schema.json
examples/petstore/chart/values.schema.json
Expanded chart coverage and integration verification for replica and disruption-budget behavior.
  • Added default, custom, disabled, single-replica, schema, legacy-schema, and invalid-value template tests.
  • Added a multi-replica rendering snapshot that pins the Deployment and PDB output.
  • Configured the access-control kind installation with two app replicas to exercise concurrent startup policy storage.
charts/foca-common/tests/test_templates.py
charts/foca-common/tests/test_snapshots.py
charts/foca-common/tests/__snapshots__/replicas.yaml
examples/petstore/chart/ci/access-control-values.yaml
Documented multi-replica operation, upgrade considerations, and the chart release update.
  • Documented rolling-update and PDB semantics, scaling caveats, state/cache behavior, startup safety, bundled dependency limitations, and scaling to zero for upgrades.
  • Added a deployment documentation pointer and bumped the library chart dependency/version to 0.3.0.
docs/helm-chart.md
docs/deployment.md
charts/foca-common/Chart.yaml
examples/petstore/chart/Chart.yaml
examples/petstore/chart/Chart.lock

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

Name the release as "before foca-common 0.3.0", which the guard of
hard-coded versions allows.

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

codecov Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #585   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           56        56           
  Lines         7068      7068           
=========================================
  Hits          7068      7068           
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 November 1, 2026 10:54

@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 5 issues

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

## Individual Comments

### Comment 1
<location path="charts/foca-common/templates/_poddisruptionbudget.tpl" line_range="32" />
<code_context>
+{{- if and $hasMin $hasMax -}}
+{{- fail (printf "%s.podDisruptionBudget: set either minAvailable or maxUnavailable, not both" $key) -}}
+{{- end }}
+apiVersion: policy/v1
+kind: PodDisruptionBudget
+metadata:
</code_context>
<issue_to_address>
**Older clusters reject the budget**

When a release with more than one replica is installed or upgraded on Kubernetes earlier than 1.21, the template emits `policy/v1`, which the API server does not serve on those clusters, so it rejects the PodDisruptionBudget and the Helm install or upgrade fails.

Render a PodDisruptionBudget API version supported by the cluster, or omit the resource when none is supported.
</issue_to_address>

### Comment 2
<location path="charts/foca-common/templates/_poddisruptionbudget.tpl" line_range="41-42" />
<code_context>
+spec:
+  {{- if $hasMin }}
+  minAvailable: {{ toJson $budget.minAvailable }}
+  {{- else if $hasMax }}
+  maxUnavailable: {{ toJson $budget.maxUnavailable }}
+  {{- else }}
+  maxUnavailable: 1
</code_context>
<issue_to_address>
**Invalid budget bounds reach Kubernetes**

When a schema-less app chart sets a bound to an invalid value such as `maxUnavailable=half`, the template checks that only one bound is set but does not validate its type or percentage format; `toJson` emits an invalid bound into the PDB, so Kubernetes rejects the resource and the install or upgrade fails.

Validate each present bound’s type, non-negative numeric value, or percentage format in the template when schema validation is absent.
</issue_to_address>

### Comment 3
<location path="charts/foca-common/templates/_poddisruptionbudget.tpl" line_range="20" />
<code_context>
+{{- end -}}
+{{- $enabled := $budget.enabled -}}
+{{- if kindIs "invalid" $enabled -}}
+{{- $enabled = true -}}
+{{- end -}}
+{{- if not (kindIs "bool" $enabled) -}}
</code_context>
<issue_to_address>
**Null setting fails schema validation**

When `podDisruptionBudget.enabled` is explicitly null and Helm coalescing preserves that value, the values schema requires `podDisruptionBudget.enabled` to be a boolean, so Helm rejects the null before this defaulting runs; rendering or upgrade fails instead of treating null as unset.

Allow null in the schema for `enabled`, or remove the template's null-as-unset behavior and document null as invalid.
</issue_to_address>

### Comment 4
<location path="charts/foca-common/templates/_poddisruptionbudget.tpl" line_range="14" />
<code_context>
+{{- include "foca-common.checkSettings" . -}}
+{{- $root := .root -}}
+{{- $key := .settings.valuesKey -}}
+{{- $budget := (index $root.Values $key).podDisruptionBudget | default dict -}}
+{{- if not (kindIs "map" $budget) -}}
+{{- fail (printf "%s.podDisruptionBudget must be a map" $key) -}}
</code_context>
<issue_to_address>
**Invalid budgets render as enabled**

When a schema-less or permissive chart passes a falsey non-map `podDisruptionBudget` value to the template, `default dict` replaces the value with an empty map before the `kindIs` check, so `$enabled` defaults to true and the template renders a default PodDisruptionBudget instead of rejecting the invalid setting.

Check the original `podDisruptionBudget` value is a map before applying defaults.
</issue_to_address>

### Comment 5
<location path="docs/helm-chart.md" line_range="770-771" />
<code_context>
+(1 if neither it nor `minAvailable` is set), or keep at least
+`minAvailable` (a number or a percentage, e.g., `50%`; set at most one of
+them). Set `podDisruptionBudget.enabled: false` to render none. With one
+replica, no budget is rendered: no budget keeps a single pod available
+during a drain, and one with `minAvailable` would block the drain; for the
+same reason, avoid `maxUnavailable: 0` or a `minAvailable` of all replicas.
+Kubernetes' scheduler spreads the pods of a Deployment across nodes and
</code_context>
<issue_to_address>
**Single pod is evicted during drains**

When the app has one replica and a node drain or other voluntary disruption evicts it, the single-replica branch renders no PodDisruptionBudget, so Kubernetes can evict the only app pod during a voluntary drain and leave the app unavailable. The docs and template comment say that omitting the budget keeps the pod available, giving operators the opposite expectation.

Clarify that without a budget the pod can be evicted and the app can become unavailable; explain that a budget requiring one available pod instead blocks the voluntary eviction.

Also at `charts/foca-common/templates/_poddisruptionbudget.tpl:6-7`.
</issue_to_address>

Linked issues

#584 feat(chart): configurable app replicas and a PodDisruptionBudget · 6 of 6 met

# Requirement Status Notes
1 Add <valuesKey>.replicas (integer >= 1, default 1) to set the number of app pods. ✅ Met The Deployment uses the shared replicas helper, which defaults to 1 and rejects non-whole numbers below 1.
2 Add <valuesKey>.podDisruptionBudget with enabled defaulting to true and either minAvailable or maxUnavailable; default maxUnavailable to 1 when neither is set. ✅ Met The schema and PDB template implement these settings and reject setting both bounds.
3 Render a PodDisruptionBudget only when enabled and replicas exceed 1. ✅ Met The template gates the PDB on both conditions and selects the app pods.
4 Add the values to the library schema and petstore chart, with template tests. ✅ Met Both schemas include the new values; the petstore chart adds values and a PDB template, and the library tests cover defaults, settings, and invalid values.
5 Document several replicas and their caveats in the Helm chart docs. ✅ Met The docs cover rolling updates, budget behavior, older app-chart upgrades, access-control changes, per-process caches, startup tasks, bundled dependencies, and upgrades requiring the app to stop.
6 Bump the library chart version to 0.3.0. ✅ Met The chart version is 0.3.0, and the petstore dependency uses that version.

Sourcery assessment

Needs a human reviewer. 5 findings to address first, and a mistaken replica count can overcommit the cluster, and a mistaken disruption budget can block node drains or allow too many pods to be evicted, affecting service availability. Reverting restores the prior Deployment and budget settings, but any outage or disruption that already occurred cannot be undone.

Blocking findings: charts/foca-common/templates/_poddisruptionbudget.tpl:32, charts/foca-common/templates/_poddisruptionbudget.tpl:42, charts/foca-common/templates/_poddisruptionbudget.tpl:20, charts/foca-common/templates/_poddisruptionbudget.tpl:14, docs/helm-chart.md:771


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

Comment thread docs/helm-chart.md Resolved
uniqueg and others added 2 commits November 1, 2026 12:08
Check minAvailable and maxUnavailable in the template too (whole
numbers of at least 0 or percentages up to 100%), reject non-map
budgets before applying defaults (e.g., false), allow a null
podDisruptionBudget.enabled in the schema (true, as in the template),
and say plainly that a single pod is evicted on drains.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Set unhealthyPodEvictionPolicy: AlwaysAllow on the app's
PodDisruptionBudget, so that pods that are not ready (e.g., crash-
looping after a failed upgrade) don't block node drains.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit 4fef0d4 into dev Nov 1, 2026
56 checks passed
@uniqueg
uniqueg deleted the feat/chart-replicas branch November 1, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(chart): configurable app replicas and a PodDisruptionBudget

1 participant