From 11d86f34d9d8d4116fa44119852b2f14d22eaf29 Mon Sep 17 00:00:00 2001 From: Leonard Du <101617499+LeonardCareer@users.noreply.github.com> Date: Mon, 30 Nov 2026 10:49:05 +1100 Subject: [PATCH 1/5] Set KWOK node heartbeats to 300-305 seconds Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- modules/python/kwok/kwok.py | 28 ++++++- modules/python/tests/kwok/test_kwok_node.py | 83 ++++++++++++++++++--- 2 files changed, 95 insertions(+), 16 deletions(-) diff --git a/modules/python/kwok/kwok.py b/modules/python/kwok/kwok.py index ae01a829b2..4b564ddd6d 100644 --- a/modules/python/kwok/kwok.py +++ b/modules/python/kwok/kwok.py @@ -1,5 +1,7 @@ """KWOK (Kubernetes WithOut Kubelet) - Virtual Node/Pod Simulator. +Node status heartbeats run every 300-305 seconds; Lease renewal is unchanged. + Node creation is sequential by default, with 50 ms spacing. Opt in with --node-create-workers; --node-create-qps caps aggregate attempts (including retries) only in parallel mode. Conflict replacement and DRA requests add API @@ -116,10 +118,28 @@ def _bootstrap_kwok(self, kwok_release, enable_metrics): "V1Deployment", ) - execute_with_retries( - self.k8s_client.apply_manifest_from_url, - stage_fast_yaml_url - ) + response = execute_with_retries(requests.get, stage_fast_yaml_url, timeout=30) + response.raise_for_status() + has_node_heartbeat_stage = False + for doc in yaml.safe_load_all(response.text): + if not doc: + continue + if (doc.get("kind") == "Stage" + and doc.get("metadata", {}).get("name") == "node-heartbeat-with-lease"): + delay = doc.get("spec", {}).get("delay") + if not isinstance(delay, dict): + raise RuntimeError("Node heartbeat Stage has no delay configuration in stage-fast.yaml.") + # KWOK's jitter field is the upper bound, not an additional delay. + delay["durationMilliseconds"] = 300000 + delay["jitterDurationMilliseconds"] = 305000 + has_node_heartbeat_stage = True + execute_with_retries( + self.k8s_client.apply_manifest_from_file, + manifest_dict=doc, + ) + if not has_node_heartbeat_stage: + raise RuntimeError("Node heartbeat Stage not found in stage-fast.yaml.") + self._replace_config_map() if enable_metrics: metrics_usage_url = (f"https://github.com/{self.kwok_repo}/releases/" diff --git a/modules/python/tests/kwok/test_kwok_node.py b/modules/python/tests/kwok/test_kwok_node.py index 09cfb72752..7531d906c4 100644 --- a/modules/python/tests/kwok/test_kwok_node.py +++ b/modules/python/tests/kwok/test_kwok_node.py @@ -65,6 +65,13 @@ def make_rendered_config_map(): ) +def make_manifest_response(manifest_name): + """Create an HTTP response from the upstream KWOK release fixtures.""" + manifest_path = os.path.join(os.path.dirname(__file__), "v0.7.0", manifest_name) + with open(manifest_path, encoding="utf-8") as manifest_file: + return MagicMock(text=manifest_file.read()) + + def make_base_controller_deployment(image="registry.k8s.io/kwok/kwok:v0.7.0"): """Create a minimal upstream KWOK controller deployment object.""" return client.V1Deployment( @@ -206,13 +213,15 @@ def create_template_side_effect(template_path, replacements): mock_kwok_yaml_response.text = yaml.dump(deployment_dict) try: - with patch('kwok.kwok.requests.get', return_value=mock_kwok_yaml_response): + with patch('kwok.kwok.requests.get', side_effect=[ + mock_kwok_yaml_response, + make_manifest_response("stage-fast.yaml"), + ]): self.node.create() print("Nodes created successfully.") - self.mock_k8s_client.apply_manifest_from_url.assert_has_calls([ - call("https://github.com/kubernetes-sigs/kwok/releases/download/v0.7.0/stage-fast.yaml"), - call("https://github.com/kubernetes-sigs/kwok/releases/download/v0.7.0/metrics-usage.yaml"), - ]) + self.mock_k8s_client.apply_manifest_from_url.assert_called_once_with( + "https://github.com/kubernetes-sigs/kwok/releases/download/v0.7.0/metrics-usage.yaml" + ) controller_manifests = collect_applied_manifests( self.mock_k8s_client.apply_manifest_from_file.call_args_list, @@ -346,13 +355,13 @@ def create_template_side_effect(template_path, replacements): deployment_dict["apiVersion"] = "apps/v1" deployment_dict["kind"] = "Deployment" mock_kwok_yaml_response.text = yaml.dump(deployment_dict) - with patch('kwok.kwok.requests.get', return_value=mock_kwok_yaml_response): + with patch('kwok.kwok.requests.get', side_effect=[ + mock_kwok_yaml_response, + make_manifest_response("stage-fast.yaml"), + ]): grouped_node.create() - self.mock_k8s_client.apply_manifest_from_url.assert_has_calls([ - call("https://github.com/kubernetes-sigs/kwok/releases/download/v0.7.0/stage-fast.yaml"), - ]) - self.assertEqual(self.mock_k8s_client.apply_manifest_from_url.call_count, 1) + self.mock_k8s_client.apply_manifest_from_url.assert_not_called() controller_manifests = collect_applied_manifests( self.mock_k8s_client.apply_manifest_from_file.call_args_list, @@ -405,9 +414,23 @@ def test_bootstrap_returns_controller_deployment(self): deployment_dict["kind"] = "Deployment" mock_response.text = yaml.dump(deployment_dict) - with patch('kwok.kwok.requests.get', return_value=mock_response): + stage_response = make_manifest_response("stage-fast.yaml") + with patch('kwok.kwok.requests.get', side_effect=[mock_response, stage_response]) as mock_get: controller_base = self.node._bootstrap_kwok("v0.7.0", enable_metrics=False) + mock_get.assert_has_calls([ + call("https://github.com/kubernetes-sigs/kwok/releases/download/v0.7.0/kwok.yaml", timeout=30), + call("https://github.com/kubernetes-sigs/kwok/releases/download/v0.7.0/stage-fast.yaml", timeout=30), + ]) + expected_stages = list(yaml.safe_load_all(stage_response.text)) + expected_stages[0]["spec"]["delay"].update( + durationMilliseconds=300000, + jitterDurationMilliseconds=305000, + ) + self.assertEqual( + collect_applied_manifests(self.mock_k8s_client.apply_manifest_from_file.call_args_list, "Stage"), + expected_stages, + ) self.assertIsInstance(controller_base, client.V1Deployment) self.assertEqual( controller_base.spec.template.spec.containers[0].image, @@ -435,6 +458,39 @@ def test_bootstrap_returns_controller_deployment(self): mock_deployment.spec.template.spec.containers[0].args, ) + def test_bootstrap_rejects_missing_node_heartbeat_stage(self): + """Fail instead of silently leaving the upstream heartbeat cadence.""" + stage_response = make_manifest_response("stage-fast.yaml") + stages = list(yaml.safe_load_all(stage_response.text)) + stage_response.text = yaml.safe_dump_all(stages[1:]) + + with patch('kwok.kwok.requests.get', side_effect=[ + make_manifest_response("kwok.yaml"), + stage_response, + ]): + with self.assertRaisesRegex(RuntimeError, "Node heartbeat Stage not found"): + self.node._bootstrap_kwok("v0.7.0", enable_metrics=False) # pylint: disable=protected-access + + self.mock_k8s_client.create_template.assert_not_called() + + def test_bootstrap_rejects_invalid_node_heartbeat_delay(self): + """Fail when the heartbeat Stage has no usable delay configuration.""" + for delay in (None, 300000): + with self.subTest(delay=delay): + stage_response = make_manifest_response("stage-fast.yaml") + stages = list(yaml.safe_load_all(stage_response.text)) + stages[0]["spec"]["delay"] = delay + stage_response.text = yaml.safe_dump_all(stages) + + with patch('kwok.kwok.requests.get', side_effect=[ + make_manifest_response("kwok.yaml"), + stage_response, + ]): + with self.assertRaisesRegex(RuntimeError, "Node heartbeat Stage has no delay configuration"): + self.node._bootstrap_kwok("v0.7.0", enable_metrics=False) # pylint: disable=protected-access + + self.mock_k8s_client.create_template.assert_not_called() + def test_tear_down_nodes(self): """ Test the deletion of KWOK nodes from the cluster. @@ -682,7 +738,10 @@ def setUp(self): deployment.update(apiVersion="apps/v1", kind="Deployment") response = MagicMock(text=yaml.safe_dump(deployment)) self.addCleanup(patch.stopall) - patch("kwok.kwok.requests.get", return_value=response).start() + patch("kwok.kwok.requests.get", side_effect=[ + response, + make_manifest_response("stage-fast.yaml"), + ]).start() self.sleep = patch("kwok.kwok.time.sleep").start() self.output = patch("builtins.print").start() From aab36b417af2be3954e97cfe2c31e5d29a27a80a Mon Sep 17 00:00:00 2001 From: Leonard Du <101617499+LeonardCareer@users.noreply.github.com> Date: Mon, 30 Nov 2026 10:56:38 +1100 Subject: [PATCH 2/5] Simplify KWOK heartbeat manifest update Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- modules/python/kwok/kwok.py | 30 +++++++--------- modules/python/tests/kwok/test_kwok_node.py | 39 +-------------------- 2 files changed, 13 insertions(+), 56 deletions(-) diff --git a/modules/python/kwok/kwok.py b/modules/python/kwok/kwok.py index 4b564ddd6d..134b2dd71b 100644 --- a/modules/python/kwok/kwok.py +++ b/modules/python/kwok/kwok.py @@ -1,7 +1,5 @@ """KWOK (Kubernetes WithOut Kubelet) - Virtual Node/Pod Simulator. -Node status heartbeats run every 300-305 seconds; Lease renewal is unchanged. - Node creation is sequential by default, with 50 ms spacing. Opt in with --node-create-workers; --node-create-qps caps aggregate attempts (including retries) only in parallel mode. Conflict replacement and DRA requests add API @@ -85,7 +83,7 @@ def fetch_latest_release(self): return response.json().get("tag_name") def _bootstrap_kwok(self, kwok_release, enable_metrics): - """Apply the shared KWOK bootstrap manifests and replace the configmap. + """Apply the shared KWOK manifests with a 300-305 second Node heartbeat. Returns the upstream kwok-controller Deployment as a V1Deployment for the caller to use as a template for per-shard controllers. @@ -120,25 +118,21 @@ def _bootstrap_kwok(self, kwok_release, enable_metrics): response = execute_with_retries(requests.get, stage_fast_yaml_url, timeout=30) response.raise_for_status() - has_node_heartbeat_stage = False - for doc in yaml.safe_load_all(response.text): - if not doc: - continue - if (doc.get("kind") == "Stage" - and doc.get("metadata", {}).get("name") == "node-heartbeat-with-lease"): - delay = doc.get("spec", {}).get("delay") - if not isinstance(delay, dict): - raise RuntimeError("Node heartbeat Stage has no delay configuration in stage-fast.yaml.") - # KWOK's jitter field is the upper bound, not an additional delay. - delay["durationMilliseconds"] = 300000 - delay["jitterDurationMilliseconds"] = 305000 - has_node_heartbeat_stage = True + stages = [doc for doc in yaml.safe_load_all(response.text) if doc] + heartbeat = next( + stage for stage in stages + if stage["metadata"]["name"] == "node-heartbeat-with-lease" + ) + # KWOK's jitter field is the upper bound, not an additional delay. + heartbeat["spec"]["delay"].update( + durationMilliseconds=300000, + jitterDurationMilliseconds=305000, + ) + for doc in stages: execute_with_retries( self.k8s_client.apply_manifest_from_file, manifest_dict=doc, ) - if not has_node_heartbeat_stage: - raise RuntimeError("Node heartbeat Stage not found in stage-fast.yaml.") self._replace_config_map() if enable_metrics: diff --git a/modules/python/tests/kwok/test_kwok_node.py b/modules/python/tests/kwok/test_kwok_node.py index 7531d906c4..4e6a44903f 100644 --- a/modules/python/tests/kwok/test_kwok_node.py +++ b/modules/python/tests/kwok/test_kwok_node.py @@ -415,13 +415,9 @@ def test_bootstrap_returns_controller_deployment(self): mock_response.text = yaml.dump(deployment_dict) stage_response = make_manifest_response("stage-fast.yaml") - with patch('kwok.kwok.requests.get', side_effect=[mock_response, stage_response]) as mock_get: + with patch('kwok.kwok.requests.get', side_effect=[mock_response, stage_response]): controller_base = self.node._bootstrap_kwok("v0.7.0", enable_metrics=False) - mock_get.assert_has_calls([ - call("https://github.com/kubernetes-sigs/kwok/releases/download/v0.7.0/kwok.yaml", timeout=30), - call("https://github.com/kubernetes-sigs/kwok/releases/download/v0.7.0/stage-fast.yaml", timeout=30), - ]) expected_stages = list(yaml.safe_load_all(stage_response.text)) expected_stages[0]["spec"]["delay"].update( durationMilliseconds=300000, @@ -458,39 +454,6 @@ def test_bootstrap_returns_controller_deployment(self): mock_deployment.spec.template.spec.containers[0].args, ) - def test_bootstrap_rejects_missing_node_heartbeat_stage(self): - """Fail instead of silently leaving the upstream heartbeat cadence.""" - stage_response = make_manifest_response("stage-fast.yaml") - stages = list(yaml.safe_load_all(stage_response.text)) - stage_response.text = yaml.safe_dump_all(stages[1:]) - - with patch('kwok.kwok.requests.get', side_effect=[ - make_manifest_response("kwok.yaml"), - stage_response, - ]): - with self.assertRaisesRegex(RuntimeError, "Node heartbeat Stage not found"): - self.node._bootstrap_kwok("v0.7.0", enable_metrics=False) # pylint: disable=protected-access - - self.mock_k8s_client.create_template.assert_not_called() - - def test_bootstrap_rejects_invalid_node_heartbeat_delay(self): - """Fail when the heartbeat Stage has no usable delay configuration.""" - for delay in (None, 300000): - with self.subTest(delay=delay): - stage_response = make_manifest_response("stage-fast.yaml") - stages = list(yaml.safe_load_all(stage_response.text)) - stages[0]["spec"]["delay"] = delay - stage_response.text = yaml.safe_dump_all(stages) - - with patch('kwok.kwok.requests.get', side_effect=[ - make_manifest_response("kwok.yaml"), - stage_response, - ]): - with self.assertRaisesRegex(RuntimeError, "Node heartbeat Stage has no delay configuration"): - self.node._bootstrap_kwok("v0.7.0", enable_metrics=False) # pylint: disable=protected-access - - self.mock_k8s_client.create_template.assert_not_called() - def test_tear_down_nodes(self): """ Test the deletion of KWOK nodes from the cluster. From 505581b24e0a80b44fc970c740fc7f1bd6bd9a16 Mon Sep 17 00:00:00 2001 From: Leonard Du <101617499+LeonardCareer@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:34:16 +1100 Subject: [PATCH 3/5] Simplify KWOK heartbeat lookup Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- modules/python/kwok/kwok.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/modules/python/kwok/kwok.py b/modules/python/kwok/kwok.py index 134b2dd71b..3c09d98a25 100644 --- a/modules/python/kwok/kwok.py +++ b/modules/python/kwok/kwok.py @@ -119,12 +119,8 @@ def _bootstrap_kwok(self, kwok_release, enable_metrics): response = execute_with_retries(requests.get, stage_fast_yaml_url, timeout=30) response.raise_for_status() stages = [doc for doc in yaml.safe_load_all(response.text) if doc] - heartbeat = next( - stage for stage in stages - if stage["metadata"]["name"] == "node-heartbeat-with-lease" - ) # KWOK's jitter field is the upper bound, not an additional delay. - heartbeat["spec"]["delay"].update( + next(doc for doc in stages if doc["metadata"]["name"] == "node-heartbeat-with-lease")["spec"]["delay"].update( durationMilliseconds=300000, jitterDurationMilliseconds=305000, ) From 103de5036151bcbffe0eb30b36b4de8a3af83d6e Mon Sep 17 00:00:00 2001 From: Leonard Du <101617499+LeonardCareer@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:05:11 +1100 Subject: [PATCH 4/5] Keep KWOK heartbeat test focused on delay Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- modules/python/tests/kwok/test_kwok_node.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/modules/python/tests/kwok/test_kwok_node.py b/modules/python/tests/kwok/test_kwok_node.py index 4e6a44903f..f990bd5763 100644 --- a/modules/python/tests/kwok/test_kwok_node.py +++ b/modules/python/tests/kwok/test_kwok_node.py @@ -414,18 +414,13 @@ def test_bootstrap_returns_controller_deployment(self): deployment_dict["kind"] = "Deployment" mock_response.text = yaml.dump(deployment_dict) - stage_response = make_manifest_response("stage-fast.yaml") - with patch('kwok.kwok.requests.get', side_effect=[mock_response, stage_response]): + with patch('kwok.kwok.requests.get', side_effect=[mock_response, make_manifest_response("stage-fast.yaml")]): controller_base = self.node._bootstrap_kwok("v0.7.0", enable_metrics=False) - expected_stages = list(yaml.safe_load_all(stage_response.text)) - expected_stages[0]["spec"]["delay"].update( - durationMilliseconds=300000, - jitterDurationMilliseconds=305000, - ) + stages = collect_applied_manifests(self.mock_k8s_client.apply_manifest_from_file.call_args_list, "Stage") self.assertEqual( - collect_applied_manifests(self.mock_k8s_client.apply_manifest_from_file.call_args_list, "Stage"), - expected_stages, + stages[0]["spec"]["delay"], + {"durationMilliseconds": 300000, "jitterDurationMilliseconds": 305000}, ) self.assertIsInstance(controller_base, client.V1Deployment) self.assertEqual( From 96398787fdc1defddb65e44d44bb65fa760263ff Mon Sep 17 00:00:00 2001 From: Leonard Du <101617499+LeonardCareer@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:15:10 +1100 Subject: [PATCH 5/5] docs: retain ConfigMap replacement in KWOK bootstrap docstring Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- modules/python/kwok/kwok.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/python/kwok/kwok.py b/modules/python/kwok/kwok.py index 3c09d98a25..22e380eb41 100644 --- a/modules/python/kwok/kwok.py +++ b/modules/python/kwok/kwok.py @@ -83,7 +83,7 @@ def fetch_latest_release(self): return response.json().get("tag_name") def _bootstrap_kwok(self, kwok_release, enable_metrics): - """Apply the shared KWOK manifests with a 300-305 second Node heartbeat. + """Apply the shared KWOK manifests with a 300-305 second Node heartbeat and replace the ConfigMap. Returns the upstream kwok-controller Deployment as a V1Deployment for the caller to use as a template for per-shard controllers.