Skip to content

Commit

Permalink
Merge pull request #1503 from marquiz/devel/maps
Browse files Browse the repository at this point in the history
Use generics for maps and slices
  • Loading branch information
k8s-ci-robot authored Dec 14, 2023
2 parents 6ef153e + cb0a46e commit f0d3bce
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 79 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/smartystreets/goconvey v1.6.4
github.com/stretchr/testify v1.8.4
github.com/vektra/errors v0.0.0-20140903201135-c64d83aba85a
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb
golang.org/x/net v0.19.0
golang.org/x/time v0.5.0
google.golang.org/grpc v1.59.0
Expand Down Expand Up @@ -163,7 +164,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/oauth2 v0.14.0 // indirect
golang.org/x/sync v0.5.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions pkg/apis/nfd/v1alpha1/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"
"strings"

"golang.org/x/exp/maps"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -259,10 +260,7 @@ func (m *MatchExpression) MatchKeys(name string, keys map[string]Nil) (bool, err
if klogV := klog.V(3); klogV.Enabled() {
klogV.InfoS("matched keys", "matchResult", matched, "matchKey", name, "matchOp", m.Op)
} else if klogV := klog.V(4); klogV.Enabled() {
k := make([]string, 0, len(keys))
for n := range keys {
k = append(k, n)
}
k := maps.Keys(keys)
sort.Strings(k)
klogV.InfoS("matched keys", "matchResult", matched, "matchKey", name, "matchOp", m.Op, "inputKeys", k)
}
Expand Down
18 changes: 6 additions & 12 deletions pkg/apis/nfd/v1alpha1/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package v1alpha1

import "maps"

// NewNodeFeatureSpec creates a new emprty instance of NodeFeatureSpec type,
// initializing all fields to proper empty values.
func NewNodeFeatureSpec() *NodeFeatureSpec {
Expand Down Expand Up @@ -75,9 +77,7 @@ func (f *Features) InsertAttributeFeatures(domain, feature string, values map[st
return
}

for k, v := range values {
f.Attributes[key].Elements[k] = v
}
maps.Copy(f.Attributes[key].Elements, values)
}

// Exists returns a non-empty string if a feature exists. The return value is
Expand All @@ -103,9 +103,7 @@ func (in *NodeFeatureSpec) MergeInto(out *NodeFeatureSpec) {
if out.Labels == nil {
out.Labels = make(map[string]string, len(in.Labels))
}
for key, val := range in.Labels {
out.Labels[key] = val
}
maps.Copy(out.Labels, in.Labels)
}
}

Expand Down Expand Up @@ -151,9 +149,7 @@ func (in *FlagFeatureSet) MergeInto(out *FlagFeatureSet) {
if out.Elements == nil {
out.Elements = make(map[string]Nil, len(in.Elements))
}
for key, val := range in.Elements {
out.Elements[key] = val
}
maps.Copy(out.Elements, in.Elements)
}
}

Expand All @@ -163,9 +159,7 @@ func (in *AttributeFeatureSet) MergeInto(out *AttributeFeatureSet) {
if out.Elements == nil {
out.Elements = make(map[string]string, len(in.Elements))
}
for key, val := range in.Elements {
out.Elements[key] = val
}
maps.Copy(out.Elements, in.Elements)
}
}

Expand Down
21 changes: 10 additions & 11 deletions pkg/apis/nfd/v1alpha1/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package v1alpha1
import (
"bytes"
"fmt"
"maps"
"slices"
"strings"
"text/template"

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

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

Expand Down Expand Up @@ -92,18 +93,16 @@ func (r *Rule) Execute(features *Features) (RuleOutput, error) {
}
}

for k, v := range r.ExtendedResources {
extendedResources[k] = v
}
maps.Copy(labels, r.Labels)
maps.Copy(vars, r.Vars)

for k, v := range r.Labels {
labels[k] = v
}
for k, v := range r.Vars {
vars[k] = v
ret := RuleOutput{
Labels: labels,
Vars: vars,
Annotations: maps.Clone(r.Annotations),
ExtendedResources: maps.Clone(r.ExtendedResources),
Taints: slices.Clone(r.Taints),
}

ret := RuleOutput{ExtendedResources: extendedResources, Labels: labels, Vars: vars, Taints: r.Taints, Annotations: r.Annotations}
klog.V(2).InfoS("rule matched", "ruleName", r.Name, "ruleOutput", utils.DelayedDumper(ret))
return ret, nil
}
Expand Down
33 changes: 10 additions & 23 deletions pkg/nfd-master/nfd-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"maps"
"net"
"os"
"path"
Expand Down Expand Up @@ -477,11 +478,9 @@ func (m *nfdMaster) prune() error {
if err != nil {
return err
}
for a := range node.Annotations {
if strings.HasPrefix(a, m.instanceAnnotation(nfdv1alpha1.AnnotationNs)) {
delete(node.Annotations, a)
}
}
maps.DeleteFunc(node.Annotations, func(k, v string) bool {
return strings.HasPrefix(k, m.instanceAnnotation(nfdv1alpha1.AnnotationNs))
})
err = m.apihelper.UpdateNode(cli, node)
if err != nil {
return fmt.Errorf("failed to prune annotations from node %q: %v", node.Name, err)
Expand Down Expand Up @@ -812,18 +811,14 @@ func (m *nfdMaster) refreshNodeFeatures(cli *kubernetes.Clientset, nodeName stri
crLabels, crAnnotations, crExtendedResources, crTaints := m.processNodeFeatureRule(nodeName, features)

// Mix in CR-originated labels
for k, v := range crLabels {
labels[k] = v
}
maps.Copy(labels, crLabels)

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

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

// Annotations
Expand Down Expand Up @@ -991,15 +986,9 @@ func (m *nfdMaster) processNodeFeatureRule(nodeName string, features *nfdv1alpha
e = addNsToMapKeys(ruleOut.ExtendedResources, nfdv1alpha1.ExtendedResourceNs)
a = addNsToMapKeys(ruleOut.Annotations, nfdv1alpha1.FeatureAnnotationNs)
}
for k, v := range l {
labels[k] = v
}
for k, v := range e {
extendedResources[k] = v
}
for k, v := range a {
annotations[k] = v
}
maps.Copy(labels, l)
maps.Copy(extendedResources, e)
maps.Copy(annotations, a)

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

// Create JSON patches for changes in labels and annotations
Expand Down
15 changes: 4 additions & 11 deletions pkg/nfd-worker/nfd-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"time"

"golang.org/x/exp/maps"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -415,10 +416,7 @@ func (w *nfdWorker) configureCore(c coreConfig) error {
}
}

w.featureSources = make([]source.FeatureSource, 0, len(featureSources))
for _, s := range featureSources {
w.featureSources = append(w.featureSources, s)
}
w.featureSources = maps.Values(featureSources)

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

Expand Down Expand Up @@ -450,10 +448,7 @@ func (w *nfdWorker) configureCore(c coreConfig) error {
}
}

w.labelSources = make([]source.LabelSource, 0, len(labelSources))
for _, s := range labelSources {
w.labelSources = append(w.labelSources, s)
}
w.labelSources = maps.Values(labelSources)

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

for name, value := range labelsFromSource {
labels[name] = value
}
maps.Copy(labels, labelsFromSource)
}
if klogV := klog.V(1); klogV.Enabled() {
klogV.InfoS("feature discovery completed", "labels", utils.DelayedDumper(labels))
Expand Down
7 changes: 3 additions & 4 deletions pkg/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"sort"
"strings"
"time"

"golang.org/x/exp/maps"
)

// RegexpVal is a wrapper for regexp command line flags
Expand Down Expand Up @@ -77,10 +79,7 @@ func (a *StringSetVal) String() string {
if *a == nil {
return ""
}
vals := make([]string, 0, len(*a))
for val := range *a {
vals = append(vals, val)
}
vals := maps.Keys(*a)
sort.Strings(vals)
return strings.Join(vals, ",")
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/utils/memory_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package utils

import (
"fmt"
"maps"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -75,9 +76,7 @@ func GetNumaMemoryResources() (NumaMemoryResources, error) {
return nil, err
}
}
for n, s := range hugepageBytes {
info[n] = s
}
maps.Copy(info, hugepageBytes)

memoryResources[nodeID] = info
}
Expand Down
7 changes: 2 additions & 5 deletions source/pci/pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"strings"

"golang.org/x/exp/maps"
"k8s.io/klog/v2"

nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
Expand Down Expand Up @@ -102,11 +103,7 @@ func (s *pciSource) GetLabels() (source.FeatureLabels, error) {
}
}
if len(configLabelFields) > 0 {
keys := []string{}
for key := range configLabelFields {
keys = append(keys, key)
}
klog.InfoS("ignoring invalid fields in deviceLabelFields", "invalidFieldNames", keys)
klog.InfoS("ignoring invalid fields in deviceLabelFields", "invalidFieldNames", maps.Keys(configLabelFields))
}
if len(deviceLabelFields) == 0 {
deviceLabelFields = []string{"class", "vendor"}
Expand Down
7 changes: 2 additions & 5 deletions source/usb/usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"strings"

"golang.org/x/exp/maps"
"k8s.io/klog/v2"

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

0 comments on commit f0d3bce

Please sign in to comment.