-
Notifications
You must be signed in to change notification settings - Fork 17
/
minisched.go
58 lines (46 loc) · 1.37 KB
/
minisched.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
package minisched
import (
"context"
"math/rand"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/apimachinery/pkg/util/wait"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func (sched *Scheduler) Run(ctx context.Context) {
wait.UntilWithContext(ctx, sched.scheduleOne, 0)
}
func (sched *Scheduler) scheduleOne(ctx context.Context) {
klog.Info("minischeduler: Try to get pod from queue....")
pod := sched.SchedulingQueue.NextPod()
klog.Info("minischeduler: Start schedule(" + pod.Name + ")")
// get nodes
nodes, err := sched.client.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
klog.Error(err)
return
}
klog.Info("minischeduler: Got Nodes successfully")
// select node randomly
selectedNode := nodes.Items[rand.Intn(len(nodes.Items))]
if err := sched.Bind(ctx, pod, selectedNode.Name); err != nil {
klog.Error(err)
return
}
klog.Info("minischeduler: Bind Pod successfully")
}
func (sched *Scheduler) Bind(ctx context.Context, p *v1.Pod, nodeName string) error {
binding := &v1.Binding{
ObjectMeta: metav1.ObjectMeta{Namespace: p.Namespace, Name: p.Name, UID: p.UID},
Target: v1.ObjectReference{Kind: "Node", Name: nodeName},
}
err := sched.client.CoreV1().Pods(binding.Namespace).Bind(ctx, binding, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}