Skip to content

Commit

Permalink
Make drivers.yaml file optional
Browse files Browse the repository at this point in the history
Change the path to the `drivers.yaml` from a constant to a
user-definable parameter `path.supported-drivers-version-db`.
If the user does not specify any value for the file path, then every
ethernet driver is considered good to use.

Signed-off-by: Andrea Panattoni <[email protected]>
  • Loading branch information
zeeke committed Apr 3, 2024
1 parent 641b7b1 commit 341f74b
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 11 deletions.
1 change: 1 addition & 0 deletions collectors/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func ResolveFilepaths() error {
resolveSriovDevFilepaths,
resolveKubePodCPUFilepaths,
resolveKubePodDeviceFilepaths,
resolveSupportedDriverVersionDbPath,
}

for _, resolveFunc := range resolveFuncs {
Expand Down
19 changes: 16 additions & 3 deletions collectors/sriovdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ import (

const (
noNumaInfo = "-1"
supportedDriversDbPath = "/etc/sriov-network-metrics-exporter/drivers.yaml"
)

var (
collectorPriority utils.StringListFlag
defaultPriority = utils.StringListFlag{"sysfs", "netlink"}
sysBusPci = flag.String("path.sysbuspci", "/sys/bus/pci/devices", "Path to sys/bus/pci/devices/ on host")
sysClassNet = flag.String("path.sysclassnet", "/sys/class/net", "Path to sys/class/net/ on host")
supportedDriversDbPath = flag.String("path.supported-drivers-version-db", "", "Path to a yaml file which describes the supported version for each driver")
pfNameFile = "net"
netClassFile = "class"
netClass int64 = 0x020000
vfStatsSubsystem = "vf"
vfStatsCollectorName = "vfstats"

supportedDrivers drvinfo.SupportedDrivers

devfs fs.FS
Expand All @@ -47,10 +46,24 @@ type vfsPCIAddr map[string]string
// init runs the registration for this collector on package import
func init() {
flag.Var(&collectorPriority, "collector.vfstatspriority", "Priority of collectors")
supportedDrivers = drvinfo.NewSupportedDrivers(supportedDriversDbPath)

register(vfStatsCollectorName, enabled, createSriovDevCollector)
}

func resolveSupportedDriverVersionDbPath() error {
if *supportedDriversDbPath == "" {
supportedDrivers = drvinfo.NewStubSupportedDrivers()
return nil
}

if err := utils.ResolveFlag("path.supported-drivers-version-db", supportedDriversDbPath); err != nil {
return err
}

supportedDrivers = drvinfo.NewSupportedDrivers(*supportedDriversDbPath)
return nil
}

// This is the generic collector for VF stats.
type sriovDevCollector struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion collectors/sriovdev_readers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = DescribeTable("test getting stats reader for pf", // getStatsReader
}

// TODO: replace with fstest.MapFS entry
supportedDrivers = drvinfo.SupportedDrivers{Drivers: drvinfo.DriversList{Drivers: []drvinfo.DriverInfo{*driver}}, DbFilePath: ""}
supportedDrivers = &drvinfo.SupportedDriversDbFile{Drivers: drvinfo.DriversList{Drivers: []drvinfo.DriverInfo{*driver}}, DbFilePath: ""}

statsReader := getStatsReader(pf, priority)

Expand Down
51 changes: 47 additions & 4 deletions collectors/sriovdev_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package collectors

import (
"flag"
"fmt"
"io/fs"
"net"
"testing/fstest"

"github.com/k8snetworkplumbingwg/sriov-network-metrics-exporter/pkg/drvinfo"
"github.com/k8snetworkplumbingwg/sriov-network-metrics-exporter/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-metrics-exporter/pkg/vfstats"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -32,7 +34,7 @@ var _ = DescribeTable("test vf stats collection", // Collect
}

// TODO: replace with fstest.MapFS entry
supportedDrivers = drvinfo.SupportedDrivers{Drivers: drvinfo.DriversList{Drivers: []drvinfo.DriverInfo{*driver}}, DbFilePath: ""}
supportedDrivers = &drvinfo.SupportedDriversDbFile{Drivers: drvinfo.DriversList{Drivers: []drvinfo.DriverInfo{*driver}}, DbFilePath: ""}

vfstats.GetLink = func(name string) (netlink.Link, error) {
return &link, nil
Expand Down Expand Up @@ -220,9 +222,8 @@ var _ = DescribeTable("test getting sriov devices from filesystem", // getSriovD
drvinfo.GetDriverInfo = func(name string) (*drvinfo.DriverInfo, error) {
return driver, nil
}

// TODO: replace with fstest.MapFS entry
supportedDrivers = drvinfo.SupportedDrivers{Drivers: drvinfo.DriversList{Drivers: []drvinfo.DriverInfo{*driver}}, DbFilePath: ""}
supportedDrivers = &drvinfo.SupportedDriversDbFile{Drivers: drvinfo.DriversList{Drivers: []drvinfo.DriverInfo{*driver}}, DbFilePath: ""}

devs := getSriovDevAddrs()
Expect(devs).To(Equal(expected))
Expand Down Expand Up @@ -267,7 +268,7 @@ var _ = DescribeTable("test getting sriov dev details", // getSriovDev
}

// TODO: replace with fstest.MapFS entry
supportedDrivers = drvinfo.SupportedDrivers{Drivers: drvinfo.DriversList{Drivers: []drvinfo.DriverInfo{*driver}}, DbFilePath: ""}
supportedDrivers = &drvinfo.SupportedDriversDbFile{Drivers: drvinfo.DriversList{Drivers: []drvinfo.DriverInfo{*driver}}, DbFilePath: ""}

sriovDev := getSriovDev(dev, priority)
Expect(sriovDev).To(Equal(expected))
Expand Down Expand Up @@ -461,3 +462,45 @@ var _ = DescribeTable("test getting pf name from pci address on filesystem", //
"0000:3e:00.0 - could not get pf interface name in path '0000:3e:00.0/net'",
"open 0000:3e:00.0/net: file does not exist"),
)

var _ = Describe("Parameter `path.supported-drivers-version-db`", func() {
BeforeEach(func() {
tmp := utils.EvalSymlinks
utils.EvalSymlinks = func(x string) (string, error) { return x, nil }

DeferCleanup(func() {
utils.EvalSymlinks = tmp
})
})

It("is honored", func() {
flag.Set("path.supported-drivers-version-db", "./testdata/supported-drivers-version-db.yaml")

Check failure on line 477 in collectors/sriovdev_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

Error return value of `flag.Set` is not checked (errcheck)
DeferCleanup(func() {
flag.Set("path.supported-drivers-version-db", "")

Check failure on line 479 in collectors/sriovdev_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

Error return value of `flag.Set` is not checked (errcheck)
})

err := resolveSupportedDriverVersionDbPath()
Expect(err).ToNot(HaveOccurred())

Expect(
supportedDrivers.IsDriverSupported(&drvinfo.DriverInfo{Name: "ice", Version: "1.9.11"}),
).To(BeTrue())

Expect(
supportedDrivers.IsDriverSupported(&drvinfo.DriverInfo{Name: "ice", Version: "1.9.10"}),
).To(BeFalse())
})

It("creates a stub supported drivers when is not set", func() {
err := resolveSupportedDriverVersionDbPath()
Expect(err).ToNot(HaveOccurred())

Expect(
supportedDrivers.IsDriverSupported(&drvinfo.DriverInfo{Name: "ice", Version: "1.9.11"}),
).To(BeTrue())

Expect(
supportedDrivers.IsDriverSupported(&drvinfo.DriverInfo{Name: "anything", Version: "1.2.3"}),
).To(BeTrue())
})
})
5 changes: 5 additions & 0 deletions collectors/testdata/supported-drivers-version-db.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
drivers:
- name: ice
version: 1.9.11
- name: mlx5_core
version: 5.15.0-53-generic
1 change: 1 addition & 0 deletions deployment/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ spec:
- --path.kubeletsocket=/host/kubelet.sock
- --collector.kubepoddevice=true
- --collector.vfstatspriority=sysfs,netlink
- --path.supported-drivers-version-db=/etc/sriov-network-metrics-exporter/drivers.yaml
image: localhost:5000/sriov-metrics-exporter
imagePullPolicy: Always
name: sriov-metrics-exporter
Expand Down
20 changes: 17 additions & 3 deletions pkg/drvinfo/drvinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ type DriversList struct {
Drivers []DriverInfo
}

type SupportedDrivers struct {
type SupportedDrivers interface {
IsDriverSupported(drv *DriverInfo) bool
}

type SupportedDriversDbFile struct {
Drivers DriversList
DbFilePath string
}

var NewSupportedDrivers = func(fp string) SupportedDrivers {
retv := SupportedDrivers{}
retv := &SupportedDriversDbFile{}
supportedDrivers, err := readSupportedDrivers(fp)
if err != nil {
log.Printf("fetching supported drivers list from %s failed with error %v", fp, err)
Expand All @@ -65,7 +69,7 @@ var GetDriverInfo = func(name string) (*DriverInfo, error) {
}, nil
}

func (dl *SupportedDrivers) IsDriverSupported(drv *DriverInfo) bool {
func (dl *SupportedDriversDbFile) IsDriverSupported(drv *DriverInfo) bool {
for _, d := range dl.Drivers.Drivers {
if d.Name != drv.Name {
continue
Expand Down Expand Up @@ -103,3 +107,13 @@ func readSupportedDrivers(filePath string) (*DriversList, error) {
}
return drivers, nil
}

func NewStubSupportedDrivers() SupportedDrivers {
return &StubSupportedDrivers{}
}

type StubSupportedDrivers struct {}

func (ssp *StubSupportedDrivers) IsDriverSupported(drv *DriverInfo) bool {
return true
}

0 comments on commit 341f74b

Please sign in to comment.