Skip to content

Commit

Permalink
k8s_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
drmorr0 committed Apr 26, 2024
1 parent 8b94daa commit df1287e
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 13 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repos:
rev: v1.4.1
hooks:
- id: mypy
additional_dependencies: [types-simplejson]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.2
hooks:
Expand Down
43 changes: 31 additions & 12 deletions datakube/k8s_utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
import typing as T

import pandas as pd
from kubernetes.client import V1Job
import simplejson as json
from kubernetes.client import V1PodList
from kubernetes.client.api_client import ApiClient


def fetch_job_start_end(jobs: T.List[V1Job]) -> T.List[T.Tuple[pd.Timestamp, pd.Timestamp]]:
job_times = []
for job in jobs:
assert job.status is not None
assert job.status.start_time is not None
assert job.status.completion_time is not None
def read_obj_from_json(filename: str, klass: str) -> T.Any:
with open(filename, encoding="utf-8") as f:
data = json.load(f)
client = ApiClient()
return client._ApiClient__deserialize(data, klass) # type: ignore

job_times.append((
pd.Timestamp(job.status.start_time),
pd.Timestamp(job.status.completion_time),
))

return job_times
def fetch_pod_intervals(pods: V1PodList) -> T.List[T.Tuple[pd.Timestamp, pd.Timestamp]]:
intervals = []
for pod in pods.items:
assert pod.status
assert pod.status.container_statuses

start = None
end = None
for cstat in pod.status.container_statuses:
assert cstat.state
assert cstat.state.terminated

if start is None or cstat.state.terminated.started_at < start:
start = cstat.state.terminated.started_at
if end is None or cstat.state.finished_at > end:
end = cstat.state.terminated.finished_at

assert start
assert end

intervals.append((pd.Timestamp(start), pd.Timestamp(end)))

return sorted(intervals)
120 changes: 119 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pandas = "^2.2.2"
pyarrow = "^16.0.0"
arrow = "^1.3.0"
kubernetes-client = "^0.1.8"
simplejson = "^3.19.2"
types-simplejson = "^3.19.0.20240310"

[tool.poetry.group.dev.dependencies]
mypy = "^1.10.0"
Expand Down
Loading

0 comments on commit df1287e

Please sign in to comment.