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

[v0.7] Backport helm release name fix #1665

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions pkg/name/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,34 @@ var (
)

// Limit the length of a string to count characters. If the string's length is
// greater than count, it will be truncated and a hash will be appended to the
// end.
// greater than count, it will be truncated and a separator will be appended to
// the end, followed by a hash.
// If the last character of the truncated string is the separator, then the
// separator itself is omitted. This prevents the result from containing two
// consecutive separators. In such a case, the result will be [count -1]
// characters long.
// If count is too small to include the shortened hash the string is simply
// truncated.
func Limit(s string, count int) string {
if len(s) <= count {
return s
}

if count <= 6 {
const hexLen int = 5
separator := "-"

if count <= hexLen+len(separator) {
return s[:count]
}
return fmt.Sprintf("%s-%s", s[:count-6], Hex(s, 5))

nbCharsBeforeTrim := count - hexLen - len(separator)

// If the last character of the truncated string is the separator, include it instead of the separator.
if string(s[nbCharsBeforeTrim-1]) == separator {
separator = ""
}

return fmt.Sprintf("%s%s%s", s[:nbCharsBeforeTrim], separator, Hex(s, hexLen))
}

// Hex returns a hex-encoded hash of the string and truncates it to length.
Expand Down
3 changes: 3 additions & 0 deletions pkg/name/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var _ = Describe("Name", func() {
{arg: "12345678", n: 8, result: "12345678"},
{arg: "12345678", n: 7, result: "1-25d55"},
{arg: "123456789", n: 8, result: "12-25f9e"},
{arg: "1-3456789", n: 8, result: "1-9b657"}, // no double dash in the result
}

It("matches expected results", func() {
Expand All @@ -51,6 +52,8 @@ var _ = Describe("Name", func() {
{arg: "long-name-test-0.App_ ", result: "long-name-test-0-app-5bf6b3fb"},
{arg: "long-name-test--App_-_12.factor", result: "long-name-test-app-12-factor-0efbac37"},
{arg: "bundle.name.example.com", result: "bundle-name-example-com-645ef821"},
// no double dash in the result
{arg: str53[0:46] + "-1234567", result: "1234567890123456789012345678901234567890123456-d0bce"},
}

It("matches expected results", func() {
Expand Down
Loading