Skip to content

Commit

Permalink
feat: add util function for processing namespaces/labels
Browse files Browse the repository at this point in the history
Signed-off-by: Bradley Jones <[email protected]>
  • Loading branch information
bradleyjones committed Jun 5, 2024
1 parent 9c58ceb commit cc86aaa
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/inventory/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package inventory

import "regexp"

func checkForRegexMatch(regex string, value string) bool {
return regexp.MustCompile(regex).MatchString(value)
}

func processAnnotationsOrLabels(annotationsOrLabels map[string]string, include []string) map[string]string {
if len(include) == 0 {
return annotationsOrLabels
}
toReturn := make(map[string]string)
for key, val := range annotationsOrLabels {
for _, includeKey := range include {
if checkForRegexMatch(includeKey, key) {
toReturn[key] = val
}
}
}
return toReturn
}
137 changes: 137 additions & 0 deletions pkg/inventory/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package inventory

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_processAnnotations(t *testing.T) {
type args struct {
annotationsOrLabels map[string]string
include []string
}
tests := []struct {
name string
args args
want map[string]string
}{
{
name: "Empty include",
args: args{
annotationsOrLabels: map[string]string{
"key1": "value1",
"key2": "value2",
},
include: []string{},
},
want: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
{
name: "Include explicit keys",
args: args{
annotationsOrLabels: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value2",
},
include: []string{"key1", "key3"},
},
want: map[string]string{
"key1": "value1",
"key3": "value2",
},
},
{
name: "Include non-existent keys",
args: args{
annotationsOrLabels: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value2",
},
include: []string{"key1", "key4"},
},
want: map[string]string{
"key1": "value1",
},
},
{
name: "Empty annotationsOrLabels",
args: args{
annotationsOrLabels: map[string]string{},
include: []string{"key1", "key4"},
},
want: map[string]string{},
},
{
name: "Include keys by regex pattern",
args: args{
annotationsOrLabels: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value2",
},
include: []string{"key[1-2]"},
},
want: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
{
name: "Include keys by regex pattern (all)",
args: args{
annotationsOrLabels: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value2",
},
include: []string{".*"},
},
want: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value2",
},
},
{
name: "Include keys by regex pattern (non-existent)",
args: args{
annotationsOrLabels: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value2",
},
include: []string{"key[4-5]"},
},
want: map[string]string{},
},
{
name: "Include keys by regex pattern and explicit",
args: args{
annotationsOrLabels: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value2",
"explicit": "value2",
},
include: []string{"key[1-2]", "explicit"},
},
want: map[string]string{
"key1": "value1",
"key2": "value2",
"explicit": "value2",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := processAnnotationsOrLabels(tt.args.annotationsOrLabels, tt.args.include)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit cc86aaa

Please sign in to comment.