Skip to content

Conversation

@majiayu000
Copy link

Fixes #1141

Changes

  • Add glob pattern support (*, ?, [...]) for the --namespace flag
  • Add helper functions hasWildcard() and filterNamespaces() in runner/test.go
  • Add comprehensive unit tests for namespace filtering logic
  • Add acceptance tests for wildcard namespace matching

Add support for glob-style wildcards (*, ?, [...]) in the --namespace
flag, allowing users to match multiple namespaces with patterns like
"main.*" or "*.gke".

Fixes open-policy-agent#1141

Signed-off-by: majiayu000 <[email protected]>
Rename "Test command with namespace wildcard pattern" to
"Test command with namespace exact match" since it tests
exact namespace matching without wildcards.

Signed-off-by: majiayu000 <[email protected]>
Copy link
Member

@jalseth jalseth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking on this PR. Can you also add support to this for the verify command?

// filterNamespaces filters the available namespaces using the given patterns.
// Patterns support glob-style matching with *, ?, and [...] syntax.
func filterNamespaces(available []string, patterns []string) []string {
seen := make(map[string]bool)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary. engine.Namespaces() returns the unique list already.

}
matched, err := path.Match(pattern, ns)
if err != nil {
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should raise an error rather than silently continuing.

}
}

func TestFilterNamespaces(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add t.Parallel().

}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add t.Parallel().

}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add t.Parallel().

"testing"
)

func TestHasWildcard(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add t.Parallel().

return files, nil
}

// hasWildcard checks if any of the given patterns contain wildcard characters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a note that this is based on what path.Match() supports.

}

// filterNamespaces filters the available namespaces using the given patterns.
// Patterns support glob-style matching with *, ?, and [...] syntax.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a note that this is based on what path.Match() supports.

// Patterns support glob-style matching with *, ?, and [...] syntax.
func filterNamespaces(available []string, patterns []string) []string {
seen := make(map[string]bool)
var result []string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Call this namespaces rather than a generic variable name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request: Support wildcards in --namespace

2 participants