diff --git a/infra/conf/router.go b/infra/conf/router.go index 5184702da..9cc9d5281 100644 --- a/infra/conf/router.go +++ b/infra/conf/router.go @@ -17,9 +17,16 @@ type RouterRulesConfig struct { DomainStrategy string `json:"domainStrategy"` } +// StrategyConfig represents a strategy config +type StrategyConfig struct { + Type string `json:"type"` + Settings *json.RawMessage `json:"settings"` +} + type BalancingRule struct { - Tag string `json:"tag"` - Selectors StringList `json:"selector"` + Tag string `json:"tag"` + Selectors StringList `json:"selector"` + Strategy StrategyConfig `json:"strategy"` } func (r *BalancingRule) Build() (*router.BalancingRule, error) { @@ -30,9 +37,20 @@ func (r *BalancingRule) Build() (*router.BalancingRule, error) { return nil, newError("empty selector list") } + var strategy string + switch strings.ToLower(r.Strategy.Type) { + case strategyRandom, "": + strategy = strategyRandom + case strategyLeastPing: + strategy = "leastPing" + default: + return nil, newError("unknown balancing strategy: " + r.Strategy.Type) + } + return &router.BalancingRule{ Tag: r.Tag, OutboundSelector: []string(r.Selectors), + Strategy: strategy, }, nil } diff --git a/infra/conf/router_strategy.go b/infra/conf/router_strategy.go new file mode 100644 index 000000000..b8536330c --- /dev/null +++ b/infra/conf/router_strategy.go @@ -0,0 +1,6 @@ +package conf + +const ( + strategyRandom string = "random" + strategyLeastPing string = "leastping" +)