Skip to content

Commit 3d34b7f

Browse files
clumsyazzhipa
authored andcommitted
fix: newlines for messages in k8s log tail
1 parent 68c629e commit 3d34b7f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

torchx/schedulers/kubernetes_scheduler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,10 @@ def log_iter(
10731073
core_api = client.CoreV1Api(self._api_client())
10741074
if should_tail:
10751075
w = watch.Watch()
1076-
iterator = w.stream(core_api.read_namespaced_pod_log, **args)
1076+
iterator = (
1077+
f"{line}\n"
1078+
for line in w.stream(core_api.read_namespaced_pod_log, **args)
1079+
)
10771080
else:
10781081
resp = core_api.read_namespaced_pod_log(**args)
10791082
iterator = split_lines(resp)

torchx/schedulers/test/kubernetes_scheduler_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,25 @@ def test_log_iter(self, read_namespaced_pod_log: MagicMock) -> None:
11441144
},
11451145
)
11461146

1147+
@patch("kubernetes.watch.Watch.stream")
1148+
def test_log_iter_tail(self, watch_stream: MagicMock) -> None:
1149+
scheduler = create_scheduler("test")
1150+
watch_stream.return_value = iter(["line1", "line2", "line3"])
1151+
lines = scheduler.log_iter(
1152+
app_id="testnamespace:testjob",
1153+
role_name="role_blah",
1154+
k=1,
1155+
should_tail=True,
1156+
)
1157+
self.assertEqual(
1158+
list(lines),
1159+
[
1160+
"line1\n",
1161+
"line2\n",
1162+
"line3\n",
1163+
],
1164+
)
1165+
11471166
def test_push_patches(self) -> None:
11481167
# Configure mock to return proper response for schedule() call
11491168
self.mock_create.return_value = {"metadata": {"name": "testjob"}}

0 commit comments

Comments
 (0)