This repository was archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.go
More file actions
124 lines (105 loc) · 3.63 KB
/
main.go
File metadata and controls
124 lines (105 loc) · 3.63 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
/*
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"
"fmt"
"github.com/aarnaud/k8s-directx-device-plugin/pkg/gpu-detection"
"github.com/golang/glog"
pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
dm "k8s.io/kubernetes/pkg/kubelet/cm/devicemanager"
"os"
"path"
"time"
)
const (
resourceName = "microsoft.com/directx"
deviceClass = "class/5B45201D-F2F2-4F3B-85BB-30FF1F953599"
)
// stubAllocFunc creates and returns allocation response for the input allocate request
func allocFunc(r *pluginapi.AllocateRequest, devs map[string]pluginapi.Device) (*pluginapi.AllocateResponse, error) {
var responses pluginapi.AllocateResponse
for _, req := range r.ContainerRequests {
response := &pluginapi.ContainerAllocateResponse{}
for _, requestID := range req.DevicesIDs {
gpu := gpu_detection.GetGPUInfo(requestID)
if gpu == nil {
return nil, fmt.Errorf("invalid allocation request with non-existing device %s", requestID)
}
if getGPUHealth(gpu) != pluginapi.Healthy {
return nil, fmt.Errorf("invalid allocation request with unhealthy device: %s", requestID)
}
response.Devices = append(response.Devices, &pluginapi.DeviceSpec{
HostPath: deviceClass,
ContainerPath: "",
Permissions: "",
})
if response.Envs == nil {
response.Envs = make(map[string]string)
}
response.Envs["DIRECTX_GPU_Name"] = gpu.Name
response.Envs["DIRECTX_GPU_PNPDeviceID"] = gpu.PNPDeviceID
response.Envs["DIRECTX_GPU_DriverVersion"] = gpu.DriverVersion
}
responses.ContainerResponses = append(responses.ContainerResponses, response)
}
return &responses, nil
}
func getGPUHealth(gpu *gpu_detection.GPUInfo) string {
if gpu.IsStatusOK() {
return pluginapi.Healthy
}
return pluginapi.Unhealthy
}
func main() {
flag.Set("alsologtostderr", "true")
devs := []*pluginapi.Device{}
gpus:= gpu_detection.GetGPUList()
pluginSocksDir := os.Getenv("PLUGIN_SOCK_DIR")
if pluginSocksDir == "" {
pluginSocksDir = pluginapi.DevicePluginPath
}
gpuMatchName := os.Getenv("DIRECTX_GPU_MATCH_NAME")
if gpuMatchName == "" {
gpuMatchName = "nvidia"
}
for _, gpuInfo := range gpus{
if !gpuInfo.MatchName(gpuMatchName) {
glog.Warningf("'%s' doesn't match '%s', ignoring this gpu", gpuInfo.Name, gpuMatchName)
continue
}
devs = append(devs, &pluginapi.Device{
ID: gpuInfo.PNPDeviceID,
Health: getGPUHealth(&gpuInfo),
})
glog.Infof("GPU %s id: %s", gpuInfo.Name, gpuInfo.PNPDeviceID)
}
glog.Infof("pluginSocksDir: %s", pluginSocksDir)
socketPath := path.Join(pluginSocksDir, "directx.sock")
glog.Infof("socketPath: %s", socketPath)
dp1 := dm.NewDevicePluginStub(devs, socketPath, resourceName, false)
if err := dp1.Start(); err != nil {
panic(err)
}
dp1.SetAllocFunc(allocFunc)
// todo: when kubelet will success to autodetect socket, change the pluginSockDir to detect DEPRECATION file
if err := dp1.Register(pluginapi.KubeletSocket, resourceName, ""); err != nil {
panic(err)
}
for {
time.Sleep(time.Second*10)
if _, err := os.Stat(socketPath); os.IsNotExist(err) {
// exit if the socketPath is missing, cause by kubelet restart, we need to start again the plugin
os.Exit(1)
}
}
}