Repository: kubernetes-sigs/node-feature-discovery
Pull request #1669 (2024-04-23 07:30:37)

api/nfd: use varargs in the NewInstanceFeatures helper
Make usage of this helper function more flexible.


Patch:
diff --git a/api/nfd/v1alpha1/feature.go b/api/nfd/v1alpha1/feature.go
index a3d066bd4..22915378a 100644
--- a/api/nfd/v1alpha1/feature.go
+++ b/api/nfd/v1alpha1/feature.go
@@ -54,7 +54,7 @@ func NewAttributeFeatures(values map[string]string) AttributeFeatureSet {
 }
 
 // NewInstanceFeatures creates a new instance of InstanceFeatureSet.
-func NewInstanceFeatures(instances []InstanceFeature) InstanceFeatureSet {
+func NewInstanceFeatures(instances ...InstanceFeature) InstanceFeatureSet {
 	return InstanceFeatureSet{Elements: instances}
 }
 
diff --git a/source/fake/fake.go b/source/fake/fake.go
index 247e36bf2..573fdb862 100644
--- a/source/fake/fake.go
+++ b/source/fake/fake.go
@@ -126,7 +126,7 @@ func (s *fakeSource) Discover() error {
 	for i, instanceAttributes := range s.config.InstanceFeatures {
 		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))
 
diff --git a/source/pci/pci.go b/source/pci/pci.go
index 3e7870c03..e6c093b1c 100644
--- a/source/pci/pci.go
+++ b/source/pci/pci.go
@@ -143,7 +143,7 @@ func (s *pciSource) Discover() error {
 	if err != nil {
 		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))
 
diff --git a/source/usb/usb.go b/source/usb/usb.go
index f48fe5bcd..37a74d6dd 100644
--- a/source/usb/usb.go
+++ b/source/usb/usb.go
@@ -142,7 +142,7 @@ func (s *usbSource) Discover() error {
 	if err != nil {
 		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))
 


Test patch:
diff --git a/api/nfd/v1alpha1/feature_test.go b/api/nfd/v1alpha1/feature_test.go
index ae2511d27..b432d3a7f 100644
--- a/api/nfd/v1alpha1/feature_test.go
+++ b/api/nfd/v1alpha1/feature_test.go
@@ -84,22 +84,20 @@ func TestInstanceFeatureSet(t *testing.T) {
 	f2.MergeInto(&f1)
 	assert.Equal(t, expectedElems, f1.Elements)
 
-	f2 = NewInstanceFeatures([]InstanceFeature{})
+	f2 = NewInstanceFeatures()
 	expectedElems = []InstanceFeature{}
 	f2.MergeInto(&f1)
 	assert.Equal(t, expectedElems, f1.Elements)
 
-	f2 = NewInstanceFeatures([]InstanceFeature{InstanceFeature{}})
+	f2 = NewInstanceFeatures(InstanceFeature{})
 	expectedElems = append(expectedElems, InstanceFeature{})
 	f2.MergeInto(&f1)
 	assert.Equal(t, expectedElems, f1.Elements)
 
-	f2 = NewInstanceFeatures([]InstanceFeature{
-		InstanceFeature{
-			Attributes: map[string]string{
-				"a1": "v1",
-				"a2": "v2",
-			},
+	f2 = NewInstanceFeatures(InstanceFeature{
+		Attributes: 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.Flags["dom.flag"] = NewFlagFeatures("k1", "k2")
 	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": "v2.1", "a2": "v2.2"}),
-	})
+	)
 	f2.MergeInto(&f)
 	assert.Equal(t, f2, f)
 
 	f2.Flags["dom.flag"] = NewFlagFeatures("k3")
 	f2.Attributes["dom.attr"] = NewAttributeFeatures(map[string]string{"k1": "v1.override"})
-	f2.Instances["dom.inst"] = NewInstanceFeatures([]InstanceFeature{
-		*NewInstanceFeature(map[string]string{"a1": "v3.1", "a3": "v3.3"}),
-	})
+	f2.Instances["dom.inst"] = NewInstanceFeatures(*NewInstanceFeature(map[string]string{"a1": "v3.1", "a3": "v3.3"}))
 	f2.MergeInto(&f)
 	expectedFeatures = *NewFeatures()
 	expectedFeatures.Flags["dom.flag"] = FlagFeatureSet{Elements: map[string]Nil{"k1": Nil{}, "k2": Nil{}, "k3": Nil{}}}
diff --git a/pkg/apis/nfd/nodefeaturerule/rule_test.go b/pkg/apis/nfd/nodefeaturerule/rule_test.go
index ab52b7d24..773e72efe 100644
--- a/pkg/apis/nfd/nodefeaturerule/rule_test.go
+++ b/pkg/apis/nfd/nodefeaturerule/rule_test.go
@@ -70,7 +70,7 @@ func TestRule(t *testing.T) {
 	// Test empty feature sets
 	f.Flags["domain-1.kf-1"] = nfdv1alpha1.NewFlagFeatures()
 	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)
 	assert.Nilf(t, err, "unexpected error: %v", err)
@@ -83,8 +83,7 @@ func TestRule(t *testing.T) {
 	// Test non-empty feature sets
 	f.Flags["domain-1.kf-1"].Elements["key-x"] = nfdv1alpha1.Nil{}
 	f.Attributes["domain-1.vf-1"].Elements["key-1"] = "val-x"
-	f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures([]nfdv1alpha1.InstanceFeature{
-		*nfdv1alpha1.NewInstanceFeature(map[string]string{"attr-1": "val-x"})})
+	f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures(*nfdv1alpha1.NewInstanceFeature(map[string]string{"attr-1": "val-x"}))
 
 	m, err = Execute(r1, f)
 	assert.Nilf(t, err, "unexpected error: %v", err)