From a9694a1dcc56434fbfa58118deae22c0ba32edd0 Mon Sep 17 00:00:00 2001 From: bartem Date: Wed, 20 Mar 2024 16:31:45 +0300 Subject: [PATCH] mr feedback Signed-off-by: bartem --- pkg/action/lint.go | 3 ++- pkg/lint/lint.go | 8 ++++---- pkg/lint/rules/template.go | 13 +++++++++---- pkg/lint/rules/template_options.go | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 pkg/lint/rules/template_options.go diff --git a/pkg/action/lint.go b/pkg/action/lint.go index cf6bd23d9..d1553f855 100644 --- a/pkg/action/lint.go +++ b/pkg/action/lint.go @@ -22,6 +22,7 @@ import ( "strings" "github.com/pkg/errors" + "helm.sh/helm/v3/pkg/lint/rules" "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/lint" @@ -126,5 +127,5 @@ func lintChart(path string, vals map[string]interface{}, releaseName, namespace return linter, errors.Wrap(err, "unable to check Chart.yaml file in chart") } - return lint.AllWithKubeVersion(chartPath, vals, releaseName, namespace, kubeVersion), nil + return lint.AllWithKubeVersion(chartPath, vals, namespace, kubeVersion, rules.WithReleaseName(releaseName)), nil } diff --git a/pkg/lint/lint.go b/pkg/lint/lint.go index 15ebcad4d..31c93c867 100644 --- a/pkg/lint/lint.go +++ b/pkg/lint/lint.go @@ -25,19 +25,19 @@ import ( ) // All runs all of the available linters on the given base directory. -func All(basedir string, values map[string]interface{}, releaseName, namespace string, _ bool) support.Linter { - return AllWithKubeVersion(basedir, values, releaseName, namespace, nil) +func All(basedir string, values map[string]interface{}, namespace string, _ bool, opts ...rules.TemplateOption) support.Linter { + return AllWithKubeVersion(basedir, values, namespace, nil, opts...) } // AllWithKubeVersion runs all the available linters on the given base directory, allowing to specify the kubernetes version. -func AllWithKubeVersion(basedir string, values map[string]interface{}, releaseName, namespace string, kubeVersion *chartutil.KubeVersion) support.Linter { +func AllWithKubeVersion(basedir string, values map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion, opts ...rules.TemplateOption) support.Linter { // Using abs path to get directory context chartDir, _ := filepath.Abs(basedir) linter := support.Linter{ChartDir: chartDir} rules.Chartfile(&linter) rules.ValuesWithOverrides(&linter, values) - rules.TemplatesWithKubeVersion(&linter, values, releaseName, namespace, kubeVersion) + rules.TemplatesWithKubeVersion(&linter, values, namespace, kubeVersion, opts...) rules.Dependencies(&linter) return linter } diff --git a/pkg/lint/rules/template.go b/pkg/lint/rules/template.go index 5cf641684..3aec094f3 100644 --- a/pkg/lint/rules/template.go +++ b/pkg/lint/rules/template.go @@ -45,12 +45,12 @@ var ( ) // Templates lints the templates in the Linter. -func Templates(linter *support.Linter, values map[string]interface{}, releaseName, namespace string, _ bool) { - TemplatesWithKubeVersion(linter, values, releaseName, namespace, nil) +func Templates(linter *support.Linter, values map[string]interface{}, namespace string, _ bool, opts ...TemplateOption) { + TemplatesWithKubeVersion(linter, values, namespace, nil, opts...) } // TemplatesWithKubeVersion lints the templates in the Linter, allowing to specify the kubernetes version. -func TemplatesWithKubeVersion(linter *support.Linter, values map[string]interface{}, releaseName, namespace string, kubeVersion *chartutil.KubeVersion) { +func TemplatesWithKubeVersion(linter *support.Linter, values map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion, opts ...TemplateOption) { fpath := "templates/" templatesPath := filepath.Join(linter.ChartDir, fpath) @@ -70,8 +70,13 @@ func TemplatesWithKubeVersion(linter *support.Linter, values map[string]interfac return } + o := defaultTemplateOptions + for _, optFn := range opts { + optFn(o) + } + options := chartutil.ReleaseOptions{ - Name: releaseName, + Name: o.releaseName, Namespace: namespace, } diff --git a/pkg/lint/rules/template_options.go b/pkg/lint/rules/template_options.go new file mode 100644 index 000000000..203801fa5 --- /dev/null +++ b/pkg/lint/rules/template_options.go @@ -0,0 +1,20 @@ +package rules + +type ( + templateOptions struct { + releaseName string + } + + TemplateOption func(o *templateOptions) +) + +var defaultTemplateOptions = &templateOptions{ + releaseName: "test-release", +} + +// WithReleaseName specify release name for linter +func WithReleaseName(name string) TemplateOption { + return func(o *templateOptions) { + o.releaseName = name + } +}