Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply random value to service name to handle the same agent declared multiple times. #521

Merged
merged 7 commits into from
Nov 3, 2023
9 changes: 8 additions & 1 deletion src/ostorlab/runtimes/lite_local/agent_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging
import uuid
import base64
import random
from typing import List, Optional

import docker
Expand All @@ -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):
Expand Down Expand Up @@ -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}",
]
Expand Down
7 changes: 7 additions & 0 deletions src/ostorlab/runtimes/local/agent_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hashlib
import uuid
import base64
import random
from typing import List, Optional

import docker
Expand All @@ -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):
Expand Down Expand Up @@ -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}",
]
Expand Down
9 changes: 6 additions & 3 deletions tests/runtimes/lite_local/runtime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"]
3 changes: 2 additions & 1 deletion tests/runtimes/local/agent_runtime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
amine3 marked this conversation as resolved.
Show resolved Hide resolved
assert kwargs["mounts"] == ["settings_mount1"]
assert kwargs["endpoint_spec"]["Ports"][0]["PublishedPort"] == 40000
assert kwargs["restart_policy"]["Condition"] == "on-failure"
Expand Down
Loading