• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

k8snetworkplumbingwg / sriov-network-operator / 3751025296

pending completion
3751025296

Pull #365

github

GitHub
Merge 421284b55 into 788d76f7e
Pull Request #365: Implementation for new systemd configuration method

958 of 958 new or added lines in 18 files covered. (100.0%)

1971 of 8330 relevant lines covered (23.66%)

0.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/plugins/virtual/virtual_plugin.go
1
package virtual
2

3
import (
4
        "reflect"
5

6
        "github.com/golang/glog"
7
        sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
8
        constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
9
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host"
10
        plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins"
11
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
12
)
13

14
var PluginName = "virtual_plugin"
15

16
// VirtualPlugin Plugin type to use on a virtual platform
17
type VirtualPlugin struct {
18
        PluginName     string
19
        SpecVersion    string
20
        DesireState    *sriovnetworkv1.SriovNetworkNodeState
21
        LastState      *sriovnetworkv1.SriovNetworkNodeState
22
        LoadVfioDriver uint
23
        RunningOnHost  bool
24
        HostManager    host.HostManagerInterface
25
}
26

27
const (
28
        unloaded = iota
29
        loading
30
        loaded
31
)
32

33
// Initialize our plugin and set up initial values
34
func NewVirtualPlugin(runningOnHost bool) (plugin.VendorPlugin, error) {
×
35
        return &VirtualPlugin{
×
36
                PluginName:     PluginName,
×
37
                SpecVersion:    "1.0",
×
38
                LoadVfioDriver: unloaded,
×
39
                RunningOnHost:  runningOnHost,
×
40
                HostManager:    host.NewHostManager(runningOnHost),
×
41
        }, nil
×
42
}
×
43

44
// Name returns the name of the plugin
45
func (p *VirtualPlugin) Name() string {
×
46
        return p.PluginName
×
47
}
×
48

49
// Spec returns the version of the spec expected by the plugin
50
func (p *VirtualPlugin) Spec() string {
×
51
        return p.SpecVersion
×
52
}
×
53

54
// OnNodeStateChange Invoked when SriovNetworkNodeState CR is created or updated, return if need dain and/or reboot node
55
func (p *VirtualPlugin) OnNodeStateChange(new *sriovnetworkv1.SriovNetworkNodeState) (needDrain bool, needReboot bool, err error) {
×
56
        glog.Info("virtual-plugin OnNodeStateChange()")
×
57
        needDrain = false
×
58
        needReboot = false
×
59
        err = nil
×
60
        p.DesireState = new
×
61

×
62
        if p.LoadVfioDriver != loaded {
×
63
                if needVfioDriver(new) {
×
64
                        p.LoadVfioDriver = loading
×
65
                }
×
66
        }
67

68
        return
×
69
}
70

71
// Apply config change
72
func (p *VirtualPlugin) Apply() error {
×
73
        glog.Infof("virtual-plugin Apply(): desiredState=%v", p.DesireState.Spec)
×
74

×
75
        if p.LoadVfioDriver == loading {
×
76
                // In virtual deployments of Kubernetes where the underlying virtualization platform does not support a virtualized iommu
×
77
                // the VFIO PCI driver needs to be loaded with a special flag.
×
78
                // This is the case for OpenStack deployments where the underlying virtualization platform is KVM.
×
79
                // NOTE: if VFIO was already loaded for some reason, we will not try to load it again with the new options.
×
80
                kernelArgs := "enable_unsafe_noiommu_mode=1"
×
81
                if err := p.HostManager.LoadKernelModule("vfio", kernelArgs); err != nil {
×
82
                        glog.Errorf("virtual-plugin Apply(): fail to load vfio kmod: %v", err)
×
83
                        return err
×
84
                }
×
85

86
                if err := p.HostManager.LoadKernelModule("vfio_pci"); err != nil {
×
87
                        glog.Errorf("virtual-plugin Apply(): fail to load vfio_pci kmod: %v", err)
×
88
                        return err
×
89
                }
×
90
                p.LoadVfioDriver = loaded
×
91
        }
92

93
        if p.LastState != nil {
×
94
                glog.Infof("virtual-plugin Apply(): lastStat=%v", p.LastState.Spec)
×
95
                if reflect.DeepEqual(p.LastState.Spec.Interfaces, p.DesireState.Spec.Interfaces) {
×
96
                        glog.Info("virtual-plugin Apply(): nothing to apply")
×
97
                        return nil
×
98
                }
×
99
        }
100
        exit, err := utils.Chroot("/host")
×
101
        if err != nil {
×
102
                return err
×
103
        }
×
104
        defer exit()
×
105
        if err := utils.SyncNodeStateVirtual(p.DesireState); err != nil {
×
106
                return err
×
107
        }
×
108
        p.LastState = &sriovnetworkv1.SriovNetworkNodeState{}
×
109
        *p.LastState = *p.DesireState
×
110

×
111
        return nil
×
112
}
113

114
func (p *VirtualPlugin) SetSystemdFlag() {
×
115
}
×
116

117
func (p *VirtualPlugin) IsSystemService() bool {
×
118
        return false
×
119
}
×
120

121
func needVfioDriver(state *sriovnetworkv1.SriovNetworkNodeState) bool {
×
122
        for _, iface := range state.Spec.Interfaces {
×
123
                for i := range iface.VfGroups {
×
124
                        if iface.VfGroups[i].DeviceType == constants.DeviceTypeVfioPci {
×
125
                                return true
×
126
                        }
×
127
                }
128
        }
129
        return false
×
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc