-
Notifications
You must be signed in to change notification settings - Fork 12
/
lib.go
172 lines (148 loc) · 3.7 KB
/
lib.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
package lib
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"text/template"
"sigs.k8s.io/yaml"
)
type ReleaseObj struct {
Name string
Namespace string
IsUpgrade bool
IsInstall bool
Revision int
Service string
}
type ChartObj struct {
ApiVersion string
Name string
Version string
KubeVersion string
Description string
Type string
Keywords []string
Home string
Sources []string
Dependencies []interface{}
Maintainers []interface{}
Icon string
AppVersion string
Deprecated bool
Annotations map[string]interface{}
}
type ValuesObj map[string]interface{}
type TemplateData struct {
Release ReleaseObj
Chart ChartObj
Values ValuesObj
}
type GetYamlReturnValue struct {
Yaml string `json:"yaml"`
Err string `json:"err"`
Warning string `json:"warning"`
}
func toJson(returnValue GetYamlReturnValue) string {
bytes, err := json.Marshal(returnValue)
if err != nil {
return `{"err":"conversion to JSON failed"}`
}
return string(bytes)
}
func GetYaml(templateYaml string, valuesYaml string) string {
valuesData := ValuesObj{}
if err := yaml.Unmarshal([]byte(valuesYaml), &valuesData); err != nil {
return toJson(GetYamlReturnValue{
Err: err.Error(),
})
}
releaseData := ReleaseObj{
Name: "example",
Namespace: "example",
IsUpgrade: false,
IsInstall: false,
Revision: 1,
Service: "Helm",
}
chartData := ChartObj{
ApiVersion: "v2",
Name: "example",
Version: "0.1.0",
KubeVersion: "",
Description: "example",
Type: "application",
Keywords: make([]string, 0),
Home: "",
Sources: make([]string, 0),
Dependencies: make([]interface{}, 0),
Maintainers: make([]interface{}, 0),
Icon: "",
AppVersion: "",
Deprecated: false,
Annotations: make(map[string]interface{}),
}
templateData := TemplateData{
Release: releaseData,
Chart: chartData,
Values: valuesData,
}
t := template.New("template")
var output bytes.Buffer
funcMap := funcMap()
/*
* Based on:
* https://github.com/helm/helm/blob/3d1bc72827e4edef273fb3d8d8ded2a25fa6f39d/pkg/engine/engine.go#L112-L125
*/
recursionMaxNums := 5
includedNames := make(map[string]int)
funcMap["include"] = func(name string, data interface{}) (interface{}, error) {
var buf strings.Builder
if v, ok := includedNames[name]; ok {
if v > recursionMaxNums {
return "", errors.New("rendering template has a nested reference name: " + name)
}
includedNames[name]++
} else {
includedNames[name] = 1
}
err := t.ExecuteTemplate(&buf, name, data)
includedNames[name]--
return buf.String(), err
}
// If the template contains `required`, we don't want to return an error which
// would prevent previewing the entire template. Simply pass the value through.
funcMap["required"] = func(warn string, val interface{}) (interface{}, error) {
return val, nil
}
// If the template contains `fail`, we don't want to return an error which
// would prevent previewing the entire template. Return an empty string.
funcMap["fail"] = func(val interface{}) (interface{}, error) {
return "", nil
}
t.Funcs(funcMap)
t, err := t.Parse(templateYaml)
if err != nil {
return toJson(GetYamlReturnValue{
Err: err.Error(),
})
}
if err := t.Execute(&output, templateData); err != nil {
return toJson(GetYamlReturnValue{
Err: err.Error(),
})
}
outputYaml := output.String()
lintValues := ValuesObj{}
if err := yaml.Unmarshal([]byte(outputYaml), &lintValues); err != nil {
fmt.Printf("%e", err)
return toJson(GetYamlReturnValue{
Warning: err.Error(),
Yaml: outputYaml,
})
}
return toJson(GetYamlReturnValue{
Yaml: outputYaml,
})
}