-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package nodeselector | ||
|
||
import ( | ||
"github.com/hashicorp/nomad-autoscaler/sdk" | ||
"github.com/hashicorp/nomad/api" | ||
) | ||
|
||
// oldestCreateIndexClusterScaleInNodeSelector is the NodeSelector | ||
// implementation of the ClusterScaleInNodeSelector interface. It selects nodes | ||
// based off the list ordering from Nomad and is the default selector. | ||
type oldestCreateIndexClusterScaleInNodeSelector struct{} | ||
|
||
// newoldestCreateIndexClusterScaleInNodeSelector returns a new | ||
// oldestCreateIndexClusterScaleInNodeSelector implementation of the | ||
// ClusterScaleInNodeSelector interface. | ||
func newOldestCreateIndexClusterScaleInNodeSelector() ClusterScaleInNodeSelector { | ||
return &oldestCreateIndexClusterScaleInNodeSelector{} | ||
} | ||
|
||
// Name satisfies the Name function on the ClusterScaleInNodeSelector | ||
// interface. | ||
func (n *oldestCreateIndexClusterScaleInNodeSelector) Name() string { | ||
return sdk.TargetNodeSelectorStrategyOldestCreateIndex | ||
} | ||
|
||
// Select satisfies the Select function on the ClusterScaleInNodeSelector | ||
// interface. | ||
func (n *oldestCreateIndexClusterScaleInNodeSelector) Select(nodes []*api.NodeListStub, num int) []*api.NodeListStub { | ||
|
||
last_index := len(nodes) - 1 | ||
if len(nodes) < num { | ||
num = len(nodes) | ||
} | ||
out := make([]*api.NodeListStub, num) | ||
|
||
for i := num - 1; i >= 0; i-- { | ||
out[i] = nodes[(last_index)-i] | ||
} | ||
return out | ||
} |
56 changes: 56 additions & 0 deletions
56
sdk/helper/scaleutils/nodeselector/oldest_create_index_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,56 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package nodeselector | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/api" | ||
"github.com/shoenig/test/must" | ||
) | ||
|
||
func Test_oldestClusterScaleInNodeSelectorName(t *testing.T) { | ||
must.Eq(t, "oldest_create_index", newOldestCreateIndexClusterScaleInNodeSelector().Name()) | ||
} | ||
|
||
func Test_oldestClusterScaleInNodeSelector_Select(t *testing.T) { | ||
testCases := []struct { | ||
inputNodes []*api.NodeListStub | ||
inputNum int | ||
expectedOutput []*api.NodeListStub | ||
name string | ||
}{ | ||
{ | ||
inputNodes: []*api.NodeListStub{ | ||
{ID: "8d01d8fe-3c6b-0fdd-ebbc-a8b5cbb9c00c"}, | ||
{ID: "a6604683-fd65-c913-77ee-a0a15b8e88e9"}, | ||
}, | ||
inputNum: 1, | ||
expectedOutput: []*api.NodeListStub{ | ||
{ID: "a6604683-fd65-c913-77ee-a0a15b8e88e9"}, | ||
}, | ||
name: "single selection needed", | ||
}, | ||
|
||
{ | ||
inputNodes: []*api.NodeListStub{ | ||
{ID: "8d01d8fe-3c6b-0fdd-ebbc-a8b5cbb9c00c"}, | ||
{ID: "a6604683-fd65-c913-77ee-a0a15b8e88e9"}, | ||
}, | ||
inputNum: 3, | ||
expectedOutput: []*api.NodeListStub{ | ||
{ID: "a6604683-fd65-c913-77ee-a0a15b8e88e9"}, | ||
{ID: "8d01d8fe-3c6b-0fdd-ebbc-a8b5cbb9c00c"}, | ||
}, | ||
name: "multiple selection", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actualOutput := newOldestCreateIndexClusterScaleInNodeSelector().Select(tc.inputNodes, tc.inputNum) | ||
must.Eq(t, tc.expectedOutput, actualOutput) | ||
}) | ||
} | ||
} |
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