From b9d2f43a8d1270c1877d1d57dc88e28a631158d6 Mon Sep 17 00:00:00 2001 From: Graham Dumpleton Date: Mon, 5 Aug 2024 11:32:48 +1000 Subject: [PATCH] Track capacity details in workshop environment status. --- .../01-crds-workshopenvironment.yaml | 6 +++ project-docs/release-notes/version-3.0.0.md | 3 ++ .../apps/workshops/manager/environments.py | 50 +++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/carvel-packages/installer/bundle/config/ytt/_ytt_lib/packages/educates/11-session-manager/01-crds-workshopenvironment.yaml b/carvel-packages/installer/bundle/config/ytt/_ytt_lib/packages/educates/11-session-manager/01-crds-workshopenvironment.yaml index 50ccb26b0..b733c1f7e 100644 --- a/carvel-packages/installer/bundle/config/ytt/_ytt_lib/packages/educates/11-session-manager/01-crds-workshopenvironment.yaml +++ b/carvel-packages/installer/bundle/config/ytt/_ytt_lib/packages/educates/11-session-manager/01-crds-workshopenvironment.yaml @@ -154,6 +154,12 @@ spec: type: string namespace: type: string + capacity: + type: integer + initial: + type: integer + reserved: + type: integer secrets: type: object properties: diff --git a/project-docs/release-notes/version-3.0.0.md b/project-docs/release-notes/version-3.0.0.md index 8ca50ff80..9901da91c 100644 --- a/project-docs/release-notes/version-3.0.0.md +++ b/project-docs/release-notes/version-3.0.0.md @@ -28,6 +28,9 @@ New Features recorded in the status of the `WorkshopSession` resource, as well as in the `WorkshopAllocation` resource. +* The capacity details for a workshop environment are now recorded in the status + of the `WorkshopEnvironment` resource. + * When requesting a workshop session via the REST API, if the `session` param is supplied along with `user` then an existing workshop session for the user will only be returned if the name of that session also matches that supplied. When diff --git a/training-portal/src/project/apps/workshops/manager/environments.py b/training-portal/src/project/apps/workshops/manager/environments.py index aecea2e3b..42a22eac6 100644 --- a/training-portal/src/project/apps/workshops/manager/environments.py +++ b/training-portal/src/project/apps/workshops/manager/environments.py @@ -421,6 +421,10 @@ def update_workshop_environments(training_portal, workshops): environment.save() + update_environment_status_details( + environment.name, environment.capacity, environment.reserved + ) + @background_task @resources_lock @@ -523,6 +527,13 @@ def process_workshop_environment(portal, workshop, position): "theme": {"name": settings.THEME_NAME}, "cookies": {"domain": settings.SESSION_COOKIE_DOMAIN}, }, + "status": { + settings.OPERATOR_STATUS_KEY: { + "capacity": environment.capacity, + "initial": environment.initial, + "reserved": environment.reserved, + }, + }, } if settings.GOOGLE_TRACKING_ID is not None: @@ -644,3 +655,42 @@ def replace_workshop_environment(environment): # Now schedule creation of the replacement workshop session. process_workshop_environment(environment.portal, workshop, position).schedule() + + +def update_environment_status_details(name, capacity, reserved): + """Update the capacity for the workshop environment recorded in the status.""" + + try: + K8SWorkshopEnvironment = pykube.object_factory( + api, + f"training.{settings.OPERATOR_API_GROUP}/v1beta1", + "WorkshopEnvironment", + ) + + resource = K8SWorkshopEnvironment.objects(api).get(name=name) + + # The status may not exist as yet if not processed by the operator. + + status = resource.obj.setdefault("status", {}).setdefault( + settings.OPERATOR_STATUS_KEY, {} + ) + + status["capacity"] = capacity + status["reserved"] = reserved + + resource.update() + + logger.info( + "Updated status of workshop environment %s with capacity=%s and reserved=%s.", + name, + capacity, + reserved, + ) + + except pykube.exceptions.ObjectDoesNotExist: + pass + + except pykube.exceptions.PyKubeError: + logger.exception( + "Failed to update status details of workshop environment %s.", name + )