-
Notifications
You must be signed in to change notification settings - Fork 4k
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 #7137 from com6056/cluster-autoscaler-release-1.30
cherry-pick least-nodes expander from #6792 into 1.30
- Loading branch information
Showing
6 changed files
with
181 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2016 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 leastnodes | ||
|
||
import ( | ||
"math" | ||
|
||
"k8s.io/autoscaler/cluster-autoscaler/expander" | ||
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework" | ||
) | ||
|
||
type leastnodes struct { | ||
} | ||
|
||
// NewFilter returns a scale up filter that picks the node group that uses the least number of nodes | ||
func NewFilter() expander.Filter { | ||
return &leastnodes{} | ||
} | ||
|
||
// BestOptions selects the expansion option that uses the least number of nodes | ||
func (m *leastnodes) BestOptions(expansionOptions []expander.Option, nodeInfo map[string]*schedulerframework.NodeInfo) []expander.Option { | ||
leastNodes := math.MaxInt | ||
var leastOptions []expander.Option | ||
|
||
for _, option := range expansionOptions { | ||
// Don't think this is possible, but just in case | ||
if option.NodeCount == 0 { | ||
continue | ||
} | ||
|
||
if option.NodeCount == leastNodes { | ||
leastOptions = append(leastOptions, option) | ||
continue | ||
} | ||
|
||
if option.NodeCount < leastNodes { | ||
leastNodes = option.NodeCount | ||
leastOptions = []expander.Option{option} | ||
} | ||
} | ||
|
||
if len(leastOptions) == 0 { | ||
return nil | ||
} | ||
|
||
return leastOptions | ||
} |
113 changes: 113 additions & 0 deletions
113
cluster-autoscaler/expander/leastnodes/leastnodes_test.go
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,113 @@ | ||
/* | ||
Copyright 2016 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 leastnodes | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"k8s.io/autoscaler/cluster-autoscaler/expander" | ||
) | ||
|
||
func TestLeastNodes(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
expansionOptions []expander.Option | ||
expectedExpansionOptions []expander.Option | ||
}{ | ||
{ | ||
name: "no options", | ||
expansionOptions: nil, | ||
expectedExpansionOptions: nil, | ||
}, | ||
{ | ||
name: "no valid options", | ||
expansionOptions: []expander.Option{ | ||
{Debug: "EO0", NodeCount: 0}, | ||
}, | ||
expectedExpansionOptions: nil, | ||
}, | ||
{ | ||
name: "1 valid option", | ||
expansionOptions: []expander.Option{ | ||
{Debug: "EO0", NodeCount: 2}, | ||
}, | ||
expectedExpansionOptions: []expander.Option{ | ||
{Debug: "EO0", NodeCount: 2}, | ||
}, | ||
}, | ||
{ | ||
name: "2 valid options, not equal", | ||
expansionOptions: []expander.Option{ | ||
{Debug: "EO0", NodeCount: 2}, | ||
{Debug: "EO1", NodeCount: 1}, | ||
}, | ||
expectedExpansionOptions: []expander.Option{ | ||
{Debug: "EO1", NodeCount: 1}, | ||
}, | ||
}, | ||
{ | ||
name: "3 valid options, 2 equal", | ||
expansionOptions: []expander.Option{ | ||
{Debug: "EO0", NodeCount: 6}, | ||
{Debug: "EO1", NodeCount: 2}, | ||
{Debug: "EO2", NodeCount: 2}, | ||
}, | ||
expectedExpansionOptions: []expander.Option{ | ||
{Debug: "EO1", NodeCount: 2}, | ||
{Debug: "EO2", NodeCount: 2}, | ||
}, | ||
}, | ||
{ | ||
name: "3 valid options, all equal", | ||
expansionOptions: []expander.Option{ | ||
{Debug: "EO0", NodeCount: 8}, | ||
{Debug: "EO1", NodeCount: 8}, | ||
{Debug: "EO2", NodeCount: 8}, | ||
}, | ||
expectedExpansionOptions: []expander.Option{ | ||
{Debug: "EO0", NodeCount: 8}, | ||
{Debug: "EO1", NodeCount: 8}, | ||
{Debug: "EO2", NodeCount: 8}, | ||
}, | ||
}, | ||
{ | ||
name: "6 valid options, 1 invalid option, 3 equal", | ||
expansionOptions: []expander.Option{ | ||
{Debug: "EO0", NodeCount: 23}, | ||
{Debug: "EO1", NodeCount: 0}, | ||
{Debug: "EO2", NodeCount: 5}, | ||
{Debug: "EO3", NodeCount: 8}, | ||
{Debug: "EO4", NodeCount: 5}, | ||
{Debug: "EO5", NodeCount: 5}, | ||
{Debug: "EO6", NodeCount: 22}, | ||
}, | ||
expectedExpansionOptions: []expander.Option{ | ||
{Debug: "EO2", NodeCount: 5}, | ||
{Debug: "EO4", NodeCount: 5}, | ||
{Debug: "EO5", NodeCount: 5}, | ||
}, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
e := NewFilter() | ||
ret := e.BestOptions(tc.expansionOptions, nil) | ||
assert.Equal(t, ret, tc.expectedExpansionOptions) | ||
}) | ||
} | ||
} |
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