diff --git a/pkg/action/lint.go b/pkg/action/lint.go index 6af92afd7..8be29a4de 100644 --- a/pkg/action/lint.go +++ b/pkg/action/lint.go @@ -1,129 +1,99 @@ -/* -Copyright The Helm Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - package action import ( - "os" - "path/filepath" - "strings" + "os" + "path/filepath" + "strings" - "github.com/pkg/errors" + "github.com/pkg/errors" - "helm.sh/helm/v3/pkg/chartutil" - "helm.sh/helm/v3/pkg/lint" - "helm.sh/helm/v3/pkg/lint/support" + "helm.sh/helm/v3/pkg/chartutil" + "helm.sh/helm/v3/pkg/lint" + "helm.sh/helm/v3/pkg/lint/rules" + "helm.sh/helm/v3/pkg/lint/support" ) // Lint is the action for checking that the semantics of a chart are well-formed. -// -// It provides the implementation of 'helm lint'. type Lint struct { - Strict bool - Namespace string - WithSubcharts bool - Quiet bool - KubeVersion *chartutil.KubeVersion - IgnoreFilePath string + Strict bool + Namespace string + WithSubcharts bool + Quiet bool + KubeVersion *chartutil.KubeVersion + IgnoreFilePath string } - - -// LintResult is the result of Lint type LintResult struct { - TotalChartsLinted int - Messages []support.Message - Errors []error + TotalChartsLinted int + Messages []support.Message + Errors []error } - -// NewLint creates a new Lint object with the given configuration. func NewLint() *Lint { - return &Lint{} + return &Lint{} } - func (l *Lint) Run(paths []string, vals map[string]interface{}) *LintResult { - lowestTolerance := support.ErrorSev - if l.Strict { - lowestTolerance = support.WarningSev - } - result := &LintResult{} - for _, path := range paths { - linter, err := lintChart(path, vals, l.Namespace, l.KubeVersion, l.IgnoreFilePath) - if err != nil { - result.Errors = append(result.Errors, err) - continue - } - - result.Messages = append(result.Messages, linter.Messages...) - result.TotalChartsLinted++ - for _, msg := range linter.Messages { - if msg.Severity >= lowestTolerance { - result.Errors = append(result.Errors, msg.Err) - } - } - } - return result + lowestTolerance := support.ErrorSev + if l.Strict { + lowestTolerance = support.WarningSev + } + + result := &LintResult{} + for _, path := range paths { + linter, err := lintChart(path, vals, l.Namespace, l.KubeVersion, l.IgnoreFilePath) + if err != nil { + result.Errors = append(result.Errors, err) + continue + } + + result.Messages = append(result.Messages, linter.Messages...) + result.TotalChartsLinted++ + for _, msg := range linter.Messages { + if msg.Severity >= lowestTolerance { + result.Errors = append(result.Errors, msg.Err) + } + } + } + return result } - -func HasWarningsOrErrors(result *LintResult) bool { - for _, msg := range result.Messages { - if msg.Severity > support.InfoSev { - return true - } - } - return len(result.Errors) > 0 -} - func lintChart(path string, vals map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion, ignoreFilePath string) (support.Linter, error) { - var chartPath string - linter := support.Linter{} - - if strings.HasSuffix(path, ".tgz") || strings.HasSuffix(path, ".tar.gz") { - tempDir, err := os.MkdirTemp("", "helm-lint") - if err != nil { - return linter, errors.Wrap(err, "unable to create temp dir to extract tarball") - } - defer os.RemoveAll(tempDir) - - file, err := os.Open(path) - if err != nil { - return linter, errors.Wrap(err, "unable to open tarball") - } - defer file.Close() - - if err = chartutil.Expand(tempDir, file); err != nil { - return linter, errors.Wrap(err, "unable to extract tarball") - } - - files, err := os.ReadDir(tempDir) - if err != nil { - return linter, errors.Wrapf(err, "unable to read temporary output directory %s", tempDir) - } - if !files[0].IsDir() { - return linter, errors.Errorf("unexpected file %s in temporary output directory %s", files[0].Name(), tempDir) - } - - chartPath = filepath.Join(tempDir, files[0].Name()) - } else { - chartPath = path - } - - // Guard: Error out if this is not a chart. - if _, err := os.Stat(filepath.Join(chartPath, "Chart.yaml")); err != nil { - return linter, errors.Wrap(err, "unable to check Chart.yaml file in chart") - } - - return lint.AllWithKubeVersion(chartPath, vals, namespace, kubeVersion, ignoreFilePath), nil + var chartPath string + linter := support.Linter{} + + if strings.HasSuffix(path, ".tgz") || strings.HasSuffix(path, ".tar.gz") { + tempDir, err := os.MkdirTemp("", "helm-lint") + if err != nil { + return linter, errors.Wrap(err, "unable to create temp dir to extract tarball") + } + defer os.RemoveAll(tempDir) + + file, err := os.Open(path) + if err != nil { + return linter, errors.Wrap(err, "unable to open tarball") + } + defer file.Close() + + if err = chartutil.Expand(tempDir, file); err != nil { + return linter, errors.Wrap(err, "unable to extract tarball") + } + + files, err := os.ReadDir(tempDir) + if err != nil { + return linter, errors.Wrapf(err, "unable to read temporary output directory %s", tempDir) + } + if !files[0].IsDir() { + return linter, errors.Errorf("unexpected file %s in temporary output directory %s", files[0].Name(), tempDir) + } + + chartPath = filepath.Join(tempDir, files[0].Name()) + } else { + chartPath = path + } + + if _, err := os.Stat(filepath.Join(chartPath, "Chart.yaml")); err != nil { + return linter, errors.Wrap(err, "Chart.yaml file not found in chart") + } + ignorePatterns, err := rules.ParseIgnoreFile(ignoreFilePath) + if err != nil { + return linter, errors.Wrap(err, "failed to parse .helmlintignore file") + } + return lint.AllWithKubeVersion(chartPath, vals, namespace, kubeVersion, ignorePatterns), nil } diff --git a/pkg/lint/rules/isignored.go b/pkg/lint/rules/isignored.go index 8f3274a0d..150521cec 100644 --- a/pkg/lint/rules/isignored.go +++ b/pkg/lint/rules/isignored.go @@ -3,6 +3,7 @@ package rules import ( "path/filepath" "strings" + "fmt" ) func IsIgnored(path string, patterns []string) bool { @@ -10,6 +11,7 @@ func IsIgnored(path string, patterns []string) bool { cleanedPath := filepath.Clean(path) cleanedPattern := filepath.Clean(pattern) if match, err := filepath.Match(cleanedPattern, cleanedPath); err == nil && match { + fmt.Printf("Ignoring path: %s due to pattern: %s\n", path, pattern) return true } if strings.HasSuffix(cleanedPattern, "/") || strings.HasSuffix(cleanedPattern, "\\") {