Skip to content

feat: add glob pattern support for --namespace flag#1247

Open
majiayu000 wants to merge 3 commits intoopen-policy-agent:masterfrom
majiayu000:fix-1141-request-support-wildcards-in---0102-0254
Open

feat: add glob pattern support for --namespace flag#1247
majiayu000 wants to merge 3 commits intoopen-policy-agent:masterfrom
majiayu000:fix-1141-request-support-wildcards-in---0102-0254

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 <1835304752@qq.com>
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 <1835304752@qq.com>
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?

runner/test.go Outdated
// 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.

runner/test.go Outdated
}
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.

runner/test.go Outdated
}

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

@jalseth
Copy link
Member

jalseth commented Feb 7, 2026

@majiayu000 Is this still being worked on?

@majiayu000
Copy link
Author

Thanks for the review @jalseth! I've updated the PR to include support for the --namespace flag (and wildcards) in the verify command as requested. I also fixed a bug in the filtering logic where the data. prefix was causing issues, and added acceptance tests to verify the behavior.

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