-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.py
76 lines (64 loc) · 2.2 KB
/
middleware.py
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
# read job from job queues
# assign job to pod
import subprocess
import time
node_map = {
1: "node1.group5project.ufl-eel6871-fa23-pg0.utah.cloudlab.us",
2: "node2.group5project.ufl-eel6871-fa23-pg0.utah.cloudlab.us"
}
import random
import string
def generate_random_string(length=4):
characters = string.ascii_lowercase + string.digits
random_string = ''.join(random.choice(characters) for _ in range(length))
return random_string
def write_yaml(pod_name, args, node, out_file):
pod_str = f'''
apiVersion: v1
kind: Pod
metadata:
name: {pod_name}
spec:
restartPolicy: Never
containers:
- image: docker.io/polinux/stress-ng:latest
name: stress-container
env:
- name: DELAY_STARTUP
value: "20"
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 30
args: [{args}]
nodeSelector:
kubernetes.io/hostname: {node_map[node]}
'''
with open(out_file, 'w') as f:
f.write(pod_str)
def start_pod(args, node):
pod_name = "stress-pod-"+generate_random_string()
out_file = pod_name + ".yaml"
write_yaml(pod_name, args, node, out_file)
command = f"kubectl apply -f {out_file}"
time.sleep(2)
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print("executed ",result.stdout)
def kill_pod():
pass
def kill_node(node):
node_name = node_map[node]
command = f"kubectl cordon {node_name}"
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print("killed node", node_name,result.stdout)
def restart_node(node):
node_name = node_map[node]
# command = "kubectl get pods --no-headers=true | grep '^stress-pod' | awk '{print $1}' | xargs kubectl delete pod"
# result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# print("drained node", node_name,result.stdout)
command = f"kubectl uncordon {node_name}"
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print("restarted node", node_name,result.stdout)