-
Notifications
You must be signed in to change notification settings - Fork 89
/
helm.go
120 lines (99 loc) · 2.94 KB
/
helm.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
package base
import (
"io/ioutil"
"os"
"path"
"strings"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/upstream"
"github.com/replicatedhq/kots/pkg/util"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/renderutil"
"k8s.io/helm/pkg/strvals"
"k8s.io/helm/pkg/timeconv"
)
func renderHelm(u *upstream.Upstream, renderOptions *RenderOptions) (*Base, error) {
chartPath, err := ioutil.TempDir("", "kots")
if err != nil {
return nil, errors.Wrap(err, "failed to create chart dir")
}
defer os.RemoveAll(chartPath)
for _, file := range u.Files {
p := path.Join(chartPath, file.Path)
d, _ := path.Split(p)
if _, err := os.Stat(d); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(d, 0744); err != nil {
return nil, errors.Wrap(err, "failed to mkdir for chart resource")
}
} else {
return nil, errors.Wrap(err, "failed to check if dir exists")
}
}
if err := ioutil.WriteFile(p, file.Content, 0644); err != nil {
return nil, errors.Wrap(err, "failed to write chart file")
}
}
vals := map[string]interface{}{}
for _, value := range renderOptions.HelmOptions {
if err := strvals.ParseInto(value, vals); err != nil {
return nil, errors.Wrap(err, "failed to parse helm value")
}
}
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: u.Name,
IsInstall: true,
IsUpgrade: false,
Time: timeconv.Now(),
Namespace: renderOptions.Namespace,
},
KubeVersion: "1.16.0",
}
rendered, err := renderutil.Render(c, config, renderOpts)
if err != nil {
return nil, errors.Wrap(err, "failed to render chart")
}
baseFiles := []BaseFile{}
for k, v := range rendered {
baseFile := BaseFile{
Path: k,
Content: []byte(v),
}
baseFiles = append(baseFiles, baseFile)
}
// remove any common prefix from all files
if len(baseFiles) > 0 {
firstFileDir, _ := path.Split(baseFiles[0].Path)
commonPrefix := strings.Split(firstFileDir, string(os.PathSeparator))
for _, file := range baseFiles {
d, _ := path.Split(file.Path)
dirs := strings.Split(d, string(os.PathSeparator))
commonPrefix = util.CommonSlicePrefix(commonPrefix, dirs)
}
cleanedBaseFiles := []BaseFile{}
for _, file := range baseFiles {
d, f := path.Split(file.Path)
d2 := strings.Split(d, string(os.PathSeparator))
cleanedBaseFile := file
d2 = d2[len(commonPrefix):]
cleanedBaseFile.Path = path.Join(path.Join(d2...), f)
cleanedBaseFiles = append(cleanedBaseFiles, cleanedBaseFile)
}
baseFiles = cleanedBaseFiles
}
return &Base{
Files: baseFiles,
}, nil
}