feat: cluster scopes add hostname regexes for client matchs cluster by hostname (#3083)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2024-02-21 14:18:12 +08:00 committed by GitHub
parent 9020a23357
commit eefd59ca49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 103 additions and 30 deletions

View File

@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"net"
"regexp"
"sort"
"strings"
@ -46,13 +47,16 @@ const (
const (
// cidrAffinityWeight is CIDR affinity weight.
cidrAffinityWeight float64 = 0.4
cidrAffinityWeight float64 = 0.3
// hostnameAffinityWeight is hostname affinity weight.
hostnameAffinityWeight = 0.3
// idcAffinityWeight is IDC affinity weight.
idcAffinityWeight float64 = 0.35
idcAffinityWeight float64 = 0.25
// locationAffinityWeight is location affinity weight.
locationAffinityWeight = 0.24
locationAffinityWeight = 0.14
// clusterTypeWeight is cluster type weight.
clusterTypeWeight float64 = 0.01
@ -76,6 +80,7 @@ type Scopes struct {
IDC string `mapstructure:"idc"`
Location string `mapstructure:"location"`
CIDRs []string `mapstructure:"cidrs"`
Hostnames []string `mapstructure:"hostnames"`
}
type Searcher interface {
@ -150,6 +155,7 @@ func FilterSchedulerClusters(conditions map[string]string, schedulerClusters []m
// Evaluate the degree of matching between scheduler cluster and dfdaemon.
func Evaluate(ip, hostname string, conditions map[string]string, scopes Scopes, cluster models.SchedulerCluster, log *zap.SugaredLogger) float64 {
return cidrAffinityWeight*calculateCIDRAffinityScore(ip, scopes.CIDRs, log) +
hostnameAffinityWeight*calculateHostnameAffinityScore(hostname, scopes.Hostnames, log) +
idcAffinityWeight*calculateIDCAffinityScore(conditions[ConditionIDC], scopes.IDC) +
locationAffinityWeight*calculateMultiElementAffinityScore(conditions[ConditionLocation], scopes.Location) +
clusterTypeWeight*calculateClusterTypeScore(cluster)
@ -186,6 +192,31 @@ func calculateCIDRAffinityScore(ip string, cidrs []string, log *zap.SugaredLogge
return maxScore
}
// calculateHostnameAffinityScore 0.0~1.0 larger and better.
func calculateHostnameAffinityScore(hostname string, hostnames []string, log *zap.SugaredLogger) float64 {
if hostname == "" {
return minScore
}
if len(hostnames) == 0 {
return minScore
}
for _, v := range hostnames {
regex, err := regexp.Compile(v)
if err != nil {
log.Error(err)
continue
}
if regex.MatchString(hostname) {
return maxScore
}
}
return minScore
}
// calculateIDCAffinityScore 0.0~1.0 larger and better.
func calculateIDCAffinityScore(dst, src string) float64 {
if dst == "" || src == "" {

View File

@ -176,6 +176,40 @@ func TestSearcher_FindSchedulerClusters(t *testing.T) {
assert.Equal(len(data), 2)
},
},
{
name: "match according to hostname condition",
schedulerClusters: []models.SchedulerCluster{
{
Name: "foo",
Scopes: map[string]any{
"hostnames": []string{"f.*"},
},
Schedulers: []models.Scheduler{
{
Hostname: "foo",
State: "active",
},
},
},
{
Name: "bar",
Scopes: map[string]any{},
Schedulers: []models.Scheduler{
{
Hostname: "bar",
State: "active",
},
},
},
},
conditions: map[string]string{},
expect: func(t *testing.T, data []models.SchedulerCluster, err error) {
assert := assert.New(t)
assert.Equal(data[0].Name, "foo")
assert.Equal(data[1].Name, "bar")
assert.Equal(len(data), 2)
},
},
{
name: "match according to idc and location conditions",
schedulerClusters: []models.SchedulerCluster{
@ -269,6 +303,7 @@ func TestSearcher_FindSchedulerClusters(t *testing.T) {
"idc": "IDC-1",
"location": "LOCATION-2",
"cidrs": []string{"128.168.1.0/24"},
"hostnames": []string{"b.*"},
},
Schedulers: []models.Scheduler{
{
@ -283,6 +318,7 @@ func TestSearcher_FindSchedulerClusters(t *testing.T) {
"idc": "IDC-1",
"location": "LOCATION-1",
"cidrs": []string{"128.168.1.0/24"},
"hostnames": []string{"c.*"},
},
Schedulers: []models.Scheduler{
{
@ -297,6 +333,7 @@ func TestSearcher_FindSchedulerClusters(t *testing.T) {
"idc": "IDC-1",
"location": "LOCATION-1|LOCATION-2",
"cidrs": []string{"128.168.1.0/24"},
"hostnames": []string{"f.*"},
},
Schedulers: []models.Scheduler{
{
@ -311,6 +348,7 @@ func TestSearcher_FindSchedulerClusters(t *testing.T) {
"idc": "IDC-1",
"location": "LOCATION-2",
"cidrs": []string{"128.168.1.0/24"},
"hostnames": []string{"d.*"},
},
Schedulers: []models.Scheduler{
{
@ -326,6 +364,7 @@ func TestSearcher_FindSchedulerClusters(t *testing.T) {
"idc": "IDC-1",
"location": "LOCATION-2",
"cidrs": []string{"128.168.1.0/24"},
"hostnames": []string{"e.*"},
},
Schedulers: []models.Scheduler{
{
@ -340,6 +379,7 @@ func TestSearcher_FindSchedulerClusters(t *testing.T) {
"idc": "IDC-1",
"location": "LOCATION-2",
"cidrs": []string{"128.168.1.0/24"},
"hostnames": []string{"a.*"},
},
Schedulers: []models.Scheduler{
{
@ -355,6 +395,7 @@ func TestSearcher_FindSchedulerClusters(t *testing.T) {
"idc": "IDC-1",
"location": "LOCATION-2",
"cidrs": []string{"192.168.1.0/24"},
"hostnames": []string{"g.*"},
},
Schedulers: []models.Scheduler{
{

View File

@ -64,4 +64,5 @@ type SchedulerClusterScopes struct {
IDC string `yaml:"idc" mapstructure:"idc" json:"idc" binding:"omitempty"`
Location string `yaml:"location" mapstructure:"location" json:"location" binding:"omitempty"`
CIDRs []string `yaml:"cidrs" mapstructure:"cidrs" json:"cidrs" binding:"omitempty"`
Hostnames []string `yaml:"hostnames" mapstructure:"hostnames" json:"hostnames" binding:"omitempty"`
}