forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
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#4174 from jsravn/allow-balancing-by-lab…
…els-exclusively Allow balancing by labels exclusively
- Loading branch information
Showing
6 changed files
with
168 additions
and
11 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
49 changes: 49 additions & 0 deletions
49
cluster-autoscaler/processors/nodegroupset/label_nodegroups.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,49 @@ | ||
/* | ||
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 nodegroupset | ||
|
||
import ( | ||
klog "k8s.io/klog/v2" | ||
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework" | ||
) | ||
|
||
// CreateLabelNodeInfoComparator returns a comparator that checks for node group similarity using the given labels. | ||
func CreateLabelNodeInfoComparator(labels []string) NodeInfoComparator { | ||
return func(n1, n2 *schedulerframework.NodeInfo) bool { | ||
return areLabelsSame(n1, n2, labels) | ||
} | ||
} | ||
|
||
func areLabelsSame(n1, n2 *schedulerframework.NodeInfo, labels []string) bool { | ||
for _, label := range labels { | ||
val1, exists := n1.Node().ObjectMeta.Labels[label] | ||
if !exists { | ||
klog.V(8).Infof("%s label not present on %s", label, n1.Node().Name) | ||
return false | ||
} | ||
val2, exists := n2.Node().ObjectMeta.Labels[label] | ||
if !exists { | ||
klog.V(8).Infof("%s label not present on %s", label, n1.Node().Name) | ||
return false | ||
} | ||
if val1 != val2 { | ||
klog.V(8).Infof("%s label did not match. %s: %s, %s: %s", label, n1.Node().Name, val1, n2.Node().Name, val2) | ||
return false | ||
} | ||
} | ||
return true | ||
} |
92 changes: 92 additions & 0 deletions
92
cluster-autoscaler/processors/nodegroupset/label_nodegroups_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,92 @@ | ||
/* | ||
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 nodegroupset | ||
|
||
import ( | ||
"testing" | ||
|
||
. "k8s.io/autoscaler/cluster-autoscaler/utils/test" | ||
) | ||
|
||
func TestNodeLabelComparison(t *testing.T) { | ||
labels := []string{"node.kubernetes.io/instance-type", "kubernetes.io/arch"} | ||
comparator := CreateLabelNodeInfoComparator(labels) | ||
node1 := BuildTestNode("node1", 1000, 2000) | ||
node2 := BuildTestNode("node2", 1000, 2000) | ||
|
||
for _, tc := range []struct { | ||
description string | ||
labels1 map[string]string | ||
labels2 map[string]string | ||
isSimilar bool | ||
}{ | ||
{ | ||
description: "both labels match", | ||
labels1: map[string]string{"node.kubernetes.io/instance-type": "m5.4xlarge", "kubernetes.io/arch": "amd64"}, | ||
labels2: map[string]string{"node.kubernetes.io/instance-type": "m5.4xlarge", "kubernetes.io/arch": "amd64"}, | ||
isSimilar: true, | ||
}, | ||
{ | ||
description: "one label doesn't match", | ||
labels1: map[string]string{"node.kubernetes.io/instance-type": "m5.4xlarge", "kubernetes.io/arch": "amd64"}, | ||
labels2: map[string]string{"node.kubernetes.io/instance-type": "m5.4xlarge", "kubernetes.io/arch": "i386"}, | ||
isSimilar: false, | ||
}, | ||
{ | ||
description: "unspecified labels are not considered", | ||
labels1: map[string]string{"node.kubernetes.io/instance-type": "m5.4xlarge", "kubernetes.io/arch": "amd64", "unspecified-label": "eu-west1"}, | ||
labels2: map[string]string{"node.kubernetes.io/instance-type": "m5.4xlarge", "kubernetes.io/arch": "amd64", "unspecified-label": "eu-west2"}, | ||
isSimilar: true, | ||
}, | ||
{ | ||
description: "no labels are set", | ||
labels1: map[string]string{}, | ||
labels2: map[string]string{}, | ||
isSimilar: false, | ||
}, | ||
{ | ||
description: "single label matches, label is unset on second group", | ||
labels1: map[string]string{"node.kubernetes.io/instance-type": "m5.4xlarge", "kubernetes.io/arch": "amd64"}, | ||
labels2: map[string]string{"kubernetes.io/arch": "amd64"}, | ||
isSimilar: false, | ||
}, | ||
{ | ||
description: "single label matches, label is unset on first group", | ||
labels1: map[string]string{"kubernetes.io/arch": "amd64"}, | ||
labels2: map[string]string{"node.kubernetes.io/instance-type": "m5.4xlarge", "kubernetes.io/arch": "amd64"}, | ||
isSimilar: false, | ||
}, | ||
{ | ||
description: "labels are explicitly set to be empty", | ||
labels1: map[string]string{"node.kubernetes.io/instance-type": "", "kubernetes.io/arch": ""}, | ||
labels2: map[string]string{"node.kubernetes.io/instance-type": "", "kubernetes.io/arch": ""}, | ||
isSimilar: true, | ||
}, | ||
{ | ||
description: "one labels is explicitly set to be empty", | ||
labels1: map[string]string{"node.kubernetes.io/instance-type": "", "kubernetes.io/arch": "amd64"}, | ||
labels2: map[string]string{"node.kubernetes.io/instance-type": "", "kubernetes.io/arch": "amd64"}, | ||
isSimilar: true, | ||
}, | ||
} { | ||
t.Run(tc.description, func(t *testing.T) { | ||
node1.ObjectMeta.Labels = tc.labels1 | ||
node2.ObjectMeta.Labels = tc.labels2 | ||
checkNodesSimilar(t, node1, node2, comparator, tc.isSimilar) | ||
}) | ||
} | ||
} |