Skip to content

Commit

Permalink
🚀 refactor pod filtering by job/cronjob to reduce number of api calls (
Browse files Browse the repository at this point in the history
  • Loading branch information
mshade authored Feb 26, 2024
1 parent 333f5a0 commit 71e20ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 3 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
get_cronjob,
get_pods,
get_pod_logs,
pod_is_owned_by,
toggle_cronjob_suspend,
trigger_cronjob,
update_cronjob,
Expand Down Expand Up @@ -98,12 +99,13 @@ def index():
def view_namespace(namespace):
cronjobs = get_cronjobs(namespace)
cronjobs_with_details = []
all_pods = get_pods(namespace=namespace)

for cronjob in cronjobs:
cronjob_detail = get_cronjob(namespace, cronjob["name"])
jobs = get_jobs(namespace=namespace, cronjob_name=cronjob["name"])
for job in jobs:
job["pods"] = get_pods(namespace, job["metadata"]["name"])
job["pods"] = [pod for pod in all_pods if pod_is_owned_by(pod, job["metadata"]["name"])]
cronjob_detail["jobs"] = jobs
cronjobs_with_details.append(cronjob_detail)

Expand Down
13 changes: 7 additions & 6 deletions kron.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def _has_label(api_object: object, k: str, v: str) -> bool:
return labels.get(k) == v


def _is_owned_by(object: object, owner_name: str) -> bool:
def pod_is_owned_by(api_dict: dict, owner_name: str) -> bool:
"""Return whether a job or pod contains an ownerReference to the given cronjob or job name
Args:
Expand All @@ -129,8 +129,8 @@ def _is_owned_by(object: object, owner_name: str) -> bool:
Returns:
bool: True if an ownerReference contains the given owner_name
"""
owner_refernces = object["metadata"].get("ownerReferences", [])
return any(owner_ref["name"] == owner_name for owner_ref in owner_refernces)
owner_references = api_dict["metadata"].get("ownerReferences", [])
return any(owner_ref["name"] == owner_name for owner_ref in owner_references)


@namespace_filter
Expand Down Expand Up @@ -224,7 +224,7 @@ def get_jobs(namespace: str, cronjob_name: str) -> List[dict]:
filtered_jobs = [
job
for job in cleaned_jobs
if _is_owned_by(job, cronjob_name)
if pod_is_owned_by(job, cronjob_name)
or _has_label(job, "kronic.mshade.org/created-from", cronjob_name)
]

Expand Down Expand Up @@ -261,7 +261,7 @@ def get_pods(namespace: str, job_name: str = None) -> List[dict]:
all_pods = v1.list_namespaced_pod(namespace=namespace)
cleaned_pods = [_clean_api_object(pod) for pod in all_pods.items]
filtered_pods = [
pod for pod in cleaned_pods if _is_owned_by(pod, job_name) or (not job_name)
pod for pod in cleaned_pods if pod_is_owned_by(pod, job_name) or (not job_name)
]

for pod in filtered_pods:
Expand Down Expand Up @@ -294,8 +294,9 @@ def get_jobs_and_pods(namespace: str, cronjob_name: str) -> List[dict]:
List of dicts: A list of job dicts, each with a jobs element containing a list of pods the job created
"""
jobs = get_jobs(namespace, cronjob_name)
all_pods = get_pods(namespace)
for job in jobs:
job["pods"] = get_pods(namespace, job["metadata"]["name"])
job["pods"] = [pod for pod in all_pods if pod_is_owned_by(pod, job["metadata"]["name"])]

return jobs

Expand Down

0 comments on commit 71e20ff

Please sign in to comment.