fix(k8s): run launcher run_k8s_config takes precedence over user depl…#33957
Open
sohamwaghe wants to merge 2 commits into
Open
fix(k8s): run launcher run_k8s_config takes precedence over user depl…#33957sohamwaghe wants to merge 2 commits into
sohamwaghe wants to merge 2 commits into
Conversation
Contributor
Comment on lines
+890
to
+947
| def test_launcher_run_k8s_config_takes_precedence_over_container_context( | ||
| kubeconfig_file, | ||
| ): | ||
| launcher = K8sRunLauncher( | ||
| service_account_name="webserver-admin", | ||
| instance_config_map="dagster-instance", | ||
| dagster_home="/opt/dagster/dagster_home", | ||
| job_image="fake_job_image", | ||
| load_incluster_config=False, | ||
| kubeconfig_file=kubeconfig_file, | ||
| run_k8s_config={"pod_spec_config": {"node_selector": {"pool": "autoscaling-pool"}}}, | ||
| ) | ||
|
|
||
| run = _run_with_container_context( | ||
| { | ||
| "k8s": { | ||
| "run_k8s_config": {"pod_spec_config": {"node_selector": {"pool": "default-pool"}}} | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| result = K8sContainerContext.create_for_run(run, launcher, include_run_tags=False) | ||
|
|
||
| node_selector = result.run_k8s_config.pod_spec_config.get("node_selector") | ||
| assert node_selector == {"pool": "autoscaling-pool"} | ||
|
|
||
|
|
||
| def test_launcher_run_k8s_config_precedence_preserves_non_conflicting_fields( | ||
| kubeconfig_file, | ||
| ): | ||
| launcher = K8sRunLauncher( | ||
| service_account_name="webserver-admin", | ||
| instance_config_map="dagster-instance", | ||
| dagster_home="/opt/dagster/dagster_home", | ||
| job_image="fake_job_image", | ||
| load_incluster_config=False, | ||
| kubeconfig_file=kubeconfig_file, | ||
| run_k8s_config={"pod_spec_config": {"node_selector": {"pool": "autoscaling-pool"}}}, | ||
| ) | ||
|
|
||
| run = _run_with_container_context( | ||
| { | ||
| "k8s": { | ||
| "run_k8s_config": { | ||
| "pod_spec_config": { | ||
| "node_selector": {"pool": "default-pool"}, | ||
| "dns_policy": "ClusterFirst", | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| result = K8sContainerContext.create_for_run(run, launcher, include_run_tags=False) | ||
|
|
||
| pod_spec = result.run_k8s_config.pod_spec_config | ||
| assert pod_spec.get("node_selector") == {"pool": "autoscaling-pool"} | ||
| assert pod_spec.get("dns_policy") == "ClusterFirst" |
Contributor
There was a problem hiding this comment.
Missing coverage for
include_run_tags=True path
Both new tests call create_for_run(..., include_run_tags=False). When include_run_tags=True, run tags are also merged into user_defined_container_context before the final merge with context (the launcher). A tag-defined pod_spec_config.node_selector is on the same side as the container-context-defined one, so the fix applies equally — but a test that exercises this path would make the invariant explicit and guard against a future regression where the tag-merge step is accidentally reordered after the launcher merge.
Author
|
Good catch — adding a test for the include_run_tags=True path in the latest commit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…oyment config
Summary & Motivation
When includeConfigInLaunchedRuns.enabled: true is set in the Helm chart, run pods ignore the nodeSelector configured in runLauncher.config.k8sRunLauncher.runK8sConfig.podSpecConfig and instead inherit the nodeSelector from the user deployment pod. This means operators who want run pods on a different node pool (e.g. autoscaling GPU nodes) than their user deployment pods (e.g. cheap always-on nodes) have no way to enforce that.
Root cause
K8sContainerContext.create_for_run() in container_context.py builds two contexts:
context — from the run launcher config
user_defined_container_context — from the run's code origin, which is populated with user deployment config when includeConfigInLaunchedRuns is enabled
The final line was:
K8sContainerContext.merge() is defined to prefer the passed-in argument (other). So user_defined_container_context was always winning for any conflicting scalar fields like pod_spec_config.node_selector, regardless of what the run launcher explicitly configured.
Fix
Flip the merge order so the launcher config is applied last and wins on conflicts:
Non-conflicting fields from both sides are still preserved — the deep merge behavior is unchanged. Only fields that exist in both configs are now resolved in favour of the launcher, which is the correct precedence for an operator-level setting.
Files changed:
python_modules/libraries/dagster-k8s/dagster_k8s/container_context.py — one line changed in create_for_run()
python_modules/libraries/dagster-k8s/dagster_k8s_tests/unit_tests/test_container_context.py — two regression tests added
Test Plan
All 24 existing tests continue to pass
test_launcher_run_k8s_config_takes_precedence_over_container_context — verifies launcher node_selector wins over a conflicting user deployment node_selector
test_launcher_run_k8s_config_precedence_preserves_non_conflicting_fields — verifies that fields only present in the user deployment config (e.g. dns_policy) still come through after the merge
Fixes #33900
p.s open to reviews and any suggestiosn and help
Changelog