diff --git a/backend/tests/test_helm_deployment_strategy.py b/backend/tests/test_helm_deployment_strategy.py new file mode 100644 index 0000000000..18d4b795d3 --- /dev/null +++ b/backend/tests/test_helm_deployment_strategy.py @@ -0,0 +1,53 @@ +"""``strategy.type: Recreate`` must render without ``rollingUpdate``. + +The gateway and provisioner default to ``RollingUpdate`` with a +``rollingUpdate`` block. Kubernetes rejects ``rollingUpdate`` next to +``type: Recreate``, and a parent chart that uses this chart as a dependency +cannot null the default block (Helm keeps subchart defaults), so a single +gateway on a ReadWriteOnce home PVC could not switch to ``Recreate``. +""" + +from __future__ import annotations + +import shutil +import subprocess +from pathlib import Path + +import pytest +import yaml + +REPO_ROOT = Path(__file__).resolve().parents[2] +CHART = REPO_ROOT / "deploy" / "helm" / "deer-flow" + + +def _strategies(chart: Path, *settings: str) -> dict[str, dict]: + helm = shutil.which("helm") + if helm is None: + pytest.skip("helm is unavailable") + command = [helm, "template", "deer-flow", str(chart)] + for setting in settings: + command.extend(["--set", setting]) + rendered = subprocess.run(command, check=True, capture_output=True, text=True, encoding="utf-8").stdout + deployments = [document for document in yaml.safe_load_all(rendered) if isinstance(document, dict) and document.get("kind") == "Deployment"] + return {name: d["spec"].get("strategy") for d in deployments for name in ("gateway", "provisioner") if d["metadata"]["name"].endswith(f"-{name}")} + + +def test_default_strategy_is_surge_then_drain() -> None: + strategies = _strategies(CHART) + assert set(strategies) == {"gateway", "provisioner"} + for strategy in strategies.values(): + assert strategy == {"type": "RollingUpdate", "rollingUpdate": {"maxSurge": 1, "maxUnavailable": 0}} + + +@pytest.mark.parametrize("component", ["gateway", "provisioner"]) +def test_recreate_drops_rolling_update(component: str) -> None: + strategies = _strategies(CHART, f"{component}.strategy.type=Recreate") + assert strategies[component] == {"type": "Recreate"} + + +def test_recreate_from_a_parent_chart(tmp_path: Path) -> None: + parent = tmp_path / "parent" + shutil.copytree(CHART, parent / "charts" / "deer-flow") + (parent / "Chart.yaml").write_text('apiVersion: v2\nname: parent\nversion: 0.0.1\ndependencies:\n - name: deer-flow\n version: "*"\n') + (parent / "values.yaml").write_text("deer-flow:\n gateway:\n strategy:\n type: Recreate\n rollingUpdate: null\n") + assert _strategies(parent)["gateway"] == {"type": "Recreate"} diff --git a/backend/tests/test_helm_provisioner_replicas.py b/backend/tests/test_helm_provisioner_replicas.py index e03e0da3ca..f11cac6607 100644 --- a/backend/tests/test_helm_provisioner_replicas.py +++ b/backend/tests/test_helm_provisioner_replicas.py @@ -141,7 +141,8 @@ def test_deployment_template_takes_replicas_and_strategy_from_values() -> None: template = DEPLOYMENT_TEMPLATE.read_text(encoding="utf-8") assert "replicas: {{ .Values.provisioner.replicas }}" in template assert re.search(r"^\s*replicas:\s*\d+\s*$", template, flags=re.MULTILINE) is None, "a literal replica count ignores provisioner.replicas" - assert re.search(r"\{\{- with \.Values\.provisioner\.strategy \}\}\n\s+strategy:\n\s+\{\{- toYaml \. \| nindent 4 \}\}\n\s+\{\{- end \}\}", template), "the rollout strategy must come from values like the gateway's" + strategy_block = r'\{\{- with \.Values\.provisioner\.strategy \}\}\n\s+strategy:\n\s+\{\{- include "deer-flow\.strategy" \. \| nindent 4 \}\}\n\s+\{\{- end \}\}' + assert re.search(strategy_block, template), "the rollout strategy must come from values like the gateway's" assert SELECTOR_BLOCK.search(template), "the Deployment selector is what the budget has to match" diff --git a/deploy/helm/deer-flow/README.md b/deploy/helm/deer-flow/README.md index a0ddf653c5..a91d57a94a 100644 --- a/deploy/helm/deer-flow/README.md +++ b/deploy/helm/deer-flow/README.md @@ -347,7 +347,17 @@ kubectl -n deer-flow exec deploy/deer-flow-provisioner -- curl -s localhost:8002 string such as `"50%"`, while `0` fails the render -- disable the budget instead) is rendered automatically for a multi-instance gateway (same rule), and the rollout strategy is surge-then-drain (`maxSurge: 1`, - `maxUnavailable: 0`). The sandbox + `maxUnavailable: 0`). A single gateway whose ReadWriteOnce home PVC can't + attach on two nodes at once needs `gateway.strategy.type: Recreate`; the + chart then drops `rollingUpdate`, so the type alone is enough, also when + this chart is a dependency of yours. Switching an installed release from + the default to `Recreate` with Helm 4's server-side apply fails with + `spec.strategy.rollingUpdate: Forbidden` when the previous release's + manifest had no `strategy` (e.g. `gateway.strategy: null`): the API server + defaulted `rollingUpdate`, no field manager owns it, so the apply can't + remove it. Run that one upgrade with `--server-side=false`; its three-way + merge patch drops `rollingUpdate`, and later upgrades can use server-side + apply again. The sandbox provisioner is not on the single-instance list: it scales independently (next item). - **Provisioner replicas.** `provisioner.replicas` (default 1) scales the diff --git a/deploy/helm/deer-flow/templates/_helpers.tpl b/deploy/helm/deer-flow/templates/_helpers.tpl index 7e079f6817..660526afd1 100644 --- a/deploy/helm/deer-flow/templates/_helpers.tpl +++ b/deploy/helm/deer-flow/templates/_helpers.tpl @@ -64,6 +64,13 @@ imagePullSecrets: {{- printf "%s:%s" .Values.gateway.initContainers.image.repository .Values.gateway.initContainers.image.tag -}} {{- end -}} +{{/* A Deployment strategy. With `type: Recreate`, `rollingUpdate` is dropped: + Kubernetes rejects the pair, and a parent chart cannot null the default + `rollingUpdate` of this chart's values (Helm keeps subchart defaults). */}} +{{- define "deer-flow.strategy" -}} +{{- if eq (.type | default "") "Recreate" -}}{{- toYaml (omit . "rollingUpdate") -}}{{- else -}}{{- toYaml . -}}{{- end -}} +{{- end -}} + {{/* PVC name for the .deer-flow home directory. */}} {{- define "deer-flow.homePVC" -}} {{- printf "%s-home" (include "deer-flow.fullname" .) -}} diff --git a/deploy/helm/deer-flow/templates/gateway-deployment.yaml b/deploy/helm/deer-flow/templates/gateway-deployment.yaml index 77924d344e..2271eb8499 100644 --- a/deploy/helm/deer-flow/templates/gateway-deployment.yaml +++ b/deploy/helm/deer-flow/templates/gateway-deployment.yaml @@ -10,7 +10,7 @@ spec: replicas: {{ .Values.gateway.replicas }} {{- with .Values.gateway.strategy }} strategy: - {{- toYaml . | nindent 4 }} + {{- include "deer-flow.strategy" . | nindent 4 }} {{- end }} selector: matchLabels: diff --git a/deploy/helm/deer-flow/templates/provisioner-deployment.yaml b/deploy/helm/deer-flow/templates/provisioner-deployment.yaml index fdaf1cfa90..278742cc31 100644 --- a/deploy/helm/deer-flow/templates/provisioner-deployment.yaml +++ b/deploy/helm/deer-flow/templates/provisioner-deployment.yaml @@ -11,7 +11,7 @@ spec: replicas: {{ .Values.provisioner.replicas }} {{- with .Values.provisioner.strategy }} strategy: - {{- toYaml . | nindent 4 }} + {{- include "deer-flow.strategy" . | nindent 4 }} {{- end }} selector: matchLabels: diff --git a/deploy/helm/deer-flow/values.yaml b/deploy/helm/deer-flow/values.yaml index b71e67452d..4fb328c02f 100644 --- a/deploy/helm/deer-flow/values.yaml +++ b/deploy/helm/deer-flow/values.yaml @@ -61,6 +61,9 @@ gateway: # Rollout strategy. Surge-then-drain keeps one Pod serving while a peer # starts; with run-ownership heartbeats the new Pod leaves the old Pod's live # runs alone (their leases keep renewing) while the old Pod drains them. + # `type: Recreate` (e.g. a single gateway on a ReadWriteOnce home PVC that + # can't attach to two nodes) drops `rollingUpdate`, so setting the type alone + # is enough, also from a parent chart. strategy: type: RollingUpdate rollingUpdate: @@ -181,6 +184,7 @@ provisioner: # Rollout strategy. Surge-then-drain keeps one Pod answering sandbox calls # while a peer starts; a replica has nothing to hand over beyond its # in-flight requests, so the default termination grace period is enough. + # `type: Recreate` drops `rollingUpdate`, as for the gateway. strategy: type: RollingUpdate rollingUpdate: