-
Notifications
You must be signed in to change notification settings - Fork 173
/
labels.go
47 lines (38 loc) · 909 Bytes
/
labels.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
package utils
import (
"os"
"strings"
"github.com/juju/errors"
log "github.com/sirupsen/logrus"
)
func GetLabelEnvString() string {
return os.Getenv("DRC_LABELS")
}
func GetLabelsFromEnv(envString string) (map[string]string, error) {
labels := make(map[string]string)
if envString == "" {
return labels, nil
}
labelPairs := strings.Split(envString, ",")
for _, pair := range labelPairs {
pairSlice := strings.Split(pair, "=")
if len(pairSlice) != 2 {
return labels, errors.Errorf("invalid label env: %v, pairSlice: %v", envString, pairSlice)
}
k := pairSlice[0]
v := pairSlice[1]
labels[k] = v
}
return labels, nil
}
func MustGetLabelsFromEnv() map[string]string {
labels, err := GetLabelsFromEnv(GetLabelEnvString())
if err != nil {
log.Fatalf("failed to get label: %v", err)
}
h, err := os.Hostname()
if err == nil {
labels["hostname"] = h
}
return labels
}