-
Notifications
You must be signed in to change notification settings - Fork 18
/
sched.go
151 lines (124 loc) · 3.61 KB
/
sched.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
package main
import (
"context"
"fmt"
"strconv"
"time"
"github.com/sanposhiho/mini-kube-scheduler/scheduler"
"golang.org/x/xerrors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"github.com/sanposhiho/mini-kube-scheduler/config"
"github.com/sanposhiho/mini-kube-scheduler/k8sapiserver"
"github.com/sanposhiho/mini-kube-scheduler/pvcontroller"
"github.com/sanposhiho/mini-kube-scheduler/scheduler/defaultconfig"
)
// entry point.
func main() {
if err := start(); err != nil {
klog.Fatalf("failed with error on running scheduler: %+v", err)
}
}
// start starts scheduler and needed k8s components.
func start() error {
cfg, err := config.NewConfig()
if err != nil {
return xerrors.Errorf("get config: %w", err)
}
restclientCfg, apiShutdown, err := k8sapiserver.StartAPIServer(cfg.EtcdURL)
if err != nil {
return xerrors.Errorf("start API server: %w", err)
}
defer apiShutdown()
client := clientset.NewForConfigOrDie(restclientCfg)
pvshutdown, err := pvcontroller.StartPersistentVolumeController(client)
if err != nil {
return xerrors.Errorf("start pv controller: %w", err)
}
defer pvshutdown()
sched := scheduler.NewSchedulerService(client, restclientCfg)
sc, err := defaultconfig.DefaultSchedulerConfig()
if err != nil {
return xerrors.Errorf("create scheduler config")
}
if err := sched.StartScheduler(sc); err != nil {
return xerrors.Errorf("start scheduler: %w", err)
}
defer sched.ShutdownScheduler()
err = scenario(client)
if err != nil {
return xerrors.Errorf("start scenario: %w", err)
}
return nil
}
func scenario(client clientset.Interface) error {
ctx := context.Background()
// create node0 ~ node9
for i := 0; i < 10; i++ {
suffix := strconv.Itoa(i)
_, err := client.CoreV1().Nodes().Create(ctx, &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node" + suffix,
},
}, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("create node: %w", err)
}
}
klog.Info("scenario: all nodes created")
_, err := client.CoreV1().Pods("default").Create(ctx, &v1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod1"},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "container1",
Image: "k8s.gcr.io/pause:3.5",
},
},
},
}, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("create pod: %w", err)
}
_, err = client.CoreV1().Pods("default").Create(ctx, &v1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod9"},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "container1",
Image: "k8s.gcr.io/pause:3.5",
},
},
},
}, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("create pod: %w", err)
}
klog.Info("scenario: pod1 created")
// wait to schedule
time.Sleep(4 * time.Second)
pod1, err := client.CoreV1().Pods("default").Get(ctx, "pod1", metav1.GetOptions{})
if err != nil {
return fmt.Errorf("get pod: %w", err)
}
pod9, err := client.CoreV1().Pods("default").Get(ctx, "pod9", metav1.GetOptions{})
if err != nil {
return fmt.Errorf("get pod: %w", err)
}
klog.Info("scenario: pod1 is bound to " + pod1.Spec.NodeName)
if pod9.Spec.NodeName != "" {
klog.Fatal("scenario: pod9 should not be bound yet")
}
klog.Info("scenario: pod9 has not be bound yet")
// wait pod9
time.Sleep(7 * time.Second)
pod9, err = client.CoreV1().Pods("default").Get(ctx, "pod9", metav1.GetOptions{})
if err != nil {
return fmt.Errorf("get pod: %w", err)
}
// pod1 always bound to node1, because node1 got high score from nodenumber score plugin
klog.Info("scenario: pod9 is bound to " + pod9.Spec.NodeName)
return nil
}