Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.

Commit 0c32006

Browse files
tomislaterhjacobs
authored andcommitted
Generate more real data (#207)
* try to generate more real data * remove blank line
1 parent fc8bd6b commit 0c32006

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

kube_ops_view/mock.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import random
23

34

45
def hash_int(x: int):
@@ -35,8 +36,19 @@ def generate_mock_pod(index: int, i: int, j: int):
3536
phase = pod_phases[hash_int((index + 1) * (i + 1) * (j + 1)) % len(pod_phases)]
3637
containers = []
3738
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)
3844
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+
},
4052
'ready': True,
4153
'state': {'running': {}}
4254
}
@@ -86,9 +98,26 @@ def query_mock_cluster(cluster):
8698
else:
8799
pod = generate_mock_pod(index, i, j)
88100
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+
}
92121
nodes[node['name']] = node
93122
pod = generate_mock_pod(index, 11, index)
94123
unassigned_pods = {'{}/{}'.format(pod['namespace'], pod['name']): pod}

0 commit comments

Comments
 (0)