From 9ec16baa6636bed9e65948d8a00fc2390437eba1 Mon Sep 17 00:00:00 2001 From: Alfred Davidson Date: Sat, 30 Nov 2024 19:55:21 +0000 Subject: [PATCH 1/2] ensure cluster context is passed through --- .../cncf/kubernetes/hooks/kubernetes.py | 2 +- .../cncf/kubernetes/hooks/test_kubernetes.py | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/providers/src/airflow/providers/cncf/kubernetes/hooks/kubernetes.py b/providers/src/airflow/providers/cncf/kubernetes/hooks/kubernetes.py index 6a377be3eb27c..1e9f881dd1d70 100644 --- a/providers/src/airflow/providers/cncf/kubernetes/hooks/kubernetes.py +++ b/providers/src/airflow/providers/cncf/kubernetes/hooks/kubernetes.py @@ -754,7 +754,7 @@ async def _load_config(self): if self.config_dict: self.log.debug(LOADING_KUBE_CONFIG_FILE_RESOURCE.format("config dictionary")) self._is_in_cluster = False - await async_config.load_kube_config_from_dict(self.config_dict) + await async_config.load_kube_config_from_dict(self.config_dict, context=cluster_context) return async_client.ApiClient() if kubeconfig is not None: diff --git a/providers/tests/cncf/kubernetes/hooks/test_kubernetes.py b/providers/tests/cncf/kubernetes/hooks/test_kubernetes.py index f55e759acd9ef..7c34890de67cd 100644 --- a/providers/tests/cncf/kubernetes/hooks/test_kubernetes.py +++ b/providers/tests/cncf/kubernetes/hooks/test_kubernetes.py @@ -875,7 +875,35 @@ async def test_load_config_with_config_dict( await hook._load_config() assert not incluster_config.called assert hook._is_in_cluster is False - kube_config_loader.assert_called_once() + kube_config_loader.assert_called_once_with( + config_dict={"a": "b"}, + config_base_path=None, + active_context=None, + temp_file_path=None + ) + + @pytest.mark.asyncio + @mock.patch(INCLUSTER_CONFIG_LOADER) + @mock.patch(KUBE_CONFIG_MERGER) + async def test_load_config_with_config_dict_and_cluster_context( + self, kube_config_merger, incluster_config, kube_config_loader + ): + cluster_context = "some_kubernetes_cluster" + hook = AsyncKubernetesHook( + conn_id=None, + in_cluster=False, + config_dict={"a": "b"}, + cluster_context=cluster_context, + ) + await hook._load_config() + assert not incluster_config.called + assert hook._is_in_cluster is False + kube_config_loader.assert_called_once_with( + config_dict={"a": "b"}, + config_base_path=None, + active_context=cluster_context, + temp_file_path=None + ) @pytest.mark.asyncio @mock.patch(INCLUSTER_CONFIG_LOADER) From 150f2a4882985c4389cae70fdfe116b26712d39f Mon Sep 17 00:00:00 2001 From: Alfred Davidson Date: Sat, 30 Nov 2024 20:00:31 +0000 Subject: [PATCH 2/2] reformat --- .../tests/cncf/kubernetes/hooks/test_kubernetes.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/providers/tests/cncf/kubernetes/hooks/test_kubernetes.py b/providers/tests/cncf/kubernetes/hooks/test_kubernetes.py index 7c34890de67cd..f88cba72bef23 100644 --- a/providers/tests/cncf/kubernetes/hooks/test_kubernetes.py +++ b/providers/tests/cncf/kubernetes/hooks/test_kubernetes.py @@ -876,10 +876,7 @@ async def test_load_config_with_config_dict( assert not incluster_config.called assert hook._is_in_cluster is False kube_config_loader.assert_called_once_with( - config_dict={"a": "b"}, - config_base_path=None, - active_context=None, - temp_file_path=None + config_dict={"a": "b"}, config_base_path=None, active_context=None, temp_file_path=None ) @pytest.mark.asyncio @@ -899,10 +896,7 @@ async def test_load_config_with_config_dict_and_cluster_context( assert not incluster_config.called assert hook._is_in_cluster is False kube_config_loader.assert_called_once_with( - config_dict={"a": "b"}, - config_base_path=None, - active_context=cluster_context, - temp_file_path=None + config_dict={"a": "b"}, config_base_path=None, active_context=cluster_context, temp_file_path=None ) @pytest.mark.asyncio