Skip to content

Commit a9b3bd9

Browse files
RoiGlinikarikalon1
authored andcommitted
ROB-746 use action exceptions for resource yaml action (#1750)
* add action exceptions to get resource yaml action * use new error code for resource permission
1 parent 00d1d1c commit a9b3bd9

2 files changed

Lines changed: 6 additions & 19 deletions

File tree

playbooks/robusta_playbooks/k8s_resource_enrichments.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -296,41 +296,27 @@ def get_resource_yaml(event: KubernetesResourceEvent):
296296
"""
297297
resource = event.get_resource()
298298
if not resource:
299-
logging.error("resource not found...")
300-
return
299+
raise ActionException(ErrorCodes.RESOURCE_NOT_FOUND, "Resource not found for get resource yaml.")
301300

302301
resource_kind = resource.kind
303302
namespace: str = resource.metadata.namespace
304303
name: str = resource.metadata.name
305304

306305
if resource_kind.lower() in RESOURCE_YAML_BLOCK_LIST:
307-
event.add_enrichment(
308-
[
309-
MarkdownBlock(f"{resource_kind} is blocked."),
310-
FileBlock(f"{name}.yaml", f"{resource_kind} is blocked.".encode()),
311-
],
312-
)
313-
return
306+
raise ActionException(ErrorCodes.RESOURCE_NOT_PERMITTED, f"{resource_kind.lower()} type is blocked for get resource yaml.")
314307

315308
try:
316309
resource_yaml = hikaru.get_yaml(resource)
317-
318310
event.add_enrichment(
319311
[
320312
MarkdownBlock(f"Your YAML file for {resource_kind} {namespace}/{name}"),
321313
FileBlock(f"{name}.yaml", resource_yaml.encode()),
322314
],
323315
)
324316
except KeyError:
325-
logging.error(f"{resource_kind} is not supported resource kind")
326-
except kubernetes.client.exceptions.ApiException as exc:
327-
if exc.status == 404:
328-
logging.error(f"{resource_kind.title()} {namespace}/{name} was not found")
329-
else:
330-
logging.error(f"A following error occurred: {str(exc)}")
331-
except Exception as exc:
332-
logging.error("Unexpected error occurred!")
333-
logging.exception(exc)
317+
raise ActionException(ErrorCodes.RESOURCE_NOT_SUPPORTED, f"{resource_kind} is not supported for get resource yaml.")
318+
except Exception as e:
319+
raise ActionException(ErrorCodes.ACTION_UNEXPECTED_ERROR, f"{e}")
334320

335321

336322
class NamedResourcesParams(ActionParams):

src/robusta/utils/error_codes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ErrorCodes(Enum):
2525
RESOURCE_NOT_SUPPORTED = 4701
2626
RESOURCE_NOT_FOUND = 4702
2727
ILLEGAL_ACTION_PARAMS = 4703
28+
RESOURCE_NOT_PERMITTED = 4704
2829

2930
ALERT_MANAGER_DISCOVERY_FAILED = 5000
3031
ALERT_MANAGER_REQUEST_FAILED = 5001

0 commit comments

Comments
 (0)