You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/04/20 18:31:02 UTC

[GitHub] [airflow] dstandish opened a new pull request, #23126: KubernetesHook should try incluster first when not otherwise configured

dstandish opened a new pull request, #23126:
URL: https://github.com/apache/airflow/pull/23126

   Currently when K8s hook receives no configuration (e.g. incluster vs config file content vs config file path) the default client generation process will try to load the kube config in the default location.  This is inconsistent with airflow core's behavior in kubernetes executor and kubernetes pod operator (in_cluster=True is the default with those).
   
   To make k8s hook's behavior consistent, we can first try incluster, then if that fails, try default kubeconfig.  This should be safe to do.  The kubernetes client will check for 2 environment variables that an in-cluster environment should have and if it doesn't find them, it will raise ConfigException (see here: https://github.com/kubernetes-client/python/blob/1271465acdb80bf174c50564a384fd6898635ea6/kubernetes/base/config/incluster_config.py#L60-L62).  If ConfigException is raised, K8s hook will fall back to looking for the default config.
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] jedcunningham commented on a diff in pull request #23126: KubernetesHook should try incluster first when not otherwise configured

Posted by GitBox <gi...@apache.org>.
jedcunningham commented on code in PR #23126:
URL: https://github.com/apache/airflow/pull/23126#discussion_r854444389


##########
tests/providers/cncf/kubernetes/hooks/test_kubernetes.py:
##########
@@ -89,15 +92,46 @@ def test_in_cluster_connection(
         Hook param should beat extra.
         """
         kubernetes_hook = KubernetesHook(conn_id=conn_id, in_cluster=in_cluster_param)
+        mock_get_default_client.return_value = kubernetes.client.api_client.ApiClient()
         api_conn = kubernetes_hook.get_conn()
         if in_cluster_called:
             mock_in_cluster_loader.assert_called_once()
             mock_merger.assert_not_called()
             mock_loader.assert_not_called()
         else:
-            mock_in_cluster_loader.assert_not_called()
+            mock_get_default_client.assert_called()
+            # mock_in_cluster_loader.assert_not_called()
+            # mock_merger.assert_called_once_with(KUBE_CONFIG_PATH)
+            # mock_loader.assert_called_once()

Review Comment:
   ```suggestion
   ```
   
   Leftovers?



##########
tests/providers/cncf/kubernetes/hooks/test_kubernetes.py:
##########
@@ -89,15 +92,46 @@ def test_in_cluster_connection(
         Hook param should beat extra.
         """
         kubernetes_hook = KubernetesHook(conn_id=conn_id, in_cluster=in_cluster_param)
+        mock_get_default_client.return_value = kubernetes.client.api_client.ApiClient()
         api_conn = kubernetes_hook.get_conn()
         if in_cluster_called:
             mock_in_cluster_loader.assert_called_once()
             mock_merger.assert_not_called()
             mock_loader.assert_not_called()
         else:
-            mock_in_cluster_loader.assert_not_called()
+            mock_get_default_client.assert_called()
+            # mock_in_cluster_loader.assert_not_called()
+            # mock_merger.assert_called_once_with(KUBE_CONFIG_PATH)
+            # mock_loader.assert_called_once()
+        assert isinstance(api_conn, kubernetes.client.api_client.ApiClient)
+
+    @pytest.mark.parametrize('in_cluster_fails', [True, False])
+    @patch("kubernetes.config.kube_config.KubeConfigLoader")
+    @patch("kubernetes.config.kube_config.KubeConfigMerger")
+    @patch("kubernetes.config.incluster_config.InClusterConfigLoader")
+    def test_get_default_client(
+        self,
+        mock_incluster,
+        mock_merger,
+        mock_loader,
+        in_cluster_fails,
+    ):
+        """
+        Verifies whether in_cluster is called depending on combination of hook param and connection extra.
+        Hook param should beat extra.

Review Comment:
   This needs some adjusting I'd think.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] dstandish commented on a diff in pull request #23126: KubernetesHook should try incluster first when not otherwise configured

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #23126:
URL: https://github.com/apache/airflow/pull/23126#discussion_r854498399


##########
tests/providers/cncf/kubernetes/hooks/test_kubernetes.py:
##########
@@ -89,15 +92,46 @@ def test_in_cluster_connection(
         Hook param should beat extra.
         """
         kubernetes_hook = KubernetesHook(conn_id=conn_id, in_cluster=in_cluster_param)
+        mock_get_default_client.return_value = kubernetes.client.api_client.ApiClient()
         api_conn = kubernetes_hook.get_conn()
         if in_cluster_called:
             mock_in_cluster_loader.assert_called_once()
             mock_merger.assert_not_called()
             mock_loader.assert_not_called()
         else:
-            mock_in_cluster_loader.assert_not_called()
+            mock_get_default_client.assert_called()
+            # mock_in_cluster_loader.assert_not_called()
+            # mock_merger.assert_called_once_with(KUBE_CONFIG_PATH)
+            # mock_loader.assert_called_once()
+        assert isinstance(api_conn, kubernetes.client.api_client.ApiClient)
+
+    @pytest.mark.parametrize('in_cluster_fails', [True, False])
+    @patch("kubernetes.config.kube_config.KubeConfigLoader")
+    @patch("kubernetes.config.kube_config.KubeConfigMerger")
+    @patch("kubernetes.config.incluster_config.InClusterConfigLoader")
+    def test_get_default_client(
+        self,
+        mock_incluster,
+        mock_merger,
+        mock_loader,
+        in_cluster_fails,
+    ):
+        """
+        Verifies whether in_cluster is called depending on combination of hook param and connection extra.
+        Hook param should beat extra.

Review Comment:
   thanks, fixed



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] github-actions[bot] commented on pull request #23126: KubernetesHook should try incluster first when not otherwise configured

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #23126:
URL: https://github.com/apache/airflow/pull/23126#issuecomment-1104440344

   The PR is likely OK to be merged with just subset of tests for default Python and Database versions without running the full matrix of tests, because it does not modify the core of Airflow. If the committers decide that the full tests matrix is needed, they will add the label 'full tests needed'. Then you should rebase to the latest main or amend the last commit of the PR, and push it with --force-with-lease.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] dstandish merged pull request #23126: KubernetesHook should try incluster first when not otherwise configured

Posted by GitBox <gi...@apache.org>.
dstandish merged PR #23126:
URL: https://github.com/apache/airflow/pull/23126


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org