mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-18 22:33:09 +00:00
Merge pull request #2088 from fmuyassarov/modernize
Apply modernize analysis improvements
This commit is contained in:
commit
0914fa442f
14 changed files with 27 additions and 33 deletions
|
@ -145,7 +145,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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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++
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue