Skip to content

Commit 995563f

Browse files
committed
Allow adding default labels to Openshift allocations
All Openshift projects created through Coldfront will now have an additional label: `'nerc.mghpcc.org/allow-unencrypted-routes': "true"` `validate_allocations` has also been changed to ensure all current Openshift projects contain a few default labels: ``` PROJECT_DEFAULT_LABELS = { 'opendatahub.io/dashboard': "true", 'modelmesh-enabled': "true", 'nerc.mghpcc.org/allow-unencrypted-routes': "true" } ``` Implementing this required code in the Openshift allocator to interact with the Namespace API. For reasons not entirely clear in the documentation, it is not possible change a Project’s labels directly. This is only possible through the Namespace API.
1 parent ca050e9 commit 995563f

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

src/coldfront_plugin_cloud/management/commands/validate_allocations.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ def sync_users(project_id, allocation, allocator, apply):
5252

5353
return failed_validation
5454

55+
@staticmethod
56+
def sync_openshift_project_labels(project_id, allocator, apply):
57+
cloud_namespace_obj = allocator._openshift_get_namespace(project_id)
58+
cloud_namespace_obj_labels = cloud_namespace_obj["metadata"]["labels"]
59+
if missing_or_incorrect_labels := [
60+
label_items[0] for label_items in openshift.PROJECT_DEFAULT_LABELS.items() if
61+
label_items not in cloud_namespace_obj_labels.items()
62+
]:
63+
logger.warning(
64+
f"Openshift project {project_id} is missing default labels: {', '.join(missing_or_incorrect_labels)}"
65+
)
66+
cloud_namespace_obj_labels.update(openshift.PROJECT_DEFAULT_LABELS)
67+
if apply:
68+
allocator.replace_project(project_id, cloud_namespace_obj)
69+
logger.warning(f"Labels updated for Openshift project {project_id}: {', '.join(missing_or_incorrect_labels)}")
70+
5571
def check_institution_specific_code(self, allocation, apply):
5672
attr = attributes.ALLOCATION_INSTITUTION_SPECIFIC_CODE
5773
isc = allocation.get_attribute(attr)
@@ -186,6 +202,7 @@ def handle(self, *args, **options):
186202
quota = allocator.get_quota(project_id)
187203

188204
failed_validation = Command.sync_users(project_id, allocation, allocator, options["apply"])
205+
Command.sync_openshift_project_labels(project_id, allocator, options["apply"])
189206

190207
for attr in attributes.ALLOCATION_QUOTA_ATTRIBUTES:
191208
if "OpenShift" in attr.name:

src/coldfront_plugin_cloud/openshift.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def clean_openshift_metadata(obj):
4444
attributes.QUOTA_PVC: lambda x: {"persistentvolumeclaims": f"{x}"},
4545
}
4646

47+
PROJECT_DEFAULT_LABELS = {
48+
'opendatahub.io/dashboard': "true",
49+
'modelmesh-enabled': "true",
50+
'nerc.mghpcc.org/allow-unencrypted-routes': "true"
51+
}
52+
4753

4854
class ApiException(Exception):
4955
def __init__(self, message):
@@ -147,6 +153,10 @@ def create_project(self, suggested_project_name):
147153
self._create_project(project_name, project_id)
148154
return self.Project(project_name, project_id)
149155

156+
def replace_project(self, project_id, new_project_spec):
157+
sanitized_project_name = utils.get_sanitized_project_name(project_id)
158+
self._openshift_replace_namespace(sanitized_project_name, new_project_spec)
159+
150160
def delete_moc_quotas(self, project_id):
151161
"""deletes all resourcequotas from an openshift project"""
152162
resourcequotas = self._openshift_get_resourcequotas(project_id)
@@ -241,14 +251,10 @@ def _create_project(self, project_name, project_id):
241251
headers = {"Content-type": "application/json"}
242252
annotations = {"cf_project_id": str(self.allocation.project_id),
243253
"cf_pi": self.allocation.project.pi.username}
244-
labels = {
245-
'opendatahub.io/dashboard': "true",
246-
'modelmesh-enabled': "true",
247-
}
248254

249255
payload = {"displayName": project_name,
250256
"annotations": annotations,
251-
"labels": labels}
257+
"labels": PROJECT_DEFAULT_LABELS}
252258
r = self.session.put(url, data=json.dumps(payload), headers=headers)
253259
self.check_response(r)
254260

@@ -326,6 +332,15 @@ def _openshift_useridentitymapping_exists(self, user_name, id_user):
326332
def _openshift_get_project(self, project_name):
327333
api = self.get_resource_api(API_PROJECT, "Project")
328334
return clean_openshift_metadata(api.get(name=project_name).to_dict())
335+
336+
def _openshift_get_namespace(self, namespace_name):
337+
api = self.get_resource_api(API_CORE, "Namespace")
338+
return clean_openshift_metadata(api.get(name=namespace_name).to_dict())
339+
340+
def _openshift_replace_namespace(self, project_name, new_project_spec):
341+
# During testing, apparently we can't patch Projects, but we can do so with Namespaces
342+
api = self.get_resource_api(API_CORE, "Namespace")
343+
res = api.replace(name=project_name, body=new_project_spec)
329344

330345
def _openshift_get_resourcequotas(self, project_id):
331346
"""Returns a list of resourcequota objects in namespace with name `project_id`"""

src/coldfront_plugin_cloud/tests/functional/openshift/test_allocation.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,26 @@ def test_reactivate_allocation(self):
214214
})
215215

216216
allocator._get_role(user.username, project_id)
217+
218+
def test_project_default_labels(self):
219+
user = self.new_user()
220+
project = self.new_project(pi=user)
221+
allocation = self.new_allocation(project, self.resource, 1)
222+
allocator = openshift.OpenShiftResourceAllocator(self.resource,
223+
allocation)
224+
225+
tasks.activate_allocation(allocation.pk)
226+
allocation.refresh_from_db()
227+
228+
project_id = allocation.get_attribute(attributes.ALLOCATION_PROJECT_ID)
229+
230+
# Check project labels
231+
namespace_dict_labels = allocator._openshift_get_namespace(project_id)["metadata"]["labels"]
232+
self.assertTrue(namespace_dict_labels.items() > openshift.PROJECT_DEFAULT_LABELS.items())
233+
234+
# What if we have a new custom label, or changed value?
235+
openshift.PROJECT_DEFAULT_LABELS["test"] = "test"
236+
call_command('validate_allocations', apply=True)
237+
238+
namespace_dict_labels = allocator._openshift_get_namespace(project_id)["metadata"]["labels"]
239+
self.assertTrue(namespace_dict_labels.items() > openshift.PROJECT_DEFAULT_LABELS.items())

0 commit comments

Comments
 (0)