-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
84 lines (71 loc) · 1.32 KB
/
env.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package v1
import (
corev1 "k8s.io/api/core/v1"
)
const (
DEBUG = "DEBUG"
Empty = "EMPTY" // crd有bug,空的env会被填上值,使用empty占位符
)
func GetEnv(env []corev1.EnvVar, key string) string {
r, _ := LookupEnv(env, key)
return r
}
func LookupEnv(env []corev1.EnvVar, key string) (string, bool) {
for _, e := range env {
if e.Name == key {
return e.Value, true
}
}
return "", false
}
func SetEnv(env []corev1.EnvVar, key, val string) []corev1.EnvVar {
var found bool
for i := range env {
if env[i].Name == key {
found = true
env[i].Value = val
break
}
}
if !found {
env = append(env, corev1.EnvVar{
Name: key,
Value: val,
})
}
return env
}
func SetEnvIfUnset(env []corev1.EnvVar, key, def string) []corev1.EnvVar {
val, ok := LookupEnv(env, key)
if ok {
if val == "" {
return SetEnv(env, key, Empty)
}
return env
}
if def == "" {
def = Empty
}
return SetEnv(env, key, def)
}
func MergeEnv(src, dst []corev1.EnvVar) []corev1.EnvVar {
// 使用set会去重
set := make(map[string]string)
for _, e := range src {
set[e.Name] = e.Value
}
for _, e := range dst {
_, ok := set[e.Name]
if !ok {
set[e.Name] = e.Value
}
}
var r []corev1.EnvVar
for k, v := range set {
r = append(r, corev1.EnvVar{
Name: k,
Value: v,
})
}
return r
}