Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Remove debug option from kubernetes client #602

Merged
merged 2 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/graderservice/graderservice/graderservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


# namespace to deploy new pods
NAMESPACE = os.environ.get("ILLUMIDESK_K8S_NAMESPACE", "test")
NAMESPACE = os.environ.get("ILLUMIDESK_K8S_NAMESPACE", "default")
# image name for grader-notebooks
GRADER_IMAGE_NAME = os.environ.get(
"GRADER_IMAGE_NAME", "illumidesk/grader-notebook:latest"
Expand All @@ -39,8 +39,8 @@
)

# user UI and GID to use within the grader container
NB_UID = os.environ.get("NB_UID", 10001)
NB_GID = os.environ.get("NB_GID", 100)
NB_UID = int(os.environ.get("NB_UID", 10001))
NB_GID = int(os.environ.get("NB_GID", 100))

# NBGrader DATABASE settings to save in nbgrader_config.py file
nbgrader_db_host = os.environ.get("POSTGRES_NBGRADER_HOST")
Expand All @@ -51,15 +51,13 @@


class GraderServiceLauncher:
def __init__(self, org_name: str, course_id: str, is_debug: bool = False):
def __init__(self, org_name: str, course_id: str):
"""
Helper class to launch grader notebooks within the kubernetes cluster

Args:
org_name: the organization name
course_id: the course id
is_debug: if true, runs the grader notebook and the kubernetes client with
debugging enabled. Defaults to False.

Raises:
ConfigException if the kubectl python client does not have a valid configuration set.
Expand All @@ -72,14 +70,9 @@ def __init__(self, org_name: str, course_id: str, is_debug: bool = False):
# next method uses the KUBECONFIG env var by default
config.load_kube_config()
# Uncomment the following lines to enable debug logging
kube_client_config = client.Configuration()
kube_client_config.debug = is_debug
self.apps_v1 = client.AppsV1Api(
api_client=client.ApiClient(configuration=kube_client_config)
)
self.apps_v1 = client.AppsV1Api()
self.coreV1Api = client.CoreV1Api()
self.course_id = course_id
self.grader_is_debug = is_debug
self.grader_name = f"grader-{self.course_id}"
self.grader_token = token_hex(32)
self.org_name = org_name
Expand All @@ -99,7 +92,6 @@ def grader_deployment_exists(self) -> bool:
)
if deployment_list and deployment_list.items:
logger.info("Deployment exists for %s", self.grader_name)
print("Deployment exists for %s", self.grader_name)
return True

return False
Expand Down Expand Up @@ -238,7 +230,6 @@ def _create_deployment_object(self):
),
security_context=client.V1SecurityContext(allow_privilege_escalation=False),
env=[
client.V1EnvVar(name="DEBUG", value=self.grader_is_debug),
client.V1EnvVar(name="JUPYTERHUB_SERVICE_NAME", value=self.course_id),
client.V1EnvVar(name="JUPYTERHUB_API_TOKEN", value=self.grader_token),
# we're using the K8s Service name 'hub' (defined in the jhub helm chart)
Expand Down
53 changes: 30 additions & 23 deletions src/graderservice/graderservice/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def launch(org_name: str, course_id: str):
url=f"http://{launcher.grader_name}:8888",
api_token=launcher.grader_token,
)
db.session.expire_on_commit = False
db.session.add(new_service)
db.session.commit()
# then do patch for jhub deployment
Expand All @@ -72,6 +71,7 @@ def launch(org_name: str, course_id: str):
return jsonify(success=False, message=str(e)), 500
finally:
db.session.close()

else:
logger.info("A grader service exists for the course_id %s" % course_id)
return (
Expand Down Expand Up @@ -100,27 +100,35 @@ def services():
}
```
"""
services = GraderService.query.all()
# format a json
services_resp = []
groups_resp = {}
for s in services:
services_resp.append(
{
"name": s.name,
"url": s.url,
"oauth_no_confirm": s.oauth_no_confirm,
"admin": s.admin,
"api_token": s.api_token,
}
)
# add the jhub user group
groups_resp.update({f"formgrade-{s.course_id}": [f"grader-{s.course_id}"]})
logger.debug(
"Adding formgrade-%s and grader-%s to response" % (s.course_id, s.course_id)
)
logger.info("Services response %s and %s" % (services_resp, groups_resp))
return jsonify(services=services_resp, groups=groups_resp)
try:
services = GraderService.query.all()
# format a json
services_resp = []
groups_resp = {}
for s in services:
services_resp.append(
{
"name": s.name,
"url": s.url,
"oauth_no_confirm": s.oauth_no_confirm,
"admin": s.admin,
"api_token": s.api_token,
}
)
# add the jhub user group
groups_resp.update({f"formgrade-{s.course_id}": [f"grader-{s.course_id}"]})
logger.debug(
"Adding formgrade-%s and grader-%s to response"
% (s.course_id, s.course_id)
)
logger.info("Services response %s and %s" % (services_resp, groups_resp))
return jsonify(services=services_resp, groups=groups_resp)
except Exception as e:
logger.error("Exception when calling services: %s" % e)
db.session.rollback()
return jsonify(success=False, error=str(e)), 500
finally:
db.session.close()


@grader_setup_bp.route("/services/<org_name>/<course_id>", methods=["DELETE"])
Expand All @@ -139,7 +147,6 @@ def services_deletion(org_name: str, course_id: str):
launcher.delete_grader_deployment()
service_saved = GraderService.query.filter_by(course_id=course_id).first()
if service_saved:
db.session.expire_on_commit = False
db.session.delete(service_saved)
db.session.commit()
logger.info("Deleted grader service for course %s:" % course_id)
Expand Down