-
Notifications
You must be signed in to change notification settings - Fork 290
/
names.go
63 lines (56 loc) · 1.66 KB
/
names.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
59
60
61
62
63
package k8s
import (
"fmt"
"strings"
)
// Calculates names for workloads by using the shortest uniquely matching identifiers
func UniqueNames(es []K8sEntity, minComponents int) []string {
ret := make([]string, len(es))
// how many resources potentially map to a given name
counts := make(map[string]int)
// count how many entities want each potential name
for _, e := range es {
for _, name := range potentialNames(e, minComponents) {
counts[name]++
}
}
// for each entity, take the shortest name that is uniquely wanted by that entity
for i, e := range es {
names := potentialNames(e, minComponents)
for _, name := range names {
if counts[name] == 1 {
ret[i] = name
break
}
}
if ret[i] == "" {
// If we hit this case, this means we have two resources with the same
// name/kind/namespace/group This usually means the user is trying to
// deploy the same resource twice. Kubernetes will not treat these as
// unique.
//
// We should surface a warning or error about this somewhere else that has
// more context on how to fix it.
// https://github.com/windmilleng/tilt/issues/1852
//
// But for now, append the index to the name to make it unique
ret[i] = fmt.Sprintf("%s:%d", names[len(names)-1], i)
}
}
return ret
}
// returns a list of potential names, in order of preference
func potentialNames(e K8sEntity, minComponents int) []string {
gvk := e.GVK()
components := []string{
e.Name(),
gvk.Kind,
e.Namespace().String(),
gvk.Group,
}
var ret []string
for i := minComponents - 1; i < len(components); i++ {
ret = append(ret, strings.ToLower(strings.Join(components[:i+1], ":")))
}
return ret
}