Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ignore-paths #515

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/configuring-kubelinter.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ checks:
## Ignore paths

To ignore checks on files or directories under certain paths, add ignored paths to `ignorePaths`.
Ignore path uses [`**` match syntax](https://pkg.go.dev/github.com/bmatcuk/doublestar#Match)
```yaml
checks:
ignorePaths:
- ~/foo/bar
- ../baz
- ~/foo/bar/**
- /**/*/foo/**
- ../baz/**
- /tmp/*.yaml
```
> Equivalent CLI flag is `--ignore-paths`
Expand Down
8 changes: 8 additions & 0 deletions e2etests/bats-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,11 @@ get_value_from() {
[[ "${failing_resource}" == "bad-irsa-role" ]]
[[ "${count}" == "2" ]]
}

@test "flag-ignore-paths" {
tmp="."
cmd="${KUBE_LINTER_BIN} lint --ignore-paths \"tests/**\" --ignore-paths \"e2etests/**\" ${tmp}"
run ${cmd}
print_info "${status}" "${output}" "${cmd}" "${tmp}"
[ "$status" -eq 0 ]
}
2 changes: 1 addition & 1 deletion e2etests/check-bats-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ run() {
local tmp_write_dir=/tmp/kubelinter/$(date +'%d-%m-%Y-%H-%M')
mkdir -p "${tmp_write_dir}"

grep "@test" e2etests/bats-tests.sh | grep -v 'template-' | cut -d'"' -f2 > ${tmp_write_dir}/batstests.log
grep "@test" e2etests/bats-tests.sh | grep -v 'flag-' | grep -v 'template-' | cut -d'"' -f2 > ${tmp_write_dir}/batstests.log
${KUBE_LINTER_BIN:-kube-linter} checks list --format json | jq -r '.[].name' > ${tmp_write_dir}/kubelinterchecks.log
diff -c ${tmp_write_dir}/kubelinterchecks.log ${tmp_write_dir}/batstests.log || { echo >&2 "ERROR: The output of '${KUBE_LINTER_BIN} checks list' differs from the tests in 'e2etests/bats-tests.sh'. See above diff."; exit 1; }
}
Expand Down
26 changes: 2 additions & 24 deletions pkg/configresolver/config_resolver.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package configresolver

import (
"path/filepath"

"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"golang.stackrox.io/kube-linter/internal/defaultchecks"
"golang.stackrox.io/kube-linter/internal/errorhelpers"
"golang.stackrox.io/kube-linter/internal/set"
"golang.stackrox.io/kube-linter/pkg/builtinchecks"
"golang.stackrox.io/kube-linter/pkg/checkregistry"
"golang.stackrox.io/kube-linter/pkg/config"
"golang.stackrox.io/kube-linter/pkg/pathutil"
)

// LoadCustomChecksInto loads the custom checks from the config into the check registry.
Expand Down Expand Up @@ -65,7 +62,7 @@ func GetIgnorePaths(cfg *config.Config) ([]string, error) {
errorList := errorhelpers.NewErrorList("check ignore paths")
ignorePaths := set.NewStringSet()
for _, path := range cfg.Checks.IgnorePaths {
res, err := getIgnorePath(path)
res, err := pathutil.GetAbsolutPath(path)
if err != nil {
errorList.AddError(err)
continue
Expand All @@ -80,22 +77,3 @@ func GetIgnorePaths(cfg *config.Config) ([]string, error) {
return i < j
}), nil
}

func getIgnorePath(path string) (string, error) {
switch {
case path[0] == '~':
expandedPath, err := homedir.Expand(path)
if err != nil {
return "", errors.Wrapf(err, "could not expand path: %q", expandedPath)
}
return expandedPath, nil
case !filepath.IsAbs(path):
absPath, err := filepath.Abs(path)
if err != nil {
return "", errors.Wrapf(err, "could not expand non-absolute path: %q", absPath)
}
return absPath, nil
default:
return path, nil
}
}
25 changes: 22 additions & 3 deletions pkg/lintcontext/create_contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sort"
"strings"

"golang.stackrox.io/kube-linter/pkg/pathutil"

"github.com/bmatcuk/doublestar/v4"
"github.com/pkg/errors"
"golang.stackrox.io/kube-linter/internal/set"
Expand Down Expand Up @@ -53,10 +55,13 @@ fileOrDirsLoop:
}

for _, path := range ignorePaths {
// Useing doublestar to enable **
// Using doublestar to enable **
// See https://github.com/golang/go/issues/11862
globMatch, _ := doublestar.PathMatch(path, fileOrDir)
if strings.HasPrefix(fileOrDir, path) || globMatch {
globMatch, err := doublestar.PathMatch(path, fileOrDir)
if err != nil {
return nil, errors.Wrapf(err, "could not match pattern %s", path)
}
if globMatch {
continue fileOrDirsLoop
}
}
Expand All @@ -70,6 +75,20 @@ fileOrDirsLoop:
return nil
}

for _, path := range ignorePaths {
absPath, err := pathutil.GetAbsolutPath(currentPath)
if err != nil {
return errors.Wrapf(err, "could not get absolute path for %s", currentPath)
}
globMatch, err := doublestar.PathMatch(path, absPath)
if err != nil {
return errors.Wrapf(err, "could not match pattern %s", path)
}
if globMatch {
return nil
}
}

if !info.IsDir() {
if strings.HasSuffix(strings.ToLower(currentPath), ".tgz") {
ctx := newCtx(options)
Expand Down
29 changes: 27 additions & 2 deletions pkg/lintcontext/create_contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"testing"

"golang.stackrox.io/kube-linter/pkg/pathutil"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -16,11 +18,34 @@ const (
chartDirectory = "../../tests/testdata/mychart"
renamedTarball = "../../tests/testdata/my-renamed-chart-0.1.0.tgz"
renamedChartDir = "../../tests/testdata/my-renamed-chart"
mockIgnorePath = "../../tests/testdata/"
mockIgnorePath = "../../tests/testdata/**"
mockGlobIgnorePath = "../../tests/**"
mockPath = "mock path"
)

func TestCreateContextsWithIgnorePaths(t *testing.T) {
ignoredPaths := []string{
"../../.golangci?yml",
"/**/*/testdata/**/*",
"/**/*/checks/**/*",
"/**/*/test_helper/**/*",
"../../pkg/**/*",
"../../.pre-commit-hooks*",
"../../.github/**",
}
ignoredAbsPaths := make([]string, 0, len(ignoredPaths))
for _, p := range ignoredPaths {
abs, err := pathutil.GetAbsolutPath(p)
assert.NoError(t, err)
ignoredAbsPaths = append(ignoredAbsPaths, abs)
}

testPath := "../../"
contexts, err := CreateContexts(ignoredAbsPaths, testPath)
assert.NoError(t, err)
checkEmptyLintContext(t, contexts)
}

func TestCreateContextsObjectPaths(t *testing.T) {
bools := []bool{false, true}

Expand Down Expand Up @@ -116,7 +141,7 @@ func createContextsAndVerifyPaths(t *testing.T, useTarball, useAbsolutePath, ren
}

func checkEmptyLintContext(t *testing.T, lintCtxs []LintContext) {
assert.Len(t, lintCtxs, 0, "expecting no lint context")
assert.Empty(t, lintCtxs, "expecting no lint context")
}

func verifyAndGetContext(t *testing.T, lintCtxs []LintContext) LintContext {
Expand Down
28 changes: 28 additions & 0 deletions pkg/pathutil/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package pathutil

import (
"path/filepath"

"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)

// GetAbsolutPath returns the absolute representation of given path.
func GetAbsolutPath(path string) (string, error) {
switch {
case path[0] == '~':
expandedPath, err := homedir.Expand(path)
if err != nil {
return "", errors.Wrapf(err, "could not expand path: %q", expandedPath)
}
return expandedPath, nil
case !filepath.IsAbs(path):
absPath, err := filepath.Abs(path)
if err != nil {
return "", errors.Wrapf(err, "could not expand non-absolute path: %q", absPath)
}
return absPath, nil
default:
return path, nil
}
}