forked from intel/intel-device-plugins-for-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlb_plugin.go
116 lines (94 loc) · 2.93 KB
/
dlb_plugin.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
// Copyright 2021 Intel Corporation. All Rights Reserved.
//
// 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 main
import (
"flag"
"path/filepath"
"reflect"
"time"
"k8s.io/klog/v2"
"github.com/intel/intel-device-plugins-for-kubernetes/cmd/internal/pluginutils"
dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)
const (
dlbDeviceFilePathRE = "/dev/dlb*"
namespace = "dlb.intel.com"
deviceTypePF = "pf"
deviceTypeVF = "vf"
sysfsDir = "/sys/class/dlb2"
// Period of device scans.
scanPeriod = 5 * time.Second
)
type DevicePlugin struct {
scanTicker *time.Ticker
scanDone chan bool
dlbDeviceFilePathReg string
sysfsDir string
}
func NewDevicePlugin(dlbDeviceFilePathReg string, sysfsDir string) *DevicePlugin {
return &DevicePlugin{
dlbDeviceFilePathReg: dlbDeviceFilePathReg,
sysfsDir: sysfsDir,
scanTicker: time.NewTicker(scanPeriod),
scanDone: make(chan bool, 1), // buffered as we may send to it before Scan starts receiving from it
}
}
func (dp *DevicePlugin) Scan(notifier dpapi.Notifier) error {
defer dp.scanTicker.Stop()
var prevDevTree dpapi.DeviceTree
for {
devTree := dp.scan()
if !reflect.DeepEqual(prevDevTree, devTree) {
klog.V(1).Info("DLB scan update: pf: ", len(devTree[deviceTypePF]), " / vf: ", len(devTree[deviceTypeVF]))
prevDevTree = devTree
}
notifier.Notify(devTree)
select {
case <-dp.scanDone:
return nil
case <-dp.scanTicker.C:
}
}
}
func (dp *DevicePlugin) scan() dpapi.DeviceTree {
files, _ := filepath.Glob(dp.dlbDeviceFilePathReg)
devTree := dpapi.NewDeviceTree()
for _, file := range files {
devs := []pluginapi.DeviceSpec{{
HostPath: file,
ContainerPath: file,
Permissions: "rw",
}}
deviceInfo := dpapi.NewDeviceInfo(pluginapi.Healthy, devs, nil, nil, nil, nil)
sysfsDev := filepath.Join(dp.sysfsDir, filepath.Base(file))
sriovNumVFs := pluginutils.GetSriovNumVFs(sysfsDev)
switch sriovNumVFs {
case "0":
devTree.AddDevice(deviceTypePF, file, deviceInfo)
case "-1":
devTree.AddDevice(deviceTypeVF, file, deviceInfo)
default:
continue
}
}
return devTree
}
func main() {
flag.Parse()
klog.V(1).Infof("DLB device plugin started")
plugin := NewDevicePlugin(dlbDeviceFilePathRE, sysfsDir)
manager := dpapi.NewManager(namespace, plugin)
manager.Run()
}