1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-05 08:17:04 +00:00

Merge pull request #1448 from AhmedGrati/feat-discover-virtual-network-interface

feat: discover virtual network interface
This commit is contained in:
Kubernetes Prow Robot 2023-12-05 17:32:06 +01:00 committed by GitHub
commit 7f859ce568
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 11 deletions

View file

@ -892,6 +892,9 @@ The following features are available for matching:
| **`network.device`** | instance | | | Physical (non-virtual) network interfaces present in the system | | **`network.device`** | instance | | | Physical (non-virtual) network interfaces present in the system |
| | | **`name`** | string | Name of the network interface | | | | **`name`** | string | Name of the network interface |
| | | **`<sysfs-attribute>`** | string | Sysfs network interface attribute, available attributes: `operstate`, `speed`, `sriov_numvfs`, `sriov_totalvfs` | | | | **`<sysfs-attribute>`** | string | Sysfs network interface attribute, available attributes: `operstate`, `speed`, `sriov_numvfs`, `sriov_totalvfs` |
| **`network.virtual`** | instance | | | Virtual network interfaces present in the system |
| | | **`name`** | string | Name of the network interface |
| | | **`<sysfs-attribute>`** | string | Sysfs network interface attribute, available attributes: `operstate` |
| **`pci.device`** | instance | | | PCI devices present in the system | | **`pci.device`** | instance | | | PCI devices present in the system |
| | | **`<sysfs-attribute>`** | string | Value of the sysfs device attribute, available attributes: `class`, `vendor`, `device`, `subsystem_vendor`, `subsystem_device`, `sriov_totalvfs`, `iommu_group/type`, `iommu/intel-iommu/version` | | | | **`<sysfs-attribute>`** | string | Value of the sysfs device attribute, available attributes: `class`, `vendor`, `device`, `subsystem_vendor`, `subsystem_device`, `sriov_totalvfs`, `iommu_group/type`, `iommu/intel-iommu/version` |
| **`storage.block`** | instance | | | Block storage devices present in the system | | **`storage.block`** | instance | | | Block storage devices present in the system |

View file

@ -34,7 +34,12 @@ import (
// Name of this feature source // Name of this feature source
const Name = "network" const Name = "network"
const DeviceFeature = "device" const (
// DeviceFeature exposes physical network devices
DeviceFeature = "device"
// VirtualFeature exposes features for network interfaces that are not attached to a physical device
VirtualFeature = "virtual"
)
const sysfsBaseDir = "class/net" const sysfsBaseDir = "class/net"
@ -51,8 +56,11 @@ var (
) )
var ( var (
// ifaceAttrs is the list of files under /sys/class/net/<iface> that we're reading // devIfaceAttrs is the list of files under /sys/class/net/<iface> that we're reading
ifaceAttrs = []string{"operstate", "speed", "device/sriov_numvfs", "device/sriov_totalvfs"} devIfaceAttrs = []string{"operstate", "speed", "device/sriov_numvfs", "device/sriov_totalvfs"}
// virtualIfaceAttrs is the list of files under /sys/class/net/<iface> that we're reading
virtualIfaceAttrs = []string{"operstate"}
) )
// Name returns an identifier string for this feature source. // Name returns an identifier string for this feature source.
@ -91,11 +99,12 @@ func (s *networkSource) GetLabels() (source.FeatureLabels, error) {
func (s *networkSource) Discover() error { func (s *networkSource) Discover() error {
s.features = nfdv1alpha1.NewFeatures() s.features = nfdv1alpha1.NewFeatures()
devs, err := detectNetDevices() devs, virts, err := detectNetDevices()
if err != nil { if err != nil {
return fmt.Errorf("failed to detect network devices: %w", err) return fmt.Errorf("failed to detect network devices: %w", err)
} }
s.features.Instances[DeviceFeature] = nfdv1alpha1.InstanceFeatureSet{Elements: devs} s.features.Instances[DeviceFeature] = nfdv1alpha1.InstanceFeatureSet{Elements: devs}
s.features.Instances[VirtualFeature] = nfdv1alpha1.InstanceFeatureSet{Elements: virts}
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))
@ -110,26 +119,28 @@ func (s *networkSource) GetFeatures() *nfdv1alpha1.Features {
return s.features return s.features
} }
func detectNetDevices() ([]nfdv1alpha1.InstanceFeature, error) { func detectNetDevices() ([]nfdv1alpha1.InstanceFeature, []nfdv1alpha1.InstanceFeature, error) {
sysfsBasePath := hostpath.SysfsDir.Path(sysfsBaseDir) sysfsBasePath := hostpath.SysfsDir.Path(sysfsBaseDir)
ifaces, err := os.ReadDir(sysfsBasePath) ifaces, err := os.ReadDir(sysfsBasePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to list network interfaces: %w", err) return nil, nil, fmt.Errorf("failed to list network interfaces: %w", err)
} }
// Iterate over devices // Iterate over devices
info := make([]nfdv1alpha1.InstanceFeature, 0, len(ifaces)) devIfacesinfo := make([]nfdv1alpha1.InstanceFeature, 0, len(ifaces))
virtualIfacesinfo := make([]nfdv1alpha1.InstanceFeature, 0, len(ifaces))
for _, iface := range ifaces { for _, iface := range ifaces {
name := iface.Name() name := iface.Name()
if _, err := os.Stat(filepath.Join(sysfsBasePath, name, "device")); err == nil { if _, err := os.Stat(filepath.Join(sysfsBasePath, name, "device")); err == nil {
info = append(info, readIfaceInfo(filepath.Join(sysfsBasePath, name), ifaceAttrs)) devIfacesinfo = append(devIfacesinfo, readIfaceInfo(filepath.Join(sysfsBasePath, name), devIfaceAttrs))
} else if klogV := klog.V(3); klogV.Enabled() { } else {
klogV.InfoS("skipping non-device iface", "interfaceName", name) virtualIfacesinfo = append(virtualIfacesinfo, readIfaceInfo(filepath.Join(sysfsBasePath, name), virtualIfaceAttrs))
} }
} }
return info, nil return devIfacesinfo, virtualIfacesinfo, nil
} }
func readIfaceInfo(path string, attrFiles []string) nfdv1alpha1.InstanceFeature { func readIfaceInfo(path string, attrFiles []string) nfdv1alpha1.InstanceFeature {