-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.go
56 lines (46 loc) · 1.13 KB
/
utils.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
package utils
import (
"github.com/stitchfix/flotilla-os/state"
"os"
"regexp"
"strings"
)
func GetLabels(run state.Run) map[string]string {
var labels = make(map[string]string)
if run.ClusterName != "" {
labels["cluster-name"] = run.ClusterName
}
if run.RunID != "" {
labels["flotilla-run-id"] = SanitizeLabel(run.RunID)
labels["flotilla-run-mode"] = SanitizeLabel(os.Getenv("FLOTILLA_MODE"))
}
if run.User != "" {
labels["owner"] = SanitizeLabel(run.User)
}
if _, workflowExists := run.Labels["kube_workflow"]; !workflowExists {
if _, taskNameExists := run.Labels["kube_task_name"]; taskNameExists {
labels["kube_workflow"] = SanitizeLabel(run.Labels["kube_task_name"])
}
}
for k, v := range run.Labels {
labels[k] = SanitizeLabel(v)
}
return labels
}
func SanitizeLabel(key string) string {
key = strings.TrimSpace(key)
key = regexp.MustCompile(`[^-a-z0-9A-Z_.]+`).ReplaceAllString(key, "_")
key = strings.TrimPrefix(key, "_")
key = strings.ToLower(key)
if len(key) > 63 {
key = key[:63]
}
for {
tempKey := strings.TrimSuffix(key, "_")
if tempKey == key {
break
}
key = tempKey
}
return key
}