Skip to content

Commit

Permalink
Azure: truncate long names better.
Browse files Browse the repository at this point in the history
The resource group name limits to 80 (90 max) and vm name limits to
50 (64 max). the vm name will attach suffix, so it limits more.

The leading "lisa-" keeps in the name for two reasons.
1. It's needed to identify auto created envs, and delete by pipelines.
2. It avoids the random start chars like "-". It causes resource
    creation failure, because Azure accept name starts with chars only.
  • Loading branch information
squirrelsc committed Mar 25, 2022
1 parent d357de1 commit f5c2b05
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lisa/sut_orchestrator/azure/platform_.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
get_public_key_data,
plugin_manager,
set_filtered_fields,
truncate_keep_prefix,
)
from lisa.util.logger import Logger
from lisa.util.parallel import run_in_parallel
Expand Down Expand Up @@ -482,11 +483,11 @@ def _deploy_environment(self, environment: Environment, log: Logger) -> None:
resource_group_name = self._azure_runbook.resource_group_name
else:
normalized_name = constants.NORMALIZE_PATTERN.sub("-", constants.RUN_NAME)
# Take last chars to make sure the length is to exceed max 64 chars
# allowed in vm names. The last chars include the datetime pattern,
# it's more unique than leading project/test pass names.
normalized_name = normalized_name[-40:]
resource_group_name = f"{normalized_name}-e{environment.id}"
# Take last chars to make sure the length is to exceed max 90 chars
# allowed in resource group name.
resource_group_name = truncate_keep_prefix(
f"{normalized_name}-e{environment.id}", 80
)
environment_context.resource_group_is_created = True

environment_context.resource_group_name = resource_group_name
Expand Down Expand Up @@ -1116,7 +1117,12 @@ def _create_node_runbook(
)

if not azure_node_runbook.name:
azure_node_runbook.name = f"{name_prefix}-n{index}"
# the max length of vm name is 64 chars. Below logic takes last 40
# chars in resource group name and keep the leading "lisa-". So it's
# easy to identify it's a lisa created node.
azure_node_runbook.name = truncate_keep_prefix(
f"{name_prefix}-n{index}", 50
)
if not azure_node_runbook.vm_size:
raise LisaException("vm_size is not detected before deploy")
if not azure_node_runbook.location:
Expand Down
23 changes: 23 additions & 0 deletions lisa/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)

import pluggy
from assertpy import assert_that
from dataclasses_json import config
from marshmallow import fields
from semver import VersionInfo
Expand Down Expand Up @@ -492,3 +493,25 @@ def field_metadata(

def is_unittest() -> bool:
return "unittest" in sys.argv[0]


def truncate_keep_prefix(content: str, kept_len: int, prefix: str = "lisa-") -> str:
"""
This method is used to truncate names, when some resource has length
limitation. It keeps meaningful part and the defined prefix.
The last chars include the datetime pattern, it's more unique than leading
project/test pass names. The name is used to identify lisa deployed
environment too, so it needs to keep the leading "lisa-" after truncated.
This makes the name from lisa-long-name... to lisa-name...
"""
if not content.startswith(prefix):
assert_that(content).described_as(
"truncate_keep_prefix should be start with prefix"
).starts_with(prefix)
if kept_len < len(prefix):
assert_that(len(prefix)).described_as(
f"kept length must be greater than prefix '{prefix}'"
).is_less_than_or_equal_to(kept_len)
return f"{prefix}{content[len(prefix) : ][-kept_len+len(prefix):]}"

0 comments on commit f5c2b05

Please sign in to comment.