You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "seanghaeli (via GitHub)" <gi...@apache.org> on 2026/10/20 20:16:13 UTC

[PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

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

   In multi-team mode, a team's KubernetesExecutor already reads its namespace, pod template and the other pod settings from the team's config, but it still connects with the global `[kubernetes_executor]` settings. So every team ends up on the same cluster. A team can set its own `client_factory`, but the factory is called with no arguments, so it can't tell which team it's building a client for.
   
   This has a second problem for subclasses that bring their own client, like the AwsEksExecutor in #74399. The only way for a subclass to install its factory is the `[kubernetes_executor] client_factory` setting. A plain KubernetesExecutor in the same scope (both global, or the same team) reads that same setting, so it ends up sending its pods to the subclass's cluster.
   
   This PR:
   
   - Reads `in_cluster`, `config_file`, `cluster_context`, `verify_ssl`, `ssl_ca_cert` and `api_client_retry_configuration` through the team's config, for the sync client, the async client and the pod watcher (which already gets the team). They don't fall back to the global section, same as all other team config. `enable_tcp_keepalive` and `tcp_keep_*` stay global, since they set socket options for the whole process.
   - Calls a `client_factory` / `async_client_factory` with `team_name` if it takes that argument. Factories with no arguments are still called with none, so the 10.24.0 contract still works.
   - Lets a subclass set `client_factory` and `async_client_factory` as class attributes (import paths, so the watcher process can resolve them). They take precedence over the config settings and are called with `team_name`, so a KubernetesExecutor running next to the subclass keeps its own client.
   
   One thing worth calling out: `verify_ssl = False` changes the process-wide default `Configuration`. Now that teams can set it differently, a team's client sets it on that client only. So one team turning off TLS verification doesn't turn it off for the other teams' clients in the same scheduler, and a team doesn't pick it up from a global executor that turned it off. The no-team path is unchanged.
   
   Behavior change for multi-team users: a team that relied on the global connection settings now needs to set them for the team. Multi-team is still experimental, and I added a note at the top of the changelog.
   
   closes: #74844
   
   ### Testing
   
   Unit tests in Breeze (`test_kube_client.py`, `test_client.py`, `executors/test_kubernetes_executor.py`): 287 passed, 1 skipped. I also reverted each of the 26 pieces of the change one at a time and checked that a test fails for each one (26/26), and with all the source reverted to main, the new tests fail (24 failed).
   
   E2E on two real EKS clusters (A and B) with a real scheduler, Dag processor and API server, and one Dag per team. Each task prints the OIDC issuer of the cluster its pod runs on (from its service account token).
   
   Two teams, both KubernetesExecutor, with team-scoped `config_file`/`cluster_context` pointing at cluster A and cluster B. The global section points at cluster A.
   
   Before (this change reverted):
   ```
   01:23:19 RUN ii_tc_team_a state=success t=44s
   01:23:26 RUN ii_tc_team_b state=success t=51s
   --- kubectl get pods -n tc-e2e (cluster a: eks-systest-lane)
   NAME                                             STATUS     NODE                                           RUN_ID
   tc-team-a-where-am-i-r851z2sy                    Completed  ip-192-168-69-92.us-west-2.compute.internal    ii_tc_team_a
   tc-team-b-where-am-i-xoyo4rf2                    Completed  ip-192-168-55-77.us-west-2.compute.internal    ii_tc_team_b
     pod/tc-team-a-where-am-i-r851z2sy: TC_E2E dag=tc_team_a pod=tc-team-a-where-am-i-r851z2sy namespace=tc-e2e issuer=.../id/FCCA5D040C7E38D31ACD37CD4CC74F77
     pod/tc-team-b-where-am-i-xoyo4rf2: TC_E2E dag=tc_team_b pod=tc-team-b-where-am-i-xoyo4rf2 namespace=tc-e2e issuer=.../id/FCCA5D040C7E38D31ACD37CD4CC74F77
   --- kubectl get pods -n tc-e2e (cluster b: eks-team-b-lane)
   No resources found in tc-e2e namespace.
   ```
   
   After:
   ```
   01:14:12 RUN ii_tc_team_a state=success t=45s
   01:14:18 RUN ii_tc_team_b state=success t=51s
   --- kubectl get pods -n tc-e2e (cluster a: eks-systest-lane)
   NAME                                             STATUS     NODE                                           RUN_ID
   tc-team-a-where-am-i-bjz65ezy                    Completed  ip-192-168-69-92.us-west-2.compute.internal    ii_tc_team_a
     pod/tc-team-a-where-am-i-bjz65ezy: TC_E2E dag=tc_team_a pod=tc-team-a-where-am-i-bjz65ezy namespace=tc-e2e issuer=.../id/FCCA5D040C7E38D31ACD37CD4CC74F77
   --- kubectl get pods -n tc-e2e (cluster b: eks-team-b-lane)
   NAME                                             STATUS     NODE                                           RUN_ID
   tc-team-b-where-am-i-zwc4tcfy                    Completed  ip-192-168-66-143.us-west-2.compute.internal   ii_tc_team_b
     pod/tc-team-b-where-am-i-zwc4tcfy: TC_E2E dag=tc_team_b pod=tc-team-b-where-am-i-zwc4tcfy namespace=tc-e2e issuer=.../id/CDA69AD28888CDCE3DE924E826AFBCDD
   ```
   
   The pod watcher builds its client from the team's settings too. The kubeconfig exec plugin logs which process asked for a token for which cluster: the scheduler and one watcher process per team, plus the async client team_b uses for pod creation:
   ```
   01:13:38 EXEC cluster=eks-systest-lane ppid=514 parent='airflow scheduler'
   01:13:40 EXEC cluster=eks-systest-lane ppid=596 parent='airflow scheduler'
   01:13:40 EXEC cluster=eks-team-b-lane ppid=514 parent='airflow scheduler'
   01:13:42 EXEC cluster=eks-team-b-lane ppid=656 parent='airflow scheduler'
   01:14:02 EXEC cluster=eks-team-b-lane ppid=514 parent='airflow scheduler'
   ```
   
   A team's `client_factory` that takes `team_name` is told its team (this one picks cluster B for team_a), and a no-argument factory (the 10.24.0 contract) still works. The scheduler and the watcher process (its child) each call the factory:
   ```
   01:20:42 RUN v3_tc_team_a state=success t=48s
   01:20:02 FACTORY build_client_for_team(team_name='team_a') called pid=1089 ppid=1079
   01:20:04 FACTORY build_client_for_team(team_name='team_a') called pid=1169 ppid=1089
     pod/tc-team-a-where-am-i-sm9o2230: TC_E2E dag=tc_team_a pod=tc-team-a-where-am-i-sm9o2230 namespace=tc-e2e issuer=.../id/CDA69AD28888CDCE3DE924E826AFBCDD
   
   01:19:29 RUN v2_tc_side_by_side state=success t=39s
   01:18:57 FACTORY build_client_no_args() called pid=790 ppid=780
   01:19:00 FACTORY build_client_no_args() called pid=834 ppid=790
     pod/tc-side-by-side-on-kubernetesexecutor-ywul9ns4: TC_E2E dag=tc_side_by_side pod=tc-side-by-side-on-kubernetesexecutor-ywul9ns4 namespace=tc-e2e issuer=.../id/CDA69AD28888CDCE3DE924E826AFBCDD
   ```
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Opus 5.5) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "seanghaeli (via GitHub)" <gi...@apache.org>.
seanghaeli commented on PR #75251:
URL: https://github.com/apache/airflow/pull/75251#issuecomment-6328772514

   @o-nikolas
   
   > There's another follow up coming for the AwsEksExecutor to make make use of this right?
   
   Correct, wanted to split up the `cncf` and `ampp` sides into separate PRs


-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "o-nikolas (via GitHub)" <gi...@apache.org>.
o-nikolas commented on code in PR #75251:
URL: https://github.com/apache/airflow/pull/75251#discussion_r4342596399


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/cli/kubernetes_command.py:
##########
@@ -148,7 +149,8 @@ def _get_pod_completion_time(pod):
 @providers_configuration_loaded
 def cleanup_pods(args):

Review Comment:
   From mine, that builds on this:
   >Building on this: the obvious fix, swapping get_kube_client for _get_executor_kube_client(team_name=team_name), only covers the config route ([<team>=kubernetes_executor] client_factory). It won't pick up the new client_factory class attribute, because cleanup-pods never loads an executor class. Once AwsEksExecutor moves to the class attribute (the follow-up this PR enables), cleanup-pods --team for an EKS team will still fall back to get_kube_client, with in_cluster=True by default.
   
   > There's also an ambiguity: a team can list more than one Kubernetes-family executor (the new docs present a plain KubernetesExecutor alongside a subclass as supported), and those can point at different clusters, so --team alone doesn't say which cluster to clean.
   
   > One option is to resolve the team's executor class and pass its factory through



-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "seanghaeli (via GitHub)" <gi...@apache.org>.
seanghaeli commented on PR #75251:
URL: https://github.com/apache/airflow/pull/75251#issuecomment-6300633472

   re:support per-team kubernetes clusters for Kubernetes and EKS executors


-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "o-nikolas (via GitHub)" <gi...@apache.org>.
o-nikolas merged PR #75251:
URL: https://github.com/apache/airflow/pull/75251


-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "o-nikolas (via GitHub)" <gi...@apache.org>.
o-nikolas commented on code in PR #75251:
URL: https://github.com/apache/airflow/pull/75251#discussion_r4361060051


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/cli/kubernetes_command.py:
##########
@@ -231,13 +252,50 @@ def cleanup_pods(args):
         list_kwargs["_continue"] = continue_token
 
 
-def _delete_pod(name, namespace):
+def _get_team_executor_cls(team_name: str, executor: str | None) -> type[KubernetesExecutor]:
+    """Return the class of the team's executor named ``executor``, or of its default one."""
+    # Only the team's own executors: a global one doesn't connect with the team's config.
+    names = [n for n in ExecutorLoader.get_executor_names(validate_teams=False) if n.team_name == team_name]
+    if not names:
+        raise SystemExit(f"Team {team_name!r} has no executors configured.")
+    try:
+        if hasattr(ExecutorLoader, "resolve_executor_name"):
+            name = ExecutorLoader.resolve_executor_name(executor, team_name, executor_names=names)
+        elif executor:  # Airflow < 3.4
+            name = ExecutorLoader.lookup_executor_name_by_str(
+                executor, team_name=team_name, validate_teams=False
+            )
+        else:
+            name = names[0]
+    except UnknownExecutorException as e:
+        raise SystemExit(str(e))
+    executor_cls, _ = ExecutorLoader.import_executor_cls(name)
+    if not issubclass(executor_cls, KubernetesExecutor):
+        raise SystemExit(
+            f"Executor {name.module_path} of team {team_name!r} isn't a KubernetesExecutor. "
+            "Pass --executor to pick one that is."
+        )
+    return executor_cls
+
+
+def _check_namespace_not_shared(team_name: str, namespace: str) -> None:
+    """Refuse a namespace another scope also uses, since their pods can't be told apart."""
+    scopes = {n.team_name for n in ExecutorLoader.get_executor_names(validate_teams=False)} - {team_name}
+    for other in sorted(scopes, key=lambda t: t or ""):
+        if conf.get("kubernetes_executor", "namespace", **_team_kwargs(other)) == namespace:

Review Comment:
   This counts every scope that has any executor, then compares its [kubernetes_executor] namespace. A scope with no Kubernetes executor never uses that setting, but it still has the built-in value default. So with CeleryExecutor;team_a=KubernetesExecutor and no namespace set, --team team_a refuses ("also used by the global executors"), though Celery makes no pods there. The "another cluster" hint is also wrong for that case.
   
   Could we only count scopes that run a KubernetesExecutor? Something like:
   
   ```python
   scopes = {
       n.team_name
       for n in ExecutorLoader.get_executor_names(validate_teams=False)
       if n.team_name != team_name
       and issubclass(ExecutorLoader.import_executor_cls(n)[0], KubernetesExecutor)
   }
   ```
   The shared-with-global and shared-with-team tests would then need a Kubernetes executor in the other scope, since the global setup and team_b only run LocalExecutor there.



-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "aaron-y-chen (via GitHub)" <gi...@apache.org>.
aaron-y-chen commented on code in PR #75251:
URL: https://github.com/apache/airflow/pull/75251#discussion_r4342689727


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/cli/kubernetes_command.py:
##########
@@ -148,7 +149,8 @@ def _get_pod_completion_time(pod):
 @providers_configuration_loaded
 def cleanup_pods(args):

Review Comment:
   agree with nikolas's comment



-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "seanghaeli (via GitHub)" <gi...@apache.org>.
seanghaeli commented on code in PR #75251:
URL: https://github.com/apache/airflow/pull/75251#discussion_r4356828787


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/cli/kubernetes_command.py:
##########
@@ -148,7 +149,8 @@ def _get_pod_completion_time(pod):
 @providers_configuration_loaded
 def cleanup_pods(args):

Review Comment:
   Funny enough, this concern is mostly subsumed by #75255, which merged earlier today, so I'm reusing that work here, and this should now be addressed. `cleanup-pods --team` now resolves the team's executor the same way the scheduler does and builds its client through that executor's client factory, so a team on a Kubernetes executor cleans its own cluster. If a team has more than one Kubernetes executor, `--executor` picks which one, and it refuses to run when another team or the global executors use the same namespace.



-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #75251:
URL: https://github.com/apache/airflow/pull/75251#discussion_r4337598962


##########
providers/cncf/kubernetes/tests/unit/cncf/kubernetes/test_kube_client.py:
##########
@@ -118,6 +162,24 @@ async def test_team_async_factory_takes_precedence_over_the_global_one(self, mon
 
         assert client == FACTORY_MARKER
 
+    @pytest.mark.skipif(not AIRFLOW_V_3_2_PLUS, reason="team config requires Airflow 3.2+")
+    @pytest.mark.asyncio
+    async def test_async_factory_is_told_the_team(self, monkeypatch):
+        monkeypatch.setenv(
+            "AIRFLOW__TEAM_A___KUBERNETES_EXECUTOR__ASYNC_CLIENT_FACTORY", f"{__name__}.build_team_client"
+        )
+
+        assert await _get_executor_async_kube_client(team_name="team_a") == (FACTORY_MARKER, "team_a")
+
+    @pytest.mark.asyncio
+    async def test_executor_async_factory_takes_precedence_over_the_config(self):
+        with conf_vars({("kubernetes_executor", "async_client_factory"): f"{__name__}.build_other_client"}):

Review Comment:
   Not a big deal but we usually use the decorator



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/cli/kubernetes_command.py:
##########
@@ -148,7 +149,8 @@ def _get_pod_completion_time(pod):
 @providers_configuration_loaded
 def cleanup_pods(args):
     """Clean up k8s pods in evicted/failed/succeeded/pending states."""
-    namespace = args.namespace
+    team_name = args.team
+    namespace = args.namespace or conf.get("kubernetes_executor", "namespace", **_team_kwargs(team_name))

Review Comment:
   Another one from my agent:
   
   > No Airflow 3.2 guard on cleanup-pods --team (kubernetes_command.py:153). generate_pod_yaml checks AIRFLOW_V_3_2_PLUS, but this command doesn't. On Airflow 2.11 to 3.1, conf.get(team_name=...) will either raise TypeError or quietly ignore the team.



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/cli/kubernetes_command.py:
##########
@@ -148,7 +149,8 @@ def _get_pod_completion_time(pod):
 @providers_configuration_loaded
 def cleanup_pods(args):

Review Comment:
   And another one :)
   
   > cleanup-pods --team deletes every team's pods in a shared namespace, because the label selector has no team filter.



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/cli/kubernetes_command.py:
##########
@@ -148,7 +149,8 @@ def _get_pod_completion_time(pod):
 @providers_configuration_loaded
 def cleanup_pods(args):

Review Comment:
   From my agent:
   
   
   > cleanup-pods --team can hit the wrong cluster (cli/kubernetes_command.py:183). It builds its client with get_kube_client and skips the team's client_factory. For a team on AwsEksExecutor with no in_cluster/config_file set, it falls back to in_cluster=True. It then deletes pods on the scheduler's own cluster, not the team's.



-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "seanghaeli (via GitHub)" <gi...@apache.org>.
seanghaeli commented on code in PR #75251:
URL: https://github.com/apache/airflow/pull/75251#discussion_r4356892624


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/cli/kubernetes_command.py:
##########
@@ -148,7 +149,8 @@ def _get_pod_completion_time(pod):
 @providers_configuration_loaded
 def cleanup_pods(args):
     """Clean up k8s pods in evicted/failed/succeeded/pending states."""
-    namespace = args.namespace
+    team_name = args.team
+    namespace = args.namespace or conf.get("kubernetes_executor", "namespace", **_team_kwargs(team_name))

Review Comment:
   Added the 3.2 check



-- 
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


Re: [PR] Let each team's KubernetesExecutor connect to its own cluster [airflow]

Posted by "seanghaeli (via GitHub)" <gi...@apache.org>.
seanghaeli commented on code in PR #75251:
URL: https://github.com/apache/airflow/pull/75251#discussion_r4356959639


##########
providers/cncf/kubernetes/tests/unit/cncf/kubernetes/test_kube_client.py:
##########
@@ -118,6 +162,24 @@ async def test_team_async_factory_takes_precedence_over_the_global_one(self, mon
 
         assert client == FACTORY_MARKER
 
+    @pytest.mark.skipif(not AIRFLOW_V_3_2_PLUS, reason="team config requires Airflow 3.2+")
+    @pytest.mark.asyncio
+    async def test_async_factory_is_told_the_team(self, monkeypatch):
+        monkeypatch.setenv(
+            "AIRFLOW__TEAM_A___KUBERNETES_EXECUTOR__ASYNC_CLIENT_FACTORY", f"{__name__}.build_team_client"
+        )
+
+        assert await _get_executor_async_kube_client(team_name="team_a") == (FACTORY_MARKER, "team_a")
+
+    @pytest.mark.asyncio
+    async def test_executor_async_factory_takes_precedence_over_the_config(self):
+        with conf_vars({("kubernetes_executor", "async_client_factory"): f"{__name__}.build_other_client"}):

Review Comment:
   This one's async, and the decorator form resets the config before the test body actually runs, so it would pass without testing anything. Async tests set it inside the body instead, e.g. https://github.com/apache/airflow/blob/01907e047f5ba28ab836b78e2b46af6805d4bdc1/airflow-core/tests/unit/jobs/test_triggerer_job.py#L1539-L1541



-- 
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