-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
85 lines (68 loc) · 1.87 KB
/
parser.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
package main
import (
"log"
"regexp"
yaml "gopkg.in/yaml.v2"
)
type deployment struct {
Spec struct {
Containers []container `yaml:"containers"`
} `yaml:"spec"`
}
type deploymentWithTemplate struct {
Spec struct {
Replicas string `yaml:"replicas"`
Template struct {
Spec struct {
Containers []container `yaml:"containers"`
} `yaml:"spec"`
} `yaml:"template"`
} `yaml:"spec"`
}
type container struct {
Name string `yaml:"name"`
EnvVars []envVars `yaml:"env"`
}
type envVars struct {
Key string `yaml:"name"`
Value string `yaml:"value"`
}
func parseDeployment(file []byte) ([]map[string]string, error) {
data := deployment{}
if err := yaml.Unmarshal(file, &data); err != nil {
log.Printf("could not unmarshal: %q", err.Error())
return []map[string]string{}, err
}
variables := []map[string]string{}
for _, container := range data.Spec.Containers {
vars := map[string]string{}
for _, envVar := range container.EnvVars {
vars[envVar.Key] = envVar.Value
}
variables = append(variables, vars)
}
return variables, nil
}
func parseDeploymentWithTemplate(file []byte) ([]map[string]string, error) {
data := deploymentWithTemplate{}
if err := yaml.Unmarshal(file, &data); err != nil {
// Ungly workaround helm templates
log.Printf("got an error parsing helm file, trying to clean it...")
re := regexp.MustCompile(`{{.*}}`)
cleanFile := re.ReplaceAllString(string(file), "")
if err := yaml.Unmarshal([]byte(cleanFile), &data); err != nil {
log.Printf("could not unmarshal: %q", err.Error())
return []map[string]string{}, err
}
log.Print("done!")
}
variables := []map[string]string{}
for _, container := range data.Spec.Template.Spec.Containers {
vars := map[string]string{}
for _, envVar := range container.EnvVars {
vars[envVar.Key] = envVar.Value
}
variables = append(variables, vars)
}
return variables, nil
}