-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathcharts.go
135 lines (116 loc) · 3.68 KB
/
charts.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
package charts
import (
"bytes"
"path"
"strings"
"github.com/linkerd/linkerd2/pkg/charts/static"
"github.com/linkerd/linkerd2/pkg/version"
"k8s.io/helm/pkg/chartutil"
helmChart "k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/renderutil"
"k8s.io/helm/pkg/timeconv"
)
const versionPlaceholder = "{version}"
// Chart holds the necessary info to render a Helm chart
type Chart struct {
Name string
Dir string
Namespace string
RawValues []byte
Files []*chartutil.BufferedFile
}
func (chart *Chart) render(partialsFiles []*chartutil.BufferedFile) (bytes.Buffer, error) {
if err := FilesReader(chart.Dir+"/", chart.Files); err != nil {
return bytes.Buffer{}, err
}
if err := FilesReader("", partialsFiles); err != nil {
return bytes.Buffer{}, err
}
// Create chart and render templates
chrt, err := chartutil.LoadFiles(append(chart.Files, partialsFiles...))
if err != nil {
return bytes.Buffer{}, err
}
renderOpts := renderutil.Options{
ReleaseOptions: chartutil.ReleaseOptions{
Name: chart.Name,
IsInstall: true,
IsUpgrade: false,
Time: timeconv.Now(),
Namespace: chart.Namespace,
},
KubeVersion: "",
}
chrtConfig := &helmChart.Config{Raw: string(chart.RawValues), Values: map[string]*helmChart.Value{}}
renderedTemplates, err := renderutil.Render(chrt, chrtConfig, renderOpts)
if err != nil {
return bytes.Buffer{}, err
}
// Merge templates and inject
var buf bytes.Buffer
for _, tmpl := range chart.Files {
t := path.Join(renderOpts.ReleaseOptions.Name, tmpl.Name)
if _, err := buf.WriteString(renderedTemplates[t]); err != nil {
return bytes.Buffer{}, err
}
}
return buf, nil
}
// Render returns a bytes buffer with the result of rendering a Helm chart
func (chart *Chart) Render() (bytes.Buffer, error) {
// Keep this slice synced with the contents of /charts/partials
l5dPartials := []*chartutil.BufferedFile{
{Name: "charts/partials/" + chartutil.ChartfileName},
{Name: "charts/partials/templates/_proxy.tpl"},
{Name: "charts/partials/templates/_proxy-init.tpl"},
{Name: "charts/partials/templates/_volumes.tpl"},
{Name: "charts/partials/templates/_resources.tpl"},
{Name: "charts/partials/templates/_metadata.tpl"},
{Name: "charts/partials/templates/_helpers.tpl"},
{Name: "charts/partials/templates/_debug.tpl"},
{Name: "charts/partials/templates/_capabilities.tpl"},
{Name: "charts/partials/templates/_trace.tpl"},
}
return chart.render(l5dPartials)
}
// RenderCNI returns a bytes buffer with the result of rendering a Helm chart
func (chart *Chart) RenderCNI() (bytes.Buffer, error) {
cniPartials := []*chartutil.BufferedFile{
{Name: "charts/partials/" + chartutil.ChartfileName},
{Name: "charts/partials/templates/_helpers.tpl"},
}
return chart.render(cniPartials)
}
// ReadFile updates the buffered file with the data read from disk
func ReadFile(dir string, f *chartutil.BufferedFile) error {
filename := dir + f.Name
if dir == "" {
filename = filename[7:]
}
file, err := static.Templates.Open(filename)
if err != nil {
return err
}
defer file.Close()
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(file); err != nil {
return err
}
f.Data = buf.Bytes()
return nil
}
// FilesReader reads all the files from a directory
func FilesReader(dir string, files []*chartutil.BufferedFile) error {
for _, f := range files {
if err := ReadFile(dir, f); err != nil {
return err
}
}
return nil
}
// InsertVersion returns the chart values file contents passed in
// with the version placeholder replaced with the current version
func InsertVersion(data []byte) []byte {
dataWithVersion := strings.Replace(string(data), versionPlaceholder, version.Version, 1)
return []byte(dataWithVersion)
}