Skip to content

Commit 92c1759

Browse files
amoghrajeshSuraj-kumar00
authored andcommitted
fix(mypy): Pass parameters to Kubernetes methods conditionally (apache#64242)
* Pass parameters to k8s methods conditionally to fix mypy
1 parent f3c8482 commit 92c1759

3 files changed

Lines changed: 23 additions & 20 deletions

File tree

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,15 +1040,19 @@ async def read_logs(
10401040
# (e.g. binary data, truncated multi-byte sequences).
10411041
# kubernetes_asyncio's default decoding uses strict UTF-8 which
10421042
# crashes the task in those cases.
1043-
raw_resp: ClientResponse = await v1_api.read_namespaced_pod_log(
1044-
name=name,
1045-
namespace=namespace,
1046-
container=container_name,
1047-
follow=False,
1048-
timestamps=True,
1049-
since_seconds=since_seconds,
1050-
_preload_content=False,
1051-
) # type: ignore # _preload_content=False makes returning ClientResponse instead of str!
1043+
kwargs = {
1044+
"name": name,
1045+
"namespace": namespace,
1046+
"follow": False,
1047+
"timestamps": True,
1048+
"_preload_content": False,
1049+
}
1050+
if container_name is not None:
1051+
kwargs["container"] = container_name
1052+
if since_seconds is not None:
1053+
kwargs["since_seconds"] = since_seconds
1054+
1055+
raw_resp: ClientResponse = await v1_api.read_namespaced_pod_log(**kwargs) # type: ignore # _preload_content=False makes returning ClientResponse instead of str!
10521056
raw_bytes = await raw_resp.read()
10531057
logs = raw_bytes.decode("utf-8", errors="replace")
10541058
logs_list: list[str] = logs.splitlines()
@@ -1070,12 +1074,15 @@ async def get_pod_events(
10701074
async with self.get_conn() as connection:
10711075
try:
10721076
v1_api = async_client.CoreV1Api(connection)
1073-
events: CoreV1EventList = await v1_api.list_namespaced_event(
1074-
field_selector=f"involvedObject.name={name}",
1075-
namespace=namespace,
1076-
resource_version=resource_version,
1077-
resource_version_match="NotOlderThan" if resource_version else None,
1078-
)
1077+
kwargs = {
1078+
"field_selector": f"involvedObject.name={name}",
1079+
"namespace": namespace,
1080+
}
1081+
if resource_version is not None:
1082+
kwargs["resource_version"] = resource_version
1083+
kwargs["resource_version_match"] = "NotOlderThan"
1084+
1085+
events: CoreV1EventList = await v1_api.list_namespaced_event(**kwargs)
10791086
return events
10801087
except HTTPError as e:
10811088
if hasattr(e, "status") and e.status == 403:

providers/cncf/kubernetes/tests/unit/cncf/kubernetes/hooks/test_kubernetes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,6 @@ async def test_async_get_pod_events_without_resource_version(
11861186
mock_list_namespaced_event.assert_called_once_with(
11871187
field_selector=f"involvedObject.name={POD_NAME}",
11881188
namespace=NAMESPACE,
1189-
resource_version=None,
1190-
resource_version_match=None,
11911189
)
11921190
assert result == mock_events
11931191

providers/google/tests/unit/google/cloud/hooks/test_kubernetes_engine.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,11 @@ async def test_read_logs(self, read_namespaced_pod_log, get_conn_mock, async_hoo
538538

539539
get_conn_mock.assert_called_once_with()
540540
read_namespaced_pod_log.assert_called_with(
541-
_preload_content=False,
542541
name=POD_NAME,
543542
namespace=POD_NAMESPACE,
544543
follow=False,
545544
timestamps=True,
546-
container=None,
547-
since_seconds=None,
545+
_preload_content=False,
548546
)
549547
assert "Test string #1" in logs
550548
assert "Test string #2" in logs

0 commit comments

Comments
 (0)