-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
87 lines (82 loc) · 2.48 KB
/
parse.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
package setup
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/rancher/charts/testing/builder"
"github.com/sirupsen/logrus"
"golang.stackrox.io/kube-linter/pkg/lintcontext"
"helm.sh/helm/v3/pkg/cli/values"
)
type Parser interface {
ParseTemplate(template string, glob string) error
ParseTemplateWithOptions(template string, glob string, options builder.ParseOptions) error
}
func ParseValuesForChart(p Parser, chartPath string, strict bool) error {
chartPath = filepath.Clean(chartPath)
logrus.Infof("Parsing templates for %s", chartPath)
// Validate that the chartPath points to a single Helm chart
if err := ensureHelmChartExists(chartPath); err != nil {
return err
}
valuesFilesDirpath := filepath.Join(testsDir, valuesFilesDir)
valuesFiles, err := ioutil.ReadDir(valuesFilesDirpath)
if err != nil {
return fmt.Errorf("Could not read values.yaml files in dir %s: %s", valuesFilesDirpath, err)
}
if len(valuesFiles) == 0 {
logrus.Warnf("Could not find any values.yaml files in dir %s: %s", valuesFilesDirpath, err)
return nil
}
for _, valuesFile := range valuesFiles {
valuesAbsPath := filepath.Join(valuesFilesDirpath, valuesFile.Name())
valuesPath := filepath.Clean(strings.TrimPrefix(valuesAbsPath, repoRoot+"/"))
err := p.ParseTemplateWithOptions(
valuesFile.Name(),
chartPath,
builder.ParseOptions{
Strict: strict,
Options: lintcontext.Options{
HelmValuesOptions: values.Options{
ValueFiles: []string{valuesPath},
},
},
},
)
if err != nil {
return fmt.Errorf("Failed to parse %s: %s", valuesFile.Name(), err)
}
}
return nil
}
func ensureHelmChartExists(chartPath string) error {
chartPathInfo, err := os.Stat(chartPath)
if err != nil {
return fmt.Errorf("Encountered error while trying to describe %s: %s", chartPath, err)
}
// Check if chartPath points to a tgz file
if !chartPathInfo.IsDir() {
if filepath.Ext(chartPath) != ".tgz" {
return fmt.Errorf("Not a valid Helm chart: %s does not point to a chart archive", chartPath)
}
return nil
}
// Check if the directory has a Chart.yaml
files, err := ioutil.ReadDir(chartPath)
if err != nil {
return fmt.Errorf("Encountered error while trying to read files in %s: %s", chartPath, err)
}
foundChartYaml := false
for _, file := range files {
if file.Name() == "Chart.yaml" {
foundChartYaml = true
break
}
}
if !foundChartYaml {
return fmt.Errorf("Not a valid Helm chart: %s does not have a Chart.yaml", chartPath)
}
return nil
}