Adds ability to ignore result.Errors

pull/13205/head
Daniel J. Pritchett 1 year ago
parent 9e6a329545
commit 1a37b28014
No known key found for this signature in database

@ -31,7 +31,6 @@ import (
"helm.sh/helm/v3/pkg/cli/values" "helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/lint/rules" "helm.sh/helm/v3/pkg/lint/rules"
"helm.sh/helm/v3/pkg/lint/support"
) )
var longLintHelp = ` var longLintHelp = `
@ -45,6 +44,7 @@ or recommendation, it will emit [WARNING] messages.
func newLintCmd(out io.Writer) *cobra.Command { func newLintCmd(out io.Writer) *cobra.Command {
client := action.NewLint() client := action.NewLint()
client.Debug = settings.Debug
valueOpts := &values.Options{} valueOpts := &values.Options{}
var kubeVersion string var kubeVersion string
var lintIgnoreFile string var lintIgnoreFile string
@ -92,15 +92,16 @@ func newLintCmd(out io.Writer) *cobra.Command {
var message strings.Builder var message strings.Builder
failed := 0 failed := 0
for _, path := range paths { for _, path := range paths {
result := client.Run([]string{path}, vals)
filteredResult := FilterIgnoredMessages(result, ignorePatterns, settings.Debug)
fmt.Fprintf(&message, "==> Linting %s\n", path) fmt.Fprintf(&message, "==> Linting %s\n", path)
for _, msg := range filteredResult.Messages { result := client.Run([]string{path}, vals)
result.Messages = rules.FilterIgnoredMessages(result.Messages, ignorePatterns)
result.Errors = rules.FilterIgnoredErrors(result.Errors, ignorePatterns)
for _, msg := range result.Messages {
fmt.Fprintf(&message, "%s\n", msg) fmt.Fprintf(&message, "%s\n", msg)
} }
if len(filteredResult.Messages) != 0 { if len(result.Messages) != 0 {
failed++ failed++
for _, err := range filteredResult.Errors { for _, err := range result.Errors {
fmt.Fprintf(&message, "Error: %s\n", err) fmt.Fprintf(&message, "Error: %s\n", err)
} }
} }
@ -125,57 +126,3 @@ func newLintCmd(out io.Writer) *cobra.Command {
return cmd return cmd
} }
func FilterIgnoredMessages(result *action.LintResult, patterns map[string][]string, debug bool) *action.LintResult {
filteredMessages := make([]support.Message, 0)
for _, msg := range result.Messages {
fullPath := extractFullPathFromError(msg.Err.Error())
if len(fullPath) == 0 {
break
}
if debug {
fmt.Printf("Extracted full path: %s\n", fullPath)
}
ignore := false
for path, pathPatterns := range patterns {
if strings.Contains(fullPath, filepath.Clean(path)) {
for _, pattern := range pathPatterns {
if strings.Contains(msg.Err.Error(), pattern) {
if debug {
fmt.Printf("Ignoring message: [%s] %s\n\n", fullPath, msg.Err.Error())
}
ignore = true
break
}
}
}
if ignore {
break
}
}
if !ignore {
filteredMessages = append(filteredMessages, msg)
}
}
return &action.LintResult{Messages: filteredMessages}
}
func extractFullPathFromError(errorString string) string {
parts := strings.Split(errorString, ":")
if len(parts) > 2 {
return strings.TrimSpace(parts[1])
}
return ""
}
/* TODO HIP-0019
- find ignore file path for a subchart
- add a chart or two for the end to end tests via testdata like in pkg/lint/lint_test.go
- review debug / output patterns across the helm project
Later/never
- XDG support
- helm config file support
- ignore file validation
-
*/

@ -9,7 +9,6 @@ import (
"helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/lint" "helm.sh/helm/v3/pkg/lint"
// "helm.sh/helm/v3/pkg/lint/rules"
"helm.sh/helm/v3/pkg/lint/support" "helm.sh/helm/v3/pkg/lint/support"
) )
@ -21,6 +20,7 @@ type Lint struct {
Quiet bool Quiet bool
KubeVersion *chartutil.KubeVersion KubeVersion *chartutil.KubeVersion
IgnoreFilePath string IgnoreFilePath string
Debug bool
} }
type LintResult struct { type LintResult struct {
TotalChartsLinted int TotalChartsLinted int
@ -38,7 +38,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.KubeVersion, l.IgnoreFilePath) linter, err := lintChart(path, vals, l.Namespace, l.KubeVersion)
if err != nil { if err != nil {
result.Errors = append(result.Errors, err) result.Errors = append(result.Errors, err)
continue continue
@ -64,7 +64,7 @@ func HasWarningsOrErrors(result *LintResult) bool {
return len(result.Errors) > 0 return len(result.Errors) > 0
} }
func lintChart(path string, vals map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion, ignoreFilePath string) (support.Linter, error) { func lintChart(path string, vals map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion) (support.Linter, error) {
var chartPath string var chartPath string
linter := support.Linter{} linter := support.Linter{}
@ -101,5 +101,5 @@ func lintChart(path string, vals map[string]interface{}, namespace string, kubeV
if _, err := os.Stat(filepath.Join(chartPath, "Chart.yaml")); err != nil { if _, err := os.Stat(filepath.Join(chartPath, "Chart.yaml")); err != nil {
return linter, errors.Wrap(err, "Chart.yaml file not found in chart") return linter, errors.Wrap(err, "Chart.yaml file not found in chart")
} }
return lint.AllWithKubeVersion(chartPath, vals, namespace, kubeVersion, ignoreFilePath), nil return lint.AllWithKubeVersion(chartPath, vals, namespace, kubeVersion), nil
} }

@ -17,34 +17,17 @@ limitations under the License.
package lint package lint
import ( import (
"fmt"
"log"
"io/ioutil"
"path/filepath"
"helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/lint/rules" "helm.sh/helm/v3/pkg/lint/rules"
"helm.sh/helm/v3/pkg/lint/support" "helm.sh/helm/v3/pkg/lint/support"
"path/filepath"
) )
func All(basedir string, values map[string]interface{}, namespace string, _ bool) support.Linter { 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)
} }
func AllWithKubeVersion(basedir string, values map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion, lintIgnoreFile string) support.Linter { func AllWithKubeVersion(basedir string, values map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion) support.Linter {
chartDir, _ := filepath.Abs(basedir) chartDir, _ := filepath.Abs(basedir)
var ignorePatterns map[string][]string
var err error
if lintIgnoreFile != "" {
ignorePatterns, err = rules.ParseIgnoreFile(lintIgnoreFile)
for key, value := range ignorePatterns {
fmt.Printf("Pattern: %s, Error: %s\n", key, value)
}
// Review this to properly handle logging
log.SetOutput(ioutil.Discard)
log.Println(err)
}
linter := support.Linter{ChartDir: chartDir} linter := support.Linter{ChartDir: chartDir}
if rules.IsIgnored(chartDir, ignorePatterns) {
return linter
}
rules.Chartfile(&linter) rules.Chartfile(&linter)
rules.ValuesWithOverrides(&linter, values) rules.ValuesWithOverrides(&linter, values)
rules.TemplatesWithKubeVersion(&linter, values, namespace, kubeVersion) rules.TemplatesWithKubeVersion(&linter, values, namespace, kubeVersion)

@ -3,18 +3,27 @@ package rules
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"helm.sh/helm/v3/pkg/lint/support"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
) )
var Debug bool
func ParseIgnoreFile(filePath string) (map[string][]string, error) { func ParseIgnoreFile(filePath string) (map[string][]string, error) {
patterns := make(map[string][]string) patterns := make(map[string][]string)
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close() defer func() {
err := file.Close()
if err != nil {
log.Printf("Failed to close ignore file at [%s]: %v", filePath, err)
}
}()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
@ -34,18 +43,105 @@ func ParseIgnoreFile(filePath string) (map[string][]string, error) {
return patterns, scanner.Err() return patterns, scanner.Err()
} }
func IsIgnored(errorMessage string, patterns map[string][]string) bool { func FilterIgnoredErrors(errors []error, patterns map[string][]string) []error {
for path, pathPatterns := range patterns { filteredErrors := make([]error, 0)
cleanedPath := filepath.Clean(path) for _, err := range errors {
if strings.Contains(errorMessage, cleanedPath) { errText := err.Error()
for _, pattern := range pathPatterns { errorFullPath := extractFullPathFromError(errText)
if strings.Contains(errorMessage, pattern) { if len(errorFullPath) == 0 {
fmt.Printf("Ignoring error related to path: %s with pattern: %s\n", path, pattern) debug("Unable to find a path for error, guess we'll keep it: %s", errText)
return true filteredErrors = append(filteredErrors, err)
continue
}
ignore := false
debug("Extracted full path: %s\n", errorFullPath)
for ignorablePath, pathPatterns := range patterns {
cleanIgnorablePath := filepath.Clean(ignorablePath)
if strings.Contains(errorFullPath, cleanIgnorablePath) {
for _, pattern := range pathPatterns {
if strings.Contains(err.Error(), pattern) {
debug("Ignoring error: [%s] %s\n\n", errorFullPath, errText)
ignore = true
break
}
} }
} }
if ignore {
break
}
}
if !ignore {
debug("keeping unignored error: [%s]", errText)
filteredErrors = append(filteredErrors, err)
} }
} }
return false
return filteredErrors
} }
func FilterIgnoredMessages(messages []support.Message, patterns map[string][]string) []support.Message {
filteredMessages := make([]support.Message, 0)
for _, msg := range messages {
errText := msg.Err.Error()
errorFullPath := extractFullPathFromError(errText)
if len(errorFullPath) == 0 {
debug("Unable to find a path for message, guess we'll keep it: %s", errText)
filteredMessages = append(filteredMessages, msg)
continue
}
ignore := false
debug("Extracted full path: %s\n", errorFullPath)
for ignorablePath, pathPatterns := range patterns {
cleanIgnorablePath := filepath.Clean(ignorablePath)
if strings.Contains(errorFullPath, cleanIgnorablePath) {
for _, pattern := range pathPatterns {
if strings.Contains(msg.Err.Error(), pattern) {
debug("Ignoring message: [%s] %s\n\n", errorFullPath, errText)
ignore = true
break
}
}
}
if ignore {
break
}
}
if !ignore {
debug("keeping unignored message: [%s]", errText)
filteredMessages = append(filteredMessages, msg)
}
}
return filteredMessages
}
// TODO: figure out & fix or remove
func extractFullPathFromError(errorString string) string {
parts := strings.Split(errorString, ":")
if len(parts) > 2 {
return strings.TrimSpace(parts[1])
}
return ""
}
// TODO: DELETE
var logger = log.New(os.Stderr, "[debug] ", log.Lshortfile)
func debug(format string, v ...interface{}) {
if Debug {
format = fmt.Sprintf("[debug] %s\n", format)
logger.Output(2, fmt.Sprintf(format, v...))
}
}
// END TODO: DELETE
/* TODO HIP-0019
- find ignore file path for a subchart
- add a chart or two for the end to end tests via testdata like in pkg/lint/lint_test.go
- review debug / output patterns across the helm project
Later/never
- XDG support
- helm config file support
- ignore file validation
-
*/

@ -0,0 +1,71 @@
package rules
import (
"fmt"
"github.com/stretchr/testify/assert"
"helm.sh/helm/v3/pkg/lint/support"
"testing"
"text/template"
)
func TestFilterIgnoredMessages(t *testing.T) {
type args struct {
messages []support.Message
ignorePatterns map[string][]string
}
tests := []struct {
name string
args args
want []support.Message
}{
{
name: "should filter ignored messages only",
args: args{
messages: []support.Message{
{
Severity: 3,
Path: "templates/",
Err: template.ExecError{
Name: "certmanager-issuer/templates/rbac-config.yaml",
Err: fmt.Errorf(`template: certmanager-issuer/templates/rbac-config.yaml:1:67: executing "certmanager-issuer/templates/rbac-config.yaml" at <.Values.global.ingress>: nil pointer evaluating interface {}.ingress`),
},
},
{
Severity: 1,
Path: "values.yaml",
Err: fmt.Errorf("file does not exist"),
},
{
Severity: 1,
Path: "Chart.yaml",
Err: fmt.Errorf("icon is recommended"),
},
},
ignorePatterns: map[string][]string{
"certmanager-issuer/templates/rbac-config.yaml": {
"<.Values.global.ingress>",
},
},
},
want: []support.Message{
{
Severity: 1,
Path: "values.yaml",
Err: fmt.Errorf("file does not exist"),
},
{
Severity: 1,
Path: "Chart.yaml",
Err: fmt.Errorf("icon is recommended"),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FilterIgnoredMessages(tt.args.messages, tt.args.ignorePatterns)
assert.Equalf(t, tt.want, got, "FilterIgnoredMessages(%v, %v)", tt.args.messages, tt.args.ignorePatterns)
})
}
}
Loading…
Cancel
Save