|
from functools import lru_cache, wraps |
|
from datetime import datetime, timedelta |
|
import random |
|
import string |
|
import tracemalloc |
|
import os |
|
|
|
|
|
TTL_SECONDS = 300 |
|
LRU_MAXENTRIES = None |
|
MIN_ALERTS, MIN_FOCS = 0, 0 |
|
MAX_ALERTS, MAX_FOCS = 10, 10 |
|
|
|
|
|
def timed_lru_cache(seconds: int, maxsize: int = 128): |
|
def wrapper_cache(func): |
|
func = lru_cache(maxsize=maxsize)(func) |
|
func.lifetime = timedelta(seconds=seconds) |
|
func.expiration = datetime.utcnow() + func.lifetime |
|
|
|
@wraps(func) |
|
def wrapped_func(*args, **kwargs): |
|
if datetime.utcnow() >= func.expiration: |
|
func.cache_clear() |
|
func.expiration = datetime.utcnow() + func.lifetime |
|
|
|
return func(*args, **kwargs) |
|
|
|
return wrapped_func |
|
|
|
return wrapper_cache |
|
|
|
|
|
@timed_lru_cache(seconds=TTL_SECONDS, maxsize=LRU_MAXENTRIES) |
|
def get_result(cluster_id): |
|
return generate_random_result() |
|
|
|
|
|
def generate_random_result(): |
|
n_alerts = random.randint(MIN_ALERTS, MAX_ALERTS) |
|
n_focs = random.randint(MIN_FOCS, MAX_FOCS) |
|
|
|
alerts = [] |
|
for _ in range(n_alerts): |
|
alert_name = get_random_string(20) |
|
namespace = get_random_string(10) |
|
alerts.append( |
|
{ |
|
"name": alert_name, |
|
"namespace": namespace, |
|
"severity": "info", |
|
"url": f"https://my-cluster.com/monitoring/alerts?orderBy=asc&sortBy=Severity&alert-name={alert_name}" |
|
} |
|
) |
|
|
|
focs = [] |
|
for _ in range(n_focs): |
|
foc_name = get_random_string(20) |
|
alerts.append( |
|
{ |
|
"condition": "Failing", |
|
"name": foc_name, |
|
"reason": "AsExpected", |
|
"url": f"https://my-cluster.com/k8s/cluster/config.openshift.io~v1~ClusterOperator/{foc_name}" |
|
} |
|
) |
|
|
|
return { |
|
"meta": { |
|
"last_checked_at": "2023-05-22T13:03:41.299Z" |
|
}, |
|
"status": "ok", |
|
"upgrade_recommendation": { |
|
"upgrade_recommended": random.randint(0,1), |
|
"upgrade_risks_predictors": { |
|
"alerts": alerts, |
|
"operator_conditions": focs |
|
} |
|
} |
|
} |
|
|
|
|
|
letters = string.ascii_lowercase |
|
|
|
|
|
def get_random_string(length): |
|
return ''.join(random.choice(letters) for _ in range(length)) |
|
|
|
if __name__ == "__main__": |
|
import uuid |
|
N_CLUSTERS = int(os.environ["N_CLUSTERS"]) |
|
|
|
clusters = [uuid.uuid4() for _ in range(N_CLUSTERS)] |
|
|
|
# starting the monitoring |
|
tracemalloc.start() |
|
|
|
for cluster in range(N_CLUSTERS): |
|
get_result(cluster_id=cluster) |
|
|
|
# displaying the memory |
|
current_mem, max_mem = tracemalloc.get_traced_memory() |
|
print(f"{N_CLUSTERS},{current_mem},{max_mem}") |
|
|
|
# stopping the library |
|
tracemalloc.stop() |