Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit fc2b78d

Browse files
committed
#33 #21 Truncate ending dots
This would otherwise lead to invalid names for k8s objects
1 parent 2e2684f commit fc2b78d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

operator/utils/truncatedname.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package utils
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"strings"
6+
)
47

58
// TruncateName Ensures that the name used for a kubernetes object doesn't exceed the 63 char length limit. This actually cuts of anything after char 57, so that we can use the randomly generated suffix from k8s `generateName`.
69
func TruncateName(name string) string {
710
if len(name) >= 57 {
8-
return fmt.Sprintf("%s-", name[0:57])
11+
name = name[0:57]
912
}
13+
14+
// Ensure that the string does not end in a dot.
15+
// This would not be a valid domain name thous rejected by kubernetes
16+
if strings.HasSuffix(name, ".") {
17+
name = name[0:(len(name) - 1)]
18+
}
19+
1020
return fmt.Sprintf("%s-", name)
1121
}

operator/utils/truncatedname_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ func TestAbc(t *testing.T) {
2424
in: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
2525
out: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-",
2626
},
27+
// Truncates strings ending in dots
28+
{
29+
in: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.",
30+
out: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-",
31+
},
32+
// Also removes dots even when they are not the last char
33+
{
34+
in: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.a",
35+
out: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-",
36+
},
2737
}
2838

2939
for _, test := range tests {

0 commit comments

Comments
 (0)