Skip to content

Commit

Permalink
Replace blocking IO with async IO in AsyncKubernetesHook (apache#35162)
Browse files Browse the repository at this point in the history
* Replace blocking IO with async IO in AsyncKubernetesHook

* Use aiofiles.NamedTemporaryFile with default mode

* Minor update

* Minor update
  • Loading branch information
functicons authored Oct 26, 2023
1 parent 86640d1 commit d400226
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
7 changes: 4 additions & 3 deletions airflow/providers/cncf/kubernetes/hooks/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from functools import cached_property
from typing import TYPE_CHECKING, Any, Generator

import aiofiles
from asgiref.sync import sync_to_async
from kubernetes import client, config, watch
from kubernetes.config import ConfigException
Expand Down Expand Up @@ -502,13 +503,13 @@ async def _load_config(self):
return async_client.ApiClient()

if kubeconfig is not None:
with tempfile.NamedTemporaryFile() as temp_config:
async with aiofiles.tempfile.NamedTemporaryFile() as temp_config:
self.log.debug(
"Reading kubernetes configuration file from connection "
"object and writing temporary config file with its content",
)
temp_config.write(kubeconfig.encode())
temp_config.flush()
await temp_config.write(kubeconfig.encode())
await temp_config.flush()
self._is_in_cluster = False
await async_config.load_kube_config(
config_file=temp_config.name,
Expand Down
1 change: 1 addition & 0 deletions airflow/providers/cncf/kubernetes/provider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ versions:
- 1.0.0

dependencies:
- aiofiles>=23.2.0
- apache-airflow>=2.5.0
- asgiref>=3.5.2
- cryptography>=2.0.0
Expand Down
1 change: 1 addition & 0 deletions generated/provider_dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
},
"cncf.kubernetes": {
"deps": [
"aiofiles>=23.2.0",
"apache-airflow>=2.5.0",
"asgiref>=3.5.2",
"cryptography>=2.0.0",
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def write_version(filename: str = str(AIRFLOW_SOURCES_ROOT / "airflow" / "git_ve
# Make sure to upgrade the mypy version in update-common-sql-api-stubs in .pre-commit-config.yaml
# when you upgrade it here !!!!
"mypy==1.2.0",
"types-aiofiles",
"types-certifi",
"types-croniter",
"types-Deprecated",
Expand Down

0 comments on commit d400226

Please sign in to comment.