Skip to content

Commit d3c13ac

Browse files
unguiculusscottrigby
authored andcommitted
Truncate names to 63 characters (#69)
There are charts with very long names which cause namespace and release names to exceed the max length of 63 characters. We, thus, need to truncate long names. Truncation is done from the left in order to preserve build ids and randomly generated suffixes. Signed-off-by: Reinhard Nägele <[email protected]>
1 parent 3ce597a commit d3c13ac

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

pkg/util/util.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
)
2929

3030
const chars = "1234567890abcdefghijklmnopqrstuvwxyz"
31+
const maxNameLength = 63
3132

3233
type Maintainer struct {
3334
Name string `yaml:"name"`
@@ -158,11 +159,11 @@ func CreateInstallParams(chart string, buildId string) (release string, namespac
158159
release = path.Base(chart)
159160
namespace = release
160161
if buildId != "" {
161-
namespace += buildId
162+
namespace = fmt.Sprintf("%s-%s", namespace, buildId)
162163
}
163164
randomSuffix := RandomString(10)
164-
release = fmt.Sprintf("%s-%s", release, randomSuffix)
165-
namespace = fmt.Sprintf("%s-%s", namespace, randomSuffix)
165+
release = TruncateLeft(fmt.Sprintf("%s-%s", release, randomSuffix), maxNameLength)
166+
namespace = TruncateLeft(fmt.Sprintf("%s-%s", namespace, randomSuffix), maxNameLength)
166167
return
167168
}
168169

@@ -173,3 +174,11 @@ func PrintDelimiterLine(delimiterChar string) {
173174
}
174175
fmt.Println(strings.Join(delim, ""))
175176
}
177+
178+
func TruncateLeft(s string, maxLength int) string {
179+
excess := len(s) - maxLength
180+
if excess > 0 {
181+
return s[excess:]
182+
}
183+
return s
184+
}

pkg/util/util_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package util
1616

1717
import (
18+
"fmt"
1819
"github.com/stretchr/testify/assert"
1920
"testing"
2021
)
@@ -65,3 +66,25 @@ func TestCompareVersions(t *testing.T) {
6566
})
6667
}
6768
}
69+
70+
func TestTruncateLeft(t *testing.T) {
71+
var testDataSlice = []struct {
72+
input string
73+
maxLength int
74+
expected string
75+
}{
76+
{"way_shorter_than_max_length", 63, "way_shorter_than_max_length"},
77+
{"max_length", len("max_length"), "max_length"},
78+
{"way_longer_than_max_length", 10, "max_length"},
79+
{"one_shorter_than_max_length", len("one_shorter_than_max_length") + 1, "one_shorter_than_max_length"},
80+
{"_one_longer_than_max_length", len("_one_longer_than_max_length") - 1, "one_longer_than_max_length"},
81+
}
82+
83+
for index, testData := range testDataSlice {
84+
t.Run(string(index), func(t *testing.T) {
85+
actual := TruncateLeft(testData.input, testData.maxLength)
86+
fmt.Printf("actual: %s,%d, input: %s,%d\n", actual, len(actual), testData.input, testData.maxLength)
87+
assert.Equal(t, testData.expected, actual)
88+
})
89+
}
90+
}

0 commit comments

Comments
 (0)