feat(lint): Allow customization of APIVersions when using helm lint

Usage: `helm lint --api-versions []string`

Signed-off-by: Antonio Muñiz <amunizmartin@gmail.com>
pull/10463/head
Antonio Muñiz 4 years ago
parent 8ca401398d
commit 519d8b96b4
No known key found for this signature in database
GPG Key ID: 0CDAE283EF4E7A5F

@ -120,6 +120,7 @@ func newLintCmd(out io.Writer) *cobra.Command {
f := cmd.Flags() f := cmd.Flags()
f.BoolVar(&client.Strict, "strict", false, "fail on lint warnings") f.BoolVar(&client.Strict, "strict", false, "fail on lint warnings")
f.BoolVar(&client.WithSubcharts, "with-subcharts", false, "lint dependent charts") f.BoolVar(&client.WithSubcharts, "with-subcharts", false, "lint dependent charts")
f.StringArrayVarP(&client.APIVersions, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions")
addValueOptionsFlags(f, valueOpts) addValueOptionsFlags(f, valueOpts)
return cmd return cmd

@ -33,6 +33,11 @@ func TestLintCmdWithSubchartsFlag(t *testing.T) {
cmd: fmt.Sprintf("lint --with-subcharts %s", testChart), cmd: fmt.Sprintf("lint --with-subcharts %s", testChart),
golden: "output/lint-chart-with-bad-subcharts-with-subcharts.txt", golden: "output/lint-chart-with-bad-subcharts-with-subcharts.txt",
wantError: true, wantError: true,
}, {
name: "lint good chart using --api-versions flag",
cmd: "lint --api-versions networking.k8s.io/v1 testdata/testcharts/chart-with-api-versions",
golden: "output/lint-chart-with-api-versions.txt",
wantError: false,
}} }}
runTestCmd(t, tests) runTestCmd(t, tests)
} }

@ -0,0 +1,5 @@
==> Linting testdata/testcharts/chart-with-api-versions
[INFO] Chart.yaml: icon is recommended
[INFO] values.yaml: file does not exist
1 chart(s) linted, 0 chart(s) failed

@ -0,0 +1,7 @@
apiVersion: v1
description: Empty testing chart
home: https://k8s.io/helm
name: empty
sources:
- https://github.com/kubernetes/helm
version: 0.1.0

@ -0,0 +1,3 @@
{{- if not (.Capabilities.APIVersions.Has "networking.k8s.io/v1") }}
{{ fail "ERROR: APIVersion networking.k8s.io/v1 not found" }}
{{- end -}}

@ -36,6 +36,7 @@ type Lint struct {
Strict bool Strict bool
Namespace string Namespace string
WithSubcharts bool WithSubcharts bool
APIVersions []string
} }
// LintResult is the result of Lint // LintResult is the result of Lint
@ -58,7 +59,7 @@ func (l *Lint) Run(paths []string, vals map[string]interface{}) *LintResult {
} }
result := &LintResult{} result := &LintResult{}
for _, path := range paths { for _, path := range paths {
linter, err := lintChart(path, vals, l.Namespace, l.Strict) linter, err := lintChart(path, vals, l.Namespace, l.Strict, l.APIVersions)
if err != nil { if err != nil {
result.Errors = append(result.Errors, err) result.Errors = append(result.Errors, err)
continue continue
@ -75,7 +76,7 @@ func (l *Lint) Run(paths []string, vals map[string]interface{}) *LintResult {
return result return result
} }
func lintChart(path string, vals map[string]interface{}, namespace string, strict bool) (support.Linter, error) { func lintChart(path string, vals map[string]interface{}, namespace string, strict bool, apiVersions []string) (support.Linter, error) {
var chartPath string var chartPath string
linter := support.Linter{} linter := support.Linter{}
@ -114,5 +115,5 @@ func lintChart(path string, vals map[string]interface{}, namespace string, stric
return linter, errors.Wrap(err, "unable to check Chart.yaml file in chart") return linter, errors.Wrap(err, "unable to check Chart.yaml file in chart")
} }
return lint.All(chartPath, vals, namespace, strict), nil return lint.All(chartPath, vals, namespace, strict, apiVersions), nil
} }

@ -78,7 +78,7 @@ func TestLintChart(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
_, err := lintChart(tt.chartPath, map[string]interface{}{}, namespace, strict) _, err := lintChart(tt.chartPath, map[string]interface{}{}, namespace, strict, nil)
switch { switch {
case err != nil && !tt.err: case err != nil && !tt.err:
t.Errorf("%s", err) t.Errorf("%s", err)

@ -24,14 +24,14 @@ import (
) )
// All runs all of the available linters on the given base directory. // All runs all of the available linters on the given base directory.
func All(basedir string, values map[string]interface{}, namespace string, strict bool) support.Linter { func All(basedir string, values map[string]interface{}, namespace string, strict bool, apiVersions []string) support.Linter {
// Using abs path to get directory context // Using abs path to get directory context
chartDir, _ := filepath.Abs(basedir) chartDir, _ := filepath.Abs(basedir)
linter := support.Linter{ChartDir: chartDir} linter := support.Linter{ChartDir: chartDir}
rules.Chartfile(&linter) rules.Chartfile(&linter)
rules.ValuesWithOverrides(&linter, values) rules.ValuesWithOverrides(&linter, values)
rules.Templates(&linter, values, namespace, strict) rules.Templates(&linter, values, namespace, strict, apiVersions)
rules.Dependencies(&linter) rules.Dependencies(&linter)
return linter return linter
} }

@ -45,7 +45,7 @@ var (
) )
// Templates lints the templates in the Linter. // Templates lints the templates in the Linter.
func Templates(linter *support.Linter, values map[string]interface{}, namespace string, strict bool) { func Templates(linter *support.Linter, values map[string]interface{}, namespace string, strict bool, apiVersions []string) {
fpath := "templates/" fpath := "templates/"
templatesPath := filepath.Join(linter.ChartDir, fpath) templatesPath := filepath.Join(linter.ChartDir, fpath)
@ -80,7 +80,10 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace
if err != nil { if err != nil {
return return
} }
valuesToRender, err := chartutil.ToRenderValues(chart, cvals, options, nil)
caps := chartutil.DefaultCapabilities
caps.APIVersions = chartutil.VersionSet(apiVersions)
valuesToRender, err := chartutil.ToRenderValues(chart, cvals, options, caps)
if err != nil { if err != nil {
linter.RunLinterRule(support.ErrorSev, fpath, err) linter.RunLinterRule(support.ErrorSev, fpath, err)
return return

Loading…
Cancel
Save