Skip to content

Commit 00cf339

Browse files
committed
refactor: Consolidate TPU info CLI command execution and enhance validation handling
1 parent 5b86b8e commit 00cf339

2 files changed

Lines changed: 28 additions & 55 deletions

File tree

dags/tpu_observability/tpu_info_format_validation_dags.py

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -273,55 +273,24 @@ def validate_latency_table(tpu_info_output: list[tpu_info.Table]):
273273
)
274274

275275

276-
def execute_tpu_info_cli_command(info, pod_name: str, tpu_args: str) -> str:
277-
"""Helper to handle KUBECONFIG and execute kubectl."""
278-
with tempfile.NamedTemporaryFile() as temp_config_file:
279-
env = os.environ.copy()
280-
env["KUBECONFIG"] = temp_config_file.name
281-
282-
cmd = " && ".join([
283-
jobset.Command.get_credentials_command(info),
284-
f"kubectl exec {pod_name} -n default -- {tpu_args}",
285-
])
286-
return subprocess.run_exec(cmd, env=env)
287-
288-
289276
@task
290277
def validate_tpu_info_cli(info: node_pool.Info, pod_name: str) -> None:
291-
"""Validates tpu-info CLI commands with internal expectation patterns."""
292-
278+
"""Validates tpu-info CLI commands using the consolidated utility."""
293279
validation_spec = {
294-
tpu_info.TpuInfoCmd.HELP: [
295-
"Display TPU info and metrics.",
296-
"options:",
297-
"-h, --help",
298-
"-v, --version",
299-
"-p, --process",
300-
"--streaming",
301-
"--rate RATE",
302-
],
303-
tpu_info.TpuInfoCmd.VERSION: [
304-
"tpu-info version:",
305-
"libtpu version:",
306-
"accelerator type:",
307-
],
308-
tpu_info.TpuInfoCmd.PROCESS: [
309-
"TPU Process Info",
310-
"Chip",
311-
"PID",
312-
"Process Name",
313-
"/dev/vfio/",
314-
"python",
315-
],
280+
tpu_info.TpuInfoCmd.HELP: ["--streaming", "--rate RATE"],
281+
tpu_info.TpuInfoCmd.VERSION: ["tpu-info version:", "libtpu version:"],
282+
tpu_info.TpuInfoCmd.PROCESS: ["TPU Process Info", "/dev/vfio/", "python"],
316283
}
317284

318285
for cmd_enum, patterns in validation_spec.items():
319-
output = execute_tpu_info_cli_command(info, pod_name, cmd_enum.value)
286+
output = tpu_info.get_tpu_info_from_pod(
287+
info, pod_name, cmd_str=cmd_enum.value
288+
)
289+
320290
for pattern in patterns:
321291
if pattern not in output:
322292
raise AssertionError(
323-
f"Validation failed for '{cmd_enum.value}': "
324-
f"Missing expected pattern '{pattern}'."
293+
f"Validation failed for '{cmd_enum.value}': Missing pattern '{pattern}'."
325294
)
326295

327296

dags/tpu_observability/utils/tpu_info_util.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class TpuInfoCmd(Enum):
1616
"""Defines the available tpu-info CLI commands."""
1717

18+
TPU_INFO = "tpu-info"
1819
HELP = "tpu-info -help"
1920
VERSION = "tpu-info --version"
2021
PROCESS = "tpu-info --process"
@@ -36,7 +37,8 @@ def parse_body(self):
3637
"""Parses the raw_body string to populate the structured body attribute."""
3738

3839
class TableLineIndex(IntEnum):
39-
"""Below is an example of the text returned by tpu-info, formatted as a table.
40+
"""Below is an example of the text returned by tpu-info,
41+
formatted as a table.
4042
4143
TPU Chips
4244
┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━┓
@@ -109,34 +111,36 @@ def parse_tpu_info_output(output: str) -> list[Table]:
109111
return parsed_tables
110112

111113

112-
@task
113-
def get_tpu_info_from_pod(info: node_pool.Info, pod_name: str) -> str:
114+
def get_tpu_info_from_pod(
115+
info: node_pool.Info, pod_name: str, cmd_str: str
116+
) -> str:
114117
"""
115-
Executes the `tpu-info` command in a specified pod and returns its output.
116-
117-
This task uses kubectl to run the 'tpu-info' command inside the given pod
118-
in the 'default' namespace. The output of the command is captured and
119-
returned.
120-
121-
Args:
122-
kubeconfig: The path to the kubeconfig file.
123-
pod_name: The name of the pod to execute the command in.
118+
Executes a command (default: tpu-info) in a specified pod
119+
and returns its output.
124120
125-
Returns:
126-
The standard output from the 'tpu-info' command.
121+
Consolidated version that handles both standard tpu-info calls and
122+
specific CLI flag validation.
127123
"""
128124
with tempfile.NamedTemporaryFile() as temp_config_file:
129125
env = os.environ.copy()
130126
env["KUBECONFIG"] = temp_config_file.name
131127

132128
cmd = " && ".join([
133129
jobset.Command.get_credentials_command(info),
134-
f"kubectl exec {pod_name} -n default -- tpu-info",
130+
f"kubectl exec {pod_name} -n default -- {cmd_str}",
135131
])
136132

137133
return subprocess.run_exec(cmd, env=env)
138134

139135

136+
@task
137+
def get_tpu_info_from_pod_task(
138+
info: node_pool.Info, pod_name: str, cmd_str: str
139+
) -> str:
140+
"""Airflow task wrapper for get_tpu_info_from_pod."""
141+
return get_tpu_info_from_pod(info, pod_name, cmd_str)
142+
143+
140144
if __name__ == "__main__":
141145
full_output = """
142146
Libtpu version: 0.0.23

0 commit comments

Comments
 (0)