1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

api/nfd: use varargs in the NewInstanceFeatures helper

Make usage of this helper function more flexible.
This commit is contained in:
Markus Lehtonen 2024-04-17 16:41:12 +03:00
parent 81bdf1dadb
commit 719c5186f6
6 changed files with 15 additions and 20 deletions

View file

@ -54,7 +54,7 @@ func NewAttributeFeatures(values map[string]string) AttributeFeatureSet {
} }
// NewInstanceFeatures creates a new instance of InstanceFeatureSet. // NewInstanceFeatures creates a new instance of InstanceFeatureSet.
func NewInstanceFeatures(instances []InstanceFeature) InstanceFeatureSet { func NewInstanceFeatures(instances ...InstanceFeature) InstanceFeatureSet {
return InstanceFeatureSet{Elements: instances} return InstanceFeatureSet{Elements: instances}
} }

View file

@ -84,22 +84,20 @@ func TestInstanceFeatureSet(t *testing.T) {
f2.MergeInto(&f1) f2.MergeInto(&f1)
assert.Equal(t, expectedElems, f1.Elements) assert.Equal(t, expectedElems, f1.Elements)
f2 = NewInstanceFeatures([]InstanceFeature{}) f2 = NewInstanceFeatures()
expectedElems = []InstanceFeature{} expectedElems = []InstanceFeature{}
f2.MergeInto(&f1) f2.MergeInto(&f1)
assert.Equal(t, expectedElems, f1.Elements) assert.Equal(t, expectedElems, f1.Elements)
f2 = NewInstanceFeatures([]InstanceFeature{InstanceFeature{}}) f2 = NewInstanceFeatures(InstanceFeature{})
expectedElems = append(expectedElems, InstanceFeature{}) expectedElems = append(expectedElems, InstanceFeature{})
f2.MergeInto(&f1) f2.MergeInto(&f1)
assert.Equal(t, expectedElems, f1.Elements) assert.Equal(t, expectedElems, f1.Elements)
f2 = NewInstanceFeatures([]InstanceFeature{ f2 = NewInstanceFeatures(InstanceFeature{
InstanceFeature{ Attributes: map[string]string{
Attributes: map[string]string{ "a1": "v1",
"a1": "v1", "a2": "v2",
"a2": "v2",
},
}, },
}) })
expectedElems = append(expectedElems, *NewInstanceFeature(map[string]string{"a1": "v1", "a2": "v2"})) expectedElems = append(expectedElems, *NewInstanceFeature(map[string]string{"a1": "v1", "a2": "v2"}))
@ -139,18 +137,16 @@ func TestFeature(t *testing.T) {
f2 = *NewFeatures() f2 = *NewFeatures()
f2.Flags["dom.flag"] = NewFlagFeatures("k1", "k2") f2.Flags["dom.flag"] = NewFlagFeatures("k1", "k2")
f2.Attributes["dom.attr"] = NewAttributeFeatures(map[string]string{"k1": "v1", "k2": "v2"}) f2.Attributes["dom.attr"] = NewAttributeFeatures(map[string]string{"k1": "v1", "k2": "v2"})
f2.Instances["dom.inst"] = NewInstanceFeatures([]InstanceFeature{ f2.Instances["dom.inst"] = NewInstanceFeatures(
*NewInstanceFeature(map[string]string{"a1": "v1.1", "a2": "v1.2"}), *NewInstanceFeature(map[string]string{"a1": "v1.1", "a2": "v1.2"}),
*NewInstanceFeature(map[string]string{"a1": "v2.1", "a2": "v2.2"}), *NewInstanceFeature(map[string]string{"a1": "v2.1", "a2": "v2.2"}),
}) )
f2.MergeInto(&f) f2.MergeInto(&f)
assert.Equal(t, f2, f) assert.Equal(t, f2, f)
f2.Flags["dom.flag"] = NewFlagFeatures("k3") f2.Flags["dom.flag"] = NewFlagFeatures("k3")
f2.Attributes["dom.attr"] = NewAttributeFeatures(map[string]string{"k1": "v1.override"}) f2.Attributes["dom.attr"] = NewAttributeFeatures(map[string]string{"k1": "v1.override"})
f2.Instances["dom.inst"] = NewInstanceFeatures([]InstanceFeature{ f2.Instances["dom.inst"] = NewInstanceFeatures(*NewInstanceFeature(map[string]string{"a1": "v3.1", "a3": "v3.3"}))
*NewInstanceFeature(map[string]string{"a1": "v3.1", "a3": "v3.3"}),
})
f2.MergeInto(&f) f2.MergeInto(&f)
expectedFeatures = *NewFeatures() expectedFeatures = *NewFeatures()
expectedFeatures.Flags["dom.flag"] = FlagFeatureSet{Elements: map[string]Nil{"k1": Nil{}, "k2": Nil{}, "k3": Nil{}}} expectedFeatures.Flags["dom.flag"] = FlagFeatureSet{Elements: map[string]Nil{"k1": Nil{}, "k2": Nil{}, "k3": Nil{}}}

View file

@ -70,7 +70,7 @@ func TestRule(t *testing.T) {
// Test empty feature sets // Test empty feature sets
f.Flags["domain-1.kf-1"] = nfdv1alpha1.NewFlagFeatures() f.Flags["domain-1.kf-1"] = nfdv1alpha1.NewFlagFeatures()
f.Attributes["domain-1.vf-1"] = nfdv1alpha1.NewAttributeFeatures(nil) f.Attributes["domain-1.vf-1"] = nfdv1alpha1.NewAttributeFeatures(nil)
f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures(nil) f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures()
m, err = Execute(r1, f) m, err = Execute(r1, f)
assert.Nilf(t, err, "unexpected error: %v", err) assert.Nilf(t, err, "unexpected error: %v", err)
@ -83,8 +83,7 @@ func TestRule(t *testing.T) {
// Test non-empty feature sets // Test non-empty feature sets
f.Flags["domain-1.kf-1"].Elements["key-x"] = nfdv1alpha1.Nil{} f.Flags["domain-1.kf-1"].Elements["key-x"] = nfdv1alpha1.Nil{}
f.Attributes["domain-1.vf-1"].Elements["key-1"] = "val-x" f.Attributes["domain-1.vf-1"].Elements["key-1"] = "val-x"
f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures([]nfdv1alpha1.InstanceFeature{ f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures(*nfdv1alpha1.NewInstanceFeature(map[string]string{"attr-1": "val-x"}))
*nfdv1alpha1.NewInstanceFeature(map[string]string{"attr-1": "val-x"})})
m, err = Execute(r1, f) m, err = Execute(r1, f)
assert.Nilf(t, err, "unexpected error: %v", err) assert.Nilf(t, err, "unexpected error: %v", err)

View file

@ -126,7 +126,7 @@ func (s *fakeSource) Discover() error {
for i, instanceAttributes := range s.config.InstanceFeatures { for i, instanceAttributes := range s.config.InstanceFeatures {
instances[i] = *nfdv1alpha1.NewInstanceFeature(instanceAttributes) instances[i] = *nfdv1alpha1.NewInstanceFeature(instanceAttributes)
} }
s.features.Instances[InstanceFeature] = nfdv1alpha1.NewInstanceFeatures(instances) s.features.Instances[InstanceFeature] = nfdv1alpha1.NewInstanceFeatures(instances...)
klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features)) klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))

View file

@ -143,7 +143,7 @@ func (s *pciSource) Discover() error {
if err != nil { if err != nil {
return fmt.Errorf("failed to detect PCI devices: %s", err.Error()) return fmt.Errorf("failed to detect PCI devices: %s", err.Error())
} }
s.features.Instances[DeviceFeature] = nfdv1alpha1.NewInstanceFeatures(devs) s.features.Instances[DeviceFeature] = nfdv1alpha1.NewInstanceFeatures(devs...)
klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features)) klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))

View file

@ -142,7 +142,7 @@ func (s *usbSource) Discover() error {
if err != nil { if err != nil {
return fmt.Errorf("failed to detect USB devices: %s", err.Error()) return fmt.Errorf("failed to detect USB devices: %s", err.Error())
} }
s.features.Instances[DeviceFeature] = nfdv1alpha1.NewInstanceFeatures(devs) s.features.Instances[DeviceFeature] = nfdv1alpha1.NewInstanceFeatures(devs...)
klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features)) klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))