-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
416 lines (353 loc) · 10.4 KB
/
server.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
/**
* Copyright (2021, ) Institute of Software, Chinese Academy of Sciences
**/
package nvidia
import (
"context"
"errors"
"fmt"
"github.com/kubesys/client-go/pkg/kubesys"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
v1 "k8s.io/api/core/v1"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
"math"
"net"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"syscall"
"time"
)
// NvidiaDevicePlugin implements the Kubernetes device plugin API
type NvidiaDevicePlugin struct {
devices []*pluginapi.Device
physicalDeviceNameById map[uint]string
server *grpc.Server
socket string
stop chan struct{}
messenger *KubeMessenger
nodeName string
}
func NewNvidiaDevicePlugin(client *kubesys.KubernetesClient, nodeName string) *NvidiaDevicePlugin {
messenger := NewKubeMessenger(client, nodeName)
return &NvidiaDevicePlugin{
devices: nil,
physicalDeviceNameById: nil,
server: nil,
socket: ServerSock,
stop: nil,
messenger: messenger,
nodeName: nodeName,
}
}
// Start mainly starts the gRPC server and register the device plugin to Kubelet
func (p *NvidiaDevicePlugin) Start() error {
log.Infoln("Starting the device plugin")
p.devices, p.physicalDeviceNameById = GetDevices()
p.server = grpc.NewServer([]grpc.ServerOption{}...)
p.stop = make(chan struct{})
err := p.messenger.PatchGPUCount(uint(len(p.physicalDeviceNameById)), uint(100*len(p.physicalDeviceNameById)))
if err != nil {
p.cleanup()
return err
}
err = p.Serve()
if err != nil {
p.cleanup()
log.Warningf("Could not serve: %s.", err)
return err
}
err = p.Register(ResourceName)
if err != nil {
p.cleanup()
log.Warningf("Could not register device plugin: %s.", err)
return err
}
log.Infoln("Registered device plugin with Kubelet.")
return nil
}
func (p *NvidiaDevicePlugin) Stop() error {
log.Infoln("Stopping the device plugin.")
if p == nil || p.server == nil {
return nil
}
p.server.Stop()
p.cleanup()
if err := p.messenger.PatchGPUCount(0, 0); err != nil {
return err
}
if err := os.Remove(p.socket); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
func (p *NvidiaDevicePlugin) Serve() error {
err := syscall.Unlink(p.socket)
if err != nil && !os.IsNotExist(err) {
log.Errorf("Remove %s error %s.", p.socket, err)
return err
}
sock, err := net.Listen("unix", p.socket)
if err != nil {
return err
}
pluginapi.RegisterDevicePluginServer(p.server, p)
go p.server.Serve(sock)
conn, err := dial(p.socket, 5*time.Second)
if err != nil {
return err
}
conn.Close()
return nil
}
func (p *NvidiaDevicePlugin) Register(resourceName string) error {
conn, err := dial(pluginapi.KubeletSocket, 5*time.Second)
if err != nil {
return err
}
defer conn.Close()
client := pluginapi.NewRegistrationClient(conn)
req := &pluginapi.RegisterRequest{
Version: pluginapi.Version,
Endpoint: path.Base(p.socket),
ResourceName: resourceName,
}
_, err = client.Register(context.Background(), req)
if err != nil {
return err
}
return nil
}
func (p *NvidiaDevicePlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.DevicePlugin_ListAndWatchServer) error {
err := s.Send(&pluginapi.ListAndWatchResponse{Devices: p.devices})
if err != nil {
log.Fatalln("Failed to send devices.")
}
log.Infof("Send %d virtual devices.", len(p.devices))
select {
case <-p.stop:
return nil
}
}
func (p *NvidiaDevicePlugin) Allocate(ctx context.Context, reqs *pluginapi.AllocateRequest) (*pluginapi.AllocateResponse, error) {
log.Infoln("Allocating GPU...")
responses := pluginapi.AllocateResponse{}
var (
podReqGPUCount uint
found bool
assumePod *v1.Pod
)
if len(reqs.ContainerRequests) < 1 {
return nil, errors.New("no container request")
}
req := reqs.ContainerRequests[0]
podReqGPUCount += uint(len(req.DevicesIDs))
log.Infof("Pod request GPU count is %d.", podReqGPUCount)
pendingPods := p.messenger.GetPendingPodsOnNode()
var candidatePods []*v1.Pod
for _, pod := range pendingPods {
if isCandidatePod(pod) {
candidatePods = append(candidatePods, pod)
}
}
if candidatePods == nil || len(candidatePods) == 0 {
log.Warningln("There is no candidate pods.")
return nil, errors.New("no candidate pod")
}
candidatePods = sortPodByAssumeTime(candidatePods)
for _, pod := range candidatePods {
var resourceTotal uint = 0
for _, container := range pod.Spec.Containers {
if val, ok := container.Resources.Limits[ResourceName]; ok {
resourceTotal += uint(val.Value())
if resourceTotal == podReqGPUCount {
assumePod = pod
found = true
break
}
}
}
if found {
break
}
}
if !found {
log.Warningln("There is no assume pod.")
return nil, errors.New("no assume pod")
}
gpuId := getGPUIDFromPodAnnotation(assumePod)
if gpuId == "" {
return nil, errors.New("failed to get gpu id")
}
isOk := false
for i := 0; i < 100; i++ {
pod := p.messenger.GetPodOnNode(assumePod.Name, assumePod.Namespace)
if pod == nil {
log.Warningf("Failed to get pod %s, on ns %s.", assumePod.Name, assumePod.Namespace)
time.Sleep(time.Millisecond * 200)
continue
}
ready := getVCUDAReadyFromPodAnnotation(pod)
if ready != "" {
isOk = true
assumePod = pod
break
}
time.Sleep(time.Millisecond * 200)
}
if !isOk {
return nil, errors.New("vcuda not ready")
}
resp := &pluginapi.ContainerAllocateResponse{
Envs: make(map[string]string),
Mounts: make([]*pluginapi.Mount, 0),
Devices: make([]*pluginapi.DeviceSpec, 0),
Annotations: make(map[string]string),
}
resp.Devices = append(resp.Devices, &pluginapi.DeviceSpec{
ContainerPath: NvidiaCtlDevice,
HostPath: NvidiaCtlDevice,
Permissions: "rwm",
})
resp.Devices = append(resp.Devices, &pluginapi.DeviceSpec{
ContainerPath: NvidiaUVMDevice,
HostPath: NvidiaUVMDevice,
Permissions: "rwm",
})
resp.Envs = map[string]string{
EnvNvidiaDriverCapabilities: "compute,utility",
EnvPodName: assumePod.Name,
EnvNvidiaGPU: gpuId,
EnvResourceUUID: gpuId,
EnvResourceUsedByPod: fmt.Sprintf("%d", podReqGPUCount),
EnvResourceUsedByContainer: fmt.Sprintf("%d", uint(len(req.DevicesIDs))),
EnvResourceTotal: fmt.Sprintf("%d", len(p.devices)),
}
resp.Envs["LD_LIBRARY_PATH"] = "/usr/local/nvidia/lib64"
resp.Mounts = append(resp.Mounts, &pluginapi.Mount{
ContainerPath: "/usr/local/nvidia",
HostPath: DriverLibraryPath,
ReadOnly: true,
})
resp.Mounts = append(resp.Mounts, &pluginapi.Mount{
ContainerPath: VCUDA_MOUNTPOINT,
HostPath: filepath.Join(VirtualManagerPath, string(assumePod.UID)),
ReadOnly: true,
})
responses.ContainerResponses = append(responses.ContainerResponses, resp)
newPod := assumePod.DeepCopy()
newPod.Annotations[AnnAssignedFlag] = "true"
err := p.messenger.UpdatePodAnnotations(newPod)
if err != nil {
log.Errorf("Failed to update pod annotation for pod %s on ns %s.", assumePod.Name, assumePod.Namespace)
return nil, errors.New("failed to update pod annotation")
}
log.Infof("Pod %s in ns %s allocate gpu successed.", newPod.Name, newPod.Namespace)
return &responses, nil
}
func (p *NvidiaDevicePlugin) PreStartContainer(context.Context, *pluginapi.PreStartContainerRequest) (*pluginapi.PreStartContainerResponse, error) {
return &pluginapi.PreStartContainerResponse{}, nil
}
func (p *NvidiaDevicePlugin) GetDevicePluginOptions(context.Context, *pluginapi.Empty) (*pluginapi.DevicePluginOptions, error) {
return &pluginapi.DevicePluginOptions{}, nil
}
func (p *NvidiaDevicePlugin) GetPreferredAllocation(context.Context, *pluginapi.PreferredAllocationRequest) (*pluginapi.PreferredAllocationResponse, error) {
return &pluginapi.PreferredAllocationResponse{}, nil
}
func (p *NvidiaDevicePlugin) cleanup() {
close(p.stop)
p.devices = nil
p.physicalDeviceNameById = nil
p.server = nil
p.stop = nil
}
func dial(unixSocketPath string, timeout time.Duration) (*grpc.ClientConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
c, err := grpc.DialContext(ctx, unixSocketPath, grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}),
)
if err != nil {
return nil, err
}
return c, nil
}
func isCandidatePod(pod *v1.Pod) bool {
var resourceTotal uint = 0
for _, container := range pod.Spec.Containers {
if val, ok := container.Resources.Limits[ResourceName]; ok {
resourceTotal += uint(val.Value())
}
}
if resourceTotal <= uint(0) {
return false
}
if _, ok := pod.ObjectMeta.Annotations[AnnResourceAssumeTime]; !ok {
return false
}
if assigned, ok := pod.ObjectMeta.Annotations[AnnAssignedFlag]; ok {
if assigned == "false" {
return true
} else {
return false
}
} else {
return false
}
}
func getVCUDAReadyFromPodAnnotation(pod *v1.Pod) string {
ready := ""
if len(pod.ObjectMeta.Annotations) > 0 {
value, found := pod.ObjectMeta.Annotations[AnnVCUDAReady]
if found {
ready = value
} else {
log.Errorf("Failed to get vcuda flag for pod %s in ns %s.", pod.Name, pod.Namespace)
}
}
return ready
}
func getGPUIDFromPodAnnotation(pod *v1.Pod) (uuid string) {
uuid = ""
if len(pod.ObjectMeta.Annotations) > 0 {
value, found := pod.ObjectMeta.Annotations[AnnResourceUUID]
if found {
uuid = value
} else {
log.Errorf("Failed to get dev id for pod %s in ns %s.", pod.Name, pod.Namespace)
}
}
return uuid
}
func sortPodByAssumeTime(pods []*v1.Pod) []*v1.Pod {
podList := make(orderedPodByAssumeTime, 0, len(pods))
for _, v := range pods {
podList = append(podList, v)
}
sort.Sort(podList)
return podList
}
type orderedPodByAssumeTime []*v1.Pod
func (this orderedPodByAssumeTime) Len() int {
return len(this)
}
func (this orderedPodByAssumeTime) Less(i, j int) bool {
return getAssumeTimeFromPodAnnotation(this[i]) <= getAssumeTimeFromPodAnnotation(this[j])
}
func (this orderedPodByAssumeTime) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
func getAssumeTimeFromPodAnnotation(pod *v1.Pod) uint64 {
if assumeTime, ok := pod.Annotations[AnnResourceAssumeTime]; ok {
predicateTime, err := strconv.ParseUint(assumeTime, 10, 64)
if err == nil {
return predicateTime
}
}
return math.MaxUint64
}