Skip to content

Commit c98b7d9

Browse files
ikholopov-omniIKholopov
authored andcommitted
Move Pod*Exceptions to separate module
1 parent 4521e8d commit c98b7d9

File tree

5 files changed

+95
-12
lines changed

5 files changed

+95
-12
lines changed

airflow/exceptions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -461,15 +461,15 @@ class TaskDeferralTimeout(AirflowException):
461461
# 2) if you have new provider, both provider and pod generator will throw the
462462
# "airflow.providers.cncf.kubernetes" as it will be imported here from the provider.
463463
try:
464-
from airflow.providers.cncf.kubernetes.pod_generator import PodMutationHookException
464+
from airflow.providers.cncf.kubernetes.exceptions import PodMutationHookException
465465
except ImportError:
466466

467467
class PodMutationHookException(AirflowException): # type: ignore[no-redef]
468468
"""Raised when exception happens during Pod Mutation Hook execution."""
469469

470470

471471
try:
472-
from airflow.providers.cncf.kubernetes.pod_generator import PodReconciliationError
472+
from airflow.providers.cncf.kubernetes.exceptions import PodReconciliationError
473473
except ImportError:
474474

475475
class PodReconciliationError(AirflowException): # type: ignore[no-redef]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from __future__ import annotations
18+
19+
from airflow.exceptions import (
20+
AirflowException,
21+
)
22+
23+
24+
class PodMutationHookException(AirflowException):
25+
"""Raised when exception happens during Pod Mutation Hook execution."""
26+
27+
28+
class PodReconciliationError(AirflowException):
29+
"""Raised when an error is encountered while trying to merge pod configs."""

providers/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@
6161
from airflow.exceptions import AirflowProviderDeprecationWarning
6262
from airflow.executors.base_executor import BaseExecutor
6363
from airflow.executors.executor_constants import KUBERNETES_EXECUTOR
64+
from airflow.providers.cncf.kubernetes.exceptions import PodMutationHookException, PodReconciliationError
6465
from airflow.providers.cncf.kubernetes.executors.kubernetes_executor_types import (
6566
ADOPTED,
6667
POD_EXECUTOR_DONE_KEY,
6768
)
6869
from airflow.providers.cncf.kubernetes.kube_config import KubeConfig
6970
from airflow.providers.cncf.kubernetes.kubernetes_helper_functions import annotations_to_key
70-
from airflow.providers.cncf.kubernetes.pod_generator import PodMutationHookException, PodReconciliationError
7171
from airflow.stats import Stats
7272
from airflow.utils.event_scheduler import EventScheduler
7373
from airflow.utils.log.logging_mixin import remove_escape_codes

providers/src/airflow/providers/cncf/kubernetes/pod_generator.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939

4040
from airflow.exceptions import (
4141
AirflowConfigException,
42-
AirflowException,
4342
)
4443
from airflow.providers.cncf.kubernetes.backcompat import get_logical_date_key
44+
from airflow.providers.cncf.kubernetes.exceptions import PodMutationHookException, PodReconciliationError
4545
from airflow.providers.cncf.kubernetes.kubernetes_helper_functions import (
4646
POD_NAME_MAX_LENGTH,
4747
add_unique_suffix,
@@ -58,14 +58,6 @@
5858
MAX_LABEL_LEN = 63
5959

6060

61-
class PodMutationHookException(AirflowException):
62-
"""Raised when exception happens during Pod Mutation Hook execution."""
63-
64-
65-
class PodReconciliationError(AirflowException):
66-
"""Raised when an error is encountered while trying to merge pod configs."""
67-
68-
6961
def make_safe_label_value(string: str) -> str:
7062
"""
7163
Normalize a provided label to be of valid length and characters.

tests/core/test_exceptions.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from __future__ import annotations
18+
19+
import sys
20+
21+
22+
class TestExceptions:
23+
def setup_method(self):
24+
self.old_modules = dict(sys.modules)
25+
26+
def teardown_method(self):
27+
# Remove any new modules imported during the test run. This lets us
28+
# import the same source files for more than one test.
29+
for mod in [m for m in sys.modules if m not in self.old_modules]:
30+
del sys.modules[mod]
31+
32+
def test_pod_mutation_hook_exceptions_compatibility(
33+
self,
34+
):
35+
from airflow.exceptions import (
36+
PodMutationHookException as CoreMutationHookException,
37+
)
38+
from airflow.providers.cncf.kubernetes.exceptions import (
39+
PodMutationHookException as ProviderMutationHookException,
40+
)
41+
from airflow.providers.cncf.kubernetes.pod_generator import (
42+
PodMutationHookException as ProviderGeneratorMutationHookException,
43+
)
44+
45+
assert ProviderMutationHookException == CoreMutationHookException
46+
assert ProviderMutationHookException == ProviderGeneratorMutationHookException
47+
48+
def test_pod_reconciliation_error_exceptions_compatibility(
49+
self,
50+
):
51+
from airflow.exceptions import (
52+
PodReconciliationError as CoreReconciliationError,
53+
)
54+
from airflow.providers.cncf.kubernetes.exceptions import (
55+
PodReconciliationError as ProviderReconciliationError,
56+
)
57+
from airflow.providers.cncf.kubernetes.pod_generator import (
58+
PodReconciliationError as ProviderGeneratorReconciliationError,
59+
)
60+
61+
assert ProviderReconciliationError == CoreReconciliationError
62+
assert ProviderReconciliationError == ProviderGeneratorReconciliationError

0 commit comments

Comments
 (0)