1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-19 23:03:13 +00:00

Merge pull request #2088 from fmuyassarov/modernize

Apply modernize analysis improvements
This commit is contained in:
Kubernetes Prow Robot 2025-03-17 02:29:48 -07:00 committed by GitHub
commit 0914fa442f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 27 additions and 33 deletions

View file

@ -145,7 +145,7 @@ func evaluateMatchExpression(m *nfdv1alpha1.MatchExpression, valid bool, value i
return false, fmt.Errorf("not a number %q", value) return false, fmt.Errorf("not a number %q", value)
} }
lr := make([]int, 2) lr := make([]int, 2)
for i := 0; i < 2; i++ { for i := range 2 {
lr[i], err = strconv.Atoi(m.Value[i]) lr[i], err = strconv.Atoi(m.Value[i])
if err != nil { if err != nil {
return false, fmt.Errorf("not a number %q in %v", m.Value[i], m) return false, fmt.Errorf("not a number %q in %v", m.Value[i], m)

View file

@ -57,7 +57,7 @@ func (h *Helper) ExpandMap(data interface{}) (map[string]string, error) {
// Split out individual key-value pairs // Split out individual key-value pairs
out := make(map[string]string) 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 // Remove leading/trailing whitespace and skip empty lines
if trimmed := strings.TrimSpace(item); trimmed != "" { if trimmed := strings.TrimSpace(item); trimmed != "" {
split := strings.SplitN(trimmed, "=", 2) split := strings.SplitN(trimmed, "=", 2)

View file

@ -18,6 +18,7 @@ package kubectlnfd
import ( import (
"fmt" "fmt"
"maps"
"os" "os"
"strings" "strings"
@ -106,9 +107,7 @@ func processNodeFeatureRule(nodeFeatureRule nfdv1alpha1.NodeFeatureRule, nodeFea
extendedResources[k] = v extendedResources[k] = v
} }
// annotations // annotations
for k, v := range ruleOut.Annotations { maps.Copy(annotations, ruleOut.Annotations)
annotations[k] = v
}
} }
if len(taints) > 0 { if len(taints) > 0 {

View file

@ -141,7 +141,7 @@ func shouldEventuallyHaveNRTs(actualI interface{}, expectedI ...interface{}) str
} }
actual := sets.Set[string]{} actual := sets.Set[string]{}
gvr := topologyv1alpha2.SchemeGroupVersion.WithResource("noderesourcetopologies") gvr := topologyv1alpha2.SchemeGroupVersion.WithResource("noderesourcetopologies")
for i := 0; i < 2; i++ { for range 2 {
rsp, err := cli.Resource(gvr).List(context.TODO(), metav1.ListOptions{}) rsp, err := cli.Resource(gvr).List(context.TODO(), metav1.ListOptions{})
if err != nil { if err != nil {
return fmt.Sprintf("failed to list: %v", err) return fmt.Sprintf("failed to list: %v", err)

View file

@ -151,7 +151,7 @@ func (u *updaterPool) start(parallelism int) {
u.queue = workqueue.NewTypedRateLimitingQueue[string](rl) u.queue = workqueue.NewTypedRateLimitingQueue[string](rl)
u.nfgQueue = workqueue.NewTypedRateLimitingQueue[string](rl) u.nfgQueue = workqueue.NewTypedRateLimitingQueue[string](rl)
for i := 0; i < parallelism; i++ { for range parallelism {
u.wg.Add(1) u.wg.Add(1)
go u.runNodeUpdater() go u.runNodeUpdater()
u.nfgWg.Add(1) u.nfgWg.Add(1)

View file

@ -19,6 +19,7 @@ package featuregate
import ( import (
"flag" "flag"
"fmt" "fmt"
"maps"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -130,7 +131,7 @@ func NewFeatureGate() *featureGate {
// map[string]bool of known keys or returns an error. // map[string]bool of known keys or returns an error.
func (f *featureGate) Set(value string) error { func (f *featureGate) Set(value string) error {
m := make(map[string]bool) m := make(map[string]bool)
for _, s := range strings.Split(value, ",") { for s := range strings.SplitSeq(value, ",") {
if len(s) == 0 { if len(s) == 0 {
continue continue
} }
@ -156,9 +157,8 @@ func (f *featureGate) SetFromMap(m map[string]bool) error {
// Copy existing state // Copy existing state
known := map[Feature]FeatureSpec{} known := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) { maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
known[k] = v
}
enabled := map[Feature]bool{} enabled := map[Feature]bool{}
for k, v := range f.enabled.Load().(map[Feature]bool) { for k, v := range f.enabled.Load().(map[Feature]bool) {
enabled[k] = v enabled[k] = v
@ -317,13 +317,9 @@ func (f *featureGate) KnownFeatures() []string {
func (f *featureGate) DeepCopy() MutableFeatureGate { func (f *featureGate) DeepCopy() MutableFeatureGate {
// Copy existing state. // Copy existing state.
known := map[Feature]FeatureSpec{} known := map[Feature]FeatureSpec{}
for k, v := range f.known.Load().(map[Feature]FeatureSpec) { maps.Copy(known, f.known.Load().(map[Feature]FeatureSpec))
known[k] = v
}
enabled := map[Feature]bool{} enabled := map[Feature]bool{}
for k, v := range f.enabled.Load().(map[Feature]bool) { maps.Copy(enabled, f.enabled.Load().(map[Feature]bool))
enabled[k] = v
}
// Construct a new featureGate around the copied state. // Construct a new featureGate around the copied state.
// Note that specialFeatures is treated as immutable by convention, // Note that specialFeatures is treated as immutable by convention,

View file

@ -49,7 +49,7 @@ type StringSetVal map[string]struct{}
// Set implements the flag.Value interface // Set implements the flag.Value interface
func (a *StringSetVal) Set(val string) error { func (a *StringSetVal) Set(val string) error {
m := map[string]struct{}{} m := map[string]struct{}{}
for _, n := range strings.Split(val, ",") { for n := range strings.SplitSeq(val, ",") {
m[n] = struct{}{} m[n] = struct{}{}
} }
*a = m *a = m

View file

@ -128,7 +128,7 @@ func readTotalMemoryFromMeminfo(path string) (int64, error) {
return -1, err return -1, err
} }
for _, line := range strings.Split(string(data), "\n") { for line := range strings.SplitSeq(string(data), "\n") {
split := strings.SplitN(line, ":", 2) split := strings.SplitN(line, ":", 2)
if len(split) != 2 { if len(split) != 2 {
continue continue

View file

@ -118,7 +118,7 @@ func TestGetMemoryResourceCounters(t *testing.T) {
} }
func makeMemoryTree(root string, numNodes int) error { func makeMemoryTree(root string, numNodes int) error {
for idx := 0; idx < numNodes; idx++ { for idx := range numNodes {
path := filepath.Join( path := filepath.Join(
root, root,
fmt.Sprintf("node%d", idx), fmt.Sprintf("node%d", idx),
@ -136,7 +136,7 @@ func makeMemoryTree(root string, numNodes int) error {
} }
func makeHugepagesTree(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} { for _, size := range []int{HugepageSize2Mi, HugepageSize1Gi} {
path := filepath.Join( path := filepath.Join(
root, root,
@ -163,5 +163,5 @@ func setHPCount(root string, nodeID, pageSize, numPages int) error {
fmt.Sprintf("hugepages-%dkB", pageSize), fmt.Sprintf("hugepages-%dkB", pageSize),
"nr_hugepages", "nr_hugepages",
) )
return os.WriteFile(path, []byte(fmt.Sprintf("%d", numPages)), 0644) return os.WriteFile(path, fmt.Appendf(nil, "%d", numPages), 0644)
} }

View file

@ -69,7 +69,7 @@ func (m *MatchExpression) Validate() error {
} }
var err error var err error
v := make([]int, 2) 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 { 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) return fmt.Errorf("value must contain integers for Op %q (have %v)", m.Op, m.Value)
} }

View file

@ -100,8 +100,8 @@ func parseKconfig(configPath string) (realKconfig, legacyKconfig map[string]stri
} }
// Process data, line-by-line // Process data, line-by-line
lines := bytes.Split(raw, []byte("\n")) lines := bytes.SplitSeq(raw, []byte("\n"))
for _, line := range lines { for line := range lines {
str := string(line) str := string(line)
if strings.HasPrefix(str, "CONFIG_") { if strings.HasPrefix(str, "CONFIG_") {
split := strings.SplitN(str, "=", 2) split := strings.SplitN(str, "=", 2)

View file

@ -265,7 +265,7 @@ func getNumberOfNonEmptyLinesFromFile(path string) (int, error) {
return 0, err return 0, err
} }
length := 0 length := 0
for _, line := range strings.Split(string(data), "\n") { for line := range strings.SplitSeq(string(data), "\n") {
if strings.TrimSpace(line) != "" { if strings.TrimSpace(line) != "" {
length++ length++
} }

View file

@ -18,6 +18,7 @@ package usb
import ( import (
"fmt" "fmt"
"maps"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -90,9 +91,7 @@ func readUsbDevInfo(devPath string) ([]nfdv1alpha1.InstanceFeature, error) {
} }
subdevAttrs := make(map[string]string, len(attrs)) subdevAttrs := make(map[string]string, len(attrs))
for k, v := range attrs { maps.Copy(subdevAttrs, attrs)
subdevAttrs[k] = v
}
subdevAttrs["class"] = attrVal subdevAttrs["class"] = attrVal
instances = append(instances, *nfdv1alpha1.NewInstanceFeature(subdevAttrs)) instances = append(instances, *nfdv1alpha1.NewInstanceFeature(subdevAttrs))

View file

@ -63,19 +63,19 @@ func cleanupNode(ctx context.Context, cs clientset.Interface) {
updateStatus := false updateStatus := false
// Gather info about all NFD-managed node assets outside the default prefix // Gather info about all NFD-managed node assets outside the default prefix
nfdLabels := map[string]struct{}{} 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, "/") { if strings.Contains(name, "/") {
nfdLabels[name] = struct{}{} nfdLabels[name] = struct{}{}
} }
} }
nfdAnnotations := map[string]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, "/") { if strings.Contains(name, "/") {
nfdAnnotations[name] = struct{}{} nfdAnnotations[name] = struct{}{}
} }
} }
nfdERs := map[string]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, "/") { if strings.Contains(name, "/") {
nfdERs[name] = struct{}{} nfdERs[name] = struct{}{}
} }
@ -142,7 +142,7 @@ func cleanupNode(ctx context.Context, cs clientset.Interface) {
for _, n := range nodeList.Items { for _, n := range nodeList.Items {
var err error var err error
for retry := 0; retry < 5; retry++ { for range 5 {
if err = cleanup(n.Name); err == nil { if err = cleanup(n.Name); err == nil {
break break
} }