forked from 4paradigm/k8s-vgpu-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vdevice-controller.go
286 lines (269 loc) · 7.52 KB
/
vdevice-controller.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
package main
import (
"k8s.io/apimachinery/pkg/labels"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
"k8s.io/kubernetes/pkg/kubelet/cm/devicemanager/checkpoint"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
listerscorev1 "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
)
const (
annRequest = "4paradigm.com/vgpu-request"
annUsing = "4paradigm.com/vgpu-using"
annSep = ","
)
const kubeletDeviceManagerCheckpoint = "kubelet_internal_checkpoint"
// VDeviceController vdevice id manager
type VDeviceController struct {
nodeName string
mux sync.Mutex
stopCh chan struct{}
idMap map[string]string
podLister listerscorev1.PodLister
checkpointManager checkpointmanager.CheckpointManager
}
// newVDeviceController new VDeviceController
func newVDeviceController(deviceIDs []string) *VDeviceController {
m := &VDeviceController{
nodeName: "",
stopCh: make(chan struct{}),
idMap: make(map[string]string),
}
for _, v := range deviceIDs {
m.idMap[v] = ""
}
var err error
m.checkpointManager, err = checkpointmanager.NewCheckpointManager(pluginapi.DevicePluginPath)
check(err)
return m
}
// updateFromCheckpoint update devices from kubelet device checkpoint
func (m *VDeviceController) updateFromCheckpoint() error {
registeredDevs := make(map[string][]string)
devEntries := make([]checkpoint.PodDevicesEntry, 0)
cp := checkpoint.New(devEntries, registeredDevs)
err := m.checkpointManager.GetCheckpoint(kubeletDeviceManagerCheckpoint, cp)
if err != nil {
log.Printf("Error: read checkpoint error, %v\n", err)
return err
}
pods, err := m.podLister.Pods("").List(labels.Everything())
podDevices, _ := cp.GetData()
for _, pde := range podDevices {
if pde.ResourceName != "nvidia.com/gpu" {
continue
}
allocResp := &pluginapi.ContainerAllocateResponse{}
err = allocResp.Unmarshal(pde.AllocResp)
if err != nil {
log.Printf("Error: unmarshal container allocate response failed\n")
continue
}
requestStr := allocResp.Annotations[annRequest]
usingStr := allocResp.Annotations[annUsing]
if requestStr == "" && usingStr == "" {
continue
}
request := strings.Split(requestStr, annSep)
using := strings.Split(usingStr, annSep)
var pod *v1.Pod = nil
for _, p := range pods {
if string(p.UID) == pde.PodUID {
pod = p
break
}
}
//pod, err := m.podLister.Pods("").Get(pde.PodUID)
if pod != nil && (pod.Status.Phase == v1.PodPending || pod.Status.Phase == v1.PodRunning) {
m.acquire(request, using)
} else {
if verboseFlag > 5 {
if pod == nil {
log.Printf("Debug: pod %v not found\n", pde.PodUID)
} else {
log.Printf("Debug: pod %v status %v\n", pde.PodUID, pod.Status.Phase)
}
log.Printf("Debug: release from checkpoint: %v\n", using)
}
m.release(using)
}
}
return nil
}
// onAddPod add pod callback func
func (m *VDeviceController) onAddPod(pod *v1.Pod) {
requestStr := pod.Annotations[annRequest]
usingStr := pod.Annotations[annUsing]
if requestStr == "" && usingStr == "" {
return
}
request := strings.Split(requestStr, annSep)
using := strings.Split(usingStr, annSep)
if verboseFlag > 5 {
log.Printf("Debug: using devices %s\n", usingStr)
}
m.acquire(request, using)
}
// onUpdatePod update pod callback func
func (m *VDeviceController) onUpdatePod(oldPod, newPod *v1.Pod) {
oldRequestStr := oldPod.Annotations[annRequest]
oldUsingStr := oldPod.Annotations[annUsing]
newRequestStr := newPod.Annotations[annRequest]
newUsingStr := newPod.Annotations[annUsing]
if oldRequestStr == newRequestStr && oldUsingStr == newUsingStr {
return
}
if oldRequestStr != "" || oldUsingStr != "" {
log.Printf("Error: vgpu changed, %s->%s, %s->%s\n", oldRequestStr, newRequestStr, oldUsingStr, newUsingStr)
}
newRequest := strings.Split(newRequestStr, annSep)
newUsing := strings.Split(newUsingStr, annSep)
if verboseFlag > 5 {
log.Printf("Debug: using devices %s -> %s\n", oldUsingStr, newUsingStr)
}
m.acquire(newRequest, newUsing)
}
// onDeletePod delete pod callback func
func (m *VDeviceController) onDeletePod(pod *v1.Pod) {
usingStr := pod.Annotations[annUsing]
if usingStr == "" {
return
}
usingIDs := strings.Split(usingStr, annSep)
if verboseFlag > 5 {
log.Printf("Debug: release devices %s\n", usingStr)
}
m.release(usingIDs)
}
// initialize initialize vdevice manager
func (m *VDeviceController) initialize() {
m.nodeName = os.Getenv("NODE_NAME")
if m.nodeName == "" {
log.Panicln("Fatal: must set NODE_NAME")
}
kubeConfig := os.Getenv("KUBECONFIG")
if kubeConfig == "" {
kubeConfig = filepath.Join(os.Getenv("HOME"), ".kube", "config")
}
config, err := rest.InClusterConfig()
if err != nil {
config, err = clientcmd.BuildConfigFromFlags("", kubeConfig)
check(err)
}
client, err := kubernetes.NewForConfig(config)
check(err)
selector := fields.SelectorFromSet(fields.Set{"spec.nodeName": m.nodeName})
informerFactory := informers.NewSharedInformerFactoryWithOptions(
client,
time.Hour*1,
informers.WithTweakListOptions(
func(options *metav1.ListOptions) {
options.FieldSelector = selector.String()
},
),
)
podInformer := informerFactory.Core().V1().Pods()
m.podLister = podInformer.Lister()
//informer := podInformer.Informer()
//informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
// AddFunc: func(obj interface{}) {
// if pod, ok := obj.(*v1.Pod); ok {
// m.onAddPod(pod)
// } else {
// log.Println("Unknown add pod")
// }
// },
// UpdateFunc: func(oldObj, newObj interface{}) {
// oldPod, ok := oldObj.(*v1.Pod)
// if !ok {
// log.Println("Unknown update old pod")
// }
// newPod, ok := newObj.(*v1.Pod)
// if !ok {
// log.Println("Unknown update new pod")
// }
// m.onUpdatePod(oldPod, newPod)
// },
// DeleteFunc: func(obj interface{}) {
// if pod, ok := obj.(*v1.Pod); ok {
// m.onDeletePod(pod)
// } else {
// log.Println("Unknown delete pod")
// }
// },
//},
//)
m.stopCh = make(chan struct{})
informerFactory.Start(m.stopCh)
informerFactory.WaitForCacheSync(m.stopCh)
}
// cleanup finalize vdevice manager
func (m *VDeviceController) cleanup() {
close(m.stopCh)
}
// available get available device ids
func (m *VDeviceController) available() []string {
m.mux.Lock()
defer m.mux.Unlock()
ids := make([]string, 0, len(m.idMap))
for k, v := range m.idMap {
if v == "" {
ids = append(ids, k)
}
}
return ids
}
// acquire acquire device ids
func (m *VDeviceController) acquire(request, using []string) {
m.mux.Lock()
defer m.mux.Unlock()
for i, v := range using {
if _, ok := m.idMap[v]; !ok {
log.Printf("Error: device %s unknown\n", v)
continue
}
if i < len(request) {
m.idMap[v] = request[i]
} else {
log.Printf("Error: %s mismatched\n", v)
m.idMap[v] = "mismatched"
}
}
}
// release release device ids
func (m *VDeviceController) release(using []string) {
m.mux.Lock()
defer m.mux.Unlock()
for _, v := range using {
if _, ok := m.idMap[v]; ok {
m.idMap[v] = ""
} else {
log.Printf("Error: device %s unknown\n", v)
}
}
}
// releaseByRequest release device ids by request ids
func (m *VDeviceController) releaseByRequest(request []string) {
m.mux.Lock()
defer m.mux.Unlock()
for k, v := range m.idMap {
for _, r := range request {
if v == r {
log.Printf("Error: device %s[%s] loss.\n", k, v)
m.idMap[k] = ""
}
}
}
}