Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add least-nodes expander to cluster-autoscaler #6792

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cluster-autoscaler/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,8 @@ smaller nodes at once.
* `least-waste` - selects the node group that will have the least idle CPU (if tied, unused memory)
after scale-up. This is useful when you have different classes of nodes, for example, high CPU or high memory nodes, and only want to expand those when there are pending pods that need a lot of those resources.

* `least-nodes` - selects the node group that will use the least number of nodes after scale-up. This is useful when you want to minimize the number of nodes in the cluster and instead opt for fewer larger nodes. Useful when chained with the `most-pods` expander before it to ensure that the node group selected can fit the most pods on the fewest nodes.

* `price` - select the node group that will cost the least and, at the same time, whose machines
would match the cluster size. This expander is described in more details
[HERE](https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/proposals/pricing.md). Currently it works only for GCE, GKE and Equinix Metal (patches welcome.)
Expand Down
2 changes: 2 additions & 0 deletions cluster-autoscaler/expander/expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
MostPodsExpanderName = "most-pods"
// LeastWasteExpanderName selects a node group that leaves the least fraction of CPU and Memory
LeastWasteExpanderName = "least-waste"
// LeastNodesExpanderName selects a node group that uses the least number of nodes
LeastNodesExpanderName = "least-nodes"
// PriceBasedExpanderName selects a node group that is the most cost-effective and consistent with
// the preferred node size for the cluster
PriceBasedExpanderName = "price"
Expand Down
2 changes: 2 additions & 0 deletions cluster-autoscaler/expander/factory/expander_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"k8s.io/autoscaler/cluster-autoscaler/context"
"k8s.io/autoscaler/cluster-autoscaler/expander"
"k8s.io/autoscaler/cluster-autoscaler/expander/grpcplugin"
"k8s.io/autoscaler/cluster-autoscaler/expander/leastnodes"
"k8s.io/autoscaler/cluster-autoscaler/expander/mostpods"
"k8s.io/autoscaler/cluster-autoscaler/expander/price"
"k8s.io/autoscaler/cluster-autoscaler/expander/priority"
Expand Down Expand Up @@ -82,6 +83,7 @@ func (f *Factory) RegisterDefaultExpanders(cloudProvider cloudprovider.CloudProv
f.RegisterFilter(expander.RandomExpanderName, random.NewFilter)
f.RegisterFilter(expander.MostPodsExpanderName, mostpods.NewFilter)
f.RegisterFilter(expander.LeastWasteExpanderName, waste.NewFilter)
f.RegisterFilter(expander.LeastNodesExpanderName, leastnodes.NewFilter)
f.RegisterFilter(expander.PriceBasedExpanderName, func() expander.Filter {
if _, err := cloudProvider.Pricing(); err != nil {
klog.Fatalf("Couldn't access cloud provider pricing for %s expander: %v", expander.PriceBasedExpanderName, err)
Expand Down
61 changes: 61 additions & 0 deletions cluster-autoscaler/expander/leastnodes/leastnodes.go
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 cluster-autoscaler/expander/leastnodes/leastnodes_test.go
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)
})
}
}
1 change: 1 addition & 0 deletions cluster-autoscaler/expander/mostpods/mostpods.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (m *mostpods) BestOptions(expansionOptions []expander.Option, nodeInfo map[
for _, option := range expansionOptions {
if len(option.Pods) == maxPods {
maxOptions = append(maxOptions, option)
continue
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny little perf improvement

}

if len(option.Pods) > maxPods {
Expand Down
Loading