Skip to content

Commit f0d3bce

Browse files
authored
Merge pull request #1503 from marquiz/devel/maps
Use generics for maps and slices
2 parents 6ef153e + cb0a46e commit f0d3bce

File tree

10 files changed

+42
-79
lines changed

10 files changed

+42
-79
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/smartystreets/goconvey v1.6.4
2121
github.com/stretchr/testify v1.8.4
2222
github.com/vektra/errors v0.0.0-20140903201135-c64d83aba85a
23+
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb
2324
golang.org/x/net v0.19.0
2425
golang.org/x/time v0.5.0
2526
google.golang.org/grpc v1.59.0
@@ -163,7 +164,6 @@ require (
163164
go.uber.org/multierr v1.11.0 // indirect
164165
go.uber.org/zap v1.24.0 // indirect
165166
golang.org/x/crypto v0.16.0 // indirect
166-
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect
167167
golang.org/x/mod v0.14.0 // indirect
168168
golang.org/x/oauth2 v0.14.0 // indirect
169169
golang.org/x/sync v0.5.0 // indirect

pkg/apis/nfd/v1alpha1/expression.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strconv"
2525
"strings"
2626

27+
"golang.org/x/exp/maps"
2728
"k8s.io/klog/v2"
2829
)
2930

@@ -259,10 +260,7 @@ func (m *MatchExpression) MatchKeys(name string, keys map[string]Nil) (bool, err
259260
if klogV := klog.V(3); klogV.Enabled() {
260261
klogV.InfoS("matched keys", "matchResult", matched, "matchKey", name, "matchOp", m.Op)
261262
} else if klogV := klog.V(4); klogV.Enabled() {
262-
k := make([]string, 0, len(keys))
263-
for n := range keys {
264-
k = append(k, n)
265-
}
263+
k := maps.Keys(keys)
266264
sort.Strings(k)
267265
klogV.InfoS("matched keys", "matchResult", matched, "matchKey", name, "matchOp", m.Op, "inputKeys", k)
268266
}

pkg/apis/nfd/v1alpha1/feature.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
package v1alpha1
1818

19+
import "maps"
20+
1921
// NewNodeFeatureSpec creates a new emprty instance of NodeFeatureSpec type,
2022
// initializing all fields to proper empty values.
2123
func NewNodeFeatureSpec() *NodeFeatureSpec {
@@ -75,9 +77,7 @@ func (f *Features) InsertAttributeFeatures(domain, feature string, values map[st
7577
return
7678
}
7779

78-
for k, v := range values {
79-
f.Attributes[key].Elements[k] = v
80-
}
80+
maps.Copy(f.Attributes[key].Elements, values)
8181
}
8282

8383
// Exists returns a non-empty string if a feature exists. The return value is
@@ -103,9 +103,7 @@ func (in *NodeFeatureSpec) MergeInto(out *NodeFeatureSpec) {
103103
if out.Labels == nil {
104104
out.Labels = make(map[string]string, len(in.Labels))
105105
}
106-
for key, val := range in.Labels {
107-
out.Labels[key] = val
108-
}
106+
maps.Copy(out.Labels, in.Labels)
109107
}
110108
}
111109

@@ -151,9 +149,7 @@ func (in *FlagFeatureSet) MergeInto(out *FlagFeatureSet) {
151149
if out.Elements == nil {
152150
out.Elements = make(map[string]Nil, len(in.Elements))
153151
}
154-
for key, val := range in.Elements {
155-
out.Elements[key] = val
156-
}
152+
maps.Copy(out.Elements, in.Elements)
157153
}
158154
}
159155

@@ -163,9 +159,7 @@ func (in *AttributeFeatureSet) MergeInto(out *AttributeFeatureSet) {
163159
if out.Elements == nil {
164160
out.Elements = make(map[string]string, len(in.Elements))
165161
}
166-
for key, val := range in.Elements {
167-
out.Elements[key] = val
168-
}
162+
maps.Copy(out.Elements, in.Elements)
169163
}
170164
}
171165

pkg/apis/nfd/v1alpha1/rule.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package v1alpha1
1919
import (
2020
"bytes"
2121
"fmt"
22+
"maps"
23+
"slices"
2224
"strings"
2325
"text/template"
2426

@@ -40,7 +42,6 @@ type RuleOutput struct {
4042

4143
// Execute the rule against a set of input features.
4244
func (r *Rule) Execute(features *Features) (RuleOutput, error) {
43-
extendedResources := make(map[string]string)
4445
labels := make(map[string]string)
4546
vars := make(map[string]string)
4647

@@ -92,18 +93,16 @@ func (r *Rule) Execute(features *Features) (RuleOutput, error) {
9293
}
9394
}
9495

95-
for k, v := range r.ExtendedResources {
96-
extendedResources[k] = v
97-
}
96+
maps.Copy(labels, r.Labels)
97+
maps.Copy(vars, r.Vars)
9898

99-
for k, v := range r.Labels {
100-
labels[k] = v
101-
}
102-
for k, v := range r.Vars {
103-
vars[k] = v
99+
ret := RuleOutput{
100+
Labels: labels,
101+
Vars: vars,
102+
Annotations: maps.Clone(r.Annotations),
103+
ExtendedResources: maps.Clone(r.ExtendedResources),
104+
Taints: slices.Clone(r.Taints),
104105
}
105-
106-
ret := RuleOutput{ExtendedResources: extendedResources, Labels: labels, Vars: vars, Taints: r.Taints, Annotations: r.Annotations}
107106
klog.V(2).InfoS("rule matched", "ruleName", r.Name, "ruleOutput", utils.DelayedDumper(ret))
108107
return ret, nil
109108
}

pkg/nfd-master/nfd-master.go

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/tls"
2121
"crypto/x509"
2222
"fmt"
23+
"maps"
2324
"net"
2425
"os"
2526
"path"
@@ -477,11 +478,9 @@ func (m *nfdMaster) prune() error {
477478
if err != nil {
478479
return err
479480
}
480-
for a := range node.Annotations {
481-
if strings.HasPrefix(a, m.instanceAnnotation(nfdv1alpha1.AnnotationNs)) {
482-
delete(node.Annotations, a)
483-
}
484-
}
481+
maps.DeleteFunc(node.Annotations, func(k, v string) bool {
482+
return strings.HasPrefix(k, m.instanceAnnotation(nfdv1alpha1.AnnotationNs))
483+
})
485484
err = m.apihelper.UpdateNode(cli, node)
486485
if err != nil {
487486
return fmt.Errorf("failed to prune annotations from node %q: %v", node.Name, err)
@@ -812,18 +811,14 @@ func (m *nfdMaster) refreshNodeFeatures(cli *kubernetes.Clientset, nodeName stri
812811
crLabels, crAnnotations, crExtendedResources, crTaints := m.processNodeFeatureRule(nodeName, features)
813812

814813
// Mix in CR-originated labels
815-
for k, v := range crLabels {
816-
labels[k] = v
817-
}
814+
maps.Copy(labels, crLabels)
818815

819816
// Remove labels which are intended to be extended resources via
820817
// -resource-labels or their NS is not whitelisted
821818
labels, extendedResources := m.filterFeatureLabels(labels, features)
822819

823820
// Mix in CR-originated extended resources with -resource-labels
824-
for k, v := range crExtendedResources {
825-
extendedResources[k] = v
826-
}
821+
maps.Copy(extendedResources, crExtendedResources)
827822
extendedResources = m.filterExtendedResources(features, extendedResources)
828823

829824
// Annotations
@@ -991,15 +986,9 @@ func (m *nfdMaster) processNodeFeatureRule(nodeName string, features *nfdv1alpha
991986
e = addNsToMapKeys(ruleOut.ExtendedResources, nfdv1alpha1.ExtendedResourceNs)
992987
a = addNsToMapKeys(ruleOut.Annotations, nfdv1alpha1.FeatureAnnotationNs)
993988
}
994-
for k, v := range l {
995-
labels[k] = v
996-
}
997-
for k, v := range e {
998-
extendedResources[k] = v
999-
}
1000-
for k, v := range a {
1001-
annotations[k] = v
1002-
}
989+
maps.Copy(labels, l)
990+
maps.Copy(extendedResources, e)
991+
maps.Copy(annotations, a)
1003992

1004993
// Feed back rule output to features map for subsequent rules to match
1005994
features.InsertAttributeFeatures(nfdv1alpha1.RuleBackrefDomain, nfdv1alpha1.RuleBackrefFeature, ruleOut.Labels)
@@ -1061,9 +1050,7 @@ func (m *nfdMaster) updateNodeObject(cli *kubernetes.Clientset, nodeName string,
10611050
}
10621051
sort.Strings(annotationKeys)
10631052
annotations[m.instanceAnnotation(nfdv1alpha1.FeatureAnnotationsTrackingAnnotation)] = strings.Join(annotationKeys, ",")
1064-
for k, v := range featureAnnotations {
1065-
annotations[k] = v
1066-
}
1053+
maps.Copy(annotations, featureAnnotations)
10671054
}
10681055

10691056
// Create JSON patches for changes in labels and annotations

pkg/nfd-worker/nfd-worker.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"strings"
2929
"time"
3030

31+
"golang.org/x/exp/maps"
3132
"golang.org/x/net/context"
3233
"google.golang.org/grpc"
3334
"google.golang.org/grpc/credentials"
@@ -415,10 +416,7 @@ func (w *nfdWorker) configureCore(c coreConfig) error {
415416
}
416417
}
417418

418-
w.featureSources = make([]source.FeatureSource, 0, len(featureSources))
419-
for _, s := range featureSources {
420-
w.featureSources = append(w.featureSources, s)
421-
}
419+
w.featureSources = maps.Values(featureSources)
422420

423421
sort.Slice(w.featureSources, func(i, j int) bool { return w.featureSources[i].Name() < w.featureSources[j].Name() })
424422

@@ -450,10 +448,7 @@ func (w *nfdWorker) configureCore(c coreConfig) error {
450448
}
451449
}
452450

453-
w.labelSources = make([]source.LabelSource, 0, len(labelSources))
454-
for _, s := range labelSources {
455-
w.labelSources = append(w.labelSources, s)
456-
}
451+
w.labelSources = maps.Values(labelSources)
457452

458453
sort.Slice(w.labelSources, func(i, j int) bool {
459454
iP, jP := w.labelSources[i].Priority(), w.labelSources[j].Priority()
@@ -560,9 +555,7 @@ func createFeatureLabels(sources []source.LabelSource, labelWhiteList regexp.Reg
560555
continue
561556
}
562557

563-
for name, value := range labelsFromSource {
564-
labels[name] = value
565-
}
558+
maps.Copy(labels, labelsFromSource)
566559
}
567560
if klogV := klog.V(1); klogV.Enabled() {
568561
klogV.InfoS("feature discovery completed", "labels", utils.DelayedDumper(labels))

pkg/utils/flags.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"sort"
2525
"strings"
2626
"time"
27+
28+
"golang.org/x/exp/maps"
2729
)
2830

2931
// RegexpVal is a wrapper for regexp command line flags
@@ -77,10 +79,7 @@ func (a *StringSetVal) String() string {
7779
if *a == nil {
7880
return ""
7981
}
80-
vals := make([]string, 0, len(*a))
81-
for val := range *a {
82-
vals = append(vals, val)
83-
}
82+
vals := maps.Keys(*a)
8483
sort.Strings(vals)
8584
return strings.Join(vals, ",")
8685
}

pkg/utils/memory_resources.go

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

1919
import (
2020
"fmt"
21+
"maps"
2122
"os"
2223
"path/filepath"
2324
"strconv"
@@ -75,9 +76,7 @@ func GetNumaMemoryResources() (NumaMemoryResources, error) {
7576
return nil, err
7677
}
7778
}
78-
for n, s := range hugepageBytes {
79-
info[n] = s
80-
}
79+
maps.Copy(info, hugepageBytes)
8180

8281
memoryResources[nodeID] = info
8382
}

source/pci/pci.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"strings"
2222

23+
"golang.org/x/exp/maps"
2324
"k8s.io/klog/v2"
2425

2526
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
@@ -102,11 +103,7 @@ func (s *pciSource) GetLabels() (source.FeatureLabels, error) {
102103
}
103104
}
104105
if len(configLabelFields) > 0 {
105-
keys := []string{}
106-
for key := range configLabelFields {
107-
keys = append(keys, key)
108-
}
109-
klog.InfoS("ignoring invalid fields in deviceLabelFields", "invalidFieldNames", keys)
106+
klog.InfoS("ignoring invalid fields in deviceLabelFields", "invalidFieldNames", maps.Keys(configLabelFields))
110107
}
111108
if len(deviceLabelFields) == 0 {
112109
deviceLabelFields = []string{"class", "vendor"}

source/usb/usb.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"strings"
2222

23+
"golang.org/x/exp/maps"
2324
"k8s.io/klog/v2"
2425

2526
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
@@ -105,11 +106,7 @@ func (s *usbSource) GetLabels() (source.FeatureLabels, error) {
105106
}
106107
}
107108
if len(configLabelFields) > 0 {
108-
keys := []string{}
109-
for key := range configLabelFields {
110-
keys = append(keys, key)
111-
}
112-
klog.InfoS("ignoring invalid fields in deviceLabelFields", "invalidFieldNames", keys)
109+
klog.InfoS("ignoring invalid fields in deviceLabelFields", "invalidFieldNames", maps.Keys(configLabelFields))
113110
}
114111
if len(deviceLabelFields) == 0 {
115112
deviceLabelFields = defaultDeviceLabelFields()

0 commit comments

Comments
 (0)