Created
September 15, 2017 15:40
-
-
Save mateobur/1fdd4f4d3e20c08fc152622a109e9b7f to your computer and use it in GitHub Desktop.
python sysdig scheduler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import random | |
import json | |
from kubernetes import client, config, watch | |
from sdcclient import SdcClient | |
config.load_incluster_config() | |
v1=client.CoreV1Api() | |
sdclient = SdcClient(open("/etc/sysdigtoken/token.txt","r").read().rstrip()) | |
metrics = [{ "id": "net.http.request.time", "aggregations": { "time": "timeAvg", "group": "avg" } }] | |
scheduler_name = "sysdigsched" | |
def get_request_time(hostname): | |
hostfilter = "host.hostName = '%s'" % hostname | |
metricdata = sdclient.get_data(metrics, -60, 0, 60, hostfilter) | |
request_time = float(metricdata[1].get('data')[0].get('d')[0]) | |
return request_time | |
def best_request_time(nodes): | |
if not nodes: | |
return [] | |
node_times = [get_request_time(hostname) for hostname in nodes] | |
return nodes[node_times.index(min(node_times))] | |
def nodes_available(): | |
ready_nodes = [] | |
for n in v1.list_node().items: | |
for status in n.status.conditions: | |
if status.status == "True" and status.type == "Ready": | |
ready_nodes.append(n.metadata.name) | |
return ready_nodes | |
def scheduler(name, node, namespace="default"): | |
body=client.V1Binding() | |
target=client.V1ObjectReference() | |
target.kind="Node" | |
target.apiVersion="v1" | |
target.name= node | |
meta=client.V1ObjectMeta() | |
meta.name=name | |
body.target=target | |
body.metadata=meta | |
return v1.create_namespaced_binding(namespace, body) | |
def main(): | |
w = watch.Watch() | |
for event in w.stream(v1.list_namespaced_pod, "default"): | |
if event['object'].status.phase == "Pending" and event['object'].spec.scheduler_name == scheduler_name: | |
try: | |
res = scheduler(event['object'].metadata.name, best_request_time(nodes_available())) | |
except client.rest.ApiException as e: | |
print json.loads(e.body)['message'] | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment