Skip to content

Commit b2f715e

Browse files
committed
Apply modernize analysis improvements
Signed-off-by: Feruzjon Muyassarov <[email protected]>
1 parent 20d280c commit b2f715e

File tree

14 files changed

+27
-33
lines changed

14 files changed

+27
-33
lines changed

pkg/apis/nfd/nodefeaturerule/expression.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func evaluateMatchExpression(m *nfdv1alpha1.MatchExpression, valid bool, value i
141141
return false, fmt.Errorf("not a number %q", value)
142142
}
143143
lr := make([]int, 2)
144-
for i := 0; i < 2; i++ {
144+
for i := range 2 {
145145
lr[i], err = strconv.Atoi(m.Value[i])
146146
if err != nil {
147147
return false, fmt.Errorf("not a number %q in %v", m.Value[i], m)

pkg/apis/nfd/template/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (h *Helper) ExpandMap(data interface{}) (map[string]string, error) {
5757

5858
// Split out individual key-value pairs
5959
out := make(map[string]string)
60-
for _, item := range strings.Split(expanded, "\n") {
60+
for item := range strings.SplitSeq(expanded, "\n") {
6161
// Remove leading/trailing whitespace and skip empty lines
6262
if trimmed := strings.TrimSpace(item); trimmed != "" {
6363
split := strings.SplitN(trimmed, "=", 2)

pkg/kubectl-nfd/dryrun.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package kubectlnfd
1818

1919
import (
2020
"fmt"
21+
"maps"
2122
"os"
2223
"strings"
2324

@@ -106,9 +107,7 @@ func processNodeFeatureRule(nodeFeatureRule nfdv1alpha1.NodeFeatureRule, nodeFea
106107
extendedResources[k] = v
107108
}
108109
// annotations
109-
for k, v := range ruleOut.Annotations {
110-
annotations[k] = v
111-
}
110+
maps.Copy(annotations, ruleOut.Annotations)
112111
}
113112

114113
if len(taints) > 0 {

pkg/nfd-gc/nfd-gc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func shouldEventuallyHaveNRTs(actualI interface{}, expectedI ...interface{}) str
141141
}
142142
actual := sets.Set[string]{}
143143
gvr := topologyv1alpha2.SchemeGroupVersion.WithResource("noderesourcetopologies")
144-
for i := 0; i < 2; i++ {
144+
for range 2 {
145145
rsp, err := cli.Resource(gvr).List(context.TODO(), metav1.ListOptions{})
146146
if err != nil {
147147
return fmt.Sprintf("failed to list: %v", err)

pkg/nfd-master/updater-pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (u *updaterPool) start(parallelism int) {
151151
u.queue = workqueue.NewTypedRateLimitingQueue[string](rl)
152152
u.nfgQueue = workqueue.NewTypedRateLimitingQueue[string](rl)
153153

154-
for i := 0; i < parallelism; i++ {
154+
for range parallelism {
155155
u.wg.Add(1)
156156
go u.runNodeUpdater()
157157
u.nfgWg.Add(1)

pkg/utils/featuregate/featuregate.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package featuregate
1919
import (
2020
"flag"
2121
"fmt"
22+
"maps"
2223
"sort"
2324
"strconv"
2425
"strings"
@@ -130,7 +131,7 @@ func NewFeatureGate() *featureGate {
130131
// map[string]bool of known keys or returns an error.
131132
func (f *featureGate) Set(value string) error {
132133
m := make(map[string]bool)
133-
for _, s := range strings.Split(value, ",") {
134+
for s := range strings.SplitSeq(value, ",") {
134135
if len(s) == 0 {
135136
continue
136137
}
@@ -156,9 +157,8 @@ func (f *featureGate) SetFromMap(m map[string]bool) error {
156157

157158
// Copy existing state
158159
known := map[Feature]FeatureSpec{}
159-
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
160-
known[k] = v
161-
}
160+
maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
161+
162162
enabled := map[Feature]bool{}
163163
for k, v := range f.enabled.Load().(map[Feature]bool) {
164164
enabled[k] = v
@@ -317,13 +317,9 @@ func (f *featureGate) KnownFeatures() []string {
317317
func (f *featureGate) DeepCopy() MutableFeatureGate {
318318
// Copy existing state.
319319
known := map[Feature]FeatureSpec{}
320-
for k, v := range f.known.Load().(map[Feature]FeatureSpec) {
321-
known[k] = v
322-
}
320+
maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
323321
enabled := map[Feature]bool{}
324-
for k, v := range f.enabled.Load().(map[Feature]bool) {
325-
enabled[k] = v
326-
}
322+
maps.Copy(enabled, f.enabled.Load().(map[Feature]bool))
327323

328324
// Construct a new featureGate around the copied state.
329325
// Note that specialFeatures is treated as immutable by convention,

pkg/utils/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type StringSetVal map[string]struct{}
4949
// Set implements the flag.Value interface
5050
func (a *StringSetVal) Set(val string) error {
5151
m := map[string]struct{}{}
52-
for _, n := range strings.Split(val, ",") {
52+
for n := range strings.SplitSeq(val, ",") {
5353
m[n] = struct{}{}
5454
}
5555
*a = m

pkg/utils/memory_resources.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func readTotalMemoryFromMeminfo(path string) (int64, error) {
128128
return -1, err
129129
}
130130

131-
for _, line := range strings.Split(string(data), "\n") {
131+
for line := range strings.SplitSeq(string(data), "\n") {
132132
split := strings.SplitN(line, ":", 2)
133133
if len(split) != 2 {
134134
continue

pkg/utils/memory_resources_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestGetMemoryResourceCounters(t *testing.T) {
118118
}
119119

120120
func makeMemoryTree(root string, numNodes int) error {
121-
for idx := 0; idx < numNodes; idx++ {
121+
for idx := range numNodes {
122122
path := filepath.Join(
123123
root,
124124
fmt.Sprintf("node%d", idx),
@@ -136,7 +136,7 @@ func makeMemoryTree(root string, numNodes int) error {
136136
}
137137

138138
func makeHugepagesTree(root string, numNodes int) error {
139-
for idx := 0; idx < numNodes; idx++ {
139+
for idx := range numNodes {
140140
for _, size := range []int{HugepageSize2Mi, HugepageSize1Gi} {
141141
path := filepath.Join(
142142
root,
@@ -163,5 +163,5 @@ func setHPCount(root string, nodeID, pageSize, numPages int) error {
163163
fmt.Sprintf("hugepages-%dkB", pageSize),
164164
"nr_hugepages",
165165
)
166-
return os.WriteFile(path, []byte(fmt.Sprintf("%d", numPages)), 0644)
166+
return os.WriteFile(path, fmt.Appendf(nil, "%d", numPages), 0644)
167167
}

source/custom/api/expression.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (m *MatchExpression) Validate() error {
6969
}
7070
var err error
7171
v := make([]int, 2)
72-
for i := 0; i < 2; i++ {
72+
for i := range 2 {
7373
if v[i], err = strconv.Atoi(m.Value[i]); err != nil {
7474
return fmt.Errorf("value must contain integers for Op %q (have %v)", m.Op, m.Value)
7575
}

0 commit comments

Comments
 (0)