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

k8snetworkplumbingwg / dra-driver-sriov / 26086512318

19 May 2026 08:47AM UTC coverage: 53.573% (+2.7%) from 50.902%
26086512318

Pull #95

github

web-flow
Merge 8c639e2fc into 7e91a77c2
Pull Request #95: WIP: Support custom MAC annotation

278 of 428 new or added lines in 9 files covered. (64.95%)

1 existing line in 1 file now uncovered.

2099 of 3918 relevant lines covered (53.57%)

3.68 hits per line

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

68.29
/pkg/types/types.go
1
package types
2

3
import (
4
        "encoding/json"
5
        "fmt"
6

7
        resourceapi "k8s.io/api/resource/v1"
8
        "k8s.io/apimachinery/pkg/runtime"
9
        k8stypes "k8s.io/apimachinery/pkg/types"
10
        "k8s.io/dynamic-resource-allocation/kubeletplugin"
11
        drapbv1 "k8s.io/kubelet/pkg/apis/dra/v1beta1"
12
        "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
13
        cdiapi "tags.cncf.io/container-device-interface/pkg/cdi"
14

15
        configapi "github.com/k8snetworkplumbingwg/dra-driver-sriov/pkg/api/virtualfunction/v1alpha1"
16
)
17

18
const (
19
        // DRANetworkMACsAnnotation contains a JSON map of network name to MAC address
20
        // for DRA networks. This is set by KubeVirt on the virt-launcher pod to allow
21
        // the DRA driver to configure MAC addresses on SR-IOV VFs.
22
        DRANetworkMACsAnnotation = "kubevirt.io/dra-network-macs"
23
)
24

25
// AllocatableDevices is a map of device pci address to dra device objects
26
type AllocatableDevices map[string]resourceapi.Device
27

28
// PreparedDevices is a slice of prepared devices
29
type PreparedDevices []*PreparedDevice
30

31
// PreparedDevicesByClaimID is a map of claim ID to prepared devices
32
type PreparedDevicesByClaimID map[k8stypes.UID]PreparedDevices
33

34
// PreparedClaimsByPodUID is a map of pod uid to map of claim ID to prepared devices
35
type PreparedClaimsByPodUID map[k8stypes.UID]PreparedDevicesByClaimID
36

37
type NetworkDataChanStruct struct {
38
        PreparedDevice    *PreparedDevice
39
        NetworkDeviceData *resourceapi.NetworkDeviceData
40
        CNIConfig         map[string]interface{}
41
        CNIResult         map[string]interface{}
42
}
43
type NetworkDataChanStructList []*NetworkDataChanStruct
44

45
// AddDeviceIDToNetConf adds the deviceID (PCI address) to the netconf
46
func AddDeviceIDToNetConf(originalConfig, deviceID string) (string, error) {
6✔
47
        // Unmarshal the existing configuration into a raw map
6✔
48
        var rawConfig map[string]interface{}
6✔
49
        if err := json.Unmarshal([]byte(originalConfig), &rawConfig); err != nil {
7✔
50
                return "", fmt.Errorf("failed to unmarshal existing config: %w", err)
1✔
51
        }
1✔
52

53
        // Set the deviceID (PCI address)
54
        rawConfig["deviceID"] = deviceID
5✔
55

5✔
56
        // Marshal the modified configuration back to a JSON string
5✔
57
        modifiedConfig, err := json.Marshal(rawConfig)
5✔
58
        if err != nil {
5✔
59
                return "", fmt.Errorf("failed to marshal modified config: %w", err)
×
60
        }
×
61

62
        return string(modifiedConfig), nil
5✔
63
}
64

65
// AddMACToNetConf adds the MAC address to the netconf
NEW
66
func AddMACToNetConf(originalConfig, macAddress string) (string, error) {
×
NEW
67
        // Unmarshal the existing configuration into a raw map
×
NEW
68
        var rawConfig map[string]interface{}
×
NEW
69
        if err := json.Unmarshal([]byte(originalConfig), &rawConfig); err != nil {
×
NEW
70
                return "", fmt.Errorf("failed to unmarshal existing config: %w", err)
×
NEW
71
        }
×
72

73
        // Set the MAC address
NEW
74
        rawConfig["mac"] = macAddress
×
NEW
75

×
NEW
76
        // Marshal the modified configuration back to a JSON string
×
NEW
77
        modifiedConfig, err := json.Marshal(rawConfig)
×
NEW
78
        if err != nil {
×
NEW
79
                return "", fmt.Errorf("failed to marshal modified config: %w", err)
×
NEW
80
        }
×
81

NEW
82
        return string(modifiedConfig), nil
×
83
}
84

85
type OpaqueDeviceConfig struct {
86
        Requests []string
87
        Config   runtime.Object
88
}
89

90
type PreparedDevice struct {
91
        Device              drapbv1.Device
92
        ClaimNamespacedName kubeletplugin.NamespacedObject
93
        ContainerEdits      *cdiapi.ContainerEdits
94
        Config              *configapi.VfConfig
95
        IfName              string
96
        PciAddress          string
97
        MultusDeviceID      string
98
        MultusResourceName  string
99
        DeviceAttributes    map[string]resourceapi.DeviceAttribute
100
        NetworkDeviceData   *resourceapi.NetworkDeviceData
101
        PodUID              string
102
        NetAttachDefConfig  string
103
        OriginalDriver      string // Store original driver for restoration during unprepare
104
}
105

106
func (p *PreparedDevice) ToKubeletPluginDevice(networkData *resourceapi.NetworkDeviceData) kubeletplugin.Device {
3✔
107
        if networkData == nil {
5✔
108
                networkData = p.NetworkDeviceData
2✔
109
        }
2✔
110
        return kubeletplugin.Device{
3✔
111
                Requests:     p.Device.GetRequestNames(),
3✔
112
                PoolName:     p.Device.GetPoolName(),
3✔
113
                DeviceName:   p.Device.GetDeviceName(),
3✔
114
                CDIDeviceIDs: p.Device.GetCdiDeviceIds(),
3✔
115
                Metadata: &kubeletplugin.DeviceMetadata{
3✔
116
                        Attributes:  p.MetadataAttributes(),
3✔
117
                        NetworkData: networkData,
3✔
118
                },
3✔
119
        }
3✔
120
}
121

NEW
122
func (p *PreparedDevice) SetNetworkDeviceData(networkData *resourceapi.NetworkDeviceData) {
×
NEW
123
        if networkData == nil {
×
NEW
124
                p.NetworkDeviceData = nil
×
NEW
125
                return
×
NEW
126
        }
×
NEW
127
        p.NetworkDeviceData = networkData.DeepCopy()
×
128
}
129

130
func (p *PreparedDevice) MetadataAttributes() map[string]resourceapi.DeviceAttribute {
3✔
131
        return p.DeviceAttributes
3✔
132
}
3✔
133

134
type Checkpoint struct {
135
        Checksum checksum.Checksum `json:"checksum"`
136
        V1       *CheckpointV1     `json:"v1,omitempty"`
137
}
138

139
type CheckpointV1 struct {
140
        PreparedClaimsByPodUID PreparedClaimsByPodUID `json:"preparedClaimsByPodUID,omitempty"`
141
}
142

143
func NewCheckpoint() *Checkpoint {
7✔
144
        pc := &Checkpoint{
7✔
145
                Checksum: 0,
7✔
146
                V1: &CheckpointV1{
7✔
147
                        PreparedClaimsByPodUID: make(PreparedClaimsByPodUID),
7✔
148
                },
7✔
149
        }
7✔
150
        return pc
7✔
151
}
7✔
152

153
func (cp *Checkpoint) MarshalCheckpoint() ([]byte, error) {
4✔
154
        cp.Checksum = 0
4✔
155
        out, err := json.Marshal(*cp)
4✔
156
        if err != nil {
4✔
157
                return nil, err
×
158
        }
×
159
        cp.Checksum = checksum.New(out)
4✔
160
        return json.Marshal(*cp)
4✔
161
}
162

163
func (cp *Checkpoint) UnmarshalCheckpoint(data []byte) error {
5✔
164
        return json.Unmarshal(data, cp)
5✔
165
}
5✔
166

167
func (cp *Checkpoint) VerifyChecksum() error {
3✔
168
        ck := cp.Checksum
3✔
169
        cp.Checksum = 0
3✔
170
        defer func() {
6✔
171
                cp.Checksum = ck
3✔
172
        }()
3✔
173
        out, err := json.Marshal(*cp)
3✔
174
        if err != nil {
3✔
175
                return err
×
176
        }
×
177
        return ck.Verify(out)
3✔
178
}
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

© 2026 Coveralls, Inc