-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvars.go
229 lines (186 loc) · 4.42 KB
/
vars.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"fmt"
"io/ioutil"
"path"
"sort"
"strings"
"github.com/ghodss/yaml"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/util/validation"
)
// Var represents a basic key/value variable
type Var struct {
Key string
Value string
}
// Vars is a Var slice
type Vars []Var
// NewVarsFromFiles takes a slice of files and returns a Vars struct
func newVarsFromFiles(files []string) (Vars, error) {
vars := Vars{}
// read in vars files
for _, filename := range files {
var v Vars
var err error
if path.Ext(filename) == ".yml" || path.Ext(filename) == ".yaml" {
v, err = readYAMLVars(filename)
} else {
v, err = readKVVars(filename)
}
if err != nil {
return vars, err
}
vars = append(vars, v...)
}
return vars, nil
}
// readKVVars reads files in "key=value" format and returns Vars
func readKVVars(filename string) (Vars, error) {
vars := Vars{}
data, err := ioutil.ReadFile(filename)
if err != nil {
return vars, err
}
lines := strings.Split(string(data), "\n")
for _, l := range lines {
if l == "" {
continue
}
lSplit := strings.Split(l, "=")
if len(lSplit) < 2 {
fmt.Printf("Skipping %s; not in key=value format\n", l)
continue
}
vars = append(vars, Var{
Key: lSplit[0],
Value: strings.Join(lSplit[1:], "="),
})
}
return vars, nil
}
// readYAMLVars reads files in "key: value" format and returns Vars
func readYAMLVars(filename string) (Vars, error) {
vars := Vars{}
data, err := ioutil.ReadFile(filename)
if err != nil {
return vars, err
}
config := make(map[string]string)
err = yaml.Unmarshal(data, &config)
if err != nil {
return vars, err
}
// dirty hack for sorting
keys := []string{}
for k, _ := range config {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vars = append(vars, Var{
Key: k,
Value: config[k],
})
}
return vars, nil
}
func (vars Vars) toEnvVar() []v1.EnvVar {
envVars := []v1.EnvVar{}
for _, v := range vars {
envVars = append(envVars, v1.EnvVar{
Name: v.Key,
Value: v.Value,
})
}
return envVars
}
func (vars Vars) toConfigMap(name string, namespace string, convert bool) ([]v1.EnvVar, *v1.ConfigMap, error) {
envVars := []v1.EnvVar{}
data := make(map[string]string)
for _, v := range vars {
key, err := validateKey(v.Key, convert)
if err != nil {
return envVars, &v1.ConfigMap{}, err
}
data[key] = v.Value
envVars = append(envVars, v1.EnvVar{
Name: v.Key,
ValueFrom: &v1.EnvVarSource{
ConfigMapKeyRef: &v1.ConfigMapKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: name,
},
Key: key,
},
},
})
}
configMap := &v1.ConfigMap{
TypeMeta: unversioned.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Data: data,
}
return envVars, configMap, nil
}
// toSecret converts vars to a Secret resource and creates the proper EnvVar
// ValuesFrom sources to be passed to the container
func (vars Vars) toSecret(name string, namespace string, convert bool) ([]v1.EnvVar, *v1.Secret, error) {
envVars := []v1.EnvVar{}
data := make(map[string][]byte)
for _, v := range vars {
key, err := validateKey(v.Key, convert)
if err != nil {
return envVars, &v1.Secret{}, err
}
// the k8s lib will handle the base64 encoding for us
data[key] = []byte(v.Value)
envVars = append(envVars, v1.EnvVar{
Name: v.Key,
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: name,
},
Key: key,
},
},
})
}
secret := &v1.Secret{
TypeMeta: unversioned.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Data: data,
}
return envVars, secret, nil
}
func validateKey(key string, convert bool) (string, error) {
if convert {
convertedKey := strings.ToLower(strings.Replace(key, "_", "-", -1))
errs := validation.IsDNS1123Subdomain(convertedKey)
if len(errs) > 0 {
err := fmt.Errorf("%s is not a valid ConfigMap key: %s", key, strings.Join(errs, ", "))
return "", err
}
return convertedKey, nil
}
errs := validation.IsConfigMapKey(key)
if len(errs) > 0 {
err := fmt.Errorf("%s is not a valid ConfigMap key: %s", key, strings.Join(errs, ", "))
return "", err
}
return key, nil
}