|
1 | 1 | import time |
| 2 | +import random |
2 | 3 |
|
3 | 4 |
|
4 | 5 | def hash_int(x: int): |
@@ -35,8 +36,19 @@ def generate_mock_pod(index: int, i: int, j: int): |
35 | 36 | phase = pod_phases[hash_int((index + 1) * (i + 1) * (j + 1)) % len(pod_phases)] |
36 | 37 | containers = [] |
37 | 38 | for k in range(1 + j % 2): |
| 39 | + # generate "more real data" |
| 40 | + requests_cpu = random.randint(50, 100) |
| 41 | + requests_memory = random.randint(256, 512) |
| 42 | + usage_cpu = requests_cpu + random.randint(-45, 50) |
| 43 | + usage_memory = requests_memory + random.randint(-128, 128) |
38 | 44 | container = { |
39 | | - 'name': 'myapp', 'image': 'foo/bar/{}'.format(j), 'resources': {'requests': {'cpu': '100m', 'memory': '100Mi'}, 'limits': {}}, |
| 45 | + 'name': 'myapp', |
| 46 | + 'image': 'foo/bar/{}'.format(j), |
| 47 | + 'resources': { |
| 48 | + 'requests': {'cpu': f'{requests_cpu}m', 'memory': f'{requests_memory}Mi'}, |
| 49 | + 'limits': {}, |
| 50 | + 'usage': {'cpu': f'{usage_cpu}m', 'memory': f'{usage_memory}Mi'}, |
| 51 | + }, |
40 | 52 | 'ready': True, |
41 | 53 | 'state': {'running': {}} |
42 | 54 | } |
@@ -86,9 +98,26 @@ def query_mock_cluster(cluster): |
86 | 98 | else: |
87 | 99 | pod = generate_mock_pod(index, i, j) |
88 | 100 | pods['{}/{}'.format(pod['namespace'], pod['name'])] = pod |
89 | | - node = {'name': 'node-{}'.format(i), 'labels': labels, 'status': { |
90 | | - 'capacity': {'cpu': '4', 'memory': '32Gi', 'pods': '110'}, |
91 | | - 'allocatable': {'cpu': '3800m', 'memory': '31Gi'}}, 'pods': pods} |
| 101 | + |
| 102 | + # use data from containers (usage) |
| 103 | + usage_cpu = 0 |
| 104 | + usage_memory = 0 |
| 105 | + for p in pods.values(): |
| 106 | + for c in p["containers"]: |
| 107 | + usage_cpu += int(c["resources"]["usage"]["cpu"].split("m")[0]) |
| 108 | + usage_memory += int(c["resources"]["usage"]["memory"].split("Mi")[0]) |
| 109 | + |
| 110 | + node = { |
| 111 | + 'name': 'node-{}'.format(i), |
| 112 | + 'labels': labels, |
| 113 | + 'status': { |
| 114 | + 'capacity': {'cpu': '4', 'memory': '32Gi', 'pods': '110'}, |
| 115 | + 'allocatable': {'cpu': '3800m', 'memory': '31Gi'} |
| 116 | + }, |
| 117 | + 'pods': pods, |
| 118 | + # get data from containers (usage) |
| 119 | + 'usage': {'cpu': f'{usage_cpu}m', 'memory': f'{usage_memory}Mi'} |
| 120 | + } |
92 | 121 | nodes[node['name']] = node |
93 | 122 | pod = generate_mock_pod(index, 11, index) |
94 | 123 | unassigned_pods = {'{}/{}'.format(pod['namespace'], pod['name']): pod} |
|
0 commit comments