-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathk8s.py
172 lines (154 loc) · 5.97 KB
/
k8s.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import logging
from collections.abc import Generator
from pathlib import Path
from typing import Literal
from kubernetes import client, config, dynamic
from jobq_server.exceptions import PodNotReadyError, WorkloadNotFound
from jobq_server.models import JobId
from jobq_server.utils.helpers import traverse
from jobq_server.utils.k8s import GroupVersionKind
from jobq_server.utils.kueue import KueueWorkload
class KubernetesService:
def __init__(self):
try:
config.load_incluster_config()
self._in_cluster = True
except config.ConfigException:
logging.warning(
"Could not load in-cluster config, attempting to load Kubeconfig",
)
config.load_kube_config()
self._in_cluster = False
self._core_v1_api = client.CoreV1Api()
@property
def namespace(self) -> str:
if not self._in_cluster:
_, active_context = config.list_kube_config_contexts()
current_namespace = traverse(active_context, "context.namespace")
else:
# When running in a cluster, determine the namespace from the mounted service account
try:
current_namespace = (
Path("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
.read_text()
.strip()
)
except FileNotFoundError as e:
raise RuntimeError("Could not determine current namespace") from e
return current_namespace
def workload_for_managed_resource(
self, uid: JobId, namespace: str | None = None
) -> KueueWorkload | None:
try:
return KueueWorkload.for_managed_resource(
uid, namespace=namespace or self.namespace
)
except WorkloadNotFound:
return None
def _sanitize_log_kwargs(self, tail: int) -> dict[str, int]:
return {"tail_lines": tail} if tail != -1 else {}
def get_pod_logs(self, pod: client.V1Pod, tail: int = -1) -> str:
try:
return self._core_v1_api.read_namespaced_pod_log(
pod.metadata.name,
pod.metadata.namespace,
**self._sanitize_log_kwargs(tail),
)
except client.ApiException as e:
if e.status == 400:
raise PodNotReadyError(
name=pod.metadata.name,
namespace=pod.metadata.namespace,
) from e
raise
def stream_pod_logs(
self, pod: client.V1Pod, tail: int = -1
) -> Generator[str, None, None]:
try:
log_stream = self._core_v1_api.read_namespaced_pod_log(
pod.metadata.name,
pod.metadata.namespace,
follow=True,
_preload_content=False,
**self._sanitize_log_kwargs(tail),
)
yield from log_stream
except client.ApiException as e:
if e.status == 400:
raise PodNotReadyError(
name=pod.metadata.name,
namespace=pod.metadata.namespace,
) from e
raise
def delete_resource(
self,
gvk: GroupVersionKind,
name: str,
namespace: str,
propagation_policy: Literal[
"Foreground", "Background", "Orphan"
] = "Foreground",
) -> None:
dyn = dynamic.DynamicClient(client.ApiClient())
resource = dyn.resources.get(
api_version=f"{gvk.group}/{gvk.version}" if gvk.group else gvk.version,
kind=gvk.kind,
)
dyn.delete(
resource,
name=name,
namespace=namespace,
body=client.V1DeleteOptions(propagation_policy=propagation_policy),
)
def list_workloads(self, namespace: str | None = None) -> list[KueueWorkload]:
api = client.CustomObjectsApi()
workloads = api.list_namespaced_custom_object(
group="kueue.x-k8s.io",
version="v1beta1",
namespace=namespace or self.namespace,
plural="workloads",
)
self._core_v1_api.list_namespaced_pod(
namespace=namespace or self.namespace,
)
return [
KueueWorkload.model_validate(workload)
for workload in workloads.get("items", [])
]
def ensure_namespace(self, name: str) -> tuple[client.V1Namespace, bool]:
"""Create or look up a namespace by name
Returns
-------
tuple[client.V1Namespace, bool]
The namespace object and a boolean indicating whether it was created
"""
try:
return self._core_v1_api.read_namespace(name), False
except client.ApiException as e:
if e.status == 404:
return self._core_v1_api.create_namespace(
client.V1Namespace(metadata=client.V1ObjectMeta(name=name))
), True
raise
def add_finalizer(
self,
resource: client.V1Namespace | client.V1CustomResourceDefinition,
finalizer: str,
) -> None:
"""Add a finalizer to a Kubernetes resource"""
if resource.metadata.finalizers is None:
resource.metadata.finalizers = []
if finalizer not in resource.metadata.finalizers:
resource.metadata.finalizers.append(finalizer)
if isinstance(resource, client.V1Namespace):
self._core_v1_api.replace_namespace(resource.metadata.name, resource)
else:
api = client.CustomObjectsApi()
api.replace_namespaced_custom_object(
group=resource.api_version.split("/")[0],
version=resource.api_version.split("/")[1],
namespace=resource.metadata.namespace,
plural=resource.kind.lower() + "s",
name=resource.metadata.name,
body=resource,
)