-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hash and trim long Helm release names instead of only trimming (#390
) fixes #46
- Loading branch information
Showing
14 changed files
with
193 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
import hashlib | ||
import logging | ||
|
||
log = logging.getLogger("HelmUtils") | ||
|
||
|
||
ENCODING = "utf-8" | ||
RELEASE_NAME_MAX_LEN = 52 | ||
|
||
|
||
def trim_release_name(name: str, suffix: str = "") -> str: | ||
"""Trim Helm release name while preserving suffix. | ||
def create_helm_release_name(name: str, suffix: str = "") -> str: | ||
"""Shortens the long Helm release name. | ||
Creates a 52 character long release name if the name length exceeds the Helm release character length. | ||
It first trims the string and fetches the first RELEASE_NAME_MAX_LEN - len(suffix) characters. | ||
Then it replaces the last 6 characters with the SHA-1 encoded string (with "-") to avoid collision | ||
and append the suffix if given. | ||
:param name: The release name including optional suffix | ||
:param name: The Helm release name to be shortened. | ||
:param suffix: The release suffix to preserve | ||
:return: Truncated release name. | ||
:return: Trimmed + hashed version of the release name if it exceeds the Helm release character length otherwise the actual release name | ||
""" | ||
if len(name) > RELEASE_NAME_MAX_LEN: | ||
new_name = name[: (RELEASE_NAME_MAX_LEN - len(suffix))] + suffix | ||
exact_name = name[: RELEASE_NAME_MAX_LEN - len(suffix)] | ||
hash_name = hashlib.sha1(name.encode(ENCODING)).hexdigest() | ||
new_name = exact_name[:-6] + "-" + hash_name[:5] + suffix | ||
log.critical( | ||
f"Invalid Helm release name '{name}'. Truncating to {RELEASE_NAME_MAX_LEN} characters: \n {name} --> {new_name}" | ||
f"Invalid Helm release name '{name}'. Truncating and hashing the release name: \n {name} --> {new_name}" | ||
) | ||
name = new_name | ||
return new_name | ||
return name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,33 @@ | ||
from kpops.component_handlers.helm_wrapper.utils import trim_release_name | ||
from kpops.component_handlers.helm_wrapper.utils import ( | ||
create_helm_release_name, | ||
) | ||
|
||
|
||
def test_trim_release_name_with_suffix(): | ||
name = trim_release_name( | ||
"example-component-name-too-long-fake-fakefakefakefakefake-clean", | ||
suffix="-clean", | ||
) | ||
assert name == "example-component-name-too-long-fake-fakefakef-clean" | ||
assert len(name) == 52 | ||
def test_helm_release_name_for_long_names(): | ||
long_release_name = "example-component-name-too-long-fake-fakefakefakefakefake" | ||
|
||
actual_release_name = create_helm_release_name(long_release_name) | ||
|
||
def test_trim_release_name_without_suffix(): | ||
name = trim_release_name( | ||
"example-component-name-too-long-fake-fakefakefakefakefake" | ||
) | ||
assert name == "example-component-name-too-long-fake-fakefakefakefak" | ||
assert len(name) == 52 | ||
expected_helm_release_name = "example-component-name-too-long-fake-fakefakef-0a7fc" | ||
assert expected_helm_release_name == actual_release_name | ||
assert len(expected_helm_release_name) == 52 | ||
|
||
|
||
def test_no_trim_release_name(): | ||
assert ( | ||
trim_release_name("normal-name-with-no-need-of-trim-clean", suffix="-clean") | ||
== "normal-name-with-no-need-of-trim-clean" | ||
) | ||
assert ( | ||
trim_release_name("normal-name-with-no-need-of-trim") | ||
== "normal-name-with-no-need-of-trim" | ||
def test_helm_release_name_for_install_and_clean_must_be_different(): | ||
long_release_name = "example-component-name-too-long-fake-fakefakefakefakefake" | ||
|
||
helm_clean_release_name = create_helm_release_name(long_release_name, "-clean") | ||
expected_helm_release_name = ( | ||
"example-component-name-too-long-fake-fakefakef-0a7fc-clean" | ||
) | ||
|
||
assert expected_helm_release_name != helm_clean_release_name | ||
|
||
|
||
def test_helm_release_name_for_short_names(): | ||
short_release_name = "example-component-name" | ||
|
||
actual_helm_release_name = create_helm_release_name(short_release_name) | ||
|
||
assert actual_helm_release_name == short_release_name | ||
assert len(actual_helm_release_name) < 53 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.