Skip to content

[feat] Support topologySpreadConstraints on backend, workflows, workers and code executor - #422

Merged
mertbozfakioglu merged 4 commits into
tryretool:mainfrom
jrhuerta:topology-spread-constraints
Nov 25, 2026
Merged

mertbozfakioglu merged 4 commits into
tryretool:mainfrom
jrhuerta:topology-spread-constraints

Conversation

@jrhuerta

@jrhuerta jrhuerta commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The chart exposes affinity, nodeSelector and tolerations but not topologySpreadConstraints. Without them, there is no way to spread Retool replicas evenly across availability zones. On clusters with per-zone node pools (for example Karpenter), the scheduler keeps placing new replicas in the first zone that has room. Running pods are never rebalanced, so one zone fills up first.

This PR adds topologySpreadConstraints to these Deployments:

  • main backend (deployment_backend.yaml)
  • workflow backend (deployment_workflows.yaml)
  • workflow, agent, agent eval, retoolos and rr agent workers (_workers.tpl)
  • code executor (deployment_code_executor.yaml)

Values

  • topologySpreadConstraints: new top-level list, default [].
  • Per-component overrides, resolved most specific first:
    • workers: <parent>.worker / agents.evalWorker, then <parent>, then global
    • workflow backend: workflows, then global
    • code executor: codeExecutor, then global

The first block that sets the key wins. An explicit [] therefore disables constraints for that component, and an unset key inherits from the next block. This uses hasKey rather than default, because default would treat [] as unset.

A constraint without a labelSelector gets the selector labels of the pod's own component, through the retool.topologySpreadConstraints helper. One global list then spreads each Deployment over its own pods only:

topologySpreadConstraints:
  - maxSkew: 1
    topologyKey: topology.kubernetes.io/zone
    whenUnsatisfiable: DoNotSchedule
  - maxSkew: 2
    topologyKey: kubernetes.io/hostname
    whenUnsatisfiable: ScheduleAnyway

A constraint that sets its own labelSelector is rendered unchanged.

Testing

New .github/test_topology_spread_constraints.py, added to the lint-chart job. It asserts that:

  • the default values render no constraints;
  • with the global list, each of the 5 Deployments gets both constraints, each selector equals that Deployment's spec.selector.matchLabels, and no two components share a selector;
  • an explicit labelSelector is kept as written (the code executor in the fixture);
  • a worker-level override (agents.evalWorker) applies to that worker only;
  • a parent override (agents) applies to both agent workers and not to the workflow worker;
  • an explicit [] override disables constraints for that component only.

I mutation-tested the assertions. Each of these changes makes at least one test fail: default semantics instead of hasKey, the backend selector used for workers, and always overwriting labelSelector.

Also checked:

  • With the default [], helm template output is identical to main for ci/test-install-values.yaml merged with each existing ci/*-option.yaml file. Only random generated secrets differ.
  • helm lint passes. Both values.yaml copies are in sync. The chart version is bumped to 6.14.3. schema/generate.sh --check passes; the new top-level key is marked @schema hidden like its neighbours.

🤖 Generated with Claude Code

@greptile-apps

greptile-apps Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The rules affect pod placement across several Retool Deployments in one chart.

The PR appears safe to merge, with non-blocking improvements needed for empty overrides and tests.

Findings

  1. P2 Empty overrides still inherit rules ▶
  2. P2 Wrong selectors can pass checks ▶

Reviews (1) · Last reviewed commit: "[feat] Support topologySpreadConstraints..." · Reviewed by Greptile

Comment thread charts/retool/templates/_workers.tpl Resolved
@jrhuerta
jrhuerta force-pushed the topology-spread-constraints branch from ee3202a to 829d716 Compare November 23, 2026 21:35
@jrhuerta

Copy link
Copy Markdown
Contributor Author

@mertbozfakioglu @tomgoren-retool could one of you take a look at this when you have a moment?

I rebased it onto main (chart version now 6.14.3) and added @schema hidden for the new top-level key, following #427. schema/generate.sh --check, helm lint and all .github/test_*.py scripts pass locally. CI will also need approval to run, since this comes from a fork.

Thanks!

@mertbozfakioglu mertbozfakioglu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking suggestion: let the workflow backend take its own workflows.backend.topologySpreadConstraints, the way the workers take workflows.worker.topologySpreadConstraints.

The three suggestions belong together. The two values.yaml copies must stay identical for values-yaml-synced, so please apply them as one batch ("Add suggestion to batch").

affinity:
{{ toYaml .Values.affinity | indent 8 }}
{{- end }}
{{- $topologySpreadConstraints := include "retool.topologySpreadConstraints" (dict "sources" (list .Values.workflows .Values) "selectorLabels" (include "retool.workflowBackend.selectorLabels" . | fromYaml)) }}

@mertbozfakioglu mertbozfakioglu Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
{{- $topologySpreadConstraints := include "retool.topologySpreadConstraints" (dict "sources" (list .Values.workflows .Values) "selectorLabels" (include "retool.workflowBackend.selectorLabels" . | fromYaml)) }}
{{- $topologySpreadConstraints := include "retool.topologySpreadConstraints" (dict "sources" (list .Values.workflows.backend .Values.workflows .Values) "selectorLabels" (include "retool.workflowBackend.selectorLabels" . | fromYaml)) }}

Small suggestion. This lets the backend read its own list from workflows.backend, the same way the workers use workflows.worker, and falls through to workflows and then the global list when it's unset.

Comment thread charts/retool/values.yaml
# Per-component overrides. The most specific block that sets the key wins and
# replaces this list. Set an override to [] to disable constraints for that
# component; leave it unset to inherit:
# workflows.topologySpreadConstraints (workflow backend and workflow workers)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
# workflows.topologySpreadConstraints (workflow backend and workflow workers)
# workflows.topologySpreadConstraints (workflow backend and workflow workers)
# workflows.backend.topologySpreadConstraints

Documents the new override. The root values.yaml needs the same edit.

Comment thread values.yaml
# Per-component overrides. The most specific block that sets the key wins and
# replaces this list. Set an override to [] to disable constraints for that
# component; leave it unset to inherit:
# workflows.topologySpreadConstraints (workflow backend and workflow workers)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
# workflows.topologySpreadConstraints (workflow backend and workflow workers)
# workflows.topologySpreadConstraints (workflow backend and workflow workers)
# workflows.backend.topologySpreadConstraints

Same edit as in charts/retool/values.yaml, to keep the two copies in sync.

@mertbozfakioglu

Copy link
Copy Markdown
Contributor

I pushed the changes for the small comments I had left, should still work for you. Thanks for doing this! Will merge soon as I verify this on real infra

jrhuerta and others added 4 commits November 24, 2026 15:28
…rs and code executor

Add a top-level topologySpreadConstraints value, with per-component
overrides that follow the existing affinity fallback order
(worker -> parent -> global, or component -> global).

A constraint without a labelSelector gets the selector labels of the pod's
own component. One list can then spread each Deployment on its own pods,
for example across zones, without a separate selector per component.

The default is an empty list, so rendered manifests do not change unless
the value is set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve the list by key presence instead of `default`: the most specific
values block that sets topologySpreadConstraints wins, so an explicit []
disables constraints for that component while an unset key still inherits.

Add .github/test_topology_spread_constraints.py with render assertions:
each component selects only its own pods, an explicit labelSelector is
kept, worker and parent overrides apply only where set, an empty override
disables constraints, and the default renders none. The option fixture
now uses an explicit selector that differs from the injected one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hema

Follows the convention from tryretool#427: top-level keys without schema
annotations are marked @Schema hidden, so they stay out of
values.schema.json and README.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…flow backend

The workflow backend now reads workflows.backend first, then workflows,
then the global list, the same way the workers read workflows.worker.
Existing values render the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mertbozfakioglu
mertbozfakioglu force-pushed the topology-spread-constraints branch from c4ead07 to 6e62e59 Compare November 24, 2026 23:28
@mertbozfakioglu
mertbozfakioglu merged commit 56d41c3 into tryretool:main Nov 25, 2026
16 checks passed
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.

3 participants