-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.go
150 lines (123 loc) · 2.81 KB
/
scheduler.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
package main
import (
"context"
"fmt"
"log"
"math/rand"
"sort"
"time"
coreV1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
rest "k8s.io/client-go/rest"
)
var config *rest.Config
var clientSet *kubernetes.Clientset
type AppScheduler struct {
pod *coreV1.Pod
}
const START_ID int = 1
const MAX_ID int = 32
const MAX_POD_NUM int = MAX_ID * MAX_ID
func (s *AppScheduler) schedulePod(podName string) (*snowflake, error) {
// 3. Get the `replicaSet` of the pod
rsName := s.pod.OwnerReferences[0].Name
pa := populateApp(rsName)
if getTotalPod(pa) == 961 {
return &snowflake{}, fmt.Errorf("the serivce `%v` has reached the maximum pod numbers of %v", rsName, MAX_POD_NUM)
}
// 4. Schedule Pod to node
nList, err := clientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{
FieldSelector: "spec.unschedulable=false",
})
if err != nil {
log.Fatalf("Try to retrieve Nodes from K8s. Err: %s", err)
}
nodes := nList.Items
for {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
selectedIndex := r.Intn(len(nodes))
selectedNodeName := nodes[selectedIndex].Name
registered := false
for i, n := range pa.Nodes {
if n.Name == selectedNodeName { // Node is registered
registered = true
if len(n.Pods) == MAX_ID { // reach the maximum number of pods that a single node can hold
break
}
// Add the pod to the node
selectedID := -1
sort.Sort(n.Pods)
for id := START_ID; id < MAX_ID; id++ {
selectedID = id
if id > len(n.Pods)-1 {
break
}
if id != n.Pods[id].ID {
break
}
}
pa.Nodes[i].Pods = append(pa.Nodes[i].Pods, pod{
ID: selectedID,
})
pa.saveApp()
return &snowflake{
nodeName: n.Name,
datacenterId: n.ID,
workerId: selectedID,
}, nil
}
}
if !registered { // Node is unregistered
if len(pa.Nodes) == MAX_ID { // reach the maximum number of nodes that app can scale on
continue
}
selectedID := -1
sort.Sort(pa.Nodes)
for id := START_ID; id < MAX_ID; id++ {
selectedID = id
if id > len(pa.Nodes)-1 {
break
}
if id != pa.Nodes[id].ID {
break
}
}
pa.Nodes = append(pa.Nodes, node{
Name: selectedNodeName,
ID: selectedID,
Pods: []pod{
{
ID: START_ID,
},
},
})
pa.saveApp()
return &snowflake{
nodeName: selectedNodeName,
datacenterId: selectedID,
workerId: START_ID,
}, nil
}
}
}
func getTotalPod(a *App) int {
var num int
for _, no := range a.Nodes {
num += len(no.Pods)
}
return num
}
func init() {
c, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
config = c
cs, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
clientSet = cs
}