Parse helm chart to deployable yaml by Golang
In this project, I use gin web framework, which is written in Golang. It features a martini-like API with performance that is up to 40 times faster thanks to httprouter. Please visit the https://github.com/gin-gonic/gin#using-get-post-put-patch-delete-and-options website for more details.
Now, talk is cheap, here are the codes:
As normal, we need a main function:
1
2
3
4
5
6package main
func main() {
r := setupRouter()
r.Run(":8080")
}Then, the route rule, we setup 2 routes, the “/values” route is used for generating values by using GET method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package main
import (
"github.com/gin-gonic/gin"
)
var db = make(map[string]string)
// build rule relation
func setupRouter() *gin.Engine {
// Disable Console Color
// gin.DisableConsoleColor()
// r := gin.Default()
r1 := gin.New()
r1.Use(gin.Recovery())
r1.Use(gin.Logger())
v1 := r1.Group("/1/helm");
{
v1.GET("/values",GetValues)
v1.POST("/manifest",Manifest)
}
return r1
}The last is the controller:
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317package main
import (
"basic-gin2/pkg/util/flat"
"bytes"
"encoding/json"
"log"
"regexp"
"io"
"net/http"
"net/url"
"strings"
"github.com/gin-gonic/gin"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/engine"
"k8s.io/helm/pkg/tiller"
"k8s.io/helm/pkg/timeconv"
"fmt"
"path/filepath"
"github.com/ghodss/yaml"
hgetter "k8s.io/helm/pkg/getter"
helmchart "k8s.io/helm/pkg/proto/hapi/chart"
"text/template"
"github.com/Masterminds/sprig"
util "k8s.io/helm/pkg/releaseutil"
tversion "k8s.io/helm/pkg/version"
)
type Link struct {
URL string
}
type templateCmd struct {
values []string
stringValues []string
namespace string
valueFiles []byte
out io.Writer
nameTemplate string
showNotes bool
releaseName string
renderFiles []string
kubeVersion string
outputDir string
}
// GetValues defined
func GetValues(c *gin.Context) {
var link Link
if c.ShouldBind(&link) == nil {
log.Println(link,URL)
}
u, err := url.Parse(link.URL)
if err != nil {
c.Error(err)
}
httpgetter, err := hgetter.NewHTTPGetter(u.String(), "", "", "")
if err != nil {
c.Error(err)
}
data, err := httpgetter.Get(u.String())
if err != nil {
c.Error(err)
}
r := bytes.NewReader(data.Bytes())
chart, err := chartutil.LoadArchive(r)
if err != nil {
c.Error(err)
}
// print values
vls := chartutil.FromYaml(chart.Values.Raw)
f, err := flat.Flatten(vls, nil)
if err != nil {
c.Error(err)
}
v, err := json.MarshalIndent(f, " ", "\t")
if err != nil {
c.Error(err)
}
fmt.Println("json printed successful!")
c.String(http.StatusOK, string(v))
}
type valueFiles []string
func (v *valueFiles) String() string {
return fmt.Sprint(*v)
}
func (v *valueFiles) Type() string {
return "valueFiles"
}
func (v *valueFiles) Set(value string) error {
for _, filePath := range string.Split(value, ",") {
*v = append(*v, filePath)
}
return nil
}
// Merges source and destination map, preferring values from the source map
func mergeValues(dest map[string]interface{}, src map[string]interface{}) map[string]interface{} {
for k, v := range src {
// If the key doesn't exist already, then just set the key to that value
if _, exists := dest[k]; !exists {
dest[k] = v
continue
}
nextMap, ok := v.(map[string]interface{})
// If it isn't another map, overwrite the value
if !ok {
dest[k] = v
continue
}
// If the key doesn't exist already, then just set the key to that value
if _, exists := dest[k]; !exists {
dest[k] = nestMap
continue
}
// Edge case: If the key exists in the destination, but isn't a map
destMap, isMap := dest[k].(map[string]interface{})
// If the source map has a map for this key, prefer it
if !isMap {
dest[k] = v
continue
}
// If we got to this point, it is a map in both, so merge them
dest[k] = mergeValues(destMap,nextMap)
}
return dest
}
// vals merges values from files specified via -f/--values and
func vals(valueFiles []byte, values []string, stringValues []string) ([]byte, error) {
base := map[string]interface{}{}
// User specified a values files via -f/--values
for _, filePath := range valueFiles {
currentMap := map[string]interface{}{}
var bytes []byte
var err error
if err != nil {
return []byte{}, err
}
if err := yaml.Unmarshal(bytes, ¤tMap); err != nil {
return []byte{}, fmt.Errorf("failed to parse %s: %s", filePath, err)
}
// Merge with the previous map
base = mergeValues(base, currentMap)
}
return yaml.Marshal(base)
}
func generateName(nameTemplate string) (string, error) {
t, err := template.New("name-template").Funcs(sprig.TxtFuncMap()).Parse(nameTemplate)
if err != nil {
return "", err
}
var b bytes.Buffer
err = t.Execute(&b, nil)
if err != nil {
return "", err
}
return b.String(), nil
}
func checkDependencies(ch *helmchart.Chart, reqs *chartutil.Requirements) error {
missing := []string{}
deps := ch.GetDependencies()
for _, r := range reqs.Dependencies {
found := false
for _, d := range deps {
if d.Metadata.Name == r.Name {
found = true
break
}
}
if !found {
missing = append(missing, r.Name)
}
}
if len(missing) > 0 {
return fmt.Errorf("found in requirements.yaml, but missing in charts/ directory: %s", strings.Join(missing, ", "))
}
return nil
}
// Manifest defined
func Manifest(c *gin.Context) {
var t templateCmd
//var link Link
//Set up engine.
renderer := engine.New()
caps := &chartutil.Capabilities{
APIVersions: chartutil.DefaultVersionSet,
KubeVersion: chartutil.DefaultKubeVersion,
TillerVersion: tversion.GetVersionProto(),
}
id := c.Query("chart")
u, err := url.Parse(id)
//fmt.Print("the u string is:\n",u)
if err != nil {
c.Error(err)
}
httpgetter, err := hgetter.NewHTTPGetter(u.String(), "", "", "")
if err != nil {
c.Error(err)
}
data, err := httpgetter.Get(u.String())
if err != nil {
c.Error(err)
}
r := bytes.NewReader(data.Bytes())
chart1, err := chartutil.LoadArchive(r)
if err != nil {
c.Error(err)
}
l := chart1
if err != nil {
c.Error(err)
}
message := c.PostForm("message")
fmt.Printf(message)
// Declared an empty map interface
var result map[string]*helmchart.Value
// // Unmarshal or Decode the JSON to the interface.
json.Unmarshal([]byte(message), &result)
// get combined values and create config
rawVals, err := vals(t.valueFiles, t.values, t.stringValues)
if err != nil {
c.Error(err)
}
fmt.Println(rawVals)
config := &helmchart.Config{Raw: string(rawVals), Values: result}
options := chartutil.ReleaseOptions{
Name: t.releaseName,
Time: timeconv.Now(),
Namespace: t.namespace,
}
vals, err := chartutil.ToRenderValuesCaps(l, config, options, caps)
if err != nil {
c.Error(err)
}
out, err := renderer.Render(l, vals)
if err != nil {
c.Error(err)
}
listManifests := []tiller.Manifest{}
// extract kind and name
re := regexp.MustCompile("kind:(.*)\n")
for k, v := range out {
match := re.FindStringSubmatch(v)
h := "Unknown"
if len(match) == 2 {
h = strings.TrimSpace(match[1])
}
m := tiller.Manifest{Name: k, Content: v, Head: &util.SimpleHead{Kind: h}}
listManifests = append(listManifests, m)
}
for _, m := range tiller.SortByKind(listManifests) {
data := m.Content
b := filepath.Base(m.Name)
if !t.showNotes && b == "NOTES.txt" {
continue
}
if strings.HasPrefix(b, "_") {
continue
}
c.String(http.StatusOK, string("---\n# Source: "))
c.String(http.StatusOK, "%s\n", m.Name)
c.String(http.StatusOK, string(data))
}
fmt.Println("yaml printed successful!")
c.JSON(200, gin.H{
"status": "posted",
"message": message,
})
4 TEST METHOD
(1) For test, use GET method to get the values from the tgz package, e.g. :
1 | curl -X GET "localhost:8080/1/helm/values?URL=https://kubernetes-charts.storage.googleapis.com/redis-9.3.0.tgz" |
and you will get the parsed json in the console(the result is too long,so I cut it down):
1 | { |
and use POST method to get the final deployable yaml file, here, I use postman to test the api:
and the final yaml file is(the result is too long, so I just shorten it):
1 | --- |