kaxil commented on code in PR #29017:
URL: https://github.com/apache/airflow/pull/29017#discussion_r1081831491
##########
airflow/providers/cncf/kubernetes/hooks/kubernetes.py:
##########
@@ -431,3 +446,150 @@ def _get_bool(val) -> bool | None:
elif val.strip().lower() == "false":
return False
return None
+
+
+class AsyncKubernetesHook(KubernetesHook):
+ """Hook to use Kubernetes SDK asynchronously."""
+
+ def __init__(self, config_dict: dict | None = None, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.config_dict = config_dict
+
+ self._extras: dict | None = None
+
+ async def _load_config(self):
+ """Returns Kubernetes API session for use with requests"""
+ in_cluster = self._coalesce_param(self.in_cluster, await
self._get_field("in_cluster"))
+ cluster_context = self._coalesce_param(self.cluster_context, await
self._get_field("cluster_context"))
+ kubeconfig = await self._get_field("kube_config")
+
+ num_selected_configuration = len([o for o in [in_cluster, kubeconfig,
self.config_dict] if o])
+
+ if num_selected_configuration > 1:
+ raise AirflowException(
+ "Invalid connection configuration. Options kube_config_path, "
+ "kube_config, in_cluster are mutually exclusive. "
+ "You can only use one option at a time."
+ )
+
+ if in_cluster:
+ self.log.debug(LOADING_KUBE_CONFIG_FILE_RESOURCE.format("within a
pod"))
+ self._is_in_cluster = True
+ async_config.load_incluster_config()
+ return async_client.ApiClient()
+
+ if self.config_dict:
+ self.log.debug(LOADING_KUBE_CONFIG_FILE_RESOURCE.format("config
dictionary"))
+ await load_kube_config_from_dict(self.config_dict)
+ return async_client.ApiClient()
+
+ if kubeconfig is not None:
+ with 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()
+ self._is_in_cluster = False
+ await async_config.load_kube_config(
+ config_file=temp_config.name,
+ client_configuration=self.client_configuration,
+ context=cluster_context,
+ )
+ return async_client.ApiClient()
+ self.log.debug(LOADING_KUBE_CONFIG_FILE_RESOURCE.format("default
configuration file"))
+ await async_config.load_kube_config(
+ client_configuration=self.client_configuration,
+ context=cluster_context,
+ )
+
+ async def get_conn_extras(self) -> dict:
+ if self._extras is None:
+ if self.conn_id:
+ connection = await
sync_to_async(self.get_connection)(self.conn_id)
+ self._extras = connection.extra_dejson
+ else:
+ self._extras = {}
+ return self._extras
+
+ async def _get_field(self, field_name):
+ if field_name.startswith("extra__"):
+ raise ValueError(
+ f"Got prefixed name {field_name}; please remove the
'extra__kubernetes__' prefix "
+ f"when using this method."
Review Comment:
```suggestion
"when using this method."
```
##########
airflow/providers/cncf/kubernetes/hooks/kubernetes.py:
##########
@@ -431,3 +446,150 @@ def _get_bool(val) -> bool | None:
elif val.strip().lower() == "false":
return False
return None
+
+
+class AsyncKubernetesHook(KubernetesHook):
+ """Hook to use Kubernetes SDK asynchronously."""
+
+ def __init__(self, config_dict: dict | None = None, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.config_dict = config_dict
+
+ self._extras: dict | None = None
+
+ async def _load_config(self):
+ """Returns Kubernetes API session for use with requests"""
+ in_cluster = self._coalesce_param(self.in_cluster, await
self._get_field("in_cluster"))
+ cluster_context = self._coalesce_param(self.cluster_context, await
self._get_field("cluster_context"))
+ kubeconfig = await self._get_field("kube_config")
+
+ num_selected_configuration = len([o for o in [in_cluster, kubeconfig,
self.config_dict] if o])
+
+ if num_selected_configuration > 1:
+ raise AirflowException(
+ "Invalid connection configuration. Options kube_config_path, "
+ "kube_config, in_cluster are mutually exclusive. "
+ "You can only use one option at a time."
+ )
+
+ if in_cluster:
+ self.log.debug(LOADING_KUBE_CONFIG_FILE_RESOURCE.format("within a
pod"))
+ self._is_in_cluster = True
+ async_config.load_incluster_config()
+ return async_client.ApiClient()
+
+ if self.config_dict:
+ self.log.debug(LOADING_KUBE_CONFIG_FILE_RESOURCE.format("config
dictionary"))
+ await load_kube_config_from_dict(self.config_dict)
+ return async_client.ApiClient()
+
+ if kubeconfig is not None:
+ with 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()
+ self._is_in_cluster = False
+ await async_config.load_kube_config(
+ config_file=temp_config.name,
+ client_configuration=self.client_configuration,
+ context=cluster_context,
+ )
+ return async_client.ApiClient()
+ self.log.debug(LOADING_KUBE_CONFIG_FILE_RESOURCE.format("default
configuration file"))
+ await async_config.load_kube_config(
+ client_configuration=self.client_configuration,
+ context=cluster_context,
+ )
+
+ async def get_conn_extras(self) -> dict:
+ if self._extras is None:
+ if self.conn_id:
+ connection = await
sync_to_async(self.get_connection)(self.conn_id)
+ self._extras = connection.extra_dejson
+ else:
+ self._extras = {}
+ return self._extras
+
+ async def _get_field(self, field_name):
Review Comment:
```suggestion
async def _get_field(self, field_name: str):
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]