Skip to content

Commit 2b1f901

Browse files
pintaoz-awspintaoz
andauthored
Trim long user name for kubernetes label (#80)
* Trim long user name for kubernetes label * Add dots in the end to indicate the username is trimmed * Add -trimmed at the end of trimmed username --------- Co-authored-by: pintaoz <[email protected]>
1 parent 8198706 commit 2b1f901

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/hyperpod_cli/commands/job.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,4 +1142,11 @@ def get_user_name():
11421142
user_name = 'Unknown'
11431143

11441144
# label value does not allow slash
1145-
return user_name.replace('/', '-')
1145+
user_name = user_name.replace('/', '-')
1146+
# 63 is the max length for a Kubernetes label
1147+
if len(user_name) > 63:
1148+
# Add dots in the end to indicate the username is trimmed
1149+
trimmed_user_name = user_name[:55] + '-trimmed'
1150+
logger.warning(f"The username is longer than the maximum length (63) of Kubernetes label, trimming to {trimmed_user_name}")
1151+
return trimmed_user_name
1152+
return user_name

test/unit_tests/test_job.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from hyperpod_cli.commands.job import (
2828
cancel_job,
2929
get_job,
30+
get_user_name,
3031
list_jobs,
3132
list_pods,
3233
patch_job,
@@ -2138,3 +2139,16 @@ def test_start_job_with_recipe_missing_required_args(self):
21382139
],
21392140
)
21402141
self.assertNotEqual(result.exit_code, 0)
2142+
2143+
@mock.patch("boto3.client")
2144+
def test_get_user_name_long_arn(self, mock_boto3_client):
2145+
mock_client = MagicMock()
2146+
mock_boto3_client.return_value = mock_client
2147+
2148+
mock_identity = MagicMock()
2149+
mock_client.get_caller_identity.return_value = mock_identity
2150+
2151+
long_user_name = "ReadOnly/AmazonSageMaker-a-long-long-long-long-long-long-long-arn-for-testing-get-user-name"
2152+
long_arn = "arn:aws:iam::0123456789012:assumed-role/" + long_user_name
2153+
mock_identity.get.return_value = long_arn
2154+
self.assertEqual(get_user_name(), "AssumedRole-" + long_user_name.replace("/", "-")[:43] + "-trimmed")

0 commit comments

Comments
 (0)