/
fluence.go
396 lines (330 loc) · 12 KB
/
fluence.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
/*
Copyright 2022 The Kubernetes Authors.
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 fluence
import (
"context"
"fmt"
"os"
"sync"
"time"
"google.golang.org/grpc"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
clientscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/cache"
corev1helpers "k8s.io/component-helpers/scheduling/corev1"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/scheduler/framework"
"k8s.io/kubernetes/pkg/scheduler/metrics"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/scheduler-plugins/apis/scheduling/v1alpha1"
coschedulingcore "sigs.k8s.io/scheduler-plugins/pkg/coscheduling/core"
fcore "sigs.k8s.io/scheduler-plugins/pkg/fluence/core"
pb "sigs.k8s.io/scheduler-plugins/pkg/fluence/fluxcli-grpc"
"sigs.k8s.io/scheduler-plugins/pkg/fluence/utils"
)
type Fluence struct {
mutex sync.Mutex
handle framework.Handle
podNameToJobId map[string]uint64
pgMgr coschedulingcore.Manager
}
var _ framework.QueueSortPlugin = &Fluence{}
var _ framework.PreFilterPlugin = &Fluence{}
var _ framework.FilterPlugin = &Fluence{}
// Name is the name of the plugin used in the Registry and configurations.
const Name = "Fluence"
func (f *Fluence) Name() string {
return Name
}
// initialize and return a new Flux Plugin
// Note from vsoch: seems analogous to:
// https://github.com/kubernetes-sigs/scheduler-plugins/blob/master/pkg/coscheduling/coscheduling.go#L63
func New(_ runtime.Object, handle framework.Handle) (framework.Plugin, error) {
f := &Fluence{handle: handle, podNameToJobId: make(map[string]uint64)}
klog.Info("Create plugin")
ctx := context.TODO()
fcore.Init()
fluxPodsInformer := handle.SharedInformerFactory().Core().V1().Pods().Informer()
fluxPodsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: f.updatePod,
DeleteFunc: f.deletePod,
})
go fluxPodsInformer.Run(ctx.Done())
klog.Info("Create generic pod informer")
scheme := runtime.NewScheme()
_ = clientscheme.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
_ = v1alpha1.AddToScheme(scheme)
client, err := client.New(handle.KubeConfig(), client.Options{Scheme: scheme})
if err != nil {
return nil, err
}
fieldSelector, err := fields.ParseSelector(",status.phase!=" + string(v1.PodSucceeded) + ",status.phase!=" + string(v1.PodFailed))
if err != nil {
klog.ErrorS(err, "ParseSelector failed")
os.Exit(1)
}
informerFactory := informers.NewSharedInformerFactoryWithOptions(handle.ClientSet(), 0, informers.WithTweakListOptions(func(opt *metav1.ListOptions) {
opt.FieldSelector = fieldSelector.String()
}))
podInformer := informerFactory.Core().V1().Pods()
scheduleTimeDuration := time.Duration(500) * time.Second
pgMgr := coschedulingcore.NewPodGroupManager(
client,
handle.SnapshotSharedLister(),
&scheduleTimeDuration,
podInformer,
)
f.pgMgr = pgMgr
// stopCh := make(chan struct{})
// defer close(stopCh)
// informerFactory.Start(stopCh)
informerFactory.Start(ctx.Done())
if !cache.WaitForCacheSync(ctx.Done(), podInformer.Informer().HasSynced) {
err := fmt.Errorf("WaitForCacheSync failed")
klog.ErrorS(err, "Cannot sync caches")
return nil, err
}
klog.Info("Fluence starts")
return f, nil
}
// Less is used to sort pods in the scheduling queue in the following order.
// 1. Compare the priorities of Pods.
// 2. Compare the initialization timestamps of PodGroups or Pods.
// 3. Compare the keys of PodGroups/Pods: <namespace>/<podname>.
func (f *Fluence) Less(podInfo1, podInfo2 *framework.QueuedPodInfo) bool {
klog.Infof("ordering pods from Coscheduling")
prio1 := corev1helpers.PodPriority(podInfo1.Pod)
prio2 := corev1helpers.PodPriority(podInfo2.Pod)
if prio1 != prio2 {
return prio1 > prio2
}
creationTime1 := f.pgMgr.GetCreationTimestamp(podInfo1.Pod, *podInfo1.InitialAttemptTimestamp)
creationTime2 := f.pgMgr.GetCreationTimestamp(podInfo2.Pod, *podInfo2.InitialAttemptTimestamp)
if creationTime1.Equal(creationTime2) {
return coschedulingcore.GetNamespacedName(podInfo1.Pod) < coschedulingcore.GetNamespacedName(podInfo2.Pod)
}
return creationTime1.Before(creationTime2)
}
func (f *Fluence) PreFilter(ctx context.Context, state *framework.CycleState, pod *v1.Pod) (*framework.PreFilterResult, *framework.Status) {
klog.Infof("Examining the pod")
var err error
var nodename string
if pgname, ok := f.isGroup(ctx, pod); ok {
if !fcore.HaveList(pgname) {
klog.Infof("Getting a pod group")
groupSize, _ := f.groupPreFilter(ctx, pod)
if _, err = f.AskFlux(ctx, pod, groupSize); err != nil {
return nil, framework.NewStatus(framework.Unschedulable, err.Error())
}
}
nodename, err = fcore.GetNextNode(pgname)
klog.Infof("Node Selected %s (%s:%s)", nodename, pod.Name, pgname)
if err != nil {
return nil, framework.NewStatus(framework.Unschedulable, err.Error())
}
} else {
nodename, err = f.AskFlux(ctx, pod, 1)
if err != nil {
return nil, framework.NewStatus(framework.Unschedulable, err.Error())
}
}
klog.Info("Node Selected: ", nodename)
state.Write(framework.StateKey(pod.Name), &fcore.FluxStateData{NodeName: nodename})
return nil, framework.NewStatus(framework.Success, "")
}
func (f *Fluence) isGroup(ctx context.Context, pod *v1.Pod) (string, bool) {
pgFullName, pg := f.pgMgr.GetPodGroup(ctx, pod)
if pg == nil {
klog.InfoS("Not in group", "pod", klog.KObj(pod))
return "", false
}
return pgFullName, true
}
func (f *Fluence) groupPreFilter(ctx context.Context, pod *v1.Pod) (int, error) {
// klog.InfoS("Flux Pre-Filter", "pod", klog.KObj(pod))
klog.InfoS("Flux Pre-Filter", "pod labels", pod.Labels)
_, pg := f.pgMgr.GetPodGroup(ctx, pod)
if pg == nil {
klog.InfoS("Not in group", "pod", klog.KObj(pod))
return 0, nil
}
klog.Info("pod group members ", pg.Spec.MinMember)
return int(pg.Spec.MinMember), nil
}
func (f *Fluence) Filter(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
klog.Info("Filtering input node ", nodeInfo.Node().Name)
if v, e := cycleState.Read(framework.StateKey(pod.Name)); e == nil {
if value, ok := v.(*fcore.FluxStateData); ok && value.NodeName != nodeInfo.Node().Name {
return framework.NewStatus(framework.Unschedulable, "pod is not permitted")
} else {
klog.Info("Filter: node selected by Flux ", value.NodeName)
}
}
return framework.NewStatus(framework.Success)
}
func (f *Fluence) PreFilterExtensions() framework.PreFilterExtensions {
return nil
}
func (f *Fluence) AskFlux(ctx context.Context, pod *v1.Pod, count int) (string, error) {
// clean up previous match if a pod has already allocated previously
f.mutex.Lock()
_, isPodAllocated := f.podNameToJobId[pod.Name]
f.mutex.Unlock()
if isPodAllocated {
klog.Info("Clean up previous allocation")
f.mutex.Lock()
f.cancelFluxJobForPod(pod.Name)
f.mutex.Unlock()
}
jobspec := utils.InspectPodInfo(pod)
conn, err := grpc.Dial("127.0.0.1:4242", grpc.WithInsecure())
if err != nil {
klog.Errorf("[FluxClient] Error connecting to server: %v", err)
return "", err
}
defer conn.Close()
grpcclient := pb.NewFluxcliServiceClient(conn)
_, cancel := context.WithTimeout(context.Background(), 200*time.Second)
defer cancel()
request := &pb.MatchRequest{
Ps: jobspec,
Request: "allocate",
Count: int32(count)}
r, err2 := grpcclient.Match(context.Background(), request)
if err2 != nil {
klog.Errorf("[FluxClient] did not receive any match response: %v", err2)
return "", err
}
klog.Infof("[FluxClient] response podID %s", r.GetPodID())
_, ok := f.isGroup(ctx, pod)
if count > 1 || ok {
pgFullName, _ := f.pgMgr.GetPodGroup(ctx, pod)
nodelist := fcore.CreateNodePodsList(r.GetNodelist(), pgFullName)
klog.Infof("[FluxClient] response nodeID %s", r.GetNodelist())
klog.Info("[FluxClient] Parsed Nodelist ", nodelist)
jobid := uint64(r.GetJobID())
f.mutex.Lock()
f.podNameToJobId[pod.Name] = jobid
klog.Info("Check job set: ", f.podNameToJobId)
f.mutex.Unlock()
} else {
nodename := r.GetNodelist()[0].GetNodeID()
jobid := uint64(r.GetJobID())
f.mutex.Lock()
f.podNameToJobId[pod.Name] = jobid
klog.Info("Check job set: ", f.podNameToJobId)
f.mutex.Unlock()
return nodename, nil
}
return "", nil
}
func (f *Fluence) cancelFluxJobForPod(podName string) error {
jobid := f.podNameToJobId[podName]
klog.Infof("Cancel flux job: %v for pod %s", jobid, podName)
start := time.Now()
conn, err := grpc.Dial("127.0.0.1:4242", grpc.WithInsecure())
if err != nil {
klog.Errorf("[FluxClient] Error connecting to server: %v", err)
return err
}
defer conn.Close()
grpcclient := pb.NewFluxcliServiceClient(conn)
_, cancel := context.WithTimeout(context.Background(), 200*time.Second)
defer cancel()
request := &pb.CancelRequest{
JobID: int64(jobid),
}
res, err := grpcclient.Cancel(context.Background(), request)
if err != nil {
klog.Errorf("[FluxClient] did not receive any cancel response: %v", err)
return err
}
if res.Error == 0 {
delete(f.podNameToJobId, podName)
} else {
klog.Warningf("Failed to delete pod %s from the podname-jobid map.", podName)
}
elapsed := metrics.SinceInSeconds(start)
klog.Info("Time elapsed (Cancel Job) :", elapsed)
klog.Infof("Job cancellation for pod %s result: %d", podName, err)
if klog.V(2).Enabled() {
klog.Info("Check job set: after delete")
klog.Info(f.podNameToJobId)
}
return nil
}
// EventHandlers
func (f *Fluence) updatePod(oldObj, newObj interface{}) {
// klog.Info("Update Pod event handler")
newPod := newObj.(*v1.Pod)
klog.Infof("Processing event for pod %s", newPod)
switch newPod.Status.Phase {
case v1.PodPending:
// in this state we don't know if a pod is going to be running, thus we don't need to update job map
case v1.PodRunning:
// if a pod is start running, we can add it state to the delta graph if it is scheduled by other scheduler
case v1.PodSucceeded:
klog.Infof("Pod %s succeeded, Fluence needs to free the resources", newPod.Name)
f.mutex.Lock()
defer f.mutex.Unlock()
if _, ok := f.podNameToJobId[newPod.Name]; ok {
f.cancelFluxJobForPod(newPod.Name)
} else {
klog.Infof("Succeeded pod %s/%s doesn't have flux jobid", newPod.Namespace, newPod.Name)
}
case v1.PodFailed:
// a corner case need to be tested, the pod exit code is not 0, can be created with segmentation fault pi test
klog.Warningf("Pod %s failed, Fluence needs to free the resources", newPod.Name)
f.mutex.Lock()
defer f.mutex.Unlock()
if _, ok := f.podNameToJobId[newPod.Name]; ok {
f.cancelFluxJobForPod(newPod.Name)
} else {
klog.Errorf("Failed pod %s/%s doesn't have flux jobid", newPod.Namespace, newPod.Name)
}
case v1.PodUnknown:
// don't know how to deal with it as it's unknown phase
default:
// shouldn't enter this branch
}
}
func (f *Fluence) deletePod(podObj interface{}) {
klog.Info("Delete Pod event handler")
pod := podObj.(*v1.Pod)
klog.Info("Pod status: ", pod.Status.Phase)
switch pod.Status.Phase {
case v1.PodSucceeded:
case v1.PodPending:
klog.Infof("Pod %s completed and is Pending termination, Fluence needs to free the resources", pod.Name)
f.mutex.Lock()
defer f.mutex.Unlock()
if _, ok := f.podNameToJobId[pod.Name]; ok {
f.cancelFluxJobForPod(pod.Name)
} else {
klog.Infof("Terminating pod %s/%s doesn't have flux jobid", pod.Namespace, pod.Name)
}
case v1.PodRunning:
f.mutex.Lock()
defer f.mutex.Unlock()
if _, ok := f.podNameToJobId[pod.Name]; ok {
f.cancelFluxJobForPod(pod.Name)
} else {
klog.Infof("Deleted pod %s/%s doesn't have flux jobid", pod.Namespace, pod.Name)
}
}
}