-
Notifications
You must be signed in to change notification settings - Fork 89
/
helm_v2.go
77 lines (66 loc) · 1.99 KB
/
helm_v2.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
package base
import (
"io/ioutil"
golog "log"
"os"
"strings"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/renderutil"
"k8s.io/helm/pkg/timeconv"
)
func renderHelmV2(chartName string, chartPath string, vals map[string]interface{}, renderOptions *RenderOptions) ([]BaseFile, error) {
marshalledVals, err := yaml.Marshal(vals)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal helm values")
}
config := &chart.Config{Raw: string(marshalledVals), Values: map[string]*chart.Value{}}
c, err := chartutil.Load(chartPath)
if err != nil {
return nil, errors.Wrap(err, "failed to load chart")
}
renderOpts := renderutil.Options{
ReleaseOptions: chartutil.ReleaseOptions{
Name: chartName,
IsInstall: true,
IsUpgrade: false,
Time: timeconv.Now(),
Namespace: renderOptions.Namespace,
},
KubeVersion: "1.16.0",
}
if renderOpts.ReleaseOptions.Namespace == "" {
renderOpts.ReleaseOptions.Namespace = NamespaceTemplateConst
}
// Silence the go logger because helm will complain about some of our template strings
golog.SetOutput(ioutil.Discard)
defer golog.SetOutput(os.Stdout)
rendered, err := renderutil.Render(c, config, renderOpts)
if err != nil {
return nil, errors.Wrap(err, "failed to render chart")
}
// need to split base files before inserting namespace
baseFiles := []BaseFile{}
for name, content := range rendered {
splitManifests := splitManifests(content)
for _, manifest := range splitManifests {
if strings.TrimSpace(manifest) == "" {
// filter out empty docs
continue
}
baseFiles = append(baseFiles, BaseFile{
Path: name,
Content: []byte(manifest),
})
}
}
// insert namespace defined in the HelmChart spec
baseFiles, err = kustomizeHelmNamespace(baseFiles, renderOptions)
if err != nil {
return nil, errors.Wrap(err, "failed to insert helm namespace")
}
// maintain order
return mergeBaseFiles(baseFiles), nil
}