/
helm.go
89 lines (72 loc) · 1.79 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
package helm
import (
"strings"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/engine"
"helm.sh/helm/v3/pkg/releaseutil"
"sigs.k8s.io/yaml"
)
func getChart(path string) (*chart.Chart, error) {
client := action.NewInstall(&action.Configuration{})
settings := cli.New()
cp, err := client.ChartPathOptions.LocateChart(path, settings)
if err != nil {
return nil, err
}
chart, err := loader.Load(cp)
if err != nil {
return nil, err
}
return chart, nil
}
func getValues(name, namespace string, bytes []byte, chart *chart.Chart) (chartutil.Values, error) {
rawMap := map[string]interface{}{}
if err := yaml.Unmarshal(bytes, &rawMap); err != nil {
return nil, err
}
options := chartutil.ReleaseOptions{
Name: name,
Namespace: namespace,
}
values, err := chartutil.ToRenderValues(chart, rawMap, options, nil)
if err != nil {
return nil, err
}
return values, nil
}
func GetManifests(name, namespace, path string, bytes []byte) ([][]byte, error) {
var result [][]byte
chart, err := getChart(path)
if err != nil {
return nil, err
}
for _, crd := range chart.CRDObjects() {
result = append(result, crd.File.Data)
}
values, err := getValues(name, namespace, bytes, chart)
if err != nil {
return nil, err
}
files, err := engine.Render(chart, values)
if err != nil {
return nil, err
}
for k := range files {
if strings.HasSuffix(k, ".txt") || strings.HasPrefix(k, "_") {
delete(files, k)
}
}
// Don't handle hooks
_, manifests, err := releaseutil.SortManifests(files, nil, releaseutil.InstallOrder)
if err != nil {
return nil, err
}
for _, m := range manifests {
result = append(result, []byte(m.Content))
}
return result, nil
}