• 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

18.28
/pkg/utils/sriov.go
1
package utils
2

3
import (
4
        "bytes"
5
        "encoding/json"
6
        "fmt"
7
        "io/ioutil"
8
        "os"
9

10
        "github.com/golang/glog"
11

12
        sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
13
)
14

15
const (
16
        SriovConfBasePath          = "/etc/sriov-operator"
17
        HostSriovConfBasePath      = "/host" + SriovConfBasePath
18
        SriovSwitchDevConfPath     = SriovConfBasePath + "/sriov_config.json"
19
        SriovHostSwitchDevConfPath = "/host" + SriovSwitchDevConfPath
20
)
21

22
type config struct {
23
        Interfaces []sriovnetworkv1.Interface `json:"interfaces"`
24
}
25

26
func IsSwitchdevModeSpec(spec sriovnetworkv1.SriovNetworkNodeStateSpec) bool {
×
27
        for _, iface := range spec.Interfaces {
×
28
                if iface.EswitchMode == sriovnetworkv1.ESwithModeSwitchDev {
×
29
                        return true
×
30
                }
×
31
        }
32
        return false
×
33
}
34

35
func findInterface(interfaces sriovnetworkv1.Interfaces, name string) (iface sriovnetworkv1.Interface, err error) {
×
36
        for _, i := range interfaces {
×
37
                if i.Name == name {
×
38
                        return i, nil
×
39
                }
×
40
        }
41
        return sriovnetworkv1.Interface{}, fmt.Errorf("unable to find interface: %v", name)
×
42
}
43

44
func WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (update bool, err error) {
1✔
45
        // Create a map with all the PFs we will need to SKIP for systemd configuration
1✔
46
        pfsToSkip, err := GetPfsToSkip(newState)
1✔
47
        if err != nil {
1✔
48
                return false, err
×
49
        }
×
50
        cfg := config{}
1✔
51
        for _, iface := range newState.Spec.Interfaces {
2✔
52
                for _, ifaceStatus := range newState.Status.Interfaces {
2✔
53
                        if iface.PciAddress != ifaceStatus.PciAddress {
1✔
54
                                continue
×
55
                        }
56

57
                        if skip := pfsToSkip[iface.PciAddress]; !skip {
2✔
58
                                continue
1✔
59
                        }
60

61
                        i := sriovnetworkv1.Interface{}
×
62
                        if iface.NumVfs > 0 {
×
63
                                var vfGroups []sriovnetworkv1.VfGroup = nil
×
64
                                ifc, err := findInterface(newState.Spec.Interfaces, iface.Name)
×
65
                                if err != nil {
×
66
                                        glog.Errorf("WriteSwitchdevConfFile(): fail find interface: %v", err)
×
67
                                } else {
×
68
                                        vfGroups = ifc.VfGroups
×
69
                                }
×
70
                                i = sriovnetworkv1.Interface{
×
71
                                        // Not passing all the contents, since only NumVfs and EswitchMode can be configured by configure-switchdev.sh currently.
×
72
                                        Name:       iface.Name,
×
73
                                        PciAddress: iface.PciAddress,
×
74
                                        NumVfs:     iface.NumVfs,
×
75
                                        Mtu:        iface.Mtu,
×
76
                                        VfGroups:   vfGroups,
×
77
                                }
×
78

×
79
                                if iface.EswitchMode == sriovnetworkv1.ESwithModeSwitchDev {
×
80
                                        i.EswitchMode = iface.EswitchMode
×
81
                                }
×
82
                                cfg.Interfaces = append(cfg.Interfaces, i)
×
83
                        }
84
                }
85
        }
86
        _, err = os.Stat(SriovHostSwitchDevConfPath)
1✔
87
        if err != nil {
2✔
88
                if os.IsNotExist(err) {
2✔
89
                        if len(cfg.Interfaces) == 0 {
2✔
90
                                err = nil
1✔
91
                                return
1✔
92
                        }
1✔
93

94
                        // Create the sriov-operator folder on the host if it doesn't exist
95
                        if _, err := os.Stat("/host" + SriovConfBasePath); os.IsNotExist(err) {
×
96
                                err = os.Mkdir("/host"+SriovConfBasePath, os.ModeDir)
×
97
                                if err != nil {
×
98
                                        glog.Errorf("WriteConfFile(): fail to create sriov-operator folder: %v", err)
×
99
                                        return false, err
×
100
                                }
×
101
                        }
102

103
                        glog.V(2).Infof("WriteSwitchdevConfFile(): file not existed, create it")
×
104
                        _, err = os.Create(SriovHostSwitchDevConfPath)
×
105
                        if err != nil {
×
106
                                glog.Errorf("WriteSwitchdevConfFile(): fail to create file: %v", err)
×
107
                                return
×
108
                        }
×
109
                } else {
×
110
                        return
×
111
                }
×
112
        }
113
        oldContent, err := ioutil.ReadFile(SriovHostSwitchDevConfPath)
×
114
        if err != nil {
×
115
                glog.Errorf("WriteSwitchdevConfFile(): fail to read file: %v", err)
×
116
                return
×
117
        }
×
118
        var newContent []byte
×
119
        if len(cfg.Interfaces) != 0 {
×
120
                newContent, err = json.Marshal(cfg)
×
121
                if err != nil {
×
122
                        glog.Errorf("WriteSwitchdevConfFile(): fail to marshal config: %v", err)
×
123
                        return
×
124
                }
×
125
        }
126

127
        if bytes.Equal(newContent, oldContent) {
×
128
                glog.V(2).Info("WriteSwitchdevConfFile(): no update")
×
129
                return
×
130
        }
×
131
        update = true
×
132
        glog.V(2).Infof("WriteSwitchdevConfFile(): write '%s' to switchdev.conf", newContent)
×
133
        err = ioutil.WriteFile(SriovHostSwitchDevConfPath, newContent, 0644)
×
134
        if err != nil {
×
135
                glog.Errorf("WriteSwitchdevConfFile(): fail to write file: %v", err)
×
136
                return
×
137
        }
×
138
        return
×
139
}
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