-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathscheduler.sh
executable file
·70 lines (61 loc) · 2.23 KB
/
scheduler.sh
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
#!/usr/bin/env bash
#
# A kubernetes scheduler written in bash
# dependencies: kubectl, curl
# Runs against localhost:8001 by default
# Use `kubectl proxy` to proxy to master without https
#
# Author: Justin Garrison
# justingarrison.com
# @rothgar
set -eo pipefail
# uncomment to see all commands in stdout
# set -x
SERVER="${SERVER:-localhost:8001}"
SCHEDULER="${SCHEDULER:-bashScheduler}"
while true; do
# Get a list of all our pods in pending state
for POD in $(kubectl --server ${SERVER} get pods \
--output jsonpath='{.items..metadata.name}' \
--all-namespaces \
--field-selector=status.phase==Pending); do
SCHEDULER_NAME=$(kubectl get pod ${POD} \
--output jsonpath='{.spec.schedulerName}')
if [ "${SCHEDULER_NAME}" == "${SCHEDULER}" ]; then
# Get the pod namespace
NAMESPACE=$(kubectl get pod ${POD} \
--output jsonpath='{.metadata.namespace}')
# Get an array for all of the nodes
# We could optionally check if the nodes are ready
NODES=($(kubectl --server ${SERVER} get nodes \
--output jsonpath='{.items..metadata.name}'))
# Store a number for the length of our NODES array
NODES_LENGTH=${#NODES[@]}
# Randomly select a node from the array
# $RANDOM % $NODES_LENGTH will be the remainder
# of a random number divided by the length of our nodes
# In the case of 1 node this is always ${NODES[0]}
NODE=${NODES[$[$RANDOM % $NODES_LENGTH]]}
# Bind the current pod to the node selected
curl --silent --fail \
--header "Content-Type:application/json" \
--request POST \
--data '{"apiVersion":"v1",
"kind": "Binding",
"metadata": {
"name": "'${POD}'"
},
"target": {
"apiVersion": "v1",
"kind": "Node",
"name": "'${NODE}'"
}
}' \
http://${SERVER}/api/v1/namespaces/${NAMESPACE}/pods/${POD}/binding/ >/dev/null \
&& echo "Assigned ${POD} to ${NODE}" \
|| echo "Failed to assign ${POD} to ${NODE}"
fi
done
echo "Nothing to do...sleeping."
sleep 6s
done