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

k8snetworkplumbingwg / sriov-network-operator / 4253702965

pending completion
4253702965

Pull #377

github

GitHub
Merge f01bcd9a4 into 331d1ae13
Pull Request #377: Vdpa introduction

36 of 36 new or added lines in 5 files covered. (100.0%)

1949 of 7564 relevant lines covered (25.77%)

0.29 hits per line

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

42.51
/api/v1/helper.go
1
package v1
2

3
import (
4
        "context"
5
        "encoding/json"
6
        "fmt"
7
        "os"
8
        "regexp"
9
        "sort"
10
        "strconv"
11
        "strings"
12

13
        corev1 "k8s.io/api/core/v1"
14
        "k8s.io/apimachinery/pkg/api/errors"
15
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
        uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
17
        "k8s.io/apimachinery/pkg/types"
18
        "k8s.io/client-go/kubernetes"
19
        "sigs.k8s.io/controller-runtime/pkg/client"
20
        logf "sigs.k8s.io/controller-runtime/pkg/log"
21

22
        netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
23
        render "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render"
24
)
25

26
const (
27
        LASTNETWORKNAMESPACE    = "operator.sriovnetwork.openshift.io/last-network-namespace"
28
        NETATTDEFFINALIZERNAME  = "netattdef.finalizers.sriovnetwork.openshift.io"
29
        POOLCONFIGFINALIZERNAME = "poolconfig.finalizers.sriovnetwork.openshift.io"
30
        ESwithModeLegacy        = "legacy"
31
        ESwithModeSwitchDev     = "switchdev"
32

33
        SriovCniStateEnable  = "enable"
34
        SriovCniStateDisable = "disable"
35
        SriovCniStateAuto    = "auto"
36
        SriovCniStateOff     = "off"
37
        SriovCniStateOn      = "on"
38
        SriovCniIpamEmpty    = "\"ipam\":{}"
39
)
40

41
const invalidVfIndex = -1
42

43
var ManifestsPath = "./bindata/manifests/cni-config"
44
var log = logf.Log.WithName("sriovnetwork")
45

46
// NicIDMap contains supported mapping of IDs with each in the format of:
47
// Vendor ID, Physical Function Device ID, Virtual Function Device ID
48
var NicIDMap = []string{}
49

50
// NetFilterType Represents the NetFilter tags to be used
51
type NetFilterType int
52

53
const (
54
        // OpenstackNetworkID network UUID
55
        OpenstackNetworkID NetFilterType = iota
56

57
        SupportedNicIDConfigmap = "supported-nic-ids"
58
)
59

60
func (e NetFilterType) String() string {
×
61
        switch e {
×
62
        case OpenstackNetworkID:
×
63
                return "openstack/NetworkID"
×
64
        default:
×
65
                return fmt.Sprintf("%d", int(e))
×
66
        }
67
}
68

69
func InitNicIDMap(client kubernetes.Interface, namespace string) error {
1✔
70
        cm, err := client.CoreV1().ConfigMaps(namespace).Get(
1✔
71
                context.Background(),
1✔
72
                SupportedNicIDConfigmap,
1✔
73
                metav1.GetOptions{},
1✔
74
        )
1✔
75
        // if the configmap does not exist, return false
1✔
76
        if err != nil {
1✔
77
                return err
×
78
        }
×
79
        for _, v := range cm.Data {
2✔
80
                NicIDMap = append(NicIDMap, v)
1✔
81
        }
1✔
82
        return nil
1✔
83
}
84

85
func IsSupportedVendor(vendorID string) bool {
1✔
86
        for _, n := range NicIDMap {
2✔
87
                ids := strings.Split(n, " ")
1✔
88
                if vendorID == ids[0] {
2✔
89
                        return true
1✔
90
                }
1✔
91
        }
92
        return false
1✔
93
}
94

95
func IsSupportedDevice(deviceID string) bool {
1✔
96
        for _, n := range NicIDMap {
2✔
97
                ids := strings.Split(n, " ")
1✔
98
                if deviceID == ids[1] {
1✔
99
                        return true
×
100
                }
×
101
        }
102
        return false
1✔
103
}
104

105
func IsSupportedModel(vendorID, deviceID string) bool {
1✔
106
        for _, n := range NicIDMap {
2✔
107
                ids := strings.Split(n, " ")
1✔
108
                if vendorID == ids[0] && deviceID == ids[1] {
2✔
109
                        return true
1✔
110
                }
1✔
111
        }
112
        log.Info("IsSupportedModel():", "Unsupported model:", "vendorId:", vendorID, "deviceId:", deviceID)
1✔
113
        return false
1✔
114
}
115

116
func IsVfSupportedModel(vendorID, deviceID string) bool {
1✔
117
        for _, n := range NicIDMap {
2✔
118
                ids := strings.Split(n, " ")
1✔
119
                if vendorID == ids[0] && deviceID == ids[2] {
2✔
120
                        return true
1✔
121
                }
1✔
122
        }
123
        log.Info("IsVfSupportedModel():", "Unsupported VF model:", "vendorId:", vendorID, "deviceId:", deviceID)
×
124
        return false
×
125
}
126

127
func IsEnabledUnsupportedVendor(vendorID string, unsupportedNicIDMap map[string]string) bool {
×
128
        for _, n := range unsupportedNicIDMap {
×
129
                if IsValidPciString(n) {
×
130
                        ids := strings.Split(n, " ")
×
131
                        if vendorID == ids[0] {
×
132
                                return true
×
133
                        }
×
134
                }
135
        }
136
        return false
×
137
}
138

139
func IsValidPciString(nicIDString string) bool {
×
140
        ids := strings.Split(nicIDString, " ")
×
141

×
142
        if len(ids) != 3 {
×
143
                log.Info("IsValidPciString(): ", nicIDString)
×
144
                return false
×
145
        }
×
146

147
        if len(ids[0]) != 4 {
×
148
                log.Info("IsValidPciString():", "Invalid vendor PciId ", ids[0])
×
149
                return false
×
150
        }
×
151
        if _, err := strconv.ParseInt(ids[0], 16, 32); err != nil {
×
152
                log.Info("IsValidPciString():", "Invalid vendor PciId ", ids[0])
×
153
        }
×
154

155
        if len(ids[1]) != 4 {
×
156
                log.Info("IsValidPciString():", "Invalid PciId of PF ", ids[1])
×
157
                return false
×
158
        }
×
159
        if _, err := strconv.ParseInt(ids[1], 16, 32); err != nil {
×
160
                log.Info("IsValidPciString():", "Invalid PciId of PF ", ids[1])
×
161
        }
×
162

163
        if len(ids[2]) != 4 {
×
164
                log.Info("IsValidPciString():", "Invalid PciId of VF ", ids[2])
×
165
                return false
×
166
        }
×
167
        if _, err := strconv.ParseInt(ids[2], 16, 32); err != nil {
×
168
                log.Info("IsValidPciString():", "Invalid PciId of VF ", ids[2])
×
169
        }
×
170

171
        return true
×
172
}
173

174
func GetSupportedVfIds() []string {
1✔
175
        var vfIds []string
1✔
176
        for _, n := range NicIDMap {
2✔
177
                ids := strings.Split(n, " ")
1✔
178
                vfID := "0x" + ids[2]
1✔
179
                if !StringInArray(vfID, vfIds) {
2✔
180
                        vfIds = append(vfIds, vfID)
1✔
181
                }
1✔
182
        }
183
        // return a sorted slice so that udev rule is stable
184
        sort.Slice(vfIds, func(i, j int) bool {
2✔
185
                ip, _ := strconv.ParseInt(vfIds[i], 0, 32)
1✔
186
                jp, _ := strconv.ParseInt(vfIds[j], 0, 32)
1✔
187
                return ip < jp
1✔
188
        })
1✔
189
        return vfIds
1✔
190
}
191

192
func GetVfDeviceID(deviceID string) string {
×
193
        for _, n := range NicIDMap {
×
194
                ids := strings.Split(n, " ")
×
195
                if deviceID == ids[1] {
×
196
                        return ids[2]
×
197
                }
×
198
        }
199
        return ""
×
200
}
201

202
type ByPriority []SriovNetworkNodePolicy
203

204
func (a ByPriority) Len() int {
×
205
        return len(a)
×
206
}
×
207

208
func (a ByPriority) Less(i, j int) bool {
×
209
        if a[i].Spec.Priority != a[j].Spec.Priority {
×
210
                return a[i].Spec.Priority > a[j].Spec.Priority
×
211
        }
×
212
        return a[i].GetName() < a[j].GetName()
×
213
}
214

215
func (a ByPriority) Swap(i, j int) {
×
216
        a[i], a[j] = a[j], a[i]
×
217
}
×
218

219
// Match check if node is selected by NodeSelector
220
func (p *SriovNetworkNodePolicy) Selected(node *corev1.Node) bool {
1✔
221
        for k, v := range p.Spec.NodeSelector {
1✔
222
                if nv, ok := node.Labels[k]; ok && nv == v {
×
223
                        continue
×
224
                }
225
                return false
×
226
        }
227
        log.Info("Selected():", "node", node.Name)
1✔
228
        return true
1✔
229
}
230

231
func StringInArray(val string, array []string) bool {
1✔
232
        for i := range array {
2✔
233
                if array[i] == val {
2✔
234
                        return true
1✔
235
                }
1✔
236
        }
237
        return false
1✔
238
}
239

240
func RemoveString(s string, slice []string) (result []string, found bool) {
1✔
241
        if len(slice) != 0 {
2✔
242
                for _, item := range slice {
2✔
243
                        if item == s {
2✔
244
                                found = true
1✔
245
                                continue
1✔
246
                        }
247
                        result = append(result, item)
×
248
                }
249
        }
250
        return
1✔
251
}
252

253
func UniqueAppend(inSlice []string, strings ...string) []string {
×
254
        for _, s := range strings {
×
255
                if !StringInArray(s, inSlice) {
×
256
                        inSlice = append(inSlice, s)
×
257
                }
×
258
        }
259
        return inSlice
×
260
}
261

262
// Apply policy to SriovNetworkNodeState CR
263
func (p *SriovNetworkNodePolicy) Apply(state *SriovNetworkNodeState, equalPriority bool) error {
×
264
        s := p.Spec.NicSelector
×
265
        if s.Vendor == "" && s.DeviceID == "" && len(s.RootDevices) == 0 && len(s.PfNames) == 0 &&
×
266
                len(s.NetFilter) == 0 {
×
267
                // Empty NicSelector match none
×
268
                return nil
×
269
        }
×
270
        for _, iface := range state.Status.Interfaces {
×
271
                if s.Selected(&iface) {
×
272
                        log.Info("Update interface", "name:", iface.Name)
×
273
                        result := Interface{
×
274
                                PciAddress:  iface.PciAddress,
×
275
                                Mtu:         p.Spec.Mtu,
×
276
                                Name:        iface.Name,
×
277
                                LinkType:    p.Spec.LinkType,
×
278
                                EswitchMode: p.Spec.EswitchMode,
×
279
                                NumVfs:      p.Spec.NumVfs,
×
280
                        }
×
281
                        if p.Spec.NumVfs > 0 {
×
282
                                group, err := p.generateVfGroup(&iface)
×
283
                                if err != nil {
×
284
                                        return err
×
285
                                }
×
286
                                result.VfGroups = []VfGroup{*group}
×
287
                                found := false
×
288
                                for i := range state.Spec.Interfaces {
×
289
                                        if state.Spec.Interfaces[i].PciAddress == result.PciAddress {
×
290
                                                found = true
×
291
                                                state.Spec.Interfaces[i].mergeConfigs(&result, equalPriority)
×
292
                                                state.Spec.Interfaces[i] = result
×
293
                                                break
×
294
                                        }
295
                                }
296
                                if !found {
×
297
                                        state.Spec.Interfaces = append(state.Spec.Interfaces, result)
×
298
                                }
×
299
                        }
300
                }
301
        }
302
        return nil
×
303
}
304

305
// mergeConfigs merges configs from multiple polices where the last one has the
306
// highest priority. This merge is dependent on: 1. SR-IOV partition is
307
// configured with the #-notation in pfName, 2. The VF groups are
308
// non-overlapping or SR-IOV policies have the same priority.
309
func (iface Interface) mergeConfigs(input *Interface, equalPriority bool) {
×
310
        m := false
×
311
        // merge VF groups (input.VfGroups already contains the highest priority):
×
312
        // - skip group with same ResourceName,
×
313
        // - skip overlapping groups (use only highest priority)
×
314
        for _, gr := range iface.VfGroups {
×
315
                if gr.ResourceName == input.VfGroups[0].ResourceName || gr.isVFRangeOverlapping(input.VfGroups[0]) {
×
316
                        continue
×
317
                }
318
                m = true
×
319
                input.VfGroups = append(input.VfGroups, gr)
×
320
        }
321

322
        if !equalPriority && !m {
×
323
                return
×
324
        }
×
325

326
        // mtu configuration we take the highest value
327
        if input.Mtu < iface.Mtu {
×
328
                input.Mtu = iface.Mtu
×
329
        }
×
330
        if input.NumVfs < iface.NumVfs {
×
331
                input.NumVfs = iface.NumVfs
×
332
        }
×
333
}
334

335
func (gr VfGroup) isVFRangeOverlapping(group VfGroup) bool {
×
336
        rngSt, rngEnd, err := parseRange(gr.VfRange)
×
337
        if err != nil {
×
338
                return false
×
339
        }
×
340
        rngSt2, rngEnd2, err := parseRange(group.VfRange)
×
341
        if err != nil {
×
342
                return false
×
343
        }
×
344
        // compare minimal range has overlap
345
        if rngSt < rngSt2 {
×
346
                return IndexInRange(rngSt2, gr.VfRange) || IndexInRange(rngEnd2, gr.VfRange)
×
347
        }
×
348
        return IndexInRange(rngSt, group.VfRange) || IndexInRange(rngEnd, group.VfRange)
×
349
}
350

351
func (p *SriovNetworkNodePolicy) generateVfGroup(iface *InterfaceExt) (*VfGroup, error) {
×
352
        var err error
×
353
        pfName := ""
×
354
        var rngStart, rngEnd int
×
355
        found := false
×
356
        for _, selector := range p.Spec.NicSelector.PfNames {
×
357
                pfName, rngStart, rngEnd, err = ParsePFName(selector)
×
358
                if err != nil {
×
359
                        log.Error(err, "Unable to parse PF Name.")
×
360
                        return nil, err
×
361
                }
×
362
                if pfName == iface.Name {
×
363
                        found = true
×
364
                        if rngStart == invalidVfIndex && rngEnd == invalidVfIndex {
×
365
                                rngStart, rngEnd = 0, p.Spec.NumVfs-1
×
366
                        }
×
367
                        break
×
368
                }
369
        }
370
        if !found {
×
371
                // assign the default vf index range if the pfName is not specified by the nicSelector
×
372
                rngStart, rngEnd = 0, p.Spec.NumVfs-1
×
373
        }
×
374
        rng := strconv.Itoa(rngStart) + "-" + strconv.Itoa(rngEnd)
×
375
        return &VfGroup{
×
376
                ResourceName: p.Spec.ResourceName,
×
377
                DeviceType:   p.Spec.DeviceType,
×
378
                VfRange:      rng,
×
379
                PolicyName:   p.GetName(),
×
380
                Mtu:          p.Spec.Mtu,
×
381
                IsRdma:       p.Spec.IsRdma,
×
382
                VdpaType:     p.Spec.VdpaType,
×
383
        }, nil
×
384
}
385

386
func IndexInRange(i int, r string) bool {
1✔
387
        rngSt, rngEnd, err := parseRange(r)
1✔
388
        if err != nil {
1✔
389
                return false
×
390
        }
×
391
        if i <= rngEnd && i >= rngSt {
2✔
392
                return true
1✔
393
        }
1✔
394
        return false
×
395
}
396

397
func parseRange(r string) (rngSt, rngEnd int, err error) {
1✔
398
        rng := strings.Split(r, "-")
1✔
399
        rngSt, err = strconv.Atoi(rng[0])
1✔
400
        if err != nil {
1✔
401
                return
×
402
        }
×
403
        rngEnd, err = strconv.Atoi(rng[1])
1✔
404
        if err != nil {
1✔
405
                return
×
406
        }
×
407
        return
1✔
408
}
409

410
// Parse PF name with VF range
411
func ParsePFName(name string) (ifName string, rngSt, rngEnd int, err error) {
1✔
412
        rngSt, rngEnd = invalidVfIndex, invalidVfIndex
1✔
413
        if strings.Contains(name, "#") {
2✔
414
                fields := strings.Split(name, "#")
1✔
415
                ifName = fields[0]
1✔
416
                rngSt, rngEnd, err = parseRange(fields[1])
1✔
417
        } else {
1✔
418
                ifName = name
×
419
        }
×
420
        return
1✔
421
}
422

423
func (selector *SriovNetworkNicSelector) Selected(iface *InterfaceExt) bool {
×
424
        if selector.Vendor != "" && selector.Vendor != iface.Vendor {
×
425
                return false
×
426
        }
×
427
        if selector.DeviceID != "" && selector.DeviceID != iface.DeviceID {
×
428
                return false
×
429
        }
×
430
        if len(selector.RootDevices) > 0 && !StringInArray(iface.PciAddress, selector.RootDevices) {
×
431
                return false
×
432
        }
×
433
        if len(selector.PfNames) > 0 {
×
434
                var pfNames []string
×
435
                for _, p := range selector.PfNames {
×
436
                        if strings.Contains(p, "#") {
×
437
                                fields := strings.Split(p, "#")
×
438
                                pfNames = append(pfNames, fields[0])
×
439
                        } else {
×
440
                                pfNames = append(pfNames, p)
×
441
                        }
×
442
                }
443
                if !StringInArray(iface.Name, pfNames) {
×
444
                        return false
×
445
                }
×
446
        }
447
        if selector.NetFilter != "" && !NetFilterMatch(selector.NetFilter, iface.NetFilter) {
×
448
                return false
×
449
        }
×
450

451
        return true
×
452
}
453

454
func (s *SriovNetworkNodeState) GetInterfaceStateByPciAddress(addr string) *InterfaceExt {
×
455
        for _, iface := range s.Status.Interfaces {
×
456
                if addr == iface.PciAddress {
×
457
                        return &iface
×
458
                }
×
459
        }
460
        return nil
×
461
}
462

463
func (s *SriovNetworkNodeState) GetDriverByPciAddress(addr string) string {
×
464
        for _, iface := range s.Status.Interfaces {
×
465
                if addr == iface.PciAddress {
×
466
                        return iface.Driver
×
467
                }
×
468
        }
469
        return ""
×
470
}
471

472
// RenderNetAttDef renders a net-att-def for ib-sriov CNI
473
func (cr *SriovIBNetwork) RenderNetAttDef() (*uns.Unstructured, error) {
1✔
474
        logger := log.WithName("renderNetAttDef")
1✔
475
        logger.Info("Start to render IB SRIOV CNI NetworkAttachementDefinition")
1✔
476

1✔
477
        // render RawCNIConfig manifests
1✔
478
        data := render.MakeRenderData()
1✔
479
        data.Data["CniType"] = "ib-sriov"
1✔
480
        data.Data["SriovNetworkName"] = cr.Name
1✔
481
        if cr.Spec.NetworkNamespace == "" {
2✔
482
                data.Data["SriovNetworkNamespace"] = cr.Namespace
1✔
483
        } else {
2✔
484
                data.Data["SriovNetworkNamespace"] = cr.Spec.NetworkNamespace
1✔
485
        }
1✔
486
        data.Data["SriovCniResourceName"] = os.Getenv("RESOURCE_PREFIX") + "/" + cr.Spec.ResourceName
1✔
487

1✔
488
        data.Data["StateConfigured"] = true
1✔
489
        switch cr.Spec.LinkState {
1✔
490
        case SriovCniStateEnable:
1✔
491
                data.Data["SriovCniState"] = SriovCniStateEnable
1✔
492
        case SriovCniStateDisable:
×
493
                data.Data["SriovCniState"] = SriovCniStateDisable
×
494
        case SriovCniStateAuto:
×
495
                data.Data["SriovCniState"] = SriovCniStateAuto
×
496
        default:
1✔
497
                data.Data["StateConfigured"] = false
1✔
498
        }
499

500
        if cr.Spec.Capabilities == "" {
2✔
501
                data.Data["CapabilitiesConfigured"] = false
1✔
502
        } else {
1✔
503
                data.Data["CapabilitiesConfigured"] = true
×
504
                data.Data["SriovCniCapabilities"] = cr.Spec.Capabilities
×
505
        }
×
506

507
        if cr.Spec.IPAM != "" {
2✔
508
                data.Data["SriovCniIpam"] = "\"ipam\":" + strings.Join(strings.Fields(cr.Spec.IPAM), "")
1✔
509
        } else {
2✔
510
                data.Data["SriovCniIpam"] = SriovCniIpamEmpty
1✔
511
        }
1✔
512

513
        // metaplugins for the infiniband cni
514
        data.Data["MetaPluginsConfigured"] = false
1✔
515
        if cr.Spec.MetaPluginsConfig != "" {
1✔
516
                data.Data["MetaPluginsConfigured"] = true
×
517
                data.Data["MetaPlugins"] = cr.Spec.MetaPluginsConfig
×
518
        }
×
519

520
        objs, err := render.RenderDir(ManifestsPath, &data)
1✔
521
        if err != nil {
1✔
522
                return nil, err
×
523
        }
×
524
        for _, obj := range objs {
2✔
525
                raw, _ := json.Marshal(obj)
1✔
526
                logger.Info("render NetworkAttachementDefinition output", "raw", string(raw))
1✔
527
        }
1✔
528
        return objs[0], nil
1✔
529
}
530

531
// DeleteNetAttDef deletes the generated net-att-def CR
532
func (cr *SriovIBNetwork) DeleteNetAttDef(c client.Client) error {
1✔
533
        // Fetch the NetworkAttachmentDefinition instance
1✔
534
        instance := &netattdefv1.NetworkAttachmentDefinition{}
1✔
535
        namespace := cr.GetNamespace()
1✔
536
        if cr.Spec.NetworkNamespace != "" {
2✔
537
                namespace = cr.Spec.NetworkNamespace
1✔
538
        }
1✔
539
        err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: cr.GetName()}, instance)
1✔
540
        if err != nil {
2✔
541
                if errors.IsNotFound(err) {
2✔
542
                        return nil
1✔
543
                }
1✔
544
                return err
×
545
        }
546
        err = c.Delete(context.TODO(), instance)
1✔
547
        if err != nil {
1✔
548
                return err
×
549
        }
×
550
        return nil
1✔
551
}
552

553
// RenderNetAttDef renders a net-att-def for sriov CNI
554
func (cr *SriovNetwork) RenderNetAttDef() (*uns.Unstructured, error) {
1✔
555
        logger := log.WithName("renderNetAttDef")
1✔
556
        logger.Info("Start to render SRIOV CNI NetworkAttachementDefinition")
1✔
557

1✔
558
        // render RawCNIConfig manifests
1✔
559
        data := render.MakeRenderData()
1✔
560
        data.Data["CniType"] = "sriov"
1✔
561
        data.Data["SriovNetworkName"] = cr.Name
1✔
562
        if cr.Spec.NetworkNamespace == "" {
2✔
563
                data.Data["SriovNetworkNamespace"] = cr.Namespace
1✔
564
        } else {
2✔
565
                data.Data["SriovNetworkNamespace"] = cr.Spec.NetworkNamespace
1✔
566
        }
1✔
567
        data.Data["SriovCniResourceName"] = os.Getenv("RESOURCE_PREFIX") + "/" + cr.Spec.ResourceName
1✔
568
        data.Data["SriovCniVlan"] = cr.Spec.Vlan
1✔
569

1✔
570
        if cr.Spec.VlanQoS <= 7 && cr.Spec.VlanQoS >= 0 {
2✔
571
                data.Data["VlanQoSConfigured"] = true
1✔
572
                data.Data["SriovCniVlanQoS"] = cr.Spec.VlanQoS
1✔
573
        } else {
1✔
574
                data.Data["VlanQoSConfigured"] = false
×
575
        }
×
576

577
        if cr.Spec.Capabilities == "" {
2✔
578
                data.Data["CapabilitiesConfigured"] = false
1✔
579
        } else {
1✔
580
                data.Data["CapabilitiesConfigured"] = true
×
581
                data.Data["SriovCniCapabilities"] = cr.Spec.Capabilities
×
582
        }
×
583

584
        data.Data["SpoofChkConfigured"] = true
1✔
585
        switch cr.Spec.SpoofChk {
1✔
586
        case SriovCniStateOff:
×
587
                data.Data["SriovCniSpoofChk"] = SriovCniStateOff
×
588
        case SriovCniStateOn:
1✔
589
                data.Data["SriovCniSpoofChk"] = SriovCniStateOn
1✔
590
        default:
1✔
591
                data.Data["SpoofChkConfigured"] = false
1✔
592
        }
593

594
        data.Data["TrustConfigured"] = true
1✔
595
        switch cr.Spec.Trust {
1✔
596
        case SriovCniStateOn:
1✔
597
                data.Data["SriovCniTrust"] = SriovCniStateOn
1✔
598
        case SriovCniStateOff:
×
599
                data.Data["SriovCniTrust"] = SriovCniStateOff
×
600
        default:
1✔
601
                data.Data["TrustConfigured"] = false
1✔
602
        }
603

604
        data.Data["StateConfigured"] = true
1✔
605
        switch cr.Spec.LinkState {
1✔
606
        case SriovCniStateEnable:
×
607
                data.Data["SriovCniState"] = SriovCniStateEnable
×
608
        case SriovCniStateDisable:
×
609
                data.Data["SriovCniState"] = SriovCniStateDisable
×
610
        case SriovCniStateAuto:
×
611
                data.Data["SriovCniState"] = SriovCniStateAuto
×
612
        default:
1✔
613
                data.Data["StateConfigured"] = false
1✔
614
        }
615

616
        data.Data["MinTxRateConfigured"] = false
1✔
617
        if cr.Spec.MinTxRate != nil {
1✔
618
                if *cr.Spec.MinTxRate >= 0 {
×
619
                        data.Data["MinTxRateConfigured"] = true
×
620
                        data.Data["SriovCniMinTxRate"] = *cr.Spec.MinTxRate
×
621
                }
×
622
        }
623

624
        data.Data["MaxTxRateConfigured"] = false
1✔
625
        if cr.Spec.MaxTxRate != nil {
1✔
626
                if *cr.Spec.MaxTxRate >= 0 {
×
627
                        data.Data["MaxTxRateConfigured"] = true
×
628
                        data.Data["SriovCniMaxTxRate"] = *cr.Spec.MaxTxRate
×
629
                }
×
630
        }
631

632
        if cr.Spec.IPAM != "" {
2✔
633
                data.Data["SriovCniIpam"] = "\"ipam\":" + strings.Join(strings.Fields(cr.Spec.IPAM), "")
1✔
634
        } else {
1✔
635
                data.Data["SriovCniIpam"] = SriovCniIpamEmpty
×
636
        }
×
637

638
        data.Data["MetaPluginsConfigured"] = false
1✔
639
        if cr.Spec.MetaPluginsConfig != "" {
1✔
640
                data.Data["MetaPluginsConfigured"] = true
×
641
                data.Data["MetaPlugins"] = cr.Spec.MetaPluginsConfig
×
642
        }
×
643

644
        objs, err := render.RenderDir(ManifestsPath, &data)
1✔
645
        if err != nil {
1✔
646
                return nil, err
×
647
        }
×
648
        for _, obj := range objs {
2✔
649
                raw, _ := json.Marshal(obj)
1✔
650
                logger.Info("render NetworkAttachementDefinition output", "raw", string(raw))
1✔
651
        }
1✔
652
        return objs[0], nil
1✔
653
}
654

655
// DeleteNetAttDef deletes the generated net-att-def CR
656
func (cr *SriovNetwork) DeleteNetAttDef(c client.Client) error {
1✔
657
        // Fetch the NetworkAttachmentDefinition instance
1✔
658
        instance := &netattdefv1.NetworkAttachmentDefinition{}
1✔
659
        namespace := cr.GetNamespace()
1✔
660
        if cr.Spec.NetworkNamespace != "" {
2✔
661
                namespace = cr.Spec.NetworkNamespace
1✔
662
        }
1✔
663
        err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: cr.GetName()}, instance)
1✔
664
        if err != nil {
2✔
665
                if errors.IsNotFound(err) {
2✔
666
                        return nil
1✔
667
                }
1✔
668
                return err
×
669
        }
670
        err = c.Delete(context.TODO(), instance)
1✔
671
        if err != nil {
1✔
672
                return err
×
673
        }
×
674
        return nil
1✔
675
}
676

677
// NetFilterMatch -- parse netFilter and check for a match
678
func NetFilterMatch(netFilter string, netValue string) (isMatch bool) {
×
679
        logger := log.WithName("NetFilterMatch")
×
680

×
681
        var re = regexp.MustCompile(`(?m)^\s*([^\s]+)\s*:\s*([^\s]+)`)
×
682

×
683
        netFilterResult := re.FindAllStringSubmatch(netFilter, -1)
×
684

×
685
        if netFilterResult == nil {
×
686
                logger.Info("Invalid NetFilter spec...", "netFilter", netFilter)
×
687
                return false
×
688
        }
×
689

690
        netValueResult := re.FindAllStringSubmatch(netValue, -1)
×
691

×
692
        if netValueResult == nil {
×
693
                logger.Info("Invalid netValue...", "netValue", netValue)
×
694
                return false
×
695
        }
×
696

697
        return netFilterResult[0][1] == netValueResult[0][1] && netFilterResult[0][2] == netValueResult[0][2]
×
698
}
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