Skip to content

Commit

Permalink
The list of vm filters is made provider aware (#137)
Browse files Browse the repository at this point in the history
* vm filters list made provider aware

* ntw performance filter added in case of ec2 and gce only
  • Loading branch information
lpuskas authored Jul 27, 2018
1 parent 646366f commit 2628af9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
23 changes: 19 additions & 4 deletions pkg/recommender/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (e *Engine) RecommendCluster(provider string, region string, req ClusterRec
}
log.Debugf("recommended values for [%s]: count:[%d] , values: [%#v./te]", attr, len(values), values)

vmFilters, _ := e.filtersForAttr(attr)
vmFilters, _ := e.filtersForAttr(attr, provider)

filteredVms, err := e.RecommendVms(provider, region, attr, values, vmFilters, req)
if err != nil {
Expand Down Expand Up @@ -466,15 +466,30 @@ func (e *Engine) RecommendAttrValues(provider string, region string, attr string
}

// filtersForAttr returns the slice for
func (e *Engine) filtersForAttr(attr string) ([]vmFilter, error) {
func (e *Engine) filtersForAttr(attr string, provider string) ([]vmFilter, error) {
var
// generic filters - not depending on providers and attributes
filters []vmFilter = []vmFilter{e.includesFilter, e.excludesFilter}

// provider specific filters
switch provider {
case "ec2":
filters = append(filters, e.currentGenFilter, e.burstFilter, e.ntwPerformanceFilter)
case "gce":
filters = append(filters, e.ntwPerformanceFilter)
}

// attribute specific filters
switch attr {
case Cpu:
return []vmFilter{e.currentGenFilter, e.ntwPerformanceFilter, e.minMemRatioFilter, e.burstFilter, e.includesFilter, e.excludesFilter}, nil
filters = append(filters, e.minMemRatioFilter)
case Memory:
return []vmFilter{e.currentGenFilter, e.ntwPerformanceFilter, e.minCpuRatioFilter, e.burstFilter, e.includesFilter, e.excludesFilter}, nil
filters = append(filters, e.minCpuRatioFilter)
default:
return nil, fmt.Errorf("unsupported attribute: [%s]", attr)
}

return filters, nil
}

// sortByAttrValue returns the slice for
Expand Down
35 changes: 20 additions & 15 deletions pkg/recommender/engine_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ var (

func TestEngine_filtersApply(t *testing.T) {
tests := []struct {
name string
engine Engine
vm VirtualMachine
req ClusterRecommendationReq
attr string
check func(filtersApply bool)
name string
engine Engine
vm VirtualMachine
req ClusterRecommendationReq
attr string
provider string
check func(filtersApply bool)
}{
{
name: "filter applies for cpu/mem and burst allowed",
engine: Engine{},
// minRatio = SumCpu/SumMem = 0.5
req: ClusterRecommendationReq{SumCpu: 4, SumMem: float64(8), AllowBurst: &trueVal},
// ratio = Cpus/Mem = 1
vm: VirtualMachine{Cpus: 4, Mem: float64(4), Burst: true, CurrentGen: true},
attr: Memory,
vm: VirtualMachine{Cpus: 4, Mem: float64(4), Burst: true, CurrentGen: true},
attr: Memory,
provider: "ec2",
check: func(filtersApply bool) {
assert.Equal(t, true, filtersApply, "vm should pass all filters")
},
Expand All @@ -38,8 +40,9 @@ func TestEngine_filtersApply(t *testing.T) {
// minRatio = SumCpu/SumMem = 0.5
req: ClusterRecommendationReq{SumCpu: 4, SumMem: float64(8), AllowBurst: &falseVal},
// ratio = Cpus/Mem = 1
vm: VirtualMachine{Cpus: 4, Mem: float64(4), Burst: true, CurrentGen: true},
attr: Cpu,
vm: VirtualMachine{Cpus: 4, Mem: float64(4), Burst: true, CurrentGen: true},
attr: Cpu,
provider: "ec2",
check: func(filtersApply bool) {
assert.Equal(t, false, filtersApply, "vm should not pass all filters")
},
Expand All @@ -50,8 +53,9 @@ func TestEngine_filtersApply(t *testing.T) {
// minRatio = AumMem/SumCpu = 2
req: ClusterRecommendationReq{SumMem: float64(8), SumCpu: 4, AllowBurst: &trueVal},
// ratio = Mem/Cpus = 1
vm: VirtualMachine{Mem: float64(20), Cpus: 4, Burst: true, CurrentGen: true},
attr: Cpu,
vm: VirtualMachine{Mem: float64(20), Cpus: 4, Burst: true, CurrentGen: true},
attr: Cpu,
provider: "ec2",
check: func(filtersApply bool) {
assert.Equal(t, true, filtersApply, "vm should pass all filters")
},
Expand All @@ -62,16 +66,17 @@ func TestEngine_filtersApply(t *testing.T) {
// minRatio = AumMem/SumCpu = 2
req: ClusterRecommendationReq{SumMem: float64(8), SumCpu: 4, AllowBurst: &falseVal},
// ratio = Mem/Cpus = 1
vm: VirtualMachine{Mem: float64(20), Cpus: 4, Burst: true, CurrentGen: true},
attr: Memory,
vm: VirtualMachine{Mem: float64(20), Cpus: 4, Burst: true, CurrentGen: true},
attr: Memory,
provider: "ec2",
check: func(filtersApply bool) {
assert.Equal(t, false, filtersApply, "vm should not pass all filters")
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
filters, err := test.engine.filtersForAttr(test.attr)
filters, err := test.engine.filtersForAttr(test.attr, test.provider)
assert.Nil(t, err, "should get filters for attribute")
test.check(test.engine.filtersApply(test.vm, filters, test.req))
})
Expand Down

0 comments on commit 2628af9

Please sign in to comment.