Skip to content

Commit af04508

Browse files
committed
Move GetZoneKey function to component-helpers
1 parent e4e9c31 commit af04508

File tree

10 files changed

+174
-121
lines changed

10 files changed

+174
-121
lines changed

pkg/controller/nodelifecycle/node_lifecycle_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ import (
5151
"k8s.io/client-go/util/flowcontrol"
5252
"k8s.io/client-go/util/workqueue"
5353
"k8s.io/component-base/metrics/prometheus/ratelimiter"
54+
utilnode "k8s.io/component-helpers/node/topology"
5455
kubeletapis "k8s.io/kubelet/pkg/apis"
5556
"k8s.io/kubernetes/pkg/controller"
5657
"k8s.io/kubernetes/pkg/controller/nodelifecycle/scheduler"
5758
nodeutil "k8s.io/kubernetes/pkg/controller/util/node"
58-
utilnode "k8s.io/kubernetes/pkg/util/node"
5959
taintutils "k8s.io/kubernetes/pkg/util/taints"
6060
)
6161

pkg/controller/testutil/test_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ import (
4040
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
4141
"k8s.io/client-go/tools/cache"
4242
ref "k8s.io/client-go/tools/reference"
43+
utilnode "k8s.io/component-helpers/node/topology"
4344
"k8s.io/klog/v2"
4445
"k8s.io/kubernetes/pkg/api/legacyscheme"
4546
api "k8s.io/kubernetes/pkg/apis/core"
46-
utilnode "k8s.io/kubernetes/pkg/util/node"
4747

4848
jsonpatch "github.com/evanphx/json-patch"
4949
)

pkg/scheduler/framework/plugins/selectorspread/selector_spread.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import (
2525
"k8s.io/apimachinery/pkg/runtime"
2626
appslisters "k8s.io/client-go/listers/apps/v1"
2727
corelisters "k8s.io/client-go/listers/core/v1"
28+
utilnode "k8s.io/component-helpers/node/topology"
2829
"k8s.io/kubernetes/pkg/scheduler/framework"
2930
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/helper"
30-
utilnode "k8s.io/kubernetes/pkg/util/node"
3131
)
3232

3333
// SelectorSpread is a plugin that calculates selector spread priority.

pkg/scheduler/internal/cache/node_tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"fmt"
2222

2323
v1 "k8s.io/api/core/v1"
24+
utilnode "k8s.io/component-helpers/node/topology"
2425
"k8s.io/klog/v2"
25-
utilnode "k8s.io/kubernetes/pkg/util/node"
2626
)
2727

2828
// nodeTree is a tree-like data structure that holds node names in each zone. Zone names are

pkg/util/node/node.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -174,42 +174,6 @@ func GetNodeIP(client clientset.Interface, name string) net.IP {
174174
return nodeIP
175175
}
176176

177-
// GetZoneKey is a helper function that builds a string identifier that is unique per failure-zone;
178-
// it returns empty-string for no zone.
179-
// Since there are currently two separate zone keys:
180-
// * "failure-domain.beta.kubernetes.io/zone"
181-
// * "topology.kubernetes.io/zone"
182-
// GetZoneKey will first check failure-domain.beta.kubernetes.io/zone and if not exists, will then check
183-
// topology.kubernetes.io/zone
184-
func GetZoneKey(node *v1.Node) string {
185-
labels := node.Labels
186-
if labels == nil {
187-
return ""
188-
}
189-
190-
// TODO: "failure-domain.beta..." names are deprecated, but will
191-
// stick around a long time due to existing on old extant objects like PVs.
192-
// Maybe one day we can stop considering them (see #88493).
193-
zone, ok := labels[v1.LabelFailureDomainBetaZone]
194-
if !ok {
195-
zone, _ = labels[v1.LabelTopologyZone]
196-
}
197-
198-
region, ok := labels[v1.LabelFailureDomainBetaRegion]
199-
if !ok {
200-
region, _ = labels[v1.LabelTopologyRegion]
201-
}
202-
203-
if region == "" && zone == "" {
204-
return ""
205-
}
206-
207-
// We include the null character just in case region or failureDomain has a colon
208-
// (We do assume there's no null characters in a region or failureDomain)
209-
// As a nice side-benefit, the null character is not printed by fmt.Print or glog
210-
return region + ":\x00:" + zone
211-
}
212-
213177
type nodeForConditionPatch struct {
214178
Status nodeStatusForPatch `json:"status"`
215179
}

pkg/util/node/node_test.go

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -266,84 +266,3 @@ func TestGetHostname(t *testing.T) {
266266

267267
}
268268
}
269-
270-
func Test_GetZoneKey(t *testing.T) {
271-
tests := []struct {
272-
name string
273-
node *v1.Node
274-
zone string
275-
}{
276-
{
277-
name: "has no zone or region keys",
278-
node: &v1.Node{
279-
ObjectMeta: metav1.ObjectMeta{
280-
Labels: map[string]string{},
281-
},
282-
},
283-
zone: "",
284-
},
285-
{
286-
name: "has beta zone and region keys",
287-
node: &v1.Node{
288-
ObjectMeta: metav1.ObjectMeta{
289-
Labels: map[string]string{
290-
v1.LabelFailureDomainBetaZone: "zone1",
291-
v1.LabelFailureDomainBetaRegion: "region1",
292-
},
293-
},
294-
},
295-
zone: "region1:\x00:zone1",
296-
},
297-
{
298-
name: "has GA zone and region keys",
299-
node: &v1.Node{
300-
ObjectMeta: metav1.ObjectMeta{
301-
Labels: map[string]string{
302-
v1.LabelTopologyZone: "zone1",
303-
v1.LabelTopologyRegion: "region1",
304-
},
305-
},
306-
},
307-
zone: "region1:\x00:zone1",
308-
},
309-
{
310-
name: "has both beta and GA zone and region keys",
311-
node: &v1.Node{
312-
ObjectMeta: metav1.ObjectMeta{
313-
Labels: map[string]string{
314-
v1.LabelTopologyZone: "zone1",
315-
v1.LabelTopologyRegion: "region1",
316-
v1.LabelFailureDomainBetaZone: "zone1",
317-
v1.LabelFailureDomainBetaRegion: "region1",
318-
},
319-
},
320-
},
321-
zone: "region1:\x00:zone1",
322-
},
323-
{
324-
name: "has both beta and GA zone and region keys, beta labels take precedent",
325-
node: &v1.Node{
326-
ObjectMeta: metav1.ObjectMeta{
327-
Labels: map[string]string{
328-
v1.LabelTopologyZone: "zone1",
329-
v1.LabelTopologyRegion: "region1",
330-
v1.LabelFailureDomainBetaZone: "zone2",
331-
v1.LabelFailureDomainBetaRegion: "region2",
332-
},
333-
},
334-
},
335-
zone: "region2:\x00:zone2",
336-
},
337-
}
338-
339-
for _, test := range tests {
340-
t.Run(test.name, func(t *testing.T) {
341-
zone := GetZoneKey(test.node)
342-
if zone != test.zone {
343-
t.Logf("actual zone key: %q", zone)
344-
t.Logf("expected zone key: %q", test.zone)
345-
t.Errorf("unexpected zone key")
346-
}
347-
})
348-
}
349-
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# See the OWNERS docs at https://go.k8s.io/owners
2+
3+
approvers:
4+
- sig-node-approvers
5+
reviewers:
6+
- sig-node-reviewers
7+
labels:
8+
- sig/node
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package topology
18+
19+
import (
20+
"k8s.io/api/core/v1"
21+
)
22+
23+
// GetZoneKey is a helper function that builds a string identifier that is unique per failure-zone;
24+
// it returns empty-string for no zone.
25+
// Since there are currently two separate zone keys:
26+
// * "failure-domain.beta.kubernetes.io/zone"
27+
// * "topology.kubernetes.io/zone"
28+
// GetZoneKey will first check failure-domain.beta.kubernetes.io/zone and if not exists, will then check
29+
// topology.kubernetes.io/zone
30+
func GetZoneKey(node *v1.Node) string {
31+
labels := node.Labels
32+
if labels == nil {
33+
return ""
34+
}
35+
36+
// TODO: "failure-domain.beta..." names are deprecated, but will
37+
// stick around a long time due to existing on old extant objects like PVs.
38+
// Maybe one day we can stop considering them (see #88493).
39+
zone, ok := labels[v1.LabelFailureDomainBetaZone]
40+
if !ok {
41+
zone, _ = labels[v1.LabelTopologyZone]
42+
}
43+
44+
region, ok := labels[v1.LabelFailureDomainBetaRegion]
45+
if !ok {
46+
region, _ = labels[v1.LabelTopologyRegion]
47+
}
48+
49+
if region == "" && zone == "" {
50+
return ""
51+
}
52+
53+
// We include the null character just in case region or failureDomain has a colon
54+
// (We do assume there's no null characters in a region or failureDomain)
55+
// As a nice side-benefit, the null character is not printed by fmt.Print or glog
56+
return region + ":\x00:" + zone
57+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package topology
18+
19+
import (
20+
"k8s.io/api/core/v1"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"testing"
23+
)
24+
25+
func Test_GetZoneKey(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
node *v1.Node
29+
zone string
30+
}{
31+
{
32+
name: "has no zone or region keys",
33+
node: &v1.Node{
34+
ObjectMeta: metav1.ObjectMeta{
35+
Labels: map[string]string{},
36+
},
37+
},
38+
zone: "",
39+
},
40+
{
41+
name: "has beta zone and region keys",
42+
node: &v1.Node{
43+
ObjectMeta: metav1.ObjectMeta{
44+
Labels: map[string]string{
45+
v1.LabelFailureDomainBetaZone: "zone1",
46+
v1.LabelFailureDomainBetaRegion: "region1",
47+
},
48+
},
49+
},
50+
zone: "region1:\x00:zone1",
51+
},
52+
{
53+
name: "has GA zone and region keys",
54+
node: &v1.Node{
55+
ObjectMeta: metav1.ObjectMeta{
56+
Labels: map[string]string{
57+
v1.LabelTopologyZone: "zone1",
58+
v1.LabelTopologyRegion: "region1",
59+
},
60+
},
61+
},
62+
zone: "region1:\x00:zone1",
63+
},
64+
{
65+
name: "has both beta and GA zone and region keys",
66+
node: &v1.Node{
67+
ObjectMeta: metav1.ObjectMeta{
68+
Labels: map[string]string{
69+
v1.LabelTopologyZone: "zone1",
70+
v1.LabelTopologyRegion: "region1",
71+
v1.LabelFailureDomainBetaZone: "zone1",
72+
v1.LabelFailureDomainBetaRegion: "region1",
73+
},
74+
},
75+
},
76+
zone: "region1:\x00:zone1",
77+
},
78+
{
79+
name: "has both beta and GA zone and region keys, beta labels take precedent",
80+
node: &v1.Node{
81+
ObjectMeta: metav1.ObjectMeta{
82+
Labels: map[string]string{
83+
v1.LabelTopologyZone: "zone1",
84+
v1.LabelTopologyRegion: "region1",
85+
v1.LabelFailureDomainBetaZone: "zone2",
86+
v1.LabelFailureDomainBetaRegion: "region2",
87+
},
88+
},
89+
},
90+
zone: "region2:\x00:zone2",
91+
},
92+
}
93+
94+
for _, test := range tests {
95+
t.Run(test.name, func(t *testing.T) {
96+
zone := GetZoneKey(test.node)
97+
if zone != test.zone {
98+
t.Logf("actual zone key: %q", zone)
99+
t.Logf("expected zone key: %q", test.zone)
100+
t.Errorf("unexpected zone key")
101+
}
102+
})
103+
}
104+
}

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,7 @@ k8s.io/component-base/version/verflag
22402240
k8s.io/component-helpers/apimachinery/lease
22412241
k8s.io/component-helpers/auth/rbac/reconciliation
22422242
k8s.io/component-helpers/auth/rbac/validation
2243+
k8s.io/component-helpers/node/topology
22432244
k8s.io/component-helpers/scheduling/corev1
22442245
k8s.io/component-helpers/scheduling/corev1/nodeaffinity
22452246
k8s.io/component-helpers/storage/volume

0 commit comments

Comments
 (0)