forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes#4233 from ryanmcnamara/rm/expander-chain
Allow specification of multiple expanders
- Loading branch information
1 parent
ff7a96d
commit ab8bbe2
Showing
16 changed files
with
355 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package factory | ||
|
||
import ( | ||
"k8s.io/autoscaler/cluster-autoscaler/expander" | ||
|
||
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1" | ||
) | ||
|
||
type chainStrategy struct { | ||
filters []expander.Filter | ||
fallback expander.Strategy | ||
} | ||
|
||
func newChainStrategy(filters []expander.Filter, fallback expander.Strategy) expander.Strategy { | ||
return &chainStrategy{ | ||
filters: filters, | ||
fallback: fallback, | ||
} | ||
} | ||
|
||
func (c *chainStrategy) BestOption(options []expander.Option, nodeInfo map[string]*schedulerframework.NodeInfo) *expander.Option { | ||
filteredOptions := options | ||
for _, filter := range c.filters { | ||
filteredOptions = filter.BestOptions(filteredOptions, nodeInfo) | ||
if len(filteredOptions) == 1 { | ||
return &filteredOptions[0] | ||
} | ||
} | ||
return c.fallback.BestOption(filteredOptions, nodeInfo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package factory | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"k8s.io/autoscaler/cluster-autoscaler/expander" | ||
|
||
"github.com/stretchr/testify/assert" | ||
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1" | ||
) | ||
|
||
type substringTestFilterStrategy struct { | ||
substring string | ||
} | ||
|
||
func newSubstringTestFilterStrategy(substring string) *substringTestFilterStrategy { | ||
return &substringTestFilterStrategy{ | ||
substring: substring, | ||
} | ||
} | ||
|
||
func (s *substringTestFilterStrategy) BestOptions(expansionOptions []expander.Option, nodeInfo map[string]*schedulerframework.NodeInfo) []expander.Option { | ||
var ret []expander.Option | ||
for _, option := range expansionOptions { | ||
if strings.Contains(option.Debug, s.substring) { | ||
ret = append(ret, option) | ||
} | ||
} | ||
return ret | ||
|
||
} | ||
|
||
func (s *substringTestFilterStrategy) BestOption(expansionOptions []expander.Option, nodeInfo map[string]*schedulerframework.NodeInfo) *expander.Option { | ||
ret := s.BestOptions(expansionOptions, nodeInfo) | ||
if len(ret) == 0 { | ||
return nil | ||
} | ||
return &ret[0] | ||
} | ||
|
||
func TestChainStrategy_BestOption(t *testing.T) { | ||
for name, tc := range map[string]struct { | ||
filters []expander.Filter | ||
fallback expander.Strategy | ||
options []expander.Option | ||
expected *expander.Option | ||
}{ | ||
"selects with no filters": { | ||
filters: []expander.Filter{}, | ||
fallback: newSubstringTestFilterStrategy("a"), | ||
options: []expander.Option{ | ||
*newOption("b"), | ||
*newOption("a"), | ||
}, | ||
expected: newOption("a"), | ||
}, | ||
"filters with one filter": { | ||
filters: []expander.Filter{ | ||
newSubstringTestFilterStrategy("a"), | ||
}, | ||
fallback: newSubstringTestFilterStrategy("b"), | ||
options: []expander.Option{ | ||
*newOption("ab"), | ||
*newOption("b"), | ||
}, | ||
expected: newOption("ab"), | ||
}, | ||
"filters with multiple filters": { | ||
filters: []expander.Filter{ | ||
newSubstringTestFilterStrategy("a"), | ||
newSubstringTestFilterStrategy("b"), | ||
}, | ||
fallback: newSubstringTestFilterStrategy("x"), | ||
options: []expander.Option{ | ||
*newOption("xab"), | ||
*newOption("xa"), | ||
*newOption("x"), | ||
}, | ||
expected: newOption("xab"), | ||
}, | ||
"selects from multiple after filters": { | ||
filters: []expander.Filter{ | ||
newSubstringTestFilterStrategy("x"), | ||
}, | ||
fallback: newSubstringTestFilterStrategy("a"), | ||
options: []expander.Option{ | ||
*newOption("xc"), | ||
*newOption("xaa"), | ||
*newOption("xab"), | ||
}, | ||
expected: newOption("xaa"), | ||
}, | ||
"short circuits": { | ||
filters: []expander.Filter{ | ||
newSubstringTestFilterStrategy("a"), | ||
newSubstringTestFilterStrategy("b"), | ||
}, | ||
fallback: newSubstringTestFilterStrategy("x"), | ||
options: []expander.Option{ | ||
*newOption("a"), | ||
}, | ||
expected: newOption("a"), | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
subject := newChainStrategy(tc.filters, tc.fallback) | ||
actual := subject.BestOption(tc.options, nil) | ||
assert.Equal(t, tc.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func newOption(debug string) *expander.Option { | ||
return &expander.Option{ | ||
Debug: debug, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.