Skip to content

Commit 911636c

Browse files
authored
feat: add constant for k8s max name len and unsafe shorten function (#148)
1 parent 85c9a64 commit 911636c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pkg/controller/hash.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ var (
1717
ErrMaxLenTooSmall = errors.New("maxLen must be greater than 10")
1818
)
1919

20+
// K8sMaxNameLength is the maximum length for Kubernetes resource names.
21+
const K8sMaxNameLength = 63
22+
2023
// version8UUID creates a new UUID (version 8) from a byte slice. Returns an error if the slice does not have a length of 16. The bytes are copied from the slice.
2124
// The bits 48-51 and 64-65 are modified to make the output recognizable as a version 8 UUID, so only 122 out of 128 bits from the input data will be kept.
2225
func version8UUID(data []byte) (uuid.UUID, error) {
@@ -136,3 +139,13 @@ func ShortenToXCharacters(input string, maxLen int) (string, error) {
136139

137140
return input[:trimLength] + suffix, nil
138141
}
142+
143+
// ShortenToXCharactersUnsafe works like ShortenToXCharacters, but panics instead of returning an error.
144+
// This should only be used in places where the input is guaranteed to be valid.
145+
func ShortenToXCharactersUnsafe(input string, maxLen int) string {
146+
result, err := ShortenToXCharacters(input, maxLen)
147+
if err != nil {
148+
panic(err)
149+
}
150+
return result
151+
}

0 commit comments

Comments
 (0)