-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathresources_manager.go
420 lines (358 loc) · 11.9 KB
/
resources_manager.go
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
/*----------------------------------------------------
2023 NVIDIA CORPORATION & AFFILIATES
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
http://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 resources
import (
"encoding/json"
"fmt"
"log"
"os"
"regexp"
"strconv"
"time"
"github.com/jaypipes/ghw"
"github.com/vishvananda/netlink"
"github.com/Mellanox/k8s-rdma-shared-dev-plugin/pkg/cdi"
"github.com/Mellanox/k8s-rdma-shared-dev-plugin/pkg/types"
"github.com/Mellanox/k8s-rdma-shared-dev-plugin/pkg/utils"
)
const (
// General constants
DefaultConfigFilePath = "/k8s-rdma-shared-dev-plugin/config.json"
kubeEndPoint = "kubelet.sock"
socketSuffix = "sock"
rdmaHcaResourcePrefix = "rdma"
// PCI related constants
netClass = 0x02 // Device class - Network controller
maxVendorNameLength = 20
maxProductNameLength = 40
// Default periodic update interval
defaultPeriodicUpdateInterval = 60 * time.Second
// RDMA subsystem network namespace mode
rdmaExclusive = "exclusive"
)
var (
activeSockDir = "/var/lib/kubelet/plugins_registry"
deprecatedSockDir = "/var/lib/kubelet/device-plugins"
)
// resourceManager for plugin
type resourceManager struct {
configFile string
defaultResourcePrefix string
socketSuffix string
watchMode bool
configList []*types.UserConfig
resourceServers []types.ResourceServer
deviceList []*ghw.PCIDevice
netlinkManager types.NetlinkManager
rds types.RdmaDeviceSpec
PeriodicUpdateInterval time.Duration
useCdi bool
}
func NewResourceManager(configFile string, useCdi bool) types.ResourceManager {
watcherMode := detectPluginWatchMode(activeSockDir)
if watcherMode {
fmt.Println("Using Kubelet Plugin Registry Mode")
} else {
fmt.Println("Using Deprecated Devie Plugin Registry Path")
}
return &resourceManager{
configFile: configFile,
defaultResourcePrefix: rdmaHcaResourcePrefix,
socketSuffix: socketSuffix,
watchMode: watcherMode,
netlinkManager: &netlinkManager{},
rds: NewRdmaDeviceSpec(requiredRdmaDevices),
useCdi: useCdi,
}
}
// ReadConfig to read configs
func (rm *resourceManager) ReadConfig() error {
log.Println("Reading", rm.configFile)
raw, err := os.ReadFile(rm.configFile)
if err != nil {
return err
}
config := &types.UserConfigList{}
if err := json.Unmarshal(raw, config); err != nil {
return err
}
log.Printf("loaded config: %+v \n", config.ConfigList)
// if periodic update is not set then use the default value
if config.PeriodicUpdateInterval == nil {
log.Println("no periodic update interval is set, use default interval 60 seconds")
rm.PeriodicUpdateInterval = defaultPeriodicUpdateInterval
} else {
PeriodicUpdateInterval := *config.PeriodicUpdateInterval
if PeriodicUpdateInterval == 0 {
log.Println("warning: periodic update interval is 0, no periodic update will run")
} else {
log.Printf("periodic update interval: %+d \n", PeriodicUpdateInterval)
}
rm.PeriodicUpdateInterval = time.Duration(PeriodicUpdateInterval) * time.Second
}
for i := range config.ConfigList {
rm.configList = append(rm.configList, &config.ConfigList[i])
}
return nil
}
// ValidateConfigs validate the configurations
func (rm *resourceManager) ValidateConfigs() error {
resourceName := make(map[string]string)
if rm.PeriodicUpdateInterval < 0 {
return fmt.Errorf("invalid \"periodicUpdateInterval\" configuration \"%d\"", rm.PeriodicUpdateInterval)
}
if len(rm.configList) < 1 {
return fmt.Errorf("no resources configuration found")
}
for _, conf := range rm.configList {
// check if name contains acceptable characters
if !validResourceName(conf.ResourceName) {
return fmt.Errorf("error: resource name \"%s\" contains invalid characters", conf.ResourceName)
}
// check resource names are unique
_, ok := resourceName[conf.ResourceName]
if ok {
// resource name already exist
return fmt.Errorf("error: resource name \"%s\" already exists", conf.ResourceName)
}
// If prefix is not configured - use the default one
if conf.ResourcePrefix == "" {
conf.ResourcePrefix = rm.defaultResourcePrefix
}
if conf.RdmaHcaMax < 0 {
return fmt.Errorf("error: Invalid value for rdmaHcaMax < 0: %d", conf.RdmaHcaMax)
}
isEmptySelector := utils.IsEmptySelector(&(conf.Selectors))
if isEmptySelector && len(conf.Devices) == 0 {
return fmt.Errorf("error: configuration missmatch. neither \"selectors\" nor \"devices\" fields exits," +
" it is recommended to use the new “selectors” field")
}
// If both "selectors" and "devices" fields are provided then fail with confusion
if !isEmptySelector && len(conf.Devices) > 0 {
return fmt.Errorf("configuration mismatch. Cannot specify both \"selectors\" and \"devices\" fields")
} else if isEmptySelector { // If no "selector" then use devices as IfNames selector
log.Println("Warning: \"devices\" field is deprecated, it is recommended to use the new “selectors” field")
conf.Selectors.IfNames = conf.Devices
}
resourceName[conf.ResourceName] = conf.ResourceName
}
return nil
}
// ValidateRdmaSystemMode ensure RDMA subsystem network namespace mode is shared
func (rm *resourceManager) ValidateRdmaSystemMode() error {
mode, err := netlink.RdmaSystemGetNetnsMode()
if err != nil {
if err.Error() == "invalid argument" {
log.Printf("too old kernel to get RDMA subsystem")
return nil
}
return fmt.Errorf("can not get RDMA subsystem network namespace mode")
}
if mode == rdmaExclusive {
return fmt.Errorf("incorrect RDMA subsystem network namespace")
}
return nil
}
// InitServers init server
func (rm *resourceManager) InitServers() error {
for _, config := range rm.configList {
log.Printf("Resource: %+v\n", config)
devices := rm.GetDevices()
filteredDevices := rm.GetFilteredDevices(devices, &config.Selectors)
// NOTE: it's a temporary workaround to bring interfaces in UP state until
// Network Operator will be able to do it.
for _, device := range filteredDevices {
link, err := rm.netlinkManager.LinkByName(device.GetIfName())
if err != nil {
log.Printf("Warning: InitServers(): unable to get NIC info: %s", err)
continue
}
if rm.netlinkManager.LinkSetUp(link) != nil {
log.Printf("Warning: InitServers(): unable to set NIC %s to up state: %s", device.GetIfName(), err)
continue
}
}
if len(filteredDevices) == 0 {
log.Printf("Warning: no devices in device pool, creating empty resource server for %s", config.ResourceName)
}
if rm.useCdi {
err := cdi.CleanupSpecs(cdiResourcePrefix)
if err != nil {
return err
}
}
rs, err := newResourceServer(config, filteredDevices, rm.watchMode, rm.socketSuffix, rm.useCdi)
if err != nil {
return err
}
rm.resourceServers = append(rm.resourceServers, rs)
}
return nil
}
// StartAllServers start all servers
func (rm *resourceManager) StartAllServers() error {
for _, rs := range rm.resourceServers {
if err := rs.Start(); err != nil {
return err
}
// start watcher
if !rm.watchMode {
go rs.Watch()
}
}
return nil
}
// StopAllServers stop all servers
func (rm *resourceManager) StopAllServers() error {
if rm.useCdi {
err := cdi.CleanupSpecs(cdiResourcePrefix)
if err != nil {
return err
}
}
for _, rs := range rm.resourceServers {
if err := rs.Stop(); err != nil {
return err
}
}
return nil
}
// RestartAllServers restart all servers
func (rm *resourceManager) RestartAllServers() error {
if rm.useCdi {
err := cdi.CleanupSpecs(cdiResourcePrefix)
if err != nil {
return err
}
}
for _, rs := range rm.resourceServers {
if err := rs.Restart(); err != nil {
return err
}
}
return nil
}
func validResourceName(name string) bool {
// name regex
var validString = regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
return validString.MatchString(name)
}
func (rm *resourceManager) DiscoverHostDevices() error {
log.Println("discovering host network devices")
pci, err := ghw.PCI()
if err != nil {
return fmt.Errorf("error getting PCI info: %v", err)
}
devices := pci.ListDevices()
if len(devices) == 0 {
log.Println("Warning: DiscoverHostDevices(): no PCI network device found")
}
// cleanup deviceList as this method is also called during periodic update for the resources
rm.deviceList = []*ghw.PCIDevice{}
for _, device := range devices {
devClass, err := strconv.ParseInt(device.Class.ID, 16, 64)
if err != nil {
log.Printf("Warning: DiscoverHostDevices(): unable to parse device class for device %+v %q", device,
err)
continue
}
if devClass != netClass {
continue
}
vendor := device.Vendor
vendorName := vendor.Name
if len(vendor.Name) > maxVendorNameLength {
vendorName = string([]byte(vendorName)[0:17]) + "..."
}
product := device.Product
productName := product.Name
if len(product.Name) > maxProductNameLength {
productName = string([]byte(productName)[0:37]) + "..."
}
log.Printf("DiscoverHostDevices(): device found: %-12s\t%-12s\t%-20s\t%-40s", device.Address,
device.Class.ID, vendorName, productName)
rm.deviceList = append(rm.deviceList, device)
}
return nil
}
func (rm *resourceManager) GetDevices() []types.PciNetDevice {
newPciDevices := make([]types.PciNetDevice, 0)
for _, device := range rm.deviceList {
if newDevice, err := NewPciNetDevice(device, rm.rds, rm.netlinkManager); err == nil {
newPciDevices = append(newPciDevices, newDevice)
} else {
log.Printf("error creating new device: %q", err)
}
}
return newPciDevices
}
func (rm *resourceManager) GetFilteredDevices(devices []types.PciNetDevice,
selector *types.Selectors) []types.PciNetDevice {
filteredDevice := devices
// filter by Vendors list
if selector.Vendors != nil && len(selector.Vendors) > 0 {
filteredDevice = NewVendorSelector(selector.Vendors).Filter(filteredDevice)
}
// filter by DeviceIDs list
if selector.DeviceIDs != nil && len(selector.DeviceIDs) > 0 {
filteredDevice = NewDeviceSelector(selector.DeviceIDs).Filter(filteredDevice)
}
// filter by Driver list
if selector.Drivers != nil && len(selector.Drivers) > 0 {
filteredDevice = NewDriverSelector(selector.Drivers).Filter(filteredDevice)
}
// filter by IfNames list
if selector.IfNames != nil && len(selector.IfNames) > 0 {
filteredDevice = NewIfNameSelector(selector.IfNames).Filter(filteredDevice)
}
// filter by LinkType list
if selector.LinkTypes != nil && len(selector.LinkTypes) > 0 {
filteredDevice = NewLinkTypeSelector(selector.LinkTypes).Filter(filteredDevice)
}
newDeviceList := make([]types.PciNetDevice, len(filteredDevice))
copy(newDeviceList, filteredDevice)
return newDeviceList
}
func (rm *resourceManager) PeriodicUpdate() func() {
stopChan := make(chan interface{})
if rm.PeriodicUpdateInterval > 0 {
ticker := time.NewTicker(rm.PeriodicUpdateInterval)
// Listen for update or stop update channels
go func() {
for {
select {
case <-ticker.C:
if err := rm.DiscoverHostDevices(); err != nil {
log.Printf("error: failed to discover host devices: %v", err)
continue
}
for index, rs := range rm.resourceServers {
devices := rm.GetDevices()
filteredDevices := rm.GetFilteredDevices(devices, &rm.configList[index].Selectors)
rs.UpdateDevices(filteredDevices)
}
case <-stopChan:
ticker.Stop()
return
}
}
}()
}
// Return stop function
return func() {
if rm.PeriodicUpdateInterval > 0 {
stopChan <- true
close(stopChan)
}
}
}