Skip to content

Commit

Permalink
feat: exclude node by label (#168)
Browse files Browse the repository at this point in the history
* feat: exclude node by label

Signed-off-by: chenk <[email protected]>

* feat: exclude node by label

Signed-off-by: chenk <[email protected]>

---------

Signed-off-by: chenk <[email protected]>
  • Loading branch information
chen-keinan authored May 24, 2023
1 parent f725fa7 commit 250729b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/trivyk8s/trivyk8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ func isNodeStatusUnknown(resource unstructured.Unstructured) bool {
}

func ignoreNodeByLabel(resource *artifacts.Artifact, ignoreLabels map[string]string) bool {
if len(ignoreLabels) == 0 {
return false
}
var matchingLabels int
for key, val := range ignoreLabels {
if lVal, ok := resource.Labels[key]; ok && lVal == val {
Expand Down
43 changes: 43 additions & 0 deletions pkg/trivyk8s/trivyk8s_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package trivyk8s

import (
"testing"

"github.com/aquasecurity/trivy-kubernetes/pkg/artifacts"
"github.com/stretchr/testify/assert"
)

func TestIgnoreNodeByLabel(t *testing.T) {
tests := []struct {
name string
ignoredLabels map[string]string
artifact *artifacts.Artifact
want bool
}{
{
name: "no ignore labels",
ignoredLabels: map[string]string{},
artifact: &artifacts.Artifact{Labels: map[string]string{"a": "b"}},
want: false,
},
{
name: "matching ignore labels",
ignoredLabels: map[string]string{"a": "b"},
artifact: &artifacts.Artifact{Labels: map[string]string{"a": "b"}},
want: true,
},
{
name: "non matching ignore labels",
ignoredLabels: map[string]string{"a": "b", "c": "d"},
artifact: &artifacts.Artifact{Labels: map[string]string{"a": "b"}},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ignoreNodeByLabel(tt.artifact, tt.ignoredLabels)
assert.Equal(t, got, tt.want)
})
}
}

0 comments on commit 250729b

Please sign in to comment.