From 1eaae6de3950ac6d28d70585c507a9bb3065c8f9 Mon Sep 17 00:00:00 2001 From: Danilo Patrucco Date: Thu, 4 Jul 2024 14:08:50 -0400 Subject: [PATCH] add changes -- prepare for testing Signed-off-by: Danilo Patrucco --- cmd/helm/lint.go | 26 +++++++++++++++++--------- pkg/lint/lint.go | 20 +++++++++++--------- pkg/lint/rules/ignore.go | 27 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 pkg/lint/rules/ignore.go diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index 6b54bdd3f..e5b4898c8 100644 --- a/cmd/helm/lint.go +++ b/cmd/helm/lint.go @@ -31,6 +31,7 @@ import ( "helm.sh/helm/v3/pkg/cli/values" "helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/lint/support" + "helm.sh/helm/v3/pkg/lint/rules" ) var longLintHelp = ` @@ -46,6 +47,7 @@ func newLintCmd(out io.Writer) *cobra.Command { client := action.NewLint() valueOpts := &values.Options{} var kubeVersion string + var lintIgnoreFile string cmd := &cobra.Command{ Use: "lint PATH", @@ -86,15 +88,25 @@ func newLintCmd(out io.Writer) *cobra.Command { return err } + var ignorePatterns []string + if lintIgnoreFile != "" { + ignorePatterns, err = rules.ParseIgnoreFile(lintIgnoreFile) + if err != nil { + return fmt.Errorf("failed to parse .helmlintignore file: %v", err) + } + } + var message strings.Builder failed := 0 errorsOrWarnings := 0 for _, path := range paths { + if rules.IsIgnored(path, ignorePatterns) { + continue + } + result := client.Run([]string{path}, vals) - // If there is no errors/warnings and quiet flag is set - // go to the next chart hasWarningsOrErrors := action.HasWarningsOrErrors(result) if hasWarningsOrErrors { errorsOrWarnings++ @@ -105,10 +117,6 @@ func newLintCmd(out io.Writer) *cobra.Command { fmt.Fprintf(&message, "==> Linting %s\n", path) - // All the Errors that are generated by a chart - // that failed a lint will be included in the - // results.Messages so we only need to print - // the Errors if there are no Messages. if len(result.Messages) == 0 { for _, err := range result.Errors { fmt.Fprintf(&message, "Error %s\n", err) @@ -125,9 +133,6 @@ func newLintCmd(out io.Writer) *cobra.Command { failed++ } - // Adding extra new line here to break up the - // results, stops this from being a big wall of - // text and makes it easier to follow. fmt.Fprint(&message, "\n") } @@ -149,7 +154,10 @@ func newLintCmd(out io.Writer) *cobra.Command { f.BoolVar(&client.WithSubcharts, "with-subcharts", false, "lint dependent charts") f.BoolVar(&client.Quiet, "quiet", false, "print only warnings and errors") f.StringVar(&kubeVersion, "kube-version", "", "Kubernetes version used for capabilities and deprecation checks") + f.StringVar(&lintIgnoreFile, "lint-ignore-file", "", "path to .helmlintignore file to specify ignore patterns") // Add the flag for .helmlintignore file addValueOptionsFlags(f, valueOpts) return cmd } + + diff --git a/pkg/lint/lint.go b/pkg/lint/lint.go index c0e79f55b..4c620d24c 100644 --- a/pkg/lint/lint.go +++ b/pkg/lint/lint.go @@ -14,27 +14,29 @@ See the License for the specific language governing permissions and limitations under the License. */ -package lint // import "helm.sh/helm/v3/pkg/lint" +package lint import ( "path/filepath" + "os" "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/lint/rules" "helm.sh/helm/v3/pkg/lint/support" ) - -// All runs all of the available linters on the given base directory. func All(basedir string, values map[string]interface{}, namespace string, _ bool) support.Linter { - return AllWithKubeVersion(basedir, values, namespace, nil) + return AllWithKubeVersion(basedir, values, namespace, nil, "") } - -// 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{}, namespace string, kubeVersion *chartutil.KubeVersion) support.Linter { - // Using abs path to get directory context +func AllWithKubeVersion(basedir string, values map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion, lintIgnoreFile string) support.Linter { chartDir, _ := filepath.Abs(basedir) - + var ignorePatterns []string + if lintIgnoreFile != "" { + ignorePatterns, _ = rules.ParseIgnoreFile(lintIgnoreFile) // Simplified error handling for the example + } linter := support.Linter{ChartDir: chartDir} + if rules.IsIgnored(chartDir, ignorePatterns) { + return linter + } rules.Chartfile(&linter) rules.ValuesWithOverrides(&linter, values) rules.TemplatesWithKubeVersion(&linter, values, namespace, kubeVersion) diff --git a/pkg/lint/rules/ignore.go b/pkg/lint/rules/ignore.go new file mode 100644 index 000000000..df50997bb --- /dev/null +++ b/pkg/lint/rules/ignore.go @@ -0,0 +1,27 @@ +package rules + +import ( + "bufio" + "os" + "strings" +) + +// ParseIgnoreFile reads and parses the .helmlintignore file, returning a list of patterns +func ParseIgnoreFile(filePath string) ([]string, error) { + var patterns []string + file, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if line != "" && !strings.HasPrefix(line, "#") { // Ignore comments and empty lines + patterns = append(patterns, line) + } + } + + return patterns, scanner.Err() +}