feat(helm): 'helm lint' lints dependent charts

Signed-off-by: Min Kyu Lee <mknicklee@gmail.com>
pull/6052/head
Min Kyu Lee 6 years ago
parent e70f012bb6
commit 2618d4142e

@ -36,7 +36,7 @@ import (
var longLintHelp = ` var longLintHelp = `
This command takes a path to a chart and runs a series of tests to verify that This command takes a path to a chart and runs a series of tests to verify that
the chart is well-formed. the chart and its dependent charts are well-formed.
If the linter encounters things that will cause the chart to fail installation, If the linter encounters things that will cause the chart to fail installation,
it will emit [ERROR] messages. If it encounters issues that break with convention it will emit [ERROR] messages. If it encounters issues that break with convention
@ -100,29 +100,32 @@ func (l *lintCmd) run() error {
var total int var total int
var failures int var failures int
for _, path := range l.paths { for _, path := range l.paths {
if linter, err := lintChart(path, rvals, l.namespace, l.strict); err != nil { chartPaths := getChartPaths(path)
fmt.Println("==> Skipping", path) for _, chartPath := range chartPaths {
fmt.Println(err) if linter, err := lintChart(chartPath, rvals, l.namespace, l.strict); err != nil {
if err == errLintNoChart { fmt.Println("==> Skipping", chartPath)
failures = failures + 1 fmt.Println(err)
} if err == errLintNoChart {
} else { failures = failures + 1
fmt.Println("==> Linting", path) }
} else {
if len(linter.Messages) == 0 { fmt.Println("==> Linting", chartPath)
fmt.Println("Lint OK")
} if len(linter.Messages) == 0 {
fmt.Println("Lint OK")
for _, msg := range linter.Messages { }
fmt.Println(msg)
} for _, msg := range linter.Messages {
fmt.Println(msg)
total = total + 1 }
if linter.HighestSeverity >= lowestTolerance {
failures = failures + 1 total = total + 1
if linter.HighestSeverity >= lowestTolerance {
failures = failures + 1
}
} }
fmt.Println("")
} }
fmt.Println("")
} }
msg := fmt.Sprintf("%d chart(s) linted", total) msg := fmt.Sprintf("%d chart(s) linted", total)
@ -135,6 +138,17 @@ func (l *lintCmd) run() error {
return nil return nil
} }
func getChartPaths(root string) []string {
charts := []string{root}
filepath.Walk(filepath.Join(root, "charts"), func(path string, info os.FileInfo, err error) error {
if info != nil && (info.Name() == "Chart.yaml" || strings.HasSuffix(path, ".tgz")) {
charts = append(charts, filepath.Dir(path))
}
return nil
})
return charts
}
func lintChart(path string, vals []byte, namespace string, strict bool) (support.Linter, error) { func lintChart(path string, vals []byte, namespace string, strict bool) (support.Linter, error) {
var chartPath string var chartPath string
linter := support.Linter{} linter := support.Linter{}

@ -17,6 +17,8 @@ limitations under the License.
package main package main
import ( import (
"io/ioutil"
"os"
"testing" "testing"
) )
@ -29,6 +31,7 @@ var (
invalidArchivedChartPath = "testdata/testcharts/invalidcompressedchart0.1.0.tgz" invalidArchivedChartPath = "testdata/testcharts/invalidcompressedchart0.1.0.tgz"
chartDirPath = "testdata/testcharts/decompressedchart/" chartDirPath = "testdata/testcharts/decompressedchart/"
chartMissingManifest = "testdata/testcharts/chart-missing-manifest" chartMissingManifest = "testdata/testcharts/chart-missing-manifest"
invalidDepChart = "testdata/testcharts/invaliddepchart"
) )
func TestLintChart(t *testing.T) { func TestLintChart(t *testing.T) {
@ -52,3 +55,19 @@ func TestLintChart(t *testing.T) {
t.Errorf("Expected a chart parsing error") t.Errorf("Expected a chart parsing error")
} }
} }
func TestLintDependentCharts(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
if err := os.Chdir(invalidDepChart); err != nil {
t.Fatal(err)
}
defer os.Chdir(pwd)
linter := newLintCmd(ioutil.Discard)
if err := linter.Execute(); err == nil {
t.Errorf("Expected a chart parsing error")
}
}

@ -0,0 +1,4 @@
apiVersion: v1
description: A Helm chart for Kubernetes
name: invaliddepchart
version: 0.1.0

@ -0,0 +1 @@
description: A Helm chart for Kubernetes

@ -0,0 +1,4 @@
dependencies:
- name: depchart
version: 0.1.0
repository: "https://example.com/charts"

@ -6,7 +6,7 @@ Examines a chart for possible issues
This command takes a path to a chart and runs a series of tests to verify that This command takes a path to a chart and runs a series of tests to verify that
the chart is well-formed. the chart and its dependent charts are well-formed.
If the linter encounters things that will cause the chart to fail installation, If the linter encounters things that will cause the chart to fail installation,
it will emit [ERROR] messages. If it encounters issues that break with convention it will emit [ERROR] messages. If it encounters issues that break with convention
@ -45,4 +45,4 @@ helm lint [flags] PATH
* [helm](helm.md) - The Helm package manager for Kubernetes. * [helm](helm.md) - The Helm package manager for Kubernetes.
###### Auto generated by spf13/cobra on 16-May-2019 ###### Auto generated by spf13/cobra on 19-Jul-2019

Loading…
Cancel
Save