-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ignore directives (#13)
* Allow ignoring specific checks for certain objects by adding specific annotations.
- Loading branch information
1 parent
4eb4607
commit 372bdaf
Showing
6 changed files
with
134 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ignore | ||
|
||
import ( | ||
"golang.stackrox.io/kube-linter/internal/stringutils" | ||
) | ||
|
||
const ( | ||
// AnnotationKeyPrefix is the prefix for annotations for kube-linter check ignores. | ||
AnnotationKeyPrefix = "kube-linter.io/ignore/" | ||
|
||
// AllAnnotationKey is used to ignore all checks for a given object. | ||
AllAnnotationKey = "kube-linter.io/ignore-all" | ||
) | ||
|
||
// ObjectForCheck returns whether to ignore the given object for the passed check name. | ||
func ObjectForCheck(annotations map[string]string, checkName string) bool { | ||
for k := range annotations { | ||
if k == AllAnnotationKey { | ||
return true | ||
} | ||
key := k | ||
if stringutils.ConsumePrefix(&key, AnnotationKeyPrefix) && key == checkName { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package ignore | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestObjectForCheck(t *testing.T) { | ||
for _, testCase := range []struct { | ||
annotations map[string]string | ||
checkName string | ||
shouldIgnore bool | ||
}{ | ||
{ | ||
annotations: nil, | ||
checkName: "some-check", | ||
}, | ||
{ | ||
annotations: map[string]string{}, | ||
checkName: "some-check", | ||
}, | ||
{ | ||
annotations: map[string]string{ | ||
"random-unrelated": "blah", | ||
"kube-linter.io/ignore/some-check": "Not applicable", | ||
}, | ||
checkName: "some-check", | ||
shouldIgnore: true, | ||
}, | ||
{ | ||
annotations: map[string]string{ | ||
"random-unrelated": "blah", | ||
"kube-linter.io/ignore/some-check": "Not applicable", | ||
}, | ||
checkName: "some-check-2", | ||
}, | ||
{ | ||
annotations: map[string]string{ | ||
"random-unrelated": "blah", | ||
"kube-linter.io/ignore/some-check": "Not applicable", | ||
}, | ||
checkName: "other-check", | ||
}, | ||
{ | ||
annotations: map[string]string{ | ||
"random-unrelated": "blah", | ||
"kube-linter.io/ignore/some-check": "Not applicable", | ||
"kube-linter.io/ignore/other-check": "Not applicable", | ||
}, | ||
checkName: "other-check", | ||
shouldIgnore: true, | ||
}, | ||
{ | ||
annotations: map[string]string{ | ||
"random-unrelated": "blah", | ||
"kube-linter.io/ignore-all": "Too much of a mess", | ||
}, | ||
checkName: "other-check", | ||
shouldIgnore: true, | ||
}, | ||
{ | ||
annotations: map[string]string{ | ||
"random-unrelated": "blah", | ||
"kube-linter.io/ignore-all": "Too much of a mess", | ||
}, | ||
checkName: "some-other-check", | ||
shouldIgnore: true, | ||
}, | ||
} { | ||
c := testCase | ||
t.Run(fmt.Sprintf("%+v", c), func(t *testing.T) { | ||
assert.Equal(t, c.shouldIgnore, ObjectForCheck(c.annotations, c.checkName)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters