Skip to content

Commit 95e073e

Browse files
pintaoz-awspintaoz
andauthored
Restructure list-cluster output (#173)
Co-authored-by: pintaoz <[email protected]>
1 parent ec8800d commit 95e073e

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/sagemaker/hyperpod/cli/commands/cluster.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def list_cluster(
233233
print(tabulate(cluster_capacities, headers=headers, tablefmt="presto"))
234234
elif output == OutputFormat.JSON.value:
235235
json_list = [dict(zip(headers, value)) for value in cluster_capacities]
236-
_restructure_output(json_list, namespace)
236+
json_list = _restructure_output(json_list, namespace)
237237
print(json.dumps(json_list, indent=4))
238238

239239

@@ -379,23 +379,34 @@ def _get_hyperpod_clusters(sm_client: boto3.client) -> List[str]:
379379

380380

381381
def _restructure_output(summary_list, namespaces):
382-
if not namespaces:
383-
return
382+
cluster_dict = dict()
384383

385384
for node_summary in summary_list:
386-
node_summary["Namespaces"] = {}
387-
for ns in namespaces:
388-
available_accelerators = node_summary[
389-
ns + AVAILABLE_ACCELERATOR_DEVICES_KEY
390-
]
391-
total_accelerators = node_summary[ns + TOTAL_ACCELERATOR_DEVICES_KEY]
392-
quota_accelerator_info = {
393-
AVAILABLE_ACCELERATOR_DEVICES_KEY: available_accelerators,
394-
TOTAL_ACCELERATOR_DEVICES_KEY: total_accelerators,
385+
cluster_name = node_summary["Cluster"]
386+
if cluster_name not in cluster_dict:
387+
cluster_dict[cluster_name] = {
388+
"Cluster": cluster_name,
389+
"Instances": []
395390
}
396-
node_summary["Namespaces"][ns] = quota_accelerator_info
397-
node_summary.pop(ns + AVAILABLE_ACCELERATOR_DEVICES_KEY, None)
398-
node_summary.pop(ns + TOTAL_ACCELERATOR_DEVICES_KEY, None)
391+
node_summary.pop("Cluster")
392+
if namespaces:
393+
node_summary["Namespaces"] = {}
394+
for ns in namespaces:
395+
available_accelerators = node_summary[
396+
ns + AVAILABLE_ACCELERATOR_DEVICES_KEY
397+
]
398+
total_accelerators = node_summary[ns + TOTAL_ACCELERATOR_DEVICES_KEY]
399+
quota_accelerator_info = {
400+
AVAILABLE_ACCELERATOR_DEVICES_KEY: available_accelerators,
401+
TOTAL_ACCELERATOR_DEVICES_KEY: total_accelerators,
402+
}
403+
node_summary["Namespaces"][ns] = quota_accelerator_info
404+
node_summary.pop(ns + AVAILABLE_ACCELERATOR_DEVICES_KEY, None)
405+
node_summary.pop(ns + TOTAL_ACCELERATOR_DEVICES_KEY, None)
406+
cluster_dict[cluster_name]["Instances"].append(node_summary)
407+
408+
return list(cluster_dict.values())
409+
399410

400411

401412
def _aggregate_nodes_info(

test/unit_tests/test_cluster.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,7 @@ def test_get_clusters_maximum_number(
422422
self.assertIn("cluster-2", result.output)
423423
# Expect JSON output
424424
output = json.loads(result.output)
425-
# Each cluster has 2 instance type, so total output size is 2 * 50 = 100
426-
self.assertTrue(len(output) == 100)
425+
self.assertEqual(len(output), 50)
427426

428427
@mock.patch("kubernetes.config.load_kube_config")
429428
@mock.patch("boto3.Session")

0 commit comments

Comments
 (0)