Skip to content

Commit

Permalink
Fix ignore-paths
Browse files Browse the repository at this point in the history
fixes: #500
  • Loading branch information
janisz committed Mar 15, 2023
1 parent d5d56b5 commit ee7ddbd
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 27 deletions.
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
}
}
1 change: 0 additions & 1 deletion pkg/lintcontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lintcontext

import (
"encoding/json"

"golang.stackrox.io/kube-linter/internal/stringutils"
"golang.stackrox.io/kube-linter/pkg/k8sutil"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
14 changes: 14 additions & 0 deletions pkg/lintcontext/create_contexts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lintcontext

import (
"golang.stackrox.io/kube-linter/pkg/pathutil"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -70,6 +71,19 @@ fileOrDirsLoop:
return nil
}

for _, path := range ignorePaths {
// Useing doublestar to enable **
// See https://github.com/golang/go/issues/11862
absPath, err := pathutil.GetAbsolutPath(currentPath)
if err != nil {
walkErr = errors.Wrapf(err, "could not get absolute path for %s", currentPath)
}
globMatch, _ := doublestar.PathMatch(path, absPath)
if strings.HasPrefix(absPath, path) || globMatch {
return nil
}
}

if !info.IsDir() {
if strings.HasSuffix(strings.ToLower(currentPath), ".tgz") {
ctx := newCtx(options)
Expand Down
25 changes: 24 additions & 1 deletion pkg/lintcontext/create_contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lintcontext

import (
"fmt"
"golang.stackrox.io/kube-linter/pkg/pathutil"
"os"
"path"
"path/filepath"
Expand All @@ -21,6 +22,28 @@ const (
mockPath = "mock path"
)

func TestCreateContextsWithIgnorePaths(t *testing.T) {
ignoredPaths := []string{
"../../.golangci.yml",
"../../tests/",
"../../e2etests/",
"../../pkg/",
"../../.pre-commit-hooks",
"../../.github/",
}
ignoredAbsPaths := []string{}
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 +139,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
27 changes: 27 additions & 0 deletions pkg/pathutil/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pathutil

import (
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"path/filepath"
)

// 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
}
}

0 comments on commit ee7ddbd

Please sign in to comment.