diff --git a/modules/python/kwok/kwok.py b/modules/python/kwok/kwok.py index ae01a829b2..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 bootstrap manifests and replace the configmap. + """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. @@ -116,10 +116,20 @@ 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() + stages = [doc for doc in yaml.safe_load_all(response.text) if doc] + # KWOK's jitter field is the upper bound, not an additional delay. + next(doc for doc in stages if doc["metadata"]["name"] == "node-heartbeat-with-lease")["spec"]["delay"].update( + durationMilliseconds=300000, + jitterDurationMilliseconds=305000, ) + for doc in stages: + execute_with_retries( + self.k8s_client.apply_manifest_from_file, + manifest_dict=doc, + ) + 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..f990bd5763 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,14 @@ 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): + 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) + stages = collect_applied_manifests(self.mock_k8s_client.apply_manifest_from_file.call_args_list, "Stage") + self.assertEqual( + stages[0]["spec"]["delay"], + {"durationMilliseconds": 300000, "jitterDurationMilliseconds": 305000}, + ) self.assertIsInstance(controller_base, client.V1Deployment) self.assertEqual( controller_base.spec.template.spec.containers[0].image, @@ -682,7 +696,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()