Repository: aws/sagemaker-hyperpod-cli

Issue:
sagemaker.user/created-by label value exceeding max length
When the user role name is exceeding 63 characters, which is the max length for a Kubernetes label, the helm install command executed by the launcher will fail.

The value should be trimmed. Also, when the helm install operation fails in the launcher, no logs are emitted by the HyperPod CLI.

Hints from issue comments:
@zhaoqizqwang: Closing this issue since fix has been merged. Opened a separate issue to track error message: https://github.com/aws/sagemaker-hyperpod-cli/issues/84

Solution patch:
diff --git a/src/hyperpod_cli/commands/job.py b/src/hyperpod_cli/commands/job.py
index 6461d39..02152aa 100644
--- a/src/hyperpod_cli/commands/job.py
+++ b/src/hyperpod_cli/commands/job.py
@@ -1142,4 +1142,11 @@ def get_user_name():
         user_name = 'Unknown'
     
     # label value does not allow slash
-    return user_name.replace('/', '-')
+    user_name = user_name.replace('/', '-')
+    # 63 is the max length for a Kubernetes label
+    if len(user_name) > 63:
+        # Add dots in the end to indicate the username is trimmed
+        trimmed_user_name = user_name[:55] + '-trimmed'
+        logger.warning(f"The username is longer than the maximum length (63) of Kubernetes label, trimming to {trimmed_user_name}")
+        return trimmed_user_name
+    return user_name


Test patch:
diff --git a/test/unit_tests/test_job.py b/test/unit_tests/test_job.py
index 30a6e0d..15d4527 100644
--- a/test/unit_tests/test_job.py
+++ b/test/unit_tests/test_job.py
@@ -27,6 +27,7 @@ from kubernetes.client import (
 from hyperpod_cli.commands.job import (
     cancel_job,
     get_job,
+    get_user_name,
     list_jobs,
     list_pods,
     patch_job,
@@ -2138,3 +2139,16 @@ class JobTest(unittest.TestCase):
             ],
         )
         self.assertNotEqual(result.exit_code, 0)
+
+    @mock.patch("boto3.client")
+    def test_get_user_name_long_arn(self, mock_boto3_client):
+        mock_client = MagicMock()
+        mock_boto3_client.return_value = mock_client
+
+        mock_identity = MagicMock()
+        mock_client.get_caller_identity.return_value = mock_identity
+
+        long_user_name = "ReadOnly/AmazonSageMaker-a-long-long-long-long-long-long-long-arn-for-testing-get-user-name"
+        long_arn = "arn:aws:iam::0123456789012:assumed-role/" + long_user_name
+        mock_identity.get.return_value = long_arn
+        self.assertEqual(get_user_name(), "AssumedRole-" + long_user_name.replace("/", "-")[:43] + "-trimmed")