• 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

6.45
/pkg/systemd/systemd.go
1
package systemd
2

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

10
        "github.com/golang/glog"
11
        "gopkg.in/yaml.v3"
12

13
        sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
14
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
15
)
16

17
const (
18
        SriovSystemdConfigPath        = utils.SriovConfBasePath + "/sriov-interface-config.yaml"
19
        SriovSystemdResultPath        = utils.SriovConfBasePath + "/sriov-interface-result.yaml"
20
        sriovSystemdSupportedNicPath  = utils.SriovConfBasePath + "/sriov-supported-nics-ids.yaml"
21
        sriovSystemdServiceBinaryPath = "/var/lib/sriov/sriov-network-config-daemon"
22

23
        SriovHostSystemdConfigPath        = "/host" + SriovSystemdConfigPath
24
        SriovHostSystemdResultPath        = "/host" + SriovSystemdResultPath
25
        sriovHostSystemdSupportedNicPath  = "/host" + sriovSystemdSupportedNicPath
26
        sriovHostSystemdServiceBinaryPath = "/host" + sriovSystemdServiceBinaryPath
27

28
        SriovServicePath     = "/etc/systemd/system/sriov-config.service"
29
        SriovHostServicePath = "/host" + SriovServicePath
30
)
31

32
type SriovConfig struct {
33
        Spec            sriovnetworkv1.SriovNetworkNodeStateSpec `yaml:"spec"`
34
        UnsupportedNics bool                                     `yaml:"unsupportedNics"`
35
        PlatformType    utils.PlatformType                       `yaml:"platformType"`
36
}
37

38
type SriovResult struct {
39
        SyncStatus    string `yaml:"syncStatus"`
40
        LastSyncError string `yaml:"lastSyncError"`
41
}
42

43
func ReadConfFile() (spec *SriovConfig, err error) {
×
44
        rawConfig, err := ioutil.ReadFile(SriovSystemdConfigPath)
×
45
        if err != nil {
×
46
                return nil, err
×
47
        }
×
48

49
        err = yaml.Unmarshal(rawConfig, &spec)
×
50

×
51
        return spec, err
×
52
}
53

54
func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState, unsupportedNics bool, platformType utils.PlatformType) (bool, error) {
×
55
        newFile := false
×
56
        // remove the device plugin revision as we don't need it here
×
57
        newState.Spec.DpConfigVersion = ""
×
58

×
59
        sriovConfig := &SriovConfig{
×
60
                newState.Spec,
×
61
                unsupportedNics,
×
62
                platformType,
×
63
        }
×
64

×
65
        _, err := os.Stat(SriovHostSystemdConfigPath)
×
66
        if err != nil {
×
67
                if os.IsNotExist(err) {
×
68
                        // Create the sriov-operator folder on the host if it doesn't exist
×
69
                        if _, err := os.Stat(utils.HostSriovConfBasePath); os.IsNotExist(err) {
×
70
                                err = os.Mkdir(utils.HostSriovConfBasePath, os.ModeDir)
×
71
                                if err != nil {
×
72
                                        glog.Errorf("WriteConfFile(): fail to create sriov-operator folder: %v", err)
×
73
                                        return false, err
×
74
                                }
×
75
                        }
76

77
                        glog.V(2).Infof("WriteConfFile(): file not existed, create it")
×
78
                        _, err = os.Create(SriovHostSystemdConfigPath)
×
79
                        if err != nil {
×
80
                                glog.Errorf("WriteConfFile(): fail to create file: %v", err)
×
81
                                return false, err
×
82
                        }
×
83
                        newFile = true
×
84
                } else {
×
85
                        return false, err
×
86
                }
×
87
        }
88

89
        oldContent, err := ioutil.ReadFile(SriovHostSystemdConfigPath)
×
90
        if err != nil {
×
91
                glog.Errorf("WriteConfFile(): fail to read file: %v", err)
×
92
                return false, err
×
93
        }
×
94

95
        oldContentObj := &SriovConfig{}
×
96
        err = yaml.Unmarshal(oldContent, oldContentObj)
×
97
        if err != nil {
×
98
                glog.Errorf("WriteConfFile(): fail to unmarshal old file: %v", err)
×
99
                return false, err
×
100
        }
×
101

102
        var newContent []byte
×
103
        newContent, err = yaml.Marshal(sriovConfig)
×
104
        if err != nil {
×
105
                glog.Errorf("WriteConfFile(): fail to marshal config: %v", err)
×
106
                return false, err
×
107
        }
×
108

109
        if bytes.Equal(newContent, oldContent) {
×
110
                glog.V(2).Info("WriteConfFile(): no update")
×
111
                return false, nil
×
112
        }
×
113
        glog.V(2).Infof("WriteConfFile(): previews configuration is not equal: old config:\n%s\nnew config:\n%s\n", string(oldContent), string(newContent))
×
114

×
115
        // this will be used to mark the first time we create this file.
×
116
        // this helps to avoid the first reboot after installation
×
117
        if newFile && len(sriovConfig.Spec.Interfaces) == 0 {
×
118
                glog.V(2).Info("WriteConfFile(): first file creation and no interfaces to configure")
×
119
                return false, nil
×
120
        }
×
121

122
        glog.V(2).Infof("WriteConfFile(): write '%s' to %s", newContent, SriovHostSystemdConfigPath)
×
123
        err = ioutil.WriteFile(SriovHostSystemdConfigPath, newContent, 0644)
×
124
        if err != nil {
×
125
                glog.Errorf("WriteConfFile(): fail to write file: %v", err)
×
126
                return false, err
×
127
        }
×
128

129
        return true, nil
×
130
}
131

132
func WriteSriovResult(result *SriovResult) error {
×
133
        _, err := os.Stat(SriovSystemdResultPath)
×
134
        if err != nil {
×
135
                if os.IsNotExist(err) {
×
136
                        glog.V(2).Infof("WriteSriovResult(): file not existed, create it")
×
137
                        _, err = os.Create(SriovSystemdResultPath)
×
138
                        if err != nil {
×
139
                                glog.Errorf("WriteSriovResult(): failed to create sriov result file on path %s: %v", SriovSystemdResultPath, err)
×
140
                                return err
×
141
                        }
×
142
                } else {
×
143
                        glog.Errorf("WriteSriovResult(): failed to check sriov result file on path %s: %v", SriovSystemdResultPath, err)
×
144
                        return err
×
145
                }
×
146
        }
147

148
        out, err := yaml.Marshal(result)
×
149
        if err != nil {
×
150
                glog.Errorf("WriteSriovResult(): failed to marshal sriov result file: %v", err)
×
151
                return err
×
152
        }
×
153

154
        glog.V(2).Infof("WriteSriovResult(): write '%s' to %s", string(out), SriovSystemdResultPath)
×
155
        err = ioutil.WriteFile(SriovSystemdResultPath, out, 0644)
×
156
        if err != nil {
×
157
                glog.Errorf("WriteSriovResult(): failed to write sriov result file on path %s: %v", SriovSystemdResultPath, err)
×
158
                return err
×
159
        }
×
160

161
        return nil
×
162
}
163

164
func ReadSriovResult() (*SriovResult, error) {
×
165
        _, err := os.Stat(SriovHostSystemdResultPath)
×
166
        if err != nil {
×
167
                if os.IsNotExist(err) {
×
168
                        glog.V(2).Infof("ReadSriovResult(): file not existed, return empty result")
×
169
                        return &SriovResult{}, err
×
170
                } else {
×
171
                        glog.Errorf("ReadSriovResult(): failed to check sriov result file on path %s: %v", SriovHostSystemdResultPath, err)
×
172
                        return nil, err
×
173
                }
×
174
        }
175

176
        rawConfig, err := ioutil.ReadFile(SriovHostSystemdResultPath)
×
177
        if err != nil {
×
178
                glog.Errorf("ReadSriovResult(): failed to read sriov result file on path %s: %v", SriovHostSystemdResultPath, err)
×
179
                return nil, err
×
180
        }
×
181

182
        result := &SriovResult{}
×
183
        err = yaml.Unmarshal(rawConfig, &result)
×
184
        if err != nil {
×
185
                glog.Errorf("ReadSriovResult(): failed to unmarshal sriov result file on path %s: %v", SriovHostSystemdResultPath, err)
×
186
                return nil, err
×
187
        }
×
188
        return result, err
×
189
}
190

191
func WriteSriovSupportedNics() error {
×
192
        _, err := os.Stat(sriovHostSystemdSupportedNicPath)
×
193
        if err != nil {
×
194
                if os.IsNotExist(err) {
×
195
                        glog.V(2).Infof("WriteSriovSupportedNics(): file not existed, create it")
×
196
                        _, err = os.Create(sriovHostSystemdSupportedNicPath)
×
197
                        if err != nil {
×
198
                                glog.Errorf("WriteSriovSupportedNics(): failed to create sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err)
×
199
                                return err
×
200
                        }
×
201
                } else {
×
202
                        glog.Errorf("WriteSriovSupportedNics(): failed to check sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err)
×
203
                        return err
×
204
                }
×
205
        }
206

207
        rawNicList := []byte{}
×
208
        for _, line := range sriovnetworkv1.NicIDMap {
×
209
                rawNicList = append(rawNicList, []byte(fmt.Sprintf("%s\n", line))...)
×
210
        }
×
211

212
        err = ioutil.WriteFile(sriovHostSystemdSupportedNicPath, rawNicList, 0644)
×
213
        if err != nil {
×
214
                glog.Errorf("WriteSriovSupportedNics(): failed to write sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err)
×
215
                return err
×
216
        }
×
217

218
        return nil
×
219
}
220

221
func ReadSriovSupportedNics() ([]string, error) {
×
222
        _, err := os.Stat(sriovSystemdSupportedNicPath)
×
223
        if err != nil {
×
224
                if os.IsNotExist(err) {
×
225
                        glog.V(2).Infof("ReadSriovSupportedNics(): file not existed, return empty result")
×
226
                        return nil, err
×
227
                } else {
×
228
                        glog.Errorf("ReadSriovSupportedNics(): failed to check sriov supporter nics file on path %s: %v", sriovSystemdSupportedNicPath, err)
×
229
                        return nil, err
×
230
                }
×
231
        }
232

233
        rawConfig, err := ioutil.ReadFile(sriovSystemdSupportedNicPath)
×
234
        if err != nil {
×
235
                glog.Errorf("ReadSriovSupportedNics(): failed to read sriov supporter nics file on path %s: %v", sriovSystemdSupportedNicPath, err)
×
236
                return nil, err
×
237
        }
×
238

239
        lines := strings.Split(string(rawConfig), "\n")
×
240
        return lines, nil
×
241
}
242

243
func CleanSriovFilesFromHost() error {
1✔
244
        err := os.Remove(SriovHostSystemdConfigPath)
1✔
245
        if err != nil && !os.IsNotExist(err) {
1✔
246
                return err
×
247
        }
×
248

249
        err = os.Remove(SriovHostSystemdResultPath)
1✔
250
        if err != nil && !os.IsNotExist(err) {
1✔
251
                return err
×
252
        }
×
253

254
        err = os.Remove(sriovHostSystemdSupportedNicPath)
1✔
255
        if err != nil && !os.IsNotExist(err) {
1✔
256
                return err
×
257
        }
×
258

259
        err = os.Remove(sriovHostSystemdServiceBinaryPath)
1✔
260
        if err != nil && !os.IsNotExist(err) {
1✔
261
                return err
×
262
        }
×
263

264
        err = os.Remove(SriovHostServicePath)
1✔
265
        if err != nil && !os.IsNotExist(err) {
1✔
266
                return err
×
267
        }
×
268

269
        return nil
1✔
270
}
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