diff --git a/pkg/apis/nfd/nodefeaturerule/expression.go b/pkg/apis/nfd/nodefeaturerule/expression.go index 7f4b17105..441a25294 100644 --- a/pkg/apis/nfd/nodefeaturerule/expression.go +++ b/pkg/apis/nfd/nodefeaturerule/expression.go @@ -141,7 +141,7 @@ func evaluateMatchExpression(m *nfdv1alpha1.MatchExpression, valid bool, value i return false, fmt.Errorf("not a number %q", value) } lr := make([]int, 2) - for i := 0; i < 2; i++ { + for i := range 2 { lr[i], err = strconv.Atoi(m.Value[i]) if err != nil { return false, fmt.Errorf("not a number %q in %v", m.Value[i], m) diff --git a/pkg/apis/nfd/template/template.go b/pkg/apis/nfd/template/template.go index 4c0242495..8b709ad6b 100644 --- a/pkg/apis/nfd/template/template.go +++ b/pkg/apis/nfd/template/template.go @@ -57,7 +57,7 @@ func (h *Helper) ExpandMap(data interface{}) (map[string]string, error) { // Split out individual key-value pairs out := make(map[string]string) - for _, item := range strings.Split(expanded, "\n") { + for item := range strings.SplitSeq(expanded, "\n") { // Remove leading/trailing whitespace and skip empty lines if trimmed := strings.TrimSpace(item); trimmed != "" { split := strings.SplitN(trimmed, "=", 2) diff --git a/pkg/kubectl-nfd/dryrun.go b/pkg/kubectl-nfd/dryrun.go index fdca04d88..8240e53e0 100644 --- a/pkg/kubectl-nfd/dryrun.go +++ b/pkg/kubectl-nfd/dryrun.go @@ -18,6 +18,7 @@ package kubectlnfd import ( "fmt" + "maps" "os" "strings" @@ -106,9 +107,7 @@ func processNodeFeatureRule(nodeFeatureRule nfdv1alpha1.NodeFeatureRule, nodeFea extendedResources[k] = v } // annotations - for k, v := range ruleOut.Annotations { - annotations[k] = v - } + maps.Copy(annotations, ruleOut.Annotations) } if len(taints) > 0 { diff --git a/pkg/nfd-gc/nfd-gc_test.go b/pkg/nfd-gc/nfd-gc_test.go index 894ac8c0f..62adb5773 100644 --- a/pkg/nfd-gc/nfd-gc_test.go +++ b/pkg/nfd-gc/nfd-gc_test.go @@ -141,7 +141,7 @@ func shouldEventuallyHaveNRTs(actualI interface{}, expectedI ...interface{}) str } actual := sets.Set[string]{} gvr := topologyv1alpha2.SchemeGroupVersion.WithResource("noderesourcetopologies") - for i := 0; i < 2; i++ { + for range 2 { rsp, err := cli.Resource(gvr).List(context.TODO(), metav1.ListOptions{}) if err != nil { return fmt.Sprintf("failed to list: %v", err) diff --git a/pkg/nfd-master/updater-pool.go b/pkg/nfd-master/updater-pool.go index 0aa48e079..2f0c98e6f 100644 --- a/pkg/nfd-master/updater-pool.go +++ b/pkg/nfd-master/updater-pool.go @@ -151,7 +151,7 @@ func (u *updaterPool) start(parallelism int) { u.queue = workqueue.NewTypedRateLimitingQueue[string](rl) u.nfgQueue = workqueue.NewTypedRateLimitingQueue[string](rl) - for i := 0; i < parallelism; i++ { + for range parallelism { u.wg.Add(1) go u.runNodeUpdater() u.nfgWg.Add(1) diff --git a/pkg/utils/featuregate/featuregate.go b/pkg/utils/featuregate/featuregate.go index 32d736437..380065438 100644 --- a/pkg/utils/featuregate/featuregate.go +++ b/pkg/utils/featuregate/featuregate.go @@ -19,6 +19,7 @@ package featuregate import ( "flag" "fmt" + "maps" "sort" "strconv" "strings" @@ -130,7 +131,7 @@ func NewFeatureGate() *featureGate { // map[string]bool of known keys or returns an error. func (f *featureGate) Set(value string) error { m := make(map[string]bool) - for _, s := range strings.Split(value, ",") { + for s := range strings.SplitSeq(value, ",") { if len(s) == 0 { continue } @@ -156,9 +157,8 @@ func (f *featureGate) SetFromMap(m map[string]bool) error { // Copy existing state known := map[Feature]FeatureSpec{} - for k, v := range f.known.Load().(map[Feature]FeatureSpec) { - known[k] = v - } + maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec)) + enabled := map[Feature]bool{} for k, v := range f.enabled.Load().(map[Feature]bool) { enabled[k] = v @@ -317,13 +317,9 @@ func (f *featureGate) KnownFeatures() []string { func (f *featureGate) DeepCopy() MutableFeatureGate { // Copy existing state. known := map[Feature]FeatureSpec{} - for k, v := range f.known.Load().(map[Feature]FeatureSpec) { - known[k] = v - } + maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec)) enabled := map[Feature]bool{} - for k, v := range f.enabled.Load().(map[Feature]bool) { - enabled[k] = v - } + maps.Copy(enabled, f.enabled.Load().(map[Feature]bool)) // Construct a new featureGate around the copied state. // Note that specialFeatures is treated as immutable by convention, diff --git a/pkg/utils/flags.go b/pkg/utils/flags.go index b6455702e..f2d0a8c6a 100644 --- a/pkg/utils/flags.go +++ b/pkg/utils/flags.go @@ -49,7 +49,7 @@ type StringSetVal map[string]struct{} // Set implements the flag.Value interface func (a *StringSetVal) Set(val string) error { m := map[string]struct{}{} - for _, n := range strings.Split(val, ",") { + for n := range strings.SplitSeq(val, ",") { m[n] = struct{}{} } *a = m diff --git a/pkg/utils/memory_resources.go b/pkg/utils/memory_resources.go index b969a6b21..8bd2943f7 100644 --- a/pkg/utils/memory_resources.go +++ b/pkg/utils/memory_resources.go @@ -128,7 +128,7 @@ func readTotalMemoryFromMeminfo(path string) (int64, error) { return -1, err } - for _, line := range strings.Split(string(data), "\n") { + for line := range strings.SplitSeq(string(data), "\n") { split := strings.SplitN(line, ":", 2) if len(split) != 2 { continue diff --git a/pkg/utils/memory_resources_test.go b/pkg/utils/memory_resources_test.go index 030149bac..29e3e4547 100644 --- a/pkg/utils/memory_resources_test.go +++ b/pkg/utils/memory_resources_test.go @@ -118,7 +118,7 @@ func TestGetMemoryResourceCounters(t *testing.T) { } func makeMemoryTree(root string, numNodes int) error { - for idx := 0; idx < numNodes; idx++ { + for idx := range numNodes { path := filepath.Join( root, fmt.Sprintf("node%d", idx), @@ -136,7 +136,7 @@ func makeMemoryTree(root string, numNodes int) error { } func makeHugepagesTree(root string, numNodes int) error { - for idx := 0; idx < numNodes; idx++ { + for idx := range numNodes { for _, size := range []int{HugepageSize2Mi, HugepageSize1Gi} { path := filepath.Join( root, @@ -163,5 +163,5 @@ func setHPCount(root string, nodeID, pageSize, numPages int) error { fmt.Sprintf("hugepages-%dkB", pageSize), "nr_hugepages", ) - return os.WriteFile(path, []byte(fmt.Sprintf("%d", numPages)), 0644) + return os.WriteFile(path, fmt.Appendf(nil, "%d", numPages), 0644) } diff --git a/source/custom/api/expression.go b/source/custom/api/expression.go index 63f4bcc2b..3ef947610 100644 --- a/source/custom/api/expression.go +++ b/source/custom/api/expression.go @@ -69,7 +69,7 @@ func (m *MatchExpression) Validate() error { } var err error v := make([]int, 2) - for i := 0; i < 2; i++ { + for i := range 2 { if v[i], err = strconv.Atoi(m.Value[i]); err != nil { return fmt.Errorf("value must contain integers for Op %q (have %v)", m.Op, m.Value) } diff --git a/source/kernel/kconfig.go b/source/kernel/kconfig.go index 69f8a6884..497ff4fa8 100644 --- a/source/kernel/kconfig.go +++ b/source/kernel/kconfig.go @@ -100,8 +100,8 @@ func parseKconfig(configPath string) (realKconfig, legacyKconfig map[string]stri } // Process data, line-by-line - lines := bytes.Split(raw, []byte("\n")) - for _, line := range lines { + lines := bytes.SplitSeq(raw, []byte("\n")) + for line := range lines { str := string(line) if strings.HasPrefix(str, "CONFIG_") { split := strings.SplitN(str, "=", 2) diff --git a/source/memory/memory.go b/source/memory/memory.go index df6bbb76f..68ef0c741 100644 --- a/source/memory/memory.go +++ b/source/memory/memory.go @@ -265,7 +265,7 @@ func getNumberOfNonEmptyLinesFromFile(path string) (int, error) { return 0, err } length := 0 - for _, line := range strings.Split(string(data), "\n") { + for line := range strings.SplitSeq(string(data), "\n") { if strings.TrimSpace(line) != "" { length++ } diff --git a/source/usb/utils.go b/source/usb/utils.go index c5aa4d69a..24772e8b6 100644 --- a/source/usb/utils.go +++ b/source/usb/utils.go @@ -18,6 +18,7 @@ package usb import ( "fmt" + "maps" "os" "path" "path/filepath" @@ -90,9 +91,7 @@ func readUsbDevInfo(devPath string) ([]nfdv1alpha1.InstanceFeature, error) { } subdevAttrs := make(map[string]string, len(attrs)) - for k, v := range attrs { - subdevAttrs[k] = v - } + maps.Copy(subdevAttrs, attrs) subdevAttrs["class"] = attrVal instances = append(instances, *nfdv1alpha1.NewInstanceFeature(subdevAttrs)) diff --git a/test/e2e/node_feature_discovery_test.go b/test/e2e/node_feature_discovery_test.go index 78cb96eee..f664cb19a 100644 --- a/test/e2e/node_feature_discovery_test.go +++ b/test/e2e/node_feature_discovery_test.go @@ -63,19 +63,19 @@ func cleanupNode(ctx context.Context, cs clientset.Interface) { updateStatus := false // Gather info about all NFD-managed node assets outside the default prefix nfdLabels := map[string]struct{}{} - for _, name := range strings.Split(node.Annotations[nfdv1alpha1.FeatureLabelsAnnotation], ",") { + for name := range strings.SplitSeq(node.Annotations[nfdv1alpha1.FeatureLabelsAnnotation], ",") { if strings.Contains(name, "/") { nfdLabels[name] = struct{}{} } } nfdAnnotations := map[string]struct{}{} - for _, name := range strings.Split(node.Annotations[nfdv1alpha1.FeatureAnnotationsTrackingAnnotation], ",") { + for name := range strings.SplitSeq(node.Annotations[nfdv1alpha1.FeatureAnnotationsTrackingAnnotation], ",") { if strings.Contains(name, "/") { nfdAnnotations[name] = struct{}{} } } nfdERs := map[string]struct{}{} - for _, name := range strings.Split(node.Annotations[nfdv1alpha1.ExtendedResourceAnnotation], ",") { + for name := range strings.SplitSeq(node.Annotations[nfdv1alpha1.ExtendedResourceAnnotation], ",") { if strings.Contains(name, "/") { nfdERs[name] = struct{}{} } @@ -142,7 +142,7 @@ func cleanupNode(ctx context.Context, cs clientset.Interface) { for _, n := range nodeList.Items { var err error - for retry := 0; retry < 5; retry++ { + for range 5 { if err = cleanup(n.Name); err == nil { break }