-
Notifications
You must be signed in to change notification settings - Fork 303
/
identifiers.go
58 lines (47 loc) · 1.52 KB
/
identifiers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package apis
import (
"encoding/hex"
"hash/fnv"
"regexp"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/tilt-dev/tilt-apiserver/pkg/server/builder/resource"
"k8s.io/apimachinery/pkg/api/validation/path"
"k8s.io/apimachinery/pkg/util/validation"
)
const MaxNameLength = validation.DNS1123SubdomainMaxLength
var invalidPathCharacters = regexp.MustCompile(`[` + strings.Join(path.NameMayNotContain, "") + `]`)
func Key(o resource.Object) types.NamespacedName {
return KeyFromMeta(*o.GetObjectMeta())
}
func KeyFromMeta(objMeta metav1.ObjectMeta) types.NamespacedName {
return types.NamespacedName{Name: objMeta.Name, Namespace: objMeta.Namespace}
}
// SanitizeName ensures a value is suitable for usage as an apiserver identifier.
func SanitizeName(name string) string {
sanitized := name
if len(path.IsValidPathSegmentName(name)) != 0 {
for _, invalidName := range path.NameMayNotBe {
if name == invalidName {
// the only strictly invalid names are `.` and `..` so this is sufficient
return strings.ReplaceAll(name, ".", "_")
}
}
sanitized = invalidPathCharacters.ReplaceAllString(sanitized, "_")
}
if len(sanitized) > MaxNameLength {
var sb strings.Builder
sb.Grow(MaxNameLength)
sb.WriteString(sanitized[:MaxNameLength-9])
sb.WriteRune('-')
sb.WriteString(hashValue(name))
sanitized = sb.String()
}
return sanitized
}
func hashValue(v string) string {
h := fnv.New32a()
_, _ = h.Write([]byte(v))
return hex.EncodeToString(h.Sum(nil))
}