/
deviceplugin.go
256 lines (224 loc) · 6.83 KB
/
deviceplugin.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
package deviceplugin
import (
"context"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"time"
"google.golang.org/grpc"
"gopkg.in/fsnotify.v1"
devicepluginv1beta1 "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
)
const (
devicePluginsDir = "/var/lib/kubelet/device-plugins"
kubeletSocketName = "kubelet.sock"
socketNamePrefix = "virtink-"
resourceNamePrefix = "devices.virtink.io/"
)
type devicePluginManager struct {
devicePlugins []*devicePlugin
}
func NewDevicePluginManager() *devicePluginManager {
return &devicePluginManager{
devicePlugins: []*devicePlugin{
newDevicePlugin("kvm", "/dev/kvm", 1000),
newDevicePlugin("tun", "/dev/net/tun", 1000),
},
}
}
func (dpm *devicePluginManager) Start(ctx context.Context) error {
for _, dp := range dpm.devicePlugins {
dp.Start()
}
<-ctx.Done()
for _, dp := range dpm.devicePlugins {
dp.Stop()
}
return nil
}
type devicePlugin struct {
deviceName string
devicePath string
devices []*devicepluginv1beta1.Device
socketPath string
server *grpc.Server
health chan string
}
func newDevicePlugin(deviceName string, devicePath string, deviceCount int) *devicePlugin {
dp := &devicePlugin{
deviceName: deviceName,
devicePath: devicePath,
}
for i := 1; i <= deviceCount; i++ {
dp.devices = append(dp.devices, &devicepluginv1beta1.Device{
ID: deviceName + strconv.Itoa(i),
Health: devicepluginv1beta1.Healthy,
})
}
return dp
}
func (dp *devicePlugin) Start() {
go func() {
for {
if err := dp.run(); err != nil {
ctrl.Log.Error(err, "run device plugin", "device", dp.deviceName)
}
time.Sleep(5 * time.Second)
}
}()
}
func (dp *devicePlugin) run() error {
dp.server = grpc.NewServer()
devicepluginv1beta1.RegisterDevicePluginServer(dp.server, dp)
dp.socketPath = filepath.Join(devicePluginsDir, fmt.Sprintf("%s%s.sock", socketNamePrefix, dp.deviceName))
_ = os.Remove(dp.socketPath)
listener, err := net.Listen("unix", dp.socketPath)
if err != nil {
return fmt.Errorf("failed to listen %s: %s", dp.socketPath, err)
}
defer listener.Close()
errChan := make(chan error, 1)
go func() {
errChan <- dp.server.Serve(listener)
}()
if err := waitForGRPCServer(dp.socketPath, 5*time.Second); err != nil {
return err
}
if err := dp.register(); err != nil {
return err
}
go func() {
errChan <- dp.healthCheck()
}()
err = <-errChan
return err
}
func (dp *devicePlugin) register() error {
kubeletSocket := filepath.Join(devicePluginsDir, kubeletSocketName)
conn, err := grpc.Dial(fmt.Sprintf("unix://%s", kubeletSocket), grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return fmt.Errorf("dial %s: %s", kubeletSocket, err)
}
defer conn.Close()
client := devicepluginv1beta1.NewRegistrationClient(conn)
req := &devicepluginv1beta1.RegisterRequest{
Version: "v1beta1",
Endpoint: filepath.Base(dp.socketPath),
ResourceName: fmt.Sprintf("%s%s", resourceNamePrefix, dp.deviceName),
}
if _, err := client.Register(context.Background(), req); err != nil {
return fmt.Errorf("register to kubelet: %s", err)
}
return nil
}
func (dp *devicePlugin) Stop() {
dp.server.Stop()
os.RemoveAll(dp.socketPath)
}
func (dp *devicePlugin) GetDevicePluginOptions(ctx context.Context, req *devicepluginv1beta1.Empty) (*devicepluginv1beta1.DevicePluginOptions, error) {
return &devicepluginv1beta1.DevicePluginOptions{}, nil
}
func (dp *devicePlugin) ListAndWatch(req *devicepluginv1beta1.Empty, stream devicepluginv1beta1.DevicePlugin_ListAndWatchServer) error {
resp := &devicepluginv1beta1.ListAndWatchResponse{Devices: dp.devices}
stream.Send(resp)
tick := time.NewTicker(30 * time.Second)
for {
select {
case health := <-dp.health:
for _, dev := range dp.devices {
dev.Health = health
}
resp := &devicepluginv1beta1.ListAndWatchResponse{Devices: dp.devices}
if err := stream.Send(resp); err != nil {
return fmt.Errorf("send response: %s", err)
}
case <-tick.C:
resp := &devicepluginv1beta1.ListAndWatchResponse{Devices: dp.devices}
if err := stream.Send(resp); err != nil {
return fmt.Errorf("send response: %s", err)
}
}
}
}
func (dp *devicePlugin) Allocate(ctx context.Context, req *devicepluginv1beta1.AllocateRequest) (*devicepluginv1beta1.AllocateResponse, error) {
var containerResps []*devicepluginv1beta1.ContainerAllocateResponse
for _, containerReq := range req.ContainerRequests {
var devices []*devicepluginv1beta1.DeviceSpec
for i := 0; i < len(containerReq.DevicesIDs); i++ {
devices = append(devices, &devicepluginv1beta1.DeviceSpec{
HostPath: dp.devicePath,
ContainerPath: dp.devicePath,
Permissions: "rwm",
})
}
containerResp := &devicepluginv1beta1.ContainerAllocateResponse{
Devices: devices,
}
containerResps = append(containerResps, containerResp)
}
return &devicepluginv1beta1.AllocateResponse{
ContainerResponses: containerResps,
}, nil
}
func (dp *devicePlugin) GetPreferredAllocation(context.Context, *devicepluginv1beta1.PreferredAllocationRequest) (*devicepluginv1beta1.PreferredAllocationResponse, error) {
return nil, nil
}
func (dp *devicePlugin) PreStartContainer(context.Context, *devicepluginv1beta1.PreStartContainerRequest) (*devicepluginv1beta1.PreStartContainerResponse, error) {
return nil, nil
}
func waitForGRPCServer(socketPath string, timeout time.Duration) error {
c, err := grpc.Dial(socketPath,
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithTimeout(timeout),
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}),
)
if err != nil {
return err
}
c.Close()
return nil
}
func (dp *devicePlugin) healthCheck() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("create watcher: %s", err)
}
defer watcher.Close()
if err := watcher.Add(filepath.Dir(dp.devicePath)); err != nil {
return fmt.Errorf("start watching %s: %s", dp.devicePath, err)
}
if err := watcher.Add(filepath.Dir(dp.socketPath)); err != nil {
return fmt.Errorf("start watching %s: %s", dp.socketPath, err)
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
continue
}
if dp.devicePath == event.Name {
if event.Op == fsnotify.Remove || event.Op == fsnotify.Rename {
ctrl.Log.Info("device file was removed", "device", dp.devicePath)
dp.health <- devicepluginv1beta1.Unhealthy
} else if event.Op == fsnotify.Create {
ctrl.Log.Info("device file was created", "device", dp.devicePath)
dp.health <- devicepluginv1beta1.Healthy
}
} else if dp.socketPath == event.Name && event.Op == fsnotify.Remove {
ctrl.Log.Info("device socket file was removed", "socket", dp.socketPath)
return nil
}
case err, ok := <-watcher.Errors:
if !ok {
continue
}
ctrl.Log.Error(err, "watching device file and socket", "device", dp.deviceName)
}
}
}