-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathparse_helm.go
82 lines (71 loc) · 2.33 KB
/
parse_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
package lintcontext
import (
"log"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"
)
func (l *lintContextImpl) loadObjectsFromHelmChart(path string, options HelmOptions) error {
metadata := ObjectMetadata{FilePath: path}
renderedFiles, err := l.renderHelmChart(path, options)
if err != nil {
l.addInvalidObjects(InvalidObject{Metadata: metadata, LoadErr: err})
return nil
}
for path, contents := range renderedFiles {
// The first element of path will be the same as the last element of dir, because
// Helm duplicates it.
pathToTemplate := filepath.Join(filepath.Dir(path), path)
if err := l.loadObjectsFromReader(pathToTemplate, strings.NewReader(contents)); err != nil {
return errors.Wrapf(err, "loading objects from rendered helm chart %s/%s", path, pathToTemplate)
}
}
return nil
}
func (l *lintContextImpl) renderHelmChart(path string, options HelmOptions) (map[string]string, error) {
// Helm doesn't have great logging behaviour, and can spam stderr, so silence their logging.
// TODO: capture these logs.
log.SetOutput(nopWriter{})
defer log.SetOutput(os.Stderr)
var chrt *chart.Chart
var err error
if options.FromDir && options.FromArchive {
return nil, errors.New("cannot specify that helm chart is both a directory and an archive")
}
switch {
case options.FromArchive:
chrt, err = loader.LoadFile(path)
case options.FromDir:
chrt, err = loader.Load(path)
default:
chrt, err = loader.LoadArchive(options.FromReader)
}
if err != nil {
return nil, err
}
if err := chrt.Validate(); err != nil {
return nil, err
}
values, err := l.helmValuesOptions.MergeValues(nil)
if err != nil {
return nil, errors.Wrap(err, "loading provided Helm value options")
}
return l.renderValues(chrt, values)
}
func (l *lintContextImpl) renderValues(chrt *chart.Chart, values map[string]interface{}) (map[string]string, error) {
valuesToRender, err := chartutil.ToRenderValues(chrt, values, chartutil.ReleaseOptions{Name: "test-release", Namespace: "default"}, nil)
if err != nil {
return nil, err
}
e := engine.Engine{LintMode: true}
rendered, err := e.Render(chrt, valuesToRender)
if err != nil {
return nil, errors.Wrap(err, "failed to render")
}
return rendered, nil
}