-
Notifications
You must be signed in to change notification settings - Fork 178
feat: general include exclude models field in sources file #1885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
google-oss-prow
merged 6 commits into
kubeflow:main
from
Al-Pragliola:al-pragliola-unify-inclusion-exclusion
Nov 21, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b6966d7
feat: general include exclude models field in sources file
Al-Pragliola cd6a614
chore: refactor source filter validation
Al-Pragliola cb3c16e
chore: refactor validation function
Al-Pragliola e6be598
feat: apply suggestions from code review
Al-Pragliola 3958a5b
chore: add case insensitive test
Al-Pragliola c049854
feat: add case-insensitive description
Al-Pragliola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,180 @@ | ||
| package catalog | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ModelFilter encapsulates include/exclude pattern matching for model names. | ||
| type ModelFilter struct { | ||
| included []*compiledPattern | ||
| excluded []*compiledPattern | ||
| } | ||
|
|
||
| type compiledPattern struct { | ||
| raw string | ||
| re *regexp.Regexp | ||
| } | ||
|
|
||
| func newCompiledPattern(field string, idx int, raw string) (*compiledPattern, error) { | ||
| value := strings.TrimSpace(raw) | ||
| if value == "" { | ||
| return nil, fmt.Errorf("%s[%d]: pattern cannot be empty", field, idx) | ||
| } | ||
|
|
||
| // Convert a simple glob (only supporting '*') into a regexp. | ||
| var b strings.Builder | ||
| b.WriteString("(?i)^") | ||
| for _, r := range value { | ||
| if r == '*' { | ||
| b.WriteString(".*") | ||
| continue | ||
| } | ||
| b.WriteString(regexp.QuoteMeta(string(r))) | ||
| } | ||
| b.WriteString("$") | ||
|
|
||
| re, err := regexp.Compile(b.String()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("%s[%d]: invalid pattern %q: %w", field, idx, value, err) | ||
| } | ||
|
|
||
| return &compiledPattern{ | ||
| raw: value, | ||
| re: re, | ||
| }, nil | ||
| } | ||
|
|
||
| func compilePatterns(field string, patterns []string) ([]*compiledPattern, error) { | ||
| if len(patterns) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| compiled := make([]*compiledPattern, 0, len(patterns)) | ||
| for i, pattern := range patterns { | ||
| cp, err := newCompiledPattern(field, i, pattern) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| compiled = append(compiled, cp) | ||
| } | ||
| return compiled, nil | ||
| } | ||
|
|
||
| // ValidateSourceFilters validates that the includedModels and excludedModels patterns | ||
| // are valid (non-empty, compilable, non-conflicting). This is useful for early validation | ||
| // at configuration load time without constructing the full ModelFilter. | ||
| func ValidateSourceFilters(included, excluded []string) error { | ||
| if err := detectConflictingPatterns(included, excluded); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if _, err := compilePatterns("includedModels", included); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if _, err := compilePatterns("excludedModels", excluded); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // NewModelFilter builds a ModelFilter from the provided include/exclude pattern lists. | ||
| func NewModelFilter(included, excluded []string) (*ModelFilter, error) { | ||
| if err := ValidateSourceFilters(included, excluded); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| inc, err := compilePatterns("includedModels", included) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| exc, err := compilePatterns("excludedModels", excluded) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if len(inc) == 0 && len(exc) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| return &ModelFilter{ | ||
| included: inc, | ||
| excluded: exc, | ||
| }, nil | ||
| } | ||
|
|
||
| func detectConflictingPatterns(included, excluded []string) error { | ||
| if len(included) == 0 || len(excluded) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| includedIdx := make(map[string]int, len(included)) | ||
| for i, pattern := range included { | ||
| value := strings.TrimSpace(pattern) | ||
| includedIdx[value] = i | ||
| } | ||
|
|
||
| for j, pattern := range excluded { | ||
| value := strings.TrimSpace(pattern) | ||
| if i, exists := includedIdx[value]; exists { | ||
| return fmt.Errorf("pattern %q is defined in both includedModels[%d] and excludedModels[%d]", value, i, j) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Allows returns true if the provided model name passes the include/exclude rules. | ||
| func (f *ModelFilter) Allows(name string) bool { | ||
| if f == nil { | ||
| return true | ||
| } | ||
|
|
||
| if len(f.included) > 0 { | ||
| matched := false | ||
| for _, pattern := range f.included { | ||
| if pattern.re.MatchString(name) { | ||
| matched = true | ||
| break | ||
| } | ||
| } | ||
| if !matched { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| for _, pattern := range f.excluded { | ||
| if pattern.re.MatchString(name) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| // NewModelFilterFromSource composes a ModelFilter using the source-level configuration and any legacy additions. | ||
| func NewModelFilterFromSource(source *Source, extraIncluded, extraExcluded []string) (*ModelFilter, error) { | ||
| if source == nil { | ||
| return nil, fmt.Errorf("source cannot be nil when building filters") | ||
| } | ||
|
|
||
| included := append([]string{}, source.IncludedModels...) | ||
| if len(extraIncluded) > 0 { | ||
| included = append(included, extraIncluded...) | ||
| } | ||
|
|
||
| excluded := append([]string{}, source.ExcludedModels...) | ||
| if len(extraExcluded) > 0 { | ||
| excluded = append(excluded, extraExcluded...) | ||
| } | ||
|
|
||
| filter, err := NewModelFilter(included, excluded) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid include/exclude configuration for source %s: %w", source.Id, err) | ||
| } | ||
|
|
||
| return filter, nil | ||
| } |
This file contains hidden or 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,109 @@ | ||
| package catalog | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| apimodels "github.com/kubeflow/model-registry/catalog/pkg/openapi" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestModelFilterAllows(t *testing.T) { | ||
| filter, err := NewModelFilter([]string{"Granite/*"}, []string{"Granite/beta-*"}) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.True(t, filter.Allows("Granite/3-1-instruct")) | ||
| assert.False(t, filter.Allows("Granite/beta-release")) | ||
| assert.False(t, filter.Allows("Other/model")) | ||
|
|
||
| // Test case-insensitive matching | ||
| assert.True(t, filter.Allows("granite/3-1-instruct")) | ||
| assert.True(t, filter.Allows("GRANITE/3-1-instruct")) | ||
| assert.False(t, filter.Allows("granite/beta-release")) | ||
|
|
||
| allowAll, err := NewModelFilter([]string{"*"}, nil) | ||
| require.NoError(t, err) | ||
| assert.True(t, allowAll.Allows("anything/goes")) | ||
| } | ||
|
|
||
| func TestModelFilterConflictsAndValidation(t *testing.T) { | ||
| _, err := NewModelFilter([]string{"Granite/*"}, []string{"Granite/*"}) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "pattern \"Granite/*\"") | ||
|
|
||
| _, err = NewModelFilter([]string{""}, nil) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "pattern cannot be empty") | ||
| } | ||
|
|
||
| func TestNewModelFilterFromSourceMergesLegacy(t *testing.T) { | ||
| source := &Source{ | ||
| CatalogSource: apimodels.CatalogSource{ | ||
| Id: "test", | ||
| Name: "Test source", | ||
| Labels: []string{}, | ||
| IncludedModels: []string{"Granite/*"}, | ||
| }, | ||
| } | ||
|
|
||
| filter, err := NewModelFilterFromSource(source, nil, []string{"Legacy/*"}) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.True(t, filter.Allows("Granite/model")) | ||
| assert.False(t, filter.Allows("Legacy/model")) | ||
| } | ||
|
|
||
| func TestModelFilterWithWildcardInMiddle(t *testing.T) { | ||
| // Test that wildcards match across the entire name | ||
| filter, err := NewModelFilter(nil, []string{"*deprecated*", "*old*"}) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.True(t, filter.Allows("Granite/empty-stable")) | ||
| assert.False(t, filter.Allows("Mistral/empty-deprecated")) | ||
| assert.False(t, filter.Allows("DeepSeek/empty-old-v1")) | ||
| assert.False(t, filter.Allows("Foo/old")) | ||
| assert.False(t, filter.Allows("Bar/deprecated")) | ||
|
|
||
| // Test that */pattern* requires the pattern immediately after / | ||
| filter2, err := NewModelFilter(nil, []string{"*/deprecated", "*/old*"}) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.True(t, filter2.Allows("Mistral/empty-deprecated")) // doesn't match */deprecated (no immediate match after /) | ||
| assert.False(t, filter2.Allows("Foo/deprecated")) // matches */deprecated | ||
| assert.False(t, filter2.Allows("Bar/old-model")) // matches */old* | ||
| } | ||
|
|
||
| func TestValidateSourceFilters(t *testing.T) { | ||
| t.Run("no filters", func(t *testing.T) { | ||
| err := ValidateSourceFilters(nil, nil) | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| t.Run("valid patterns", func(t *testing.T) { | ||
| err := ValidateSourceFilters([]string{"Granite/*", "Meta/*"}, []string{"*-beta"}) | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| t.Run("conflicting patterns", func(t *testing.T) { | ||
| err := ValidateSourceFilters([]string{"Granite/*"}, []string{"Granite/*"}) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "Granite/*") | ||
| }) | ||
|
|
||
| t.Run("empty pattern in includedModels", func(t *testing.T) { | ||
| err := ValidateSourceFilters([]string{"Granite/*", ""}, nil) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "pattern cannot be empty") | ||
| }) | ||
|
|
||
| t.Run("whitespace-only pattern", func(t *testing.T) { | ||
| err := ValidateSourceFilters([]string{" "}, nil) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "pattern cannot be empty") | ||
| }) | ||
|
|
||
| t.Run("valid glob patterns", func(t *testing.T) { | ||
| err := ValidateSourceFilters([]string{"valid/*"}, nil) | ||
| assert.NoError(t, err) // Our conversion always produces valid regex | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To confirm, we want this to be case-insensitive? Maybe worth mentioning that here. I would generally assume a glob is case-sensitive like it would be in a shell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah making it case insensitive makes totally sense in my opinion in our intended use case, users probably don't even know the exact naming of the models that are needed to be excluded/included or what a glob is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. We should just make sure to document that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes thank you @jonburdo , please check the latest commit c049854