Repository navigation
[feat] Support topologySpreadConstraints on backend, workflows, workers and code executor #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mertbozfakioglu
merged 4 commits into
tryretool:main
from
jrhuerta:topology-spread-constraints
Nov 25, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3f9ff8f
[feat] Support topologySpreadConstraints on backend, workflows, worke…
jrhuerta 5f794de
Let an empty topologySpreadConstraints override disable inherited rules
jrhuerta d2f13e9
Mark topologySpreadConstraints as not yet annotated for the values sc…
jrhuerta 6e62e59
Let workflows.backend override topologySpreadConstraints for the work…
mertbozfakioglu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| """Render checks for topologySpreadConstraints selectors and override order.""" | ||
|
|
||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| ROOT = Path(__file__).resolve().parents[1] | ||
| sys.dont_write_bytecode = True | ||
|
|
||
| OPTION_FILE = "charts/retool/ci/test-topology-spread-constraints-option.yaml" | ||
| # Deployments that read the global list in the option file. | ||
| SPREAD_DEPLOYMENTS = ( | ||
| "retool", | ||
| "retool-workflow-backend", | ||
| "retool-workflow-worker", | ||
| "retool-agent-worker", | ||
| "retool-agent-eval-worker", | ||
| ) | ||
| CODE_EXECUTOR = "retool-code-executor" | ||
| ZONE = "topology.kubernetes.io/zone" | ||
| HOSTNAME = "kubernetes.io/hostname" | ||
|
|
||
|
|
||
| def render(*extra_values): | ||
| """Render the chart with the install values, the option file, and extra YAML snippets.""" | ||
| command = [ | ||
| "helm", "template", "retool", "charts/retool", | ||
| "--values", "charts/retool/ci/test-install-values.yaml", | ||
| "--values", OPTION_FILE, | ||
| ] | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| for index, snippet in enumerate(extra_values): | ||
| path = Path(tmp) / f"extra-{index}.yaml" | ||
| path.write_text(snippet) | ||
| command.extend(("--values", str(path))) | ||
| return subprocess.check_output(command, cwd=ROOT, text=True) | ||
|
|
||
|
|
||
| def deployments(output): | ||
| """Map Deployment name to its manifest text.""" | ||
| found = {} | ||
| for document in output.split("\n---\n"): | ||
| if "\nkind: Deployment\n" not in f"\n{document}\n": | ||
| continue | ||
| metadata = document.split("\nmetadata:\n", 1)[1] | ||
| name = next(line.split(":", 1)[1].strip() for line in metadata.splitlines() if line.startswith(" name:")) | ||
| found[name] = document | ||
| return found | ||
|
|
||
|
|
||
| def block(text, header, indent): | ||
| """Return the lines nested under `header` (deeper than `indent` spaces).""" | ||
| lines = text.splitlines() | ||
| start = lines.index(header) + 1 | ||
| nested = [] | ||
| for line in lines[start:]: | ||
| if line.strip() and len(line) - len(line.lstrip()) <= indent: | ||
| break | ||
| nested.append(line) | ||
| return nested | ||
|
|
||
|
|
||
| def labels(lines): | ||
| return dict(line.strip().split(": ", 1) for line in lines if line.strip()) | ||
|
|
||
|
|
||
| def selector(deployment): | ||
| return labels(block(deployment, " matchLabels:", 4)) | ||
|
|
||
|
|
||
| def constraints(deployment): | ||
| """Parse the pod's topologySpreadConstraints into dicts with a matchLabels entry.""" | ||
| if " topologySpreadConstraints:" not in deployment.splitlines(): | ||
| return [] | ||
| entries = [] | ||
| for line in block(deployment, " topologySpreadConstraints:", 6): | ||
| if line.startswith(" - "): | ||
| entries.append([]) | ||
| line = " " + line[len(" - "):] | ||
| entries[-1].append(line) | ||
| parsed = [] | ||
| for entry in entries: | ||
| text = "\n".join(entry) | ||
| item = { | ||
| line.strip().split(": ", 1)[0]: line.strip().split(": ", 1)[1] | ||
| for line in entry | ||
| if line.startswith(" ") and not line.startswith(" ") and ": " in line | ||
| } | ||
| item["matchLabels"] = labels(block(text, " matchLabels:", 12)) if "matchLabels:" in text else {} | ||
| parsed.append(item) | ||
| return parsed | ||
|
|
||
|
|
||
| class TopologySpreadConstraintsTests(unittest.TestCase): | ||
| def test_default_renders_no_constraints(self): | ||
| output = subprocess.check_output( | ||
| ["helm", "template", "retool", "charts/retool", | ||
| "--values", "charts/retool/ci/test-install-values.yaml", | ||
| "--set", "workflows.enabled=true", "--set", "agents.enabled=true", | ||
| "--set", "codeExecutor.enabled=true"], | ||
| cwd=ROOT, text=True, | ||
| ) | ||
| self.assertNotIn("topologySpreadConstraints", output) | ||
|
|
||
| def test_global_list_selects_each_component_own_pods(self): | ||
| rendered = deployments(render()) | ||
| for name in SPREAD_DEPLOYMENTS: | ||
| with self.subTest(deployment=name): | ||
| deployment = rendered[name] | ||
| spread = constraints(deployment) | ||
| self.assertEqual([c["topologyKey"] for c in spread], [ZONE, HOSTNAME]) | ||
| self.assertEqual([c["whenUnsatisfiable"] for c in spread], ["DoNotSchedule", "ScheduleAnyway"]) | ||
| for constraint in spread: | ||
| self.assertEqual(constraint["matchLabels"], selector(deployment)) | ||
| selectors = [tuple(sorted(selector(rendered[name]).items())) for name in SPREAD_DEPLOYMENTS] | ||
| self.assertEqual(len(set(selectors)), len(selectors), "components must not share a selector") | ||
|
|
||
| def test_explicit_label_selector_is_kept(self): | ||
| spread = constraints(deployments(render())[CODE_EXECUTOR]) | ||
| self.assertEqual(len(spread), 1) | ||
| self.assertEqual(spread[0]["whenUnsatisfiable"], "ScheduleAnyway") | ||
| self.assertEqual(spread[0]["matchLabels"], {"example.com/spread-group": "code-executor"}) | ||
|
|
||
| def test_worker_override_applies_to_that_worker_only(self): | ||
| rendered = deployments(render( | ||
| "agents:\n" | ||
| " evalWorker:\n" | ||
| " topologySpreadConstraints:\n" | ||
| " - maxSkew: 3\n" | ||
| " topologyKey: kubernetes.io/hostname\n" | ||
| " whenUnsatisfiable: ScheduleAnyway\n" | ||
| )) | ||
| eval_worker = constraints(rendered["retool-agent-eval-worker"]) | ||
| self.assertEqual([(c["topologyKey"], c["maxSkew"]) for c in eval_worker], [(HOSTNAME, "3")]) | ||
| self.assertEqual(eval_worker[0]["matchLabels"], selector(rendered["retool-agent-eval-worker"])) | ||
| for name in ("retool-agent-worker", "retool-workflow-worker"): | ||
| with self.subTest(deployment=name): | ||
| self.assertEqual([c["topologyKey"] for c in constraints(rendered[name])], [ZONE, HOSTNAME]) | ||
|
|
||
| def test_parent_override_applies_to_its_workers(self): | ||
| rendered = deployments(render( | ||
| "agents:\n" | ||
| " topologySpreadConstraints:\n" | ||
| " - maxSkew: 4\n" | ||
| " topologyKey: topology.kubernetes.io/zone\n" | ||
| " whenUnsatisfiable: DoNotSchedule\n" | ||
| )) | ||
| for name in ("retool-agent-worker", "retool-agent-eval-worker"): | ||
| with self.subTest(deployment=name): | ||
| self.assertEqual([c["maxSkew"] for c in constraints(rendered[name])], ["4"]) | ||
| self.assertEqual([c["maxSkew"] for c in constraints(rendered["retool-workflow-worker"])], ["1", "2"]) | ||
|
|
||
| def test_empty_override_disables_constraints(self): | ||
| rendered = deployments(render( | ||
| "agents:\n" | ||
| " evalWorker:\n" | ||
| " topologySpreadConstraints: []\n" | ||
| "codeExecutor:\n" | ||
| " topologySpreadConstraints: []\n" | ||
| )) | ||
| for name in ("retool-agent-eval-worker", CODE_EXECUTOR): | ||
| with self.subTest(deployment=name): | ||
| self.assertEqual(constraints(rendered[name]), []) | ||
| self.assertEqual(len(constraints(rendered["retool-agent-worker"])), 2) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
charts/retool/ci/test-topology-spread-constraints-option.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| workflows: | ||
| enabled: true | ||
| agents: | ||
| enabled: true | ||
| codeExecutor: | ||
| enabled: true | ||
| # Component override with an explicit labelSelector, which is kept as-is. | ||
| topologySpreadConstraints: | ||
| - maxSkew: 1 | ||
| topologyKey: topology.kubernetes.io/zone | ||
| whenUnsatisfiable: ScheduleAnyway | ||
| labelSelector: | ||
| matchLabels: | ||
| example.com/spread-group: code-executor | ||
| # Global list without labelSelector: each component gets its own selector labels. | ||
| topologySpreadConstraints: | ||
| - maxSkew: 1 | ||
| topologyKey: topology.kubernetes.io/zone | ||
| whenUnsatisfiable: DoNotSchedule | ||
| - maxSkew: 2 | ||
| topologyKey: kubernetes.io/hostname | ||
| whenUnsatisfiable: ScheduleAnyway | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.