diff --git a/src/ostorlab/runtimes/lite_local/agent_runtime.py b/src/ostorlab/runtimes/lite_local/agent_runtime.py index e8c665d3c..d05a51bbe 100644 --- a/src/ostorlab/runtimes/lite_local/agent_runtime.py +++ b/src/ostorlab/runtimes/lite_local/agent_runtime.py @@ -10,6 +10,7 @@ import logging import uuid import base64 +import random from typing import List, Optional import docker @@ -33,6 +34,8 @@ HEALTHCHECK_TIMEOUT = 30 * SECOND HEALTHCHECK_START_PERIOD = 2 * SECOND HEALTHCHECK_INTERVAL = 30 * SECOND +MAX_SERVICE_NAME_LEN = 63 +MAX_RANDOM_NAME_LEN = 5 class Error(exceptions.OstorlabError): @@ -345,11 +348,15 @@ def create_agent_service( service_name = ( agent_definition.service_name - or self.agent.container_image.replace(":", "_").replace(".", "") + or self.agent.container_image.split(":")[0].replace(".", "") + "_" + self.runtime_name ) + # We apply the random str only if it will not break the max docker service name characters (63) + if len(service_name) + MAX_RANDOM_NAME_LEN < MAX_SERVICE_NAME_LEN: + service_name = service_name + "_" + str(random.randrange(0, 9999)) + env = [ f"UNIVERSE={self.runtime_name}", ] diff --git a/src/ostorlab/runtimes/local/agent_runtime.py b/src/ostorlab/runtimes/local/agent_runtime.py index ad3d59104..41e2c2e55 100644 --- a/src/ostorlab/runtimes/local/agent_runtime.py +++ b/src/ostorlab/runtimes/local/agent_runtime.py @@ -9,6 +9,7 @@ import hashlib import uuid import base64 +import random from typing import List, Optional import docker @@ -35,6 +36,8 @@ HEALTHCHECK_TIMEOUT = 30 * SECOND HEALTHCHECK_START_PERIOD = 2 * SECOND HEALTHCHECK_INTERVAL = 30 * SECOND +MAX_SERVICE_NAME_LEN = 63 +MAX_RANDOM_NAME_LEN = 5 class Error(exceptions.OstorlabError): @@ -341,6 +344,10 @@ def create_agent_service( + self.runtime_name ) + # We apply the random str only if it will not break the max docker service name characters (63) + if len(service_name) + MAX_RANDOM_NAME_LEN < MAX_SERVICE_NAME_LEN: + service_name = service_name + "_" + str(random.randrange(0, 9999)) + env = [ f"UNIVERSE={self.runtime_name}", ] diff --git a/tests/runtimes/lite_local/runtime_test.py b/tests/runtimes/lite_local/runtime_test.py index 66cedeb91..f6f95643d 100644 --- a/tests/runtimes/lite_local/runtime_test.py +++ b/tests/runtimes/lite_local/runtime_test.py @@ -129,7 +129,7 @@ def testLiteLocalCreateAgentService_whenAgentDefAndAgentSettingsAreNotEmpty_serv name="agent_name_from_def", mounts=["def_mount1", "def_mount2"], mem_limit=420000, - service_name="my_service", + service_name="complex_long_name_special_duplicate_duplicate_name_agent_def", restart_policy="", ) mocker.patch( @@ -185,7 +185,10 @@ def testLiteLocalCreateAgentService_whenAgentDefAndAgentSettingsAreNotEmpty_serv assert kwargs["resources"]["Limits"]["MemoryBytes"] == 700000 assert kwargs["mounts"] == ["settings_mount1"] assert kwargs["restart_policy"]["Condition"] == "on-failure" - assert kwargs["name"] == "my_service" + assert len(kwargs["name"]) < 63 + assert ( + kwargs["name"] == "complex_long_name_special_duplicate_duplicate_name_agent_def" + ) def testLiteLocalCreateAgentService_whenAgentDefAndAgentSettingsCapsAreNotEmpty_serviceCreatedwithAgentSettings( @@ -255,5 +258,5 @@ def testLiteLocalCreateAgentService_whenAgentDefAndAgentSettingsCapsAreNotEmpty_ assert kwargs["resources"]["Limits"]["MemoryBytes"] == 700000 assert kwargs["mounts"] == ["settings_mount1"] assert kwargs["restart_policy"]["Condition"] == "on-failure" - assert kwargs["name"] == "my_service" + assert "my_service_" in kwargs["name"] assert kwargs["cap_add"] == ["NET_ADMIN"] diff --git a/tests/runtimes/local/agent_runtime_test.py b/tests/runtimes/local/agent_runtime_test.py index c6b10561b..ea29f8285 100644 --- a/tests/runtimes/local/agent_runtime_test.py +++ b/tests/runtimes/local/agent_runtime_test.py @@ -11,7 +11,7 @@ def container_name_mock(name): del name - return "name" + return "complex_long_name_special_duplicate_agent:v1026" def testCreateAgentService_whenAgentDefAndAgentSettingsAreNotEmpty_serviceCreatedwithAgentSettings( @@ -83,6 +83,8 @@ def testCreateAgentService_whenAgentDefAndAgentSettingsAreNotEmpty_serviceCreate # assert arguments were overridden by the agent settings. assert kwargs["resources"]["Limits"]["MemoryBytes"] == 700000 + assert len(kwargs["name"]) < 63 + assert "complex_long_name_special_duplicate_agent_42" in kwargs["name"] assert kwargs["mounts"] == ["settings_mount1"] assert kwargs["endpoint_spec"]["Ports"][0]["PublishedPort"] == 40000 assert kwargs["restart_policy"]["Condition"] == "on-failure"