Skip to content

Commit f07cbfd

Browse files
committed
chore(node-util): move GetNode to utill
we move GetNode from main.go to util package so it can be used by other packages.
1 parent de1c457 commit f07cbfd

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

cmd/local-volume-provisioner/main.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"context"
2120
"flag"
2221
"log"
2322
"math/rand"
@@ -33,15 +32,12 @@ import (
3332
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/deleter"
3433
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/metrics"
3534
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/metrics/collectors"
35+
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/util"
3636
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/watcher"
3737

38-
v1 "k8s.io/api/core/v1"
3938
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
40-
"k8s.io/client-go/kubernetes"
4139
)
4240

43-
const maxGetNodesRetries = 3
44-
4541
var (
4642
optListenAddress string
4743
optMetricsPath string
@@ -86,7 +82,7 @@ func main() {
8682
}
8783

8884
client := common.SetupClient()
89-
node := getNode(client, nodeName)
85+
node := util.GetNode(client.CoreV1(), nodeName)
9086

9187
configUpdate := make(chan common.ProvisionerConfiguration)
9288
defer close(configUpdate)
@@ -115,22 +111,3 @@ func main() {
115111
http.Handle(optMetricsPath, promhttp.Handler())
116112
log.Fatal(http.ListenAndServe(optListenAddress, nil))
117113
}
118-
119-
func getNode(client *kubernetes.Clientset, name string) *v1.Node {
120-
var retries int
121-
122-
for {
123-
node, err := client.CoreV1().Nodes().Get(context.TODO(), name, metav1.GetOptions{})
124-
if err == nil {
125-
return node
126-
}
127-
128-
retries++
129-
klog.Infof("Could not get node information (remaining retries: %d): %v", maxGetNodesRetries-retries, err)
130-
131-
if retries >= maxGetNodesRetries {
132-
klog.Fatalf("Could not get node information: %v", err)
133-
}
134-
time.Sleep(time.Second)
135-
}
136-
}

pkg/util/node_util.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2025 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 util
18+
19+
import (
20+
"context"
21+
"time"
22+
23+
v1 "k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
26+
"k8s.io/klog/v2"
27+
)
28+
29+
const maxGetNodesRetries = 3
30+
31+
// GetNode returns the node with the given name.
32+
func GetNode(client corev1.CoreV1Interface, name string) *v1.Node {
33+
var retries int
34+
35+
for {
36+
node, err := client.Nodes().Get(context.TODO(), name, metav1.GetOptions{})
37+
if err == nil {
38+
return node
39+
}
40+
41+
retries++
42+
klog.Infof("Could not get node information (remaining retries: %d): %v", maxGetNodesRetries-retries, err)
43+
44+
if retries >= maxGetNodesRetries {
45+
klog.Fatalf("Could not get node information: %v", err)
46+
}
47+
time.Sleep(time.Second)
48+
}
49+
}

0 commit comments

Comments
 (0)