-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
executable file
·109 lines (89 loc) · 2.44 KB
/
main.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
package main
import (
"flag"
"os"
"path/filepath"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
"fmt"
"k8s.io/api/core/v1"
"k8s.io/client-go/rest"
"math/rand"
)
var clientset *kubernetes.Clientset
func main() {
var kubeconfig *string
var config *rest.Config
var err error
schedulerName := flag.String("scheduler-name", "packt-scheduler", "Name of the scheduler")
// Check for default kubeconfig location
if home := os.Getenv("HOME"); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// Use the current context in kubeconfig
if *kubeconfig != "" {
config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
// Use in-cluster configuration
config, err = rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
}
}
// Create clientset from configuration
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
fmt.Printf("Starting scheduler: %s\n", *schedulerName)
for {
// Request pods from all namespaces
pods, err := clientset.CoreV1().Pods(v1.NamespaceAll).List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
// Check for pods
for _, pod := range pods.Items {
// If scheduler name is set and node is not assigned
if pod.Spec.SchedulerName == *schedulerName && pod.Spec.NodeName == "" {
// Schedule the pod to a random node
err := schedule(pod.Name, randomNode(), pod.Namespace)
if err != nil {
panic(err.Error())
}
}
}
time.Sleep(10 * time.Second)
}
}
func randomNode() string {
nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
if len(nodes.Items) < 1 {
panic("no nodes found")
}
return nodes.Items[rand.Intn(len(nodes.Items))].Name
}
func schedule(pod, node, namespace string) error {
fmt.Printf("Assigning %s/%s to %s\n", namespace, pod, node)
// Create a binding with pod and node
binding := v1.Binding{
ObjectMeta: metav1.ObjectMeta{
Name: pod,
},
Target: v1.ObjectReference{
Kind: "Node",
APIVersion: "v1",
Name: node,
}}
return clientset.CoreV1().Pods(namespace).Bind(&binding)
}