Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Correctly display job creation errors #133

Merged
merged 3 commits into from
Apr 10, 2024
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
5 changes: 3 additions & 2 deletions prefect_kubernetes/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
import asyncio
import base64
import enum
import json
import logging
import math
import os
Expand Down Expand Up @@ -795,8 +796,8 @@ def _create_job(
message = ""
if exc.reason:
message += ": " + exc.reason
if exc.body and "message" in exc.body:
message += ": " + exc.body["message"]
if exc.body and "message" in (body := json.loads(exc.body)):
message += ": " + body["message"]

raise InfrastructureError(
f"Unable to create Kubernetes job{message}"
Expand Down
89 changes: 49 additions & 40 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import json
import re
import uuid
from contextlib import contextmanager
Expand Down Expand Up @@ -1008,7 +1009,7 @@ def flow_run(self):

@pytest.fixture
def deployment(self):
return DeploymentResponse(name="my-deployment-name")
return DeploymentResponse(name="my-deployment-name", flow_id=uuid.uuid4())

@pytest.fixture
def flow(self):
Expand Down Expand Up @@ -1522,16 +1523,18 @@ async def test_create_job_failure(
mock_batch_client,
):
response = MagicMock()
response.data = {
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
response.data = json.dumps(
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
)
response.status = 403
response.reason = "Forbidden"

Expand Down Expand Up @@ -1563,16 +1566,18 @@ async def test_create_job_retries(
):
MAX_ATTEMPTS = 3
response = MagicMock()
response.data = {
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
response.data = json.dumps(
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
)
response.status = 403
response.reason = "Forbidden"

Expand Down Expand Up @@ -1605,16 +1610,18 @@ async def test_create_job_failure_no_reason(
mock_batch_client,
):
response = MagicMock()
response.data = {
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
response.data = json.dumps(
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
)
response.status = 403
response.reason = None

Expand Down Expand Up @@ -1645,15 +1652,17 @@ async def test_create_job_failure_no_message(
mock_batch_client,
):
response = MagicMock()
response.data = {
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
response.data = json.dumps(
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
)
response.status = 403
response.reason = "Test"

Expand Down