forked from RedHatQE/openshift-python-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
92 lines (73 loc) · 2.81 KB
/
benchmark.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from ocp_resources.constants import NOT_FOUND_ERROR_EXCEPTION_DICT
from ocp_resources.resource import NamespacedResource
from timeout_sampler import TimeoutSampler
class Benchmark(NamespacedResource):
"""
Benchmark resource
Defined by https://github.com/cloud-bulldozer/benchmark-operator
The benchmark-operator monitors a namespace for `Benchmark` resources.
When a new `Benchmark` is created, the benchmark-operator creates and starts the pods or VMs necessary,
and triggers the benchmark run.
"""
api_group = NamespacedResource.ApiGroup.RIPSAW_CLOUDBULLDOZER_IO
class Status:
NONE = None # None state is valid for newly created benchmark resources
class Workload:
class Kind:
VM = "vm"
POD = "pod"
def _wait_for_instance_key(self, parent, key):
"""
Wait for key to exist in parent attribute of instance
Args:
parent (str): An attribute of self.instance that should contain key
key (str): A dictionary entry within parent
Returns:
str or None: Value of key if found, otherwise None
"""
samples = TimeoutSampler(
wait_timeout=30,
sleep=1,
func=lambda: getattr(self.instance, parent, None),
exceptions_dict=NOT_FOUND_ERROR_EXCEPTION_DICT,
)
for sample in samples:
if sample:
return sample.get(key)
@property
def uuid(self):
"""
Returns:
str: UUID string from resource instance
"""
return self._wait_for_instance_key(parent="status", key="uuid")
@property
def suuid(self):
"""
Returns:
str: (short)UUID string from resource instance
"""
return self._wait_for_instance_key(parent="status", key="suuid")
@property
def workload_kind(self):
"""
Retrieve the value of spec.workload.args.kind
Not all Benchmarks have a 'kind' defined, this was added for vms. The default is 'pod'
Returns:
str: Value representing workload kind
"""
return self.workload_arg(arg="kind", default="pod")
def workload_arg(self, arg, default=None):
"""
Retrieve the value of spec.workload.args[arg]
To provide a similar usage as .get(), a default can be defined if needed.
Args:
arg (str): Argument to retrieve from spec.workload.args
default (any): Default value to return if arg is not found in workload args
Returns:
any: Value of workload arg or 'default' if does not exist
"""
workload = self._wait_for_instance_key(parent="spec", key="workload")
if workload:
return workload.get("args", {}).get(arg, default)
return default