From 26929b24b80eb1fccfa0c7b41c430c153a228dd7 Mon Sep 17 00:00:00 2001 From: amine Date: Thu, 2 Nov 2023 07:33:03 +0100 Subject: [PATCH 1/4] Apply random value to service name to handle the same agent declared multiple times. --- src/ostorlab/runtimes/lite_local/agent_runtime.py | 9 ++++++++- src/ostorlab/runtimes/local/agent_runtime.py | 7 +++++++ tests/runtimes/lite_local/runtime_test.py | 7 +++++-- tests/runtimes/local/agent_runtime_test.py | 3 ++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ostorlab/runtimes/lite_local/agent_runtime.py b/src/ostorlab/runtimes/lite_local/agent_runtime.py index e8c665d3c..043dc0dd6 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 MAX_SERVICE_NAME_LEN - len(service_name) - MAX_RANDOM_NAME_LEN > 0: + 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..cf90f4735 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 MAX_SERVICE_NAME_LEN - len(service_name) - MAX_RANDOM_NAME_LEN > 0: + 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..ec48fae29 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( diff --git a/tests/runtimes/local/agent_runtime_test.py b/tests/runtimes/local/agent_runtime_test.py index c6b10561b..74e1220fc 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,7 @@ def testCreateAgentService_whenAgentDefAndAgentSettingsAreNotEmpty_serviceCreate # assert arguments were overridden by the agent settings. assert kwargs["resources"]["Limits"]["MemoryBytes"] == 700000 + assert len(kwargs["name"]) < 63 assert kwargs["mounts"] == ["settings_mount1"] assert kwargs["endpoint_spec"]["Ports"][0]["PublishedPort"] == 40000 assert kwargs["restart_policy"]["Condition"] == "on-failure" From d4128400043ad4738757cf49f99d4bb2a6c91243 Mon Sep 17 00:00:00 2001 From: amine Date: Thu, 2 Nov 2023 07:35:11 +0100 Subject: [PATCH 2/4] Apply random value to service name to handle the same agent declared multiple times. --- src/ostorlab/runtimes/lite_local/agent_runtime.py | 2 +- src/ostorlab/runtimes/local/agent_runtime.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ostorlab/runtimes/lite_local/agent_runtime.py b/src/ostorlab/runtimes/lite_local/agent_runtime.py index 043dc0dd6..d05a51bbe 100644 --- a/src/ostorlab/runtimes/lite_local/agent_runtime.py +++ b/src/ostorlab/runtimes/lite_local/agent_runtime.py @@ -354,7 +354,7 @@ def create_agent_service( ) # We apply the random str only if it will not break the max docker service name characters (63) - if MAX_SERVICE_NAME_LEN - len(service_name) - MAX_RANDOM_NAME_LEN > 0: + if len(service_name) + MAX_RANDOM_NAME_LEN < MAX_SERVICE_NAME_LEN: service_name = service_name + "_" + str(random.randrange(0, 9999)) env = [ diff --git a/src/ostorlab/runtimes/local/agent_runtime.py b/src/ostorlab/runtimes/local/agent_runtime.py index cf90f4735..41e2c2e55 100644 --- a/src/ostorlab/runtimes/local/agent_runtime.py +++ b/src/ostorlab/runtimes/local/agent_runtime.py @@ -345,7 +345,7 @@ def create_agent_service( ) # We apply the random str only if it will not break the max docker service name characters (63) - if MAX_SERVICE_NAME_LEN - len(service_name) - MAX_RANDOM_NAME_LEN > 0: + if len(service_name) + MAX_RANDOM_NAME_LEN < MAX_SERVICE_NAME_LEN: service_name = service_name + "_" + str(random.randrange(0, 9999)) env = [ From b51933b3956b6959a1e799b4629681e3a5c866ad Mon Sep 17 00:00:00 2001 From: amine Date: Thu, 2 Nov 2023 07:47:43 +0100 Subject: [PATCH 3/4] Apply random value to service name to handle the same agent declared multiple times. --- tests/runtimes/lite_local/runtime_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/runtimes/lite_local/runtime_test.py b/tests/runtimes/lite_local/runtime_test.py index ec48fae29..f6f95643d 100644 --- a/tests/runtimes/lite_local/runtime_test.py +++ b/tests/runtimes/lite_local/runtime_test.py @@ -258,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"] From 72586fe38b60e912e7048a9b873575b5b32ece7f Mon Sep 17 00:00:00 2001 From: amine Date: Thu, 2 Nov 2023 15:05:02 +0100 Subject: [PATCH 4/4] Apply random value to service name to handle the same agent declared multiple times. --- tests/runtimes/local/agent_runtime_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/runtimes/local/agent_runtime_test.py b/tests/runtimes/local/agent_runtime_test.py index 74e1220fc..ea29f8285 100644 --- a/tests/runtimes/local/agent_runtime_test.py +++ b/tests/runtimes/local/agent_runtime_test.py @@ -84,6 +84,7 @@ 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"