Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rest): detect session status from the pod state #611

Merged
merged 1 commit into from
Jan 16, 2025
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
16 changes: 16 additions & 0 deletions reana_workflow_controller/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from reana_commons.config import (
REANA_WORKFLOW_UMASK,
REANA_RUNTIME_SESSIONS_KUBERNETES_NODE_LABEL,
REANA_RUNTIME_KUBERNETES_NAMESPACE,
)
from reana_commons.k8s.api_client import (
current_k8s_appsv1_api_client,
Expand Down Expand Up @@ -479,3 +480,18 @@
plural="middlewares",
name=f"replacepath-{workflow_id}",
)


def check_pod_status_by_prefix(
pod_name_prefix, namespace=REANA_RUNTIME_KUBERNETES_NAMESPACE
):
"""Check if there is a Pod in the given namespace whose name starts with the specified prefix. We assume that there exists 0 or 1 pod with a given prefix."""
try:
pods = current_k8s_corev1_api_client.list_namespaced_pod(namespace=namespace)

Check warning on line 490 in reana_workflow_controller/k8s.py

View check run for this annotation

Codecov / codecov/patch

reana_workflow_controller/k8s.py#L489-L490

Added lines #L489 - L490 were not covered by tests

for pod in pods.items:
if pod.metadata.name.startswith(pod_name_prefix):
return pod.status.phase
return "Not Found"
except ApiException as e:
return f"Error: {e.reason}"

Check warning on line 497 in reana_workflow_controller/k8s.py

View check run for this annotation

Codecov / codecov/patch

reana_workflow_controller/k8s.py#L492-L497

Added lines #L492 - L497 were not covered by tests
16 changes: 16 additions & 0 deletions reana_workflow_controller/rest/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from webargs import fields, validate
from webargs.flaskparser import use_args, use_kwargs
from reana_commons.config import WORKFLOW_TIME_FORMAT
from reana_commons.utils import build_unique_component_name
from reana_db.database import Session
from reana_db.models import RunStatus, User, UserWorkflow, Workflow, WorkflowResource
from reana_db.utils import (
Expand All @@ -48,6 +49,8 @@
use_paginate_args,
)

from reana_workflow_controller.k8s import check_pod_status_by_prefix

START = "start"
STOP = "stop"
DELETED = "deleted"
Expand Down Expand Up @@ -398,7 +401,20 @@
if int_session:
workflow_response["session_type"] = int_session.type_.name
workflow_response["session_uri"] = int_session.path
int_session_pod_name_prefix = build_unique_component_name(

Check warning on line 404 in reana_workflow_controller/rest/workflows.py

View check run for this annotation

Codecov / codecov/patch

reana_workflow_controller/rest/workflows.py#L404

Added line #L404 was not covered by tests
"run-session", int_session.workflow[0].id_
)
if int_session.status == RunStatus.created:
pod_status = check_pod_status_by_prefix(

Check warning on line 408 in reana_workflow_controller/rest/workflows.py

View check run for this annotation

Codecov / codecov/patch

reana_workflow_controller/rest/workflows.py#L407-L408

Added lines #L407 - L408 were not covered by tests
pod_name_prefix=int_session_pod_name_prefix
)
if pod_status == "Running":
int_session.status = RunStatus.running
db_session = Session.object_session(int_session)
db_session.commit()

Check warning on line 414 in reana_workflow_controller/rest/workflows.py

View check run for this annotation

Codecov / codecov/patch

reana_workflow_controller/rest/workflows.py#L411-L414

Added lines #L411 - L414 were not covered by tests

workflow_response["session_status"] = int_session.status.name

# Skip workflow if type is interactive and there is no session
elif type_ == "interactive":
continue
Expand Down
Loading