-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathopenshift.py
More file actions
469 lines (380 loc) · 16.1 KB
/
openshift.py
File metadata and controls
469 lines (380 loc) · 16.1 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import functools
import json
import logging
import os
import re
import requests
from requests.auth import HTTPBasicAuth
import time
from simplejson.errors import JSONDecodeError
import kubernetes
import kubernetes.dynamic.exceptions as kexc
from openshift.dynamic import DynamicClient
from coldfront_plugin_cloud import attributes, base, utils
logger = logging.getLogger(__name__)
API_PROJECT = "project.openshift.io/v1"
API_USER = "user.openshift.io/v1"
API_RBAC = "rbac.authorization.k8s.io/v1"
API_CORE = "v1"
IGNORED_ATTRIBUTES = [
"resourceVersion",
"creationTimestamp",
"uid",
]
PROJECT_DEFAULT_LABELS = {
"opendatahub.io/dashboard": "true",
"modelmesh-enabled": "true",
"nerc.mghpcc.org/allow-unencrypted-routes": "true",
}
def clean_openshift_metadata(obj):
if "metadata" in obj:
for attr in IGNORED_ATTRIBUTES:
if attr in obj["metadata"]:
del obj["metadata"][attr]
return obj
def parse_openshift_quota_value(attr_name, quota_value):
PATTERN = r"([0-9]+)(m|Ki|Mi|Gi|Ti|Pi|Ei|K|M|G|T|P|E)?"
suffix = {
"Ki": 2**10,
"Mi": 2**20,
"Gi": 2**30,
"Ti": 2**40,
"Pi": 2**50,
"Ei": 2**60,
"m": 10**-3,
"K": 10**3,
"M": 10**6,
"G": 10**9,
"T": 10**12,
"P": 10**15,
"E": 10**18,
}
if quota_value and quota_value != "0":
result = re.search(PATTERN, quota_value)
if result is None:
raise ValueError(
f"Unable to parse quota_value = '{quota_value}' for {attr_name}"
)
value = int(result.groups()[0])
unit = result.groups()[1]
# Convert to number i.e. without any unit suffix
if unit is not None:
quota_value = value * suffix[unit]
else:
quota_value = value
# Convert some attributes to units that coldfront uses
if "RAM" in attr_name:
return round(quota_value / suffix["Mi"])
elif "Storage" in attr_name:
return round(quota_value / suffix["Gi"])
return quota_value
elif quota_value and quota_value == "0":
return 0
class ApiException(Exception):
def __init__(self, message):
self.message = message
class NotFound(ApiException):
pass
class Conflict(ApiException):
pass
class OpenShiftResourceAllocator(base.ResourceAllocator):
QUOTA_KEY_MAPPING = {
attributes.QUOTA_LIMITS_CPU: lambda x: {"limits.cpu": f"{x * 1000}m"},
attributes.QUOTA_LIMITS_MEMORY: lambda x: {"limits.memory": f"{x}Mi"},
attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB: lambda x: {
"limits.ephemeral-storage": f"{x}Gi"
},
attributes.QUOTA_REQUESTS_STORAGE: lambda x: {"requests.storage": f"{x}Gi"},
attributes.QUOTA_REQUESTS_GPU: lambda x: {"requests.nvidia.com/gpu": f"{x}"},
attributes.QUOTA_PVC: lambda x: {"persistentvolumeclaims": f"{x}"},
}
CF_QUOTA_KEY_MAPPING = {
list(quota_lambda_func(0).keys())[0]: cf_attr
for cf_attr, quota_lambda_func in QUOTA_KEY_MAPPING.items()
}
resource_type = "openshift"
project_name_max_length = 63
def __init__(self, resource, allocation):
super().__init__(resource, allocation)
self.safe_resource_name = utils.env_safe_name(resource.name)
self.id_provider = resource.get_attribute(attributes.RESOURCE_IDENTITY_NAME)
self.apis = {}
self.functional_tests = os.environ.get("FUNCTIONAL_TESTS", "").lower()
self.verify = os.getenv(
f"OPENSHIFT_{self.safe_resource_name}_VERIFY", ""
).lower()
@functools.cached_property
def k8_client(self):
# Load Endpoint URL and Auth token for new k8 client
openshift_token = os.getenv(f"OPENSHIFT_{self.safe_resource_name}_TOKEN")
openshift_url = self.resource.get_attribute(attributes.RESOURCE_API_URL)
k8_config = kubernetes.client.Configuration()
k8_config.api_key["authorization"] = openshift_token
k8_config.api_key_prefix["authorization"] = "Bearer"
k8_config.host = openshift_url
if self.verify == "false":
k8_config.verify_ssl = False
else:
k8_config.verify_ssl = True
k8s_client = kubernetes.client.ApiClient(configuration=k8_config)
return DynamicClient(k8s_client)
@functools.cached_property
def session(self):
var_name = utils.env_safe_name(self.resource.name)
username = os.getenv(f"OPENSHIFT_{var_name}_USERNAME")
password = os.getenv(f"OPENSHIFT_{var_name}_PASSWORD")
session = requests.session()
if username and password:
session.auth = HTTPBasicAuth(username, password)
functional_tests = os.environ.get("FUNCTIONAL_TESTS", "").lower()
verify = os.getenv(f"OPENSHIFT_{var_name}_VERIFY", "").lower()
if functional_tests == "true" or verify == "false":
session.verify = False
return session
@staticmethod
def check_response(response: requests.Response):
if 200 <= response.status_code < 300:
try:
return response.json()
except JSONDecodeError:
# https://github.com/CCI-MOC/openshift-acct-mgt/issues/54
return response.text
if response.status_code == 404:
raise NotFound(f"{response.status_code}: {response.text}")
elif "does not exist" in response.text or "not found" in response.text:
raise NotFound(f"{response.status_code}: {response.text}")
elif "already exists" in response.text:
raise Conflict(f"{response.status_code}: {response.text}")
else:
raise ApiException(f"{response.status_code}: {response.text}")
def qualified_id_user(self, id_user):
return f"{self.id_provider}:{id_user}"
def get_resource_api(self, api_version: str, kind: str):
"""Either return the cached resource api from self.apis, or fetch a
new one, store it in self.apis, and return it."""
k = f"{api_version}:{kind}"
api = self.apis.setdefault(
k, self.k8_client.resources.get(api_version=api_version, kind=kind)
)
return api
def create_project(self, suggested_project_name):
sanitized_project_name = utils.get_sanitized_project_name(
suggested_project_name
)
project_id = utils.get_unique_project_name(
sanitized_project_name, max_length=self.project_name_max_length
)
project_name = project_id
self._create_project(project_name, project_id)
return self.Project(project_name, project_id)
def patch_project(self, project_id, new_project_spec):
self._openshift_patch_namespace(project_id, new_project_spec)
def delete_moc_quotas(self, project_id):
"""deletes all resourcequotas from an openshift project"""
resourcequotas = self._openshift_get_resourcequotas(project_id)
for resourcequota in resourcequotas:
self._openshift_delete_resourcequota(
project_id, resourcequota["metadata"]["name"]
)
logger.info(f"All quotas for {project_id} successfully deleted")
def set_quota(self, project_id):
"""Sets the quota for a project, creating a minimal resourcequota
object in the project namespace with no extra scopes"""
quota_spec = {}
for key, func in self.QUOTA_KEY_MAPPING.items():
if (x := self.allocation.get_attribute(key)) is not None:
quota_spec.update(func(x))
quota_def = {
"metadata": {"name": f"{project_id}-project"},
"spec": {"hard": quota_spec},
}
self.delete_moc_quotas(project_id)
self._openshift_create_resourcequota(project_id, quota_def)
logger.info(f"Quota for {project_id} successfully created")
def get_quota(self, project_id):
cloud_quotas = self._openshift_get_resourcequotas(project_id)
combined_quota = {}
for cloud_quota in cloud_quotas:
combined_quota.update(cloud_quota["spec"]["hard"])
return combined_quota
def get_usage(self, project_id):
cloud_quotas = self._openshift_get_resourcequotas(project_id)
combined_quota_used = {}
for cloud_quota in cloud_quotas:
combined_quota_used.update(
{
self.CF_QUOTA_KEY_MAPPING[quota_key]: parse_openshift_quota_value(
self.CF_QUOTA_KEY_MAPPING[quota_key], value
)
for quota_key, value in cloud_quota["status"]["used"].items()
}
)
return combined_quota_used
def create_project_defaults(self, project_id):
pass
def disable_project(self, project_id):
url = f"{self.auth_url}/projects/{project_id}"
r = self.session.delete(url)
self.check_response(r)
def reactivate_project(self, project_id):
project_name = self.allocation.get_attribute(attributes.ALLOCATION_PROJECT_NAME)
try:
self._create_project(project_name, project_id)
except Conflict:
# This is a reactivation of an already active project
# most likely for a quota update
pass
def get_federated_user(self, username):
if (
self._openshift_user_exists(username)
and self._openshift_identity_exists(username)
and self._openshift_useridentitymapping_exists(username, username)
):
return {"username": username}
logger.info(f"User ({username}) does not exist")
def create_federated_user(self, unique_id):
url = f"{self.auth_url}/users/{unique_id}"
try:
r = self.session.put(url)
self.check_response(r)
except Conflict:
pass
def assign_role_on_user(self, username, project_id):
# /users/<user_name>/projects/<project>/roles/<role>
url = (
f"{self.auth_url}/users/{username}/projects/{project_id}"
f"/roles/{self.member_role_name}"
)
try:
r = self.session.put(url)
self.check_response(r)
except Conflict:
pass
def remove_role_from_user(self, username, project_id):
# /users/<user_name>/projects/<project>/roles/<role>
url = (
f"{self.auth_url}/users/{username}/projects/{project_id}"
f"/roles/{self.member_role_name}"
)
r = self.session.delete(url)
self.check_response(r)
def _create_project(self, project_name, project_id):
url = f"{self.auth_url}/projects/{project_id}"
headers = {"Content-type": "application/json"}
annotations = {
"cf_project_id": str(self.allocation.project_id),
"cf_pi": self.allocation.project.pi.username,
}
payload = {
"displayName": project_name,
"annotations": annotations,
"labels": PROJECT_DEFAULT_LABELS,
}
r = self.session.put(url, data=json.dumps(payload), headers=headers)
self.check_response(r)
def _get_role(self, username, project_id):
# /users/<user_name>/projects/<project>/roles/<role>
url = (
f"{self.auth_url}/users/{username}/projects/{project_id}"
f"/roles/{self.member_role_name}"
)
r = self.session.get(url)
return self.check_response(r)
def _get_project(self, project_id):
url = f"{self.auth_url}/projects/{project_id}"
r = self.session.get(url)
return self.check_response(r)
def _delete_user(self, username):
url = f"{self.auth_url}/users/{username}"
r = self.session.delete(url)
return self.check_response(r)
def get_users(self, project_id):
url = f"{self.auth_url}/projects/{project_id}/users"
r = self.session.get(url)
return set(self.check_response(r))
def _openshift_get_user(self, username):
api = self.get_resource_api(API_USER, "User")
return clean_openshift_metadata(api.get(name=username).to_dict())
def _openshift_get_identity(self, id_user):
api = self.get_resource_api(API_USER, "Identity")
return clean_openshift_metadata(
api.get(name=self.qualified_id_user(id_user)).to_dict()
)
def _openshift_user_exists(self, user_name):
try:
self._openshift_get_user(user_name)
except kexc.NotFoundError as e:
# Ensures error raise because resource not found,
# not because of other reasons, like incorrect url
e_info = json.loads(e.body)
if (
e_info["reason"] == "NotFound"
and e_info["details"]["name"] == user_name
):
return False
raise e
return True
def _openshift_identity_exists(self, id_user):
try:
self._openshift_get_identity(id_user)
except kexc.NotFoundError as e:
e_info = json.loads(e.body)
if e_info.get("reason") == "NotFound":
return False
raise e
return True
def _openshift_useridentitymapping_exists(self, user_name, id_user):
try:
user = self._openshift_get_user(user_name)
except kexc.NotFoundError as e:
e_info = json.loads(e.body)
if e_info.get("reason") == "NotFound":
return False
raise e
return any(
identity == self.qualified_id_user(id_user)
for identity in user.get("identities", [])
)
def _openshift_get_project(self, project_name):
api = self.get_resource_api(API_PROJECT, "Project")
return clean_openshift_metadata(api.get(name=project_name).to_dict())
def _openshift_get_namespace(self, namespace_name):
api = self.get_resource_api(API_CORE, "Namespace")
return clean_openshift_metadata(api.get(name=namespace_name).to_dict())
def _openshift_patch_namespace(self, project_name, new_project_spec):
# During testing, apparently we can't patch Projects, but we can do so with Namespaces
api = self.get_resource_api(API_CORE, "Namespace")
api.patch(name=project_name, body=new_project_spec)
def _openshift_get_resourcequotas(self, project_id):
"""Returns a list of resourcequota objects in namespace with name `project_id`"""
# Raise a NotFound error if the project doesn't exist
self._openshift_get_project(project_id)
api = self.get_resource_api(API_CORE, "ResourceQuota")
res = clean_openshift_metadata(api.get(namespace=project_id).to_dict())
return res["items"]
def _wait_for_quota_to_settle(self, project_id, resource_quota):
"""Wait for quota on resourcequotas to settle.
When creating a new resourcequota that sets a quota on resourcequota objects, we need to
wait for OpenShift to calculate the quota usage before we attempt to create any new
resourcequota objects.
"""
if "resourcequotas" in resource_quota["spec"]["hard"]:
logger.info("waiting for resourcequota quota")
api = self.get_resource_api(API_CORE, "ResourceQuota")
while True:
resp = clean_openshift_metadata(
api.get(
namespace=project_id, name=resource_quota["metadata"]["name"]
).to_dict()
)
if "resourcequotas" in resp["status"].get("used", {}):
break
time.sleep(0.1)
def _openshift_create_resourcequota(self, project_id, quota_def):
api = self.get_resource_api(API_CORE, "ResourceQuota")
res = api.create(namespace=project_id, body=quota_def).to_dict()
self._wait_for_quota_to_settle(project_id, res)
def _openshift_delete_resourcequota(self, project_id, resourcequota_name):
"""In an openshift namespace {project_id) delete a specified resourcequota"""
api = self.get_resource_api(API_CORE, "ResourceQuota")
return api.delete(namespace=project_id, name=resourcequota_name).to_dict()