Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions modules/python/kwok/kwok.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we make this interval configurable rather than changing the default? The longer upstream interval might be intentional to reduce API server load at scale

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I’ll make the interval configurable, with 300–305 seconds as the default to align with kubelet’s default 5-minute reporting interva

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/"
Expand Down
41 changes: 29 additions & 12 deletions modules/python/tests/kwok/test_kwok_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down
Loading