satellite/overlay: implement an exclude filter for placement configuration

https://github.com/storj/storj/issues/6126

Change-Id: I05215b5d46bec958001cc020edf1fa97b00d3299
This commit is contained in:
Márton Elek 2023-08-14 13:30:27 +02:00 committed by Storj Robot
parent 0e17b1018c
commit c08792f066
3 changed files with 34 additions and 0 deletions

View File

@ -196,3 +196,22 @@ func (t TagFilter) MatchInclude(node *SelectedNode) bool {
}
var _ NodeFilter = TagFilter{}
// ExcludeFilter excludes only the matched nodes.
type ExcludeFilter struct {
matchToExclude NodeFilter
}
// MatchInclude implements NodeFilter interface.
func (e ExcludeFilter) MatchInclude(node *SelectedNode) bool {
return !e.matchToExclude.MatchInclude(node)
}
// NewExcludeFilter creates filter, nodes matching the given filter will be excluded.
func NewExcludeFilter(filter NodeFilter) ExcludeFilter {
return ExcludeFilter{
matchToExclude: filter,
}
}
var _ NodeFilter = ExcludeFilter{}

View File

@ -126,6 +126,9 @@ func (d *ConfigurablePlacementRule) AddPlacementFromString(definitions string) e
key: value,
}, nil
},
"exclude": func(filter nodeselection.NodeFilter) (nodeselection.NodeFilter, error) {
return nodeselection.NewExcludeFilter(filter), nil
},
}
for _, definition := range strings.Split(definitions, ";") {
definition = strings.TrimSpace(definition)

View File

@ -124,6 +124,18 @@ func TestPlacementFromString(t *testing.T) {
require.Equal(t, nodeselection.GetAnnotation(filters, "autoExcludeSubnet"), "off")
})
t.Run("exclude", func(t *testing.T) {
p := NewPlacementRules()
err := p.AddPlacementFromString(`11:exclude(country("GB"))`)
require.NoError(t, err)
filters := p.placements[storj.PlacementConstraint(11)]
require.False(t, filters.MatchInclude(&nodeselection.SelectedNode{
CountryCode: location.UnitedKingdom,
}))
require.True(t, filters.MatchInclude(&nodeselection.SelectedNode{
CountryCode: location.Germany,
}))
})
t.Run("legacy geofencing rules", func(t *testing.T) {
p := NewPlacementRules()