feat: searcher plugin change return params (#844)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2021-12-01 12:07:12 +08:00
parent d13042bfb5
commit cd0811a964
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
5 changed files with 70 additions and 35 deletions

View File

@ -18,6 +18,8 @@ package searcher
import ( import (
"context" "context"
"errors"
"fmt"
"strings" "strings"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -73,7 +75,7 @@ type Scopes struct {
} }
type Searcher interface { type Searcher interface {
FindSchedulerCluster(context.Context, []model.SchedulerCluster, *manager.ListSchedulersRequest) (model.SchedulerCluster, bool) FindSchedulerCluster(context.Context, []model.SchedulerCluster, *manager.ListSchedulersRequest) (model.SchedulerCluster, error)
} }
type searcher struct{} type searcher struct{}
@ -89,10 +91,14 @@ func New() Searcher {
return s return s
} }
func (s *searcher) FindSchedulerCluster(ctx context.Context, schedulerClusters []model.SchedulerCluster, client *manager.ListSchedulersRequest) (model.SchedulerCluster, bool) { func (s *searcher) FindSchedulerCluster(ctx context.Context, schedulerClusters []model.SchedulerCluster, client *manager.ListSchedulersRequest) (model.SchedulerCluster, error) {
conditions := client.HostInfo conditions := client.HostInfo
if len(schedulerClusters) <= 0 || len(conditions) <= 0 { if len(conditions) <= 0 {
return model.SchedulerCluster{}, false return model.SchedulerCluster{}, errors.New("empty conditions")
}
if len(schedulerClusters) <= 0 {
return model.SchedulerCluster{}, errors.New("empty scheduler clusters")
} }
// If there are security domain conditions, match clusters of the same security domain. // If there are security domain conditions, match clusters of the same security domain.
@ -121,10 +127,10 @@ func (s *searcher) FindSchedulerCluster(ctx context.Context, schedulerClusters [
switch len(clusters) { switch len(clusters) {
case 0: case 0:
// If the security domain does not match, there is no cluster available // If the security domain does not match, there is no cluster available
return model.SchedulerCluster{}, false return model.SchedulerCluster{}, fmt.Errorf("security domain %s does not match", securityDomain)
case 1: case 1:
// If only one cluster matches the security domain, return the cluster directly // If only one cluster matches the security domain, return the cluster directly
return clusters[0], true return clusters[0], nil
default: default:
// If there are multiple clusters matching the security domain, // If there are multiple clusters matching the security domain,
// select the schuelder cluster with a higher score // select the schuelder cluster with a higher score
@ -143,7 +149,7 @@ func (s *searcher) FindSchedulerCluster(ctx context.Context, schedulerClusters [
result = cluster result = cluster
} }
} }
return result, true return result, nil
} }
} }

View File

@ -31,24 +31,53 @@ func TestSchedulerCluster(t *testing.T) {
name string name string
schedulerClusters []model.SchedulerCluster schedulerClusters []model.SchedulerCluster
conditions map[string]string conditions map[string]string
expect func(t *testing.T, data model.SchedulerCluster, ok bool) expect func(t *testing.T, data model.SchedulerCluster, err error)
}{ }{
{ {
name: "conditions is empty", name: "conditions is empty",
schedulerClusters: []model.SchedulerCluster{{Name: "foo"}}, schedulerClusters: []model.SchedulerCluster{{Name: "foo"}},
conditions: map[string]string{}, conditions: map[string]string{},
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(ok, false) assert.EqualError(err, "empty conditions")
}, },
}, },
{ {
name: "scheduler clusters is empty", name: "scheduler clusters is empty",
schedulerClusters: []model.SchedulerCluster{}, schedulerClusters: []model.SchedulerCluster{},
conditions: map[string]string{"location": "foo"}, conditions: map[string]string{"location": "foo"},
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(ok, false) assert.EqualError(err, "empty scheduler clusters")
},
},
{
name: "security_domain does not match",
schedulerClusters: []model.SchedulerCluster{
{
Name: "foo",
SecurityGroup: model.SecurityGroup{
SecurityRules: []model.SecurityRule{
{
Domain: "domain-2",
},
},
},
Schedulers: []model.Scheduler{
{
HostName: "foo",
State: "active",
},
},
},
{
Name: "bar",
},
},
conditions: map[string]string{"security_domain": "domain-1"},
expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t)
assert.EqualError(err, "security domain domain-1 does not match")
}, },
}, },
{ {
@ -75,10 +104,10 @@ func TestSchedulerCluster(t *testing.T) {
}, },
}, },
conditions: map[string]string{"security_domain": "domain-1"}, conditions: map[string]string{"security_domain": "domain-1"},
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(data.Name, "foo") assert.Equal(data.Name, "foo")
assert.Equal(ok, true) assert.NoError(err)
}, },
}, },
{ {
@ -107,10 +136,10 @@ func TestSchedulerCluster(t *testing.T) {
}, },
}, },
conditions: map[string]string{"location": "location-1"}, conditions: map[string]string{"location": "location-1"},
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(data.Name, "foo") assert.Equal(data.Name, "foo")
assert.Equal(ok, true) assert.NoError(err)
}, },
}, },
{ {
@ -139,10 +168,10 @@ func TestSchedulerCluster(t *testing.T) {
}, },
}, },
conditions: map[string]string{"idc": "idc-1"}, conditions: map[string]string{"idc": "idc-1"},
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(data.Name, "foo") assert.Equal(data.Name, "foo")
assert.Equal(ok, true) assert.NoError(err)
}, },
}, },
{ {
@ -171,10 +200,10 @@ func TestSchedulerCluster(t *testing.T) {
}, },
}, },
conditions: map[string]string{"net_topology": "net-topology-1"}, conditions: map[string]string{"net_topology": "net-topology-1"},
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(data.Name, "foo") assert.Equal(data.Name, "foo")
assert.Equal(ok, true) assert.NoError(err)
}, },
}, },
{ {
@ -207,10 +236,10 @@ func TestSchedulerCluster(t *testing.T) {
"location": "location-1", "location": "location-1",
"idc": "idc-1", "idc": "idc-1",
}, },
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(data.Name, "foo") assert.Equal(data.Name, "foo")
assert.Equal(ok, true) assert.NoError(err)
}, },
}, },
{ {
@ -249,10 +278,10 @@ func TestSchedulerCluster(t *testing.T) {
"security_domain": "domain-1", "security_domain": "domain-1",
"location": "location-1", "location": "location-1",
}, },
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(data.Name, "foo") assert.Equal(data.Name, "foo")
assert.Equal(ok, true) assert.NoError(err)
}, },
}, },
{ {
@ -291,10 +320,10 @@ func TestSchedulerCluster(t *testing.T) {
"security_domain": "domain-1", "security_domain": "domain-1",
"idc": "idc-1", "idc": "idc-1",
}, },
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(data.Name, "foo") assert.Equal(data.Name, "foo")
assert.Equal(ok, true) assert.NoError(err)
}, },
}, },
{ {
@ -335,10 +364,10 @@ func TestSchedulerCluster(t *testing.T) {
"idc": "idc-1", "idc": "idc-1",
"location": "location-1", "location": "location-1",
}, },
expect: func(t *testing.T, data model.SchedulerCluster, ok bool) { expect: func(t *testing.T, data model.SchedulerCluster, err error) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(data.Name, "foo") assert.Equal(data.Name, "foo")
assert.Equal(ok, true) assert.NoError(err)
}, },
}, },
} }

View File

@ -41,8 +41,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }
cluster, ok := s.FindSchedulerCluster(context.Background(), []model.SchedulerCluster{}, &manager.ListSchedulersRequest{}) cluster, err := s.FindSchedulerCluster(context.Background(), []model.SchedulerCluster{}, &manager.ListSchedulersRequest{})
if !ok { if err != nil {
fmt.Println("scheduler cluster not found") fmt.Println("scheduler cluster not found")
os.Exit(1) os.Exit(1)
} }

View File

@ -25,8 +25,8 @@ import (
type searcher struct{} type searcher struct{}
func (s *searcher) FindSchedulerCluster(ctx context.Context, schedulerClusters []model.SchedulerCluster, client *manager.ListSchedulersRequest) (model.SchedulerCluster, bool) { func (s *searcher) FindSchedulerCluster(ctx context.Context, schedulerClusters []model.SchedulerCluster, client *manager.ListSchedulersRequest) (model.SchedulerCluster, error) {
return model.SchedulerCluster{Name: "foo"}, true return model.SchedulerCluster{Name: "foo"}, nil
} }
func DragonflyPluginInit(option map[string]string) (interface{}, map[string]string, error) { func DragonflyPluginInit(option map[string]string) (interface{}, map[string]string, error) {

View File

@ -385,9 +385,9 @@ func (s *GRPC) ListSchedulers(ctx context.Context, req *manager.ListSchedulersRe
// Search optimal scheduler cluster // Search optimal scheduler cluster
log.Infof("list scheduler clusters %+v with hostInfo %+v", schedulerClusters, req.HostInfo) log.Infof("list scheduler clusters %+v with hostInfo %+v", schedulerClusters, req.HostInfo)
schedulerCluster, ok := s.searcher.FindSchedulerCluster(ctx, schedulerClusters, req) schedulerCluster, err := s.searcher.FindSchedulerCluster(ctx, schedulerClusters, req)
if !ok { if err != nil {
log.Errorf("can not matching scheduler cluster") log.Errorf("can not matching scheduler cluster %v", err)
return nil, status.Error(codes.NotFound, "scheduler cluster not found") return nil, status.Error(codes.NotFound, "scheduler cluster not found")
} }
log.Infof("find matching scheduler cluster %v", schedulerCluster) log.Infof("find matching scheduler cluster %v", schedulerCluster)