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

raise if __exit__ fail #2294

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions ocp_resources/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Any
from warnings import warn

from ocp_resources.resource import NamespacedResource, Resource


class MissingRequiredArgumentError(Exception):
def __init__(self, argument: str) -> None:
Expand Down Expand Up @@ -53,3 +55,11 @@ def __str__(self):

class NNCPConfigurationFailed(Exception):
pass


class ResourceTeardownError(Exception):
def __init__(self, resource: Resource | NamespacedResource):
self.resource = resource

def _str__(self):
return f"Failed to excute teardown for resource {self.resource}"
32 changes: 15 additions & 17 deletions ocp_resources/resource.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
from __future__ import annotations

from collections.abc import Callable, Generator
import contextlib

import copy
import json
from warnings import warn

import os
import re
import sys
from collections.abc import Callable, Generator
from io import StringIO
from signal import SIGINT, signal
from types import TracebackType
from typing import Any
from warnings import warn

import kubernetes
from kubernetes.dynamic import DynamicClient, ResourceInstance
import yaml
from benedict import benedict
from kubernetes.dynamic import DynamicClient, ResourceInstance
from kubernetes.dynamic.exceptions import (
ConflictError,
ForbiddenError,
MethodNotAllowedError,
NotFoundError,
ForbiddenError,
ResourceNotFoundError,
)
from kubernetes.dynamic.resource import ResourceField
from packaging.version import Version
from simple_logger.logger import get_logger, logging
from timeout_sampler import (
TimeoutExpiredError,
TimeoutSampler,
TimeoutWatch,
)
from urllib3.exceptions import MaxRetryError

from ocp_resources.event import Event
from ocp_resources.exceptions import MissingRequiredArgumentError, MissingResourceResError, ResourceTeardownError
from ocp_resources.utils.constants import (
DEFAULT_CLUSTER_RETRY_EXCEPTIONS,
NOT_FOUND_ERROR_EXCEPTION_DICT,
PROTOCOL_ERROR_EXCEPTION_DICT,
TIMEOUT_1MINUTE,
TIMEOUT_1SEC,
TIMEOUT_4MINUTES,
TIMEOUT_5SEC,
TIMEOUT_10SEC,
TIMEOUT_30SEC,
TIMEOUT_5SEC,
TIMEOUT_1SEC,
)
from ocp_resources.event import Event
from timeout_sampler import (
TimeoutExpiredError,
TimeoutSampler,
TimeoutWatch,
)
from ocp_resources.exceptions import MissingRequiredArgumentError, MissingResourceResError
from ocp_resources.utils.resource_constants import ResourceConstants
from ocp_resources.utils.utils import skip_existing_resource_creation_teardown


LOGGER = get_logger(name=__name__)
MAX_SUPPORTED_API_VERSION = "v2"

Expand Down Expand Up @@ -571,7 +568,8 @@ def __exit__(
exc_tb: TracebackType | None = None,
) -> None:
if self.teardown:
self.clean_up()
if not self.clean_up():
raise ResourceTeardownError(resource=self)

def _sigint_handler(self, signal_received: int, frame: Any) -> None:
self.__exit__()
Expand Down