2023-06-30 11:13:18 +01:00
|
|
|
// Copyright (C) 2023 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
2023-09-14 17:42:59 +01:00
|
|
|
"bytes"
|
2023-07-06 13:35:26 +01:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jtolio/mito"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2023-06-30 11:13:18 +01:00
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/storj/location"
|
2023-07-07 09:31:58 +01:00
|
|
|
"storj.io/storj/satellite/nodeselection"
|
2023-06-30 11:13:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// PlacementRules can crate filter based on the placement identifier.
|
2023-08-01 12:50:22 +01:00
|
|
|
type PlacementRules func(constraint storj.PlacementConstraint) (filter nodeselection.NodeFilter)
|
2023-06-30 11:13:18 +01:00
|
|
|
|
|
|
|
// ConfigurablePlacementRule can include the placement definitions for each known identifier.
|
|
|
|
type ConfigurablePlacementRule struct {
|
2023-08-01 12:50:22 +01:00
|
|
|
placements map[storj.PlacementConstraint]nodeselection.NodeFilter
|
2023-06-30 11:13:18 +01:00
|
|
|
}
|
|
|
|
|
2023-07-06 13:35:26 +01:00
|
|
|
// String implements pflag.Value.
|
|
|
|
func (d *ConfigurablePlacementRule) String() string {
|
|
|
|
parts := []string{}
|
|
|
|
for id, filter := range d.placements {
|
|
|
|
// we can hide the internal rules...
|
|
|
|
if id > 9 {
|
|
|
|
// TODO: we need proper String implementation for all the used filters
|
|
|
|
parts = append(parts, fmt.Sprintf("%d:%s", id, filter))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(parts, ";")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set implements pflag.Value.
|
|
|
|
func (d *ConfigurablePlacementRule) Set(s string) error {
|
|
|
|
if d.placements == nil {
|
2023-08-14 15:27:53 +01:00
|
|
|
d.placements = map[storj.PlacementConstraint]nodeselection.NodeFilter{
|
|
|
|
storj.EveryCountry: nodeselection.AnyFilter{},
|
|
|
|
}
|
2023-07-06 13:35:26 +01:00
|
|
|
}
|
|
|
|
d.AddLegacyStaticRules()
|
|
|
|
return d.AddPlacementFromString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type implements pflag.Value.
|
|
|
|
func (d *ConfigurablePlacementRule) Type() string {
|
|
|
|
return "placement-rule"
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ pflag.Value = &ConfigurablePlacementRule{}
|
|
|
|
|
2023-06-30 11:13:18 +01:00
|
|
|
// NewPlacementRules creates a fully initialized NewPlacementRules.
|
|
|
|
func NewPlacementRules() *ConfigurablePlacementRule {
|
|
|
|
return &ConfigurablePlacementRule{
|
2023-08-14 15:27:53 +01:00
|
|
|
placements: map[storj.PlacementConstraint]nodeselection.NodeFilter{
|
|
|
|
storj.EveryCountry: nodeselection.AnyFilter{}},
|
2023-06-30 11:13:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLegacyStaticRules initializes all the placement rules defined earlier in static golang code.
|
|
|
|
func (d *ConfigurablePlacementRule) AddLegacyStaticRules() {
|
2023-08-16 09:36:05 +01:00
|
|
|
d.placements[storj.EEA] = nodeselection.NodeFilters{nodeselection.NewCountryFilter(location.NewSet(nodeselection.EeaCountriesWithoutEu...).With(nodeselection.EuCountries...))}
|
|
|
|
d.placements[storj.EU] = nodeselection.NodeFilters{nodeselection.NewCountryFilter(location.NewSet(nodeselection.EuCountries...))}
|
2023-07-10 16:48:33 +01:00
|
|
|
d.placements[storj.US] = nodeselection.NodeFilters{nodeselection.NewCountryFilter(location.NewSet(location.UnitedStates))}
|
|
|
|
d.placements[storj.DE] = nodeselection.NodeFilters{nodeselection.NewCountryFilter(location.NewSet(location.Germany))}
|
2023-07-31 08:42:45 +01:00
|
|
|
d.placements[storj.NR] = nodeselection.NodeFilters{nodeselection.NewCountryFilter(location.NewFullSet().Without(location.Russia, location.Belarus, location.None))}
|
2023-06-30 11:13:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddPlacementRule registers a new placement.
|
2023-08-01 12:50:22 +01:00
|
|
|
func (d *ConfigurablePlacementRule) AddPlacementRule(id storj.PlacementConstraint, filter nodeselection.NodeFilter) {
|
|
|
|
d.placements[id] = filter
|
2023-06-30 11:13:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-14 17:42:59 +01:00
|
|
|
type stringNotMatch string
|
|
|
|
|
2023-07-06 13:35:26 +01:00
|
|
|
// AddPlacementFromString parses placement definition form string representations from id:definition;id:definition;...
|
|
|
|
func (d *ConfigurablePlacementRule) AddPlacementFromString(definitions string) error {
|
|
|
|
env := map[any]any{
|
|
|
|
"country": func(countries ...string) (nodeselection.NodeFilters, error) {
|
2023-07-10 16:48:33 +01:00
|
|
|
var set location.Set
|
|
|
|
for _, country := range countries {
|
2023-08-16 09:36:05 +01:00
|
|
|
apply := func(modified location.Set, code ...location.CountryCode) location.Set {
|
|
|
|
return modified.With(code...)
|
|
|
|
}
|
|
|
|
if country[0] == '!' {
|
|
|
|
apply = func(modified location.Set, code ...location.CountryCode) location.Set {
|
|
|
|
return modified.Without(code...)
|
|
|
|
}
|
|
|
|
country = country[1:]
|
|
|
|
}
|
|
|
|
switch strings.ToLower(country) {
|
|
|
|
case "all", "*", "any":
|
|
|
|
set = location.NewFullSet()
|
|
|
|
case "none":
|
|
|
|
set = apply(set, location.None)
|
|
|
|
case "eu":
|
|
|
|
set = apply(set, nodeselection.EuCountries...)
|
|
|
|
case "eea":
|
|
|
|
set = apply(set, nodeselection.EuCountries...)
|
|
|
|
set = apply(set, nodeselection.EeaCountriesWithoutEu...)
|
|
|
|
default:
|
|
|
|
code := location.ToCountryCode(country)
|
|
|
|
if code == location.None {
|
|
|
|
return nil, errs.New("invalid country code %q", code)
|
|
|
|
}
|
|
|
|
set = apply(set, code)
|
2023-07-10 16:01:48 +01:00
|
|
|
}
|
2023-07-06 13:35:26 +01:00
|
|
|
}
|
2023-07-10 16:48:33 +01:00
|
|
|
return nodeselection.NodeFilters{nodeselection.NewCountryFilter(set)}, nil
|
2023-07-06 13:35:26 +01:00
|
|
|
},
|
2023-08-16 09:36:05 +01:00
|
|
|
"placement": func(ix int64) nodeselection.NodeFilter {
|
|
|
|
return d.placements[storj.PlacementConstraint(ix)]
|
|
|
|
},
|
2023-07-06 13:35:26 +01:00
|
|
|
"all": func(filters ...nodeselection.NodeFilters) (nodeselection.NodeFilters, error) {
|
|
|
|
res := nodeselection.NodeFilters{}
|
|
|
|
for _, filter := range filters {
|
|
|
|
res = append(res, filter...)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
},
|
2023-08-16 09:36:05 +01:00
|
|
|
mito.OpAnd: func(env map[any]any, a, b any) (any, error) {
|
|
|
|
filter1, ok1 := a.(nodeselection.NodeFilter)
|
|
|
|
filter2, ok2 := b.(nodeselection.NodeFilter)
|
|
|
|
if !ok1 || !ok2 {
|
|
|
|
return nil, errs.New("&& is supported only between NodeFilter instances")
|
|
|
|
}
|
|
|
|
res := nodeselection.NodeFilters{filter1, filter2}
|
|
|
|
return res, nil
|
|
|
|
},
|
2023-07-06 13:35:26 +01:00
|
|
|
"tag": func(nodeIDstr string, key string, value any) (nodeselection.NodeFilters, error) {
|
|
|
|
nodeID, err := storj.NodeIDFromString(nodeIDstr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-14 17:42:59 +01:00
|
|
|
|
2023-07-06 13:35:26 +01:00
|
|
|
var rawValue []byte
|
2023-09-14 17:42:59 +01:00
|
|
|
match := bytes.Equal
|
2023-07-06 13:35:26 +01:00
|
|
|
switch v := value.(type) {
|
|
|
|
case string:
|
|
|
|
rawValue = []byte(v)
|
|
|
|
case []byte:
|
|
|
|
rawValue = v
|
2023-09-14 17:42:59 +01:00
|
|
|
case stringNotMatch:
|
|
|
|
match = func(a, b []byte) bool {
|
|
|
|
return !bytes.Equal(a, b)
|
|
|
|
}
|
|
|
|
rawValue = []byte(v)
|
2023-07-06 13:35:26 +01:00
|
|
|
default:
|
|
|
|
return nil, errs.New("3rd argument of tag() should be string or []byte")
|
|
|
|
}
|
|
|
|
res := nodeselection.NodeFilters{
|
2023-09-14 17:42:59 +01:00
|
|
|
nodeselection.NewTagFilter(nodeID, key, rawValue, match),
|
2023-07-06 13:35:26 +01:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
},
|
2023-08-23 14:47:49 +01:00
|
|
|
"annotated": func(filter nodeselection.NodeFilter, kv ...nodeselection.Annotation) (nodeselection.AnnotatedNodeFilter, error) {
|
2023-08-01 12:50:22 +01:00
|
|
|
return nodeselection.AnnotatedNodeFilter{
|
|
|
|
Filter: filter,
|
|
|
|
Annotations: kv,
|
|
|
|
}, nil
|
|
|
|
},
|
2023-08-23 14:47:49 +01:00
|
|
|
"annotation": func(key string, value string) (nodeselection.Annotation, error) {
|
|
|
|
return nodeselection.Annotation{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
2023-08-01 12:50:22 +01:00
|
|
|
}, nil
|
|
|
|
},
|
2023-08-14 12:30:27 +01:00
|
|
|
"exclude": func(filter nodeselection.NodeFilter) (nodeselection.NodeFilter, error) {
|
|
|
|
return nodeselection.NewExcludeFilter(filter), nil
|
|
|
|
},
|
2023-09-14 17:42:59 +01:00
|
|
|
"empty": func() string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
"notEmpty": func() any {
|
|
|
|
return stringNotMatch("")
|
|
|
|
},
|
2023-07-06 13:35:26 +01:00
|
|
|
}
|
|
|
|
for _, definition := range strings.Split(definitions, ";") {
|
|
|
|
definition = strings.TrimSpace(definition)
|
|
|
|
if definition == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
idDef := strings.SplitN(definition, ":", 2)
|
|
|
|
|
|
|
|
val, err := mito.Eval(idDef[1], env)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
id, err := strconv.Atoi(idDef[0])
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
2023-08-01 12:50:22 +01:00
|
|
|
d.placements[storj.PlacementConstraint(id)] = val.(nodeselection.NodeFilter)
|
2023-07-06 13:35:26 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:13:18 +01:00
|
|
|
// CreateFilters implements PlacementCondition.
|
2023-08-01 12:50:22 +01:00
|
|
|
func (d *ConfigurablePlacementRule) CreateFilters(constraint storj.PlacementConstraint) (filter nodeselection.NodeFilter) {
|
2023-06-30 11:13:18 +01:00
|
|
|
if filters, found := d.placements[constraint]; found {
|
2023-08-01 12:50:22 +01:00
|
|
|
return filters
|
2023-07-10 16:01:48 +01:00
|
|
|
}
|
|
|
|
return nodeselection.NodeFilters{
|
|
|
|
nodeselection.ExcludeAllFilter{},
|
2023-06-30 11:13:18 +01:00
|
|
|
}
|
|
|
|
}
|