forked from google/dranet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdra_hooks.go
More file actions
435 lines (398 loc) · 15.8 KB
/
dra_hooks.go
File metadata and controls
435 lines (398 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package driver
import (
"context"
"errors"
"fmt"
"slices"
"time"
"github.com/google/dranet/pkg/apis"
"github.com/google/dranet/pkg/filter"
"github.com/Mellanox/rdmamap"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
resourceapi "k8s.io/api/resource/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/dynamic-resource-allocation/kubeletplugin"
"k8s.io/dynamic-resource-allocation/resourceslice"
"k8s.io/klog/v2"
)
const (
rdmaCmPath = "/dev/infiniband/rdma_cm"
)
// DRA hooks exposes Network Devices to Kubernetes, the Network devices and its attributes are
// obtained via the netdb to decouple the discovery of the interfaces with the execution.
// The exposed devices can be allocated to one or mod pods via Claim, the Claim lifecycle is
// the ones that defines the lifecycle of a device assigned to a Pod.
// The hooks NodePrepareResources and NodeUnprepareResources are needed to collect the necessary
// information so the NRI hooks can perform the configuration and attachment of Pods at runtime.
func (np *NetworkDriver) PublishResources(ctx context.Context) {
klog.V(2).Infof("Publishing resources")
for {
select {
case devices := <-np.netdb.GetResources(ctx):
klog.V(4).Infof("Received %d devices", len(devices))
devices = filter.FilterDevices(np.celProgram, devices)
klog.V(4).Infof("After filtering %d devices", len(devices))
np.publishResourcesPrometheusMetrics(devices)
resources := resourceslice.DriverResources{
Pools: map[string]resourceslice.Pool{
np.nodeName: {Slices: []resourceslice.Slice{{Devices: devices}}}},
}
err := np.draPlugin.PublishResources(ctx, resources)
if err != nil {
klog.Error(err, "unexpected error trying to publish resources")
} else {
lastPublishedTime.SetToCurrentTime()
}
case <-ctx.Done():
klog.Error(ctx.Err(), "context canceled")
return
}
// poor man rate limit
time.Sleep(3 * time.Second)
}
}
func (np *NetworkDriver) publishResourcesPrometheusMetrics(devices []resourceapi.Device) {
rdmaCount := 0
for _, device := range devices {
if attr, ok := device.Attributes[apis.AttrRDMA]; ok && attr.BoolValue != nil && *attr.BoolValue {
rdmaCount++
}
}
publishedDevicesTotal.WithLabelValues("rdma").Set(float64(rdmaCount))
publishedDevicesTotal.WithLabelValues("total").Set(float64(len(devices)))
}
func (np *NetworkDriver) PrepareResourceClaims(ctx context.Context, claims []*resourceapi.ResourceClaim) (map[types.UID]kubeletplugin.PrepareResult, error) {
klog.V(2).Infof("PrepareResourceClaims is called: number of claims: %d", len(claims))
start := time.Now()
defer func() {
draPluginRequestsLatencySeconds.WithLabelValues(methodPrepareResourceClaims).Observe(time.Since(start).Seconds())
}()
result, err := np.prepareResourceClaims(ctx, claims)
if err != nil {
draPluginRequestsTotal.WithLabelValues(methodPrepareResourceClaims, statusFailed).Inc()
return result, err
}
// identify errors and log metrics
isError := false
for _, res := range result {
if res.Err != nil {
isError = true
break
}
}
if isError {
draPluginRequestsTotal.WithLabelValues(methodPrepareResourceClaims, statusFailed).Inc()
} else {
draPluginRequestsTotal.WithLabelValues(methodPrepareResourceClaims, statusSuccess).Inc()
}
return result, err
}
func (np *NetworkDriver) prepareResourceClaims(ctx context.Context, claims []*resourceapi.ResourceClaim) (map[types.UID]kubeletplugin.PrepareResult, error) {
if len(claims) == 0 {
return nil, nil
}
result := make(map[types.UID]kubeletplugin.PrepareResult)
for _, claim := range claims {
klog.V(2).Infof("NodePrepareResources: Claim Request %s/%s", claim.Namespace, claim.Name)
result[claim.UID] = np.prepareResourceClaim(ctx, claim)
}
return result, nil
}
// prepareResourceClaim gets all the configuration required to be applied at runtime and passes it downs to the handlers.
// This happens in the kubelet so it can be a "slow" operation, so we can execute fast in RunPodsandbox, that happens in the
// container runtime and has strong expectactions to be executed fast (default hook timeout is 2 seconds).
func (np *NetworkDriver) prepareResourceClaim(ctx context.Context, claim *resourceapi.ResourceClaim) kubeletplugin.PrepareResult {
klog.V(2).Infof("PrepareResourceClaim Claim %s/%s", claim.Namespace, claim.Name)
start := time.Now()
defer func() {
klog.V(2).Infof("PrepareResourceClaim Claim %s/%s took %v", claim.Namespace, claim.Name, time.Since(start))
}()
// TODO: shared devices may allocate the same device to multiple pods, i.e. macvlan, ipvlan, ...
podUIDs := []types.UID{}
for _, reserved := range claim.Status.ReservedFor {
if reserved.Resource != "pods" || reserved.APIGroup != "" {
klog.Infof("Driver only supports Pods, unsupported reference %#v", reserved)
continue
}
podUIDs = append(podUIDs, reserved.UID)
}
if len(podUIDs) == 0 {
klog.Infof("no pods allocated to claim %s/%s", claim.Namespace, claim.Name)
return kubeletplugin.PrepareResult{}
}
nlHandle, err := netlink.NewHandle()
if err != nil {
return kubeletplugin.PrepareResult{
Err: fmt.Errorf("error creating netlink handle %v", err),
}
}
var errorList []error
charDevices := sets.New[string]()
for _, result := range claim.Status.Allocation.Devices.Results {
// A single ResourceClaim can have devices managed by distinct DRA
// drivers. One common use case for this is device topology alignment
// (think NIC and GPU alignment). In such cases, we should ignore the
// devices which are not managed by our driver.
//
// TODO: Test running a different driver alongside DraNet in e2e. This
// requires an easy way to spin up a mock DRA driver.
if result.Driver != np.driverName {
continue
}
requestName := result.Request
netconf := apis.NetworkConfig{}
for _, config := range claim.Status.Allocation.Devices.Config {
// Check there is a config associated to this device
if config.Opaque == nil ||
config.Opaque.Driver != np.driverName ||
len(config.Requests) > 0 && !slices.Contains(config.Requests, requestName) {
continue
}
// Check if there is a custom configuration
conf, errs := apis.ValidateConfig(&config.Opaque.Parameters)
if len(errs) > 0 {
errorList = append(errorList, errs...)
continue
}
// TODO: define a strategy for multiple configs
if conf != nil {
netconf = *conf
break
}
}
klog.V(4).Infof("PrepareResourceClaim %s/%s final Configuration %#v", claim.Namespace, claim.Name, netconf)
podCfg := PodConfig{
Claim: types.NamespacedName{
Namespace: claim.Namespace,
Name: claim.Name,
},
NetworkInterfaceConfigInPod: netconf,
}
ifName, err := np.netdb.GetNetInterfaceName(result.Device)
if err != nil {
errorList = append(errorList, fmt.Errorf("failed to get network interface name for device %s: %v", result.Device, err))
continue
}
// Get Network configuration and merge it
link, err := nlHandle.LinkByName(ifName)
if err != nil {
errorList = append(errorList, fmt.Errorf("failed to get netlink to interface %s: %v", ifName, err))
continue
}
podCfg.NetworkInterfaceConfigInHost.Interface.Name = ifName
if podCfg.NetworkInterfaceConfigInPod.Interface.Name == "" {
// If the interface name was not explicitly overridden, use the same
// interface name within the pod's network namespace.
podCfg.NetworkInterfaceConfigInPod.Interface.Name = ifName
}
// If DHCP is requested, do a DHCP request to gather the network parameters (IPs and Routes)
// ... but we DO NOT apply them in the root namespace
if podCfg.NetworkInterfaceConfigInPod.Interface.DHCP != nil && *podCfg.NetworkInterfaceConfigInPod.Interface.DHCP {
klog.V(2).Infof("trying to get network configuration via DHCP")
contextCancel, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
ip, routes, err := getDHCP(contextCancel, ifName)
if err != nil {
errorList = append(errorList, fmt.Errorf("fail to get configuration via DHCP for %s: %w", ifName, err))
} else {
podCfg.NetworkInterfaceConfigInPod.Interface.Addresses = []string{ip}
podCfg.NetworkInterfaceConfigInPod.Routes = append(podCfg.NetworkInterfaceConfigInPod.Routes, routes...)
}
} else if len(podCfg.NetworkInterfaceConfigInPod.Interface.Addresses) == 0 {
// If there is no custom addresses and no DHCP, then use the existing ones
// get the existing IP addresses
nlAddresses, err := nlHandle.AddrList(link, netlink.FAMILY_ALL)
if err != nil && !errors.Is(err, netlink.ErrDumpInterrupted) {
errorList = append(errorList, fmt.Errorf("fail to get ip addresses for interface %s : %w", ifName, err))
} else {
for _, address := range nlAddresses {
// Only move IP addresses with global scope because those are not host-specific, auto-configured,
// or have limited network scope, making them unsuitable inside the container namespace.
// Ref: https://www.ietf.org/rfc/rfc3549.txt
if address.Scope != unix.RT_SCOPE_UNIVERSE {
continue
}
podCfg.NetworkInterfaceConfigInPod.Interface.Addresses = append(podCfg.NetworkInterfaceConfigInPod.Interface.Addresses, address.IPNet.String())
}
}
}
// Obtain the existing supported ethtool features and validate the config
if podCfg.NetworkInterfaceConfigInPod.Ethtool != nil {
client, err := newEthtoolClient(0)
if err != nil {
errorList = append(errorList, fmt.Errorf("fail to create ethtool client %v", err))
continue
}
defer client.Close()
ifFeatures, err := client.GetFeatures(ifName)
if err != nil {
errorList = append(errorList, fmt.Errorf("fail to get ethtool features %v", err))
continue
}
// translate features to the actual kernel names
ethtoolFeatures := map[string]bool{}
for feature, value := range podCfg.NetworkInterfaceConfigInPod.Ethtool.Features {
aliases := ifFeatures.Get(feature)
if len(aliases) == 0 {
errorList = append(errorList, fmt.Errorf("feature %s not supported by interface", feature))
continue
}
for _, alias := range aliases {
ethtoolFeatures[alias] = value
}
}
podCfg.NetworkInterfaceConfigInPod.Ethtool.Features = ethtoolFeatures
}
// Obtain the routes associated to the interface
// TODO: only considers outgoing traffic
filter := &netlink.Route{
LinkIndex: link.Attrs().Index,
}
routes, err := nlHandle.RouteListFiltered(netlink.FAMILY_ALL, filter, netlink.RT_FILTER_OIF)
if err != nil {
klog.Infof("fail to get ip routes for interface %s : %v", ifName, err)
}
for _, route := range routes {
routeCfg := apis.RouteConfig{}
// routes need a destination
if route.Dst == nil {
continue
}
// Discard IPv6 link-local routes, but allow IPv4 link-local.
if route.Dst.IP.To4() == nil && route.Dst.IP.IsLinkLocalUnicast() {
continue
}
routeCfg.Destination = route.Dst.String()
if route.Gw != nil {
routeCfg.Gateway = route.Gw.String()
}
if route.Src != nil {
routeCfg.Source = route.Src.String()
}
routeCfg.Scope = uint8(route.Scope)
podCfg.NetworkInterfaceConfigInPod.Routes = append(podCfg.NetworkInterfaceConfigInPod.Routes, routeCfg)
}
// Obtain the neighbors associated to the interface
neighs, err := nlHandle.NeighList(link.Attrs().Index, netlink.FAMILY_ALL)
if err != nil {
klog.Infof("failed to get neighbors for interface %s: %v", ifName, err)
}
for _, neigh := range neighs {
if neigh.IP == nil || neigh.HardwareAddr == nil {
continue
}
// We are only interested in permanent neighbor entries
if neigh.State != netlink.NUD_PERMANENT {
continue
}
neighCfg := apis.NeighborConfig{
Destination: neigh.IP.String(),
HardwareAddr: neigh.HardwareAddr.String(),
}
podCfg.NetworkInterfaceConfigInPod.Neighbors = append(podCfg.NetworkInterfaceConfigInPod.Neighbors, neighCfg)
}
// Get RDMA configuration: link and char devices
if rdmaDev, _ := rdmamap.GetRdmaDeviceForNetdevice(ifName); rdmaDev != "" {
klog.V(2).Infof("RunPodSandbox processing RDMA device: %s", rdmaDev)
podCfg.RDMADevice.LinkDev = rdmaDev
// Obtain the char devices associated to the rdma device
charDevices.Insert(rdmaCmPath)
charDevices.Insert(rdmamap.GetRdmaCharDevices(rdmaDev)...)
for _, devpath := range charDevices.UnsortedList() {
dev, err := GetDeviceInfo(devpath)
if err != nil {
klog.Infof("fail to get device info for %s : %v", devpath, err)
} else {
podCfg.RDMADevice.DevChars = append(podCfg.RDMADevice.DevChars, dev)
}
}
}
// Remove the pinned programs before the NRI hooks since it
// has to walk the entire bpf virtual filesystem and is slow
// TODO: check if there is some other way to do this
if podCfg.NetworkInterfaceConfigInPod.Interface.DisableEBPFPrograms != nil &&
*podCfg.NetworkInterfaceConfigInPod.Interface.DisableEBPFPrograms {
err := unpinBPFPrograms(ifName)
if err != nil {
klog.Infof("error unpinning ebpf programs for %s : %v", ifName, err)
}
}
// TODO: support for multiple pods sharing the same device
// we'll create the subinterface here
for _, uid := range podUIDs {
np.podConfigStore.Set(uid, result.Device, podCfg)
}
klog.V(4).Infof("Claim Resources for pods %v : %#v", podUIDs, podCfg)
}
if len(errorList) > 0 {
klog.Infof("claim %s contain errors: %v", claim.UID, errors.Join(errorList...))
return kubeletplugin.PrepareResult{
Err: fmt.Errorf("claim %s contain errors: %w", claim.UID, errors.Join(errorList...)),
}
}
return kubeletplugin.PrepareResult{}
}
func (np *NetworkDriver) UnprepareResourceClaims(ctx context.Context, claims []kubeletplugin.NamespacedObject) (map[types.UID]error, error) {
klog.V(2).Infof("UnprepareResourceClaims is called: number of claims: %d", len(claims))
start := time.Now()
defer func() {
draPluginRequestsLatencySeconds.WithLabelValues(methodUnprepareResourceClaims).Observe(time.Since(start).Seconds())
}()
result, err := np.unprepareResourceClaims(ctx, claims)
if err != nil {
draPluginRequestsTotal.WithLabelValues(methodUnprepareResourceClaims, statusFailed).Inc()
return result, err
}
// identify errors and log metrics
isError := false
for _, res := range result {
if res != nil {
isError = true
break
}
}
if isError {
draPluginRequestsTotal.WithLabelValues(methodUnprepareResourceClaims, statusFailed).Inc()
} else {
draPluginRequestsTotal.WithLabelValues(methodUnprepareResourceClaims, statusSuccess).Inc()
}
return result, err
}
func (np *NetworkDriver) unprepareResourceClaims(ctx context.Context, claims []kubeletplugin.NamespacedObject) (map[types.UID]error, error) {
if len(claims) == 0 {
return nil, nil
}
result := make(map[types.UID]error)
for _, claim := range claims {
err := np.unprepareResourceClaim(ctx, claim)
result[claim.UID] = err
if err != nil {
klog.Infof("error unpreparing ressources for claim %s/%s : %v", claim.Namespace, claim.Name, err)
}
}
return result, nil
}
func (np *NetworkDriver) unprepareResourceClaim(_ context.Context, claim kubeletplugin.NamespacedObject) error {
np.podConfigStore.DeleteClaim(claim.NamespacedName)
return nil
}
func (np *NetworkDriver) HandleError(ctx context.Context, err error, msg string) {
// For now we just follow the advice documented in the DRAPlugin API docs.
// See: https://pkg.go.dev/k8s.io/apimachinery/pkg/util/runtime#HandleErrorWithContext
runtime.HandleErrorWithContext(ctx, err, msg)
}