Repository navigation
Reduce K8s test resource usage with single-node Kind clusters #73998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aaron-y-chen
wants to merge
2
commits into
apache:main
Choose a base branch
from
aaron-y-chen:single-node-kind-k8s-tests-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| import yaml | ||
|
|
||
| from airflow_breeze.utils.kubernetes_utils import FORWARDED_NODE_PORT, get_kubernetes_port_numbers | ||
| from airflow_breeze.utils.path_utils import AIRFLOW_ROOT_PATH | ||
|
|
||
| KUBERNETES_CI_PATH = AIRFLOW_ROOT_PATH / "scripts" / "ci" / "kubernetes" | ||
|
|
||
| SINGLE_NODE_CONFIG = """\ | ||
| kind: Cluster | ||
| apiVersion: kind.x-k8s.io/v1alpha4 | ||
| networking: | ||
| ipFamily: ipv4 | ||
| apiServerAddress: "127.0.0.1" | ||
| apiServerPort: 48366 | ||
| nodes: | ||
| - role: control-plane | ||
| extraPortMappings: | ||
| - containerPort: 30007 | ||
| hostPort: 18150 | ||
| listenAddress: "127.0.0.1" | ||
| protocol: TCP | ||
| """ | ||
|
|
||
| # Rendered config of a cluster created before the switch to single-node clusters — | ||
| # port extraction must keep working for clusters that already exist on disk. | ||
| LEGACY_TWO_NODE_CONFIG = """\ | ||
| kind: Cluster | ||
| apiVersion: kind.x-k8s.io/v1alpha4 | ||
| networking: | ||
| ipFamily: ipv4 | ||
| apiServerAddress: "127.0.0.1" | ||
| apiServerPort: 48366 | ||
| nodes: | ||
| - role: control-plane | ||
| - role: worker | ||
| extraPortMappings: | ||
| - containerPort: 30007 | ||
| hostPort: 18150 | ||
| listenAddress: "127.0.0.1" | ||
| protocol: TCP | ||
| """ | ||
|
|
||
| NO_FORWARDED_PORT_CONFIG = """\ | ||
| kind: Cluster | ||
| apiVersion: kind.x-k8s.io/v1alpha4 | ||
| networking: | ||
| ipFamily: ipv4 | ||
| apiServerAddress: "127.0.0.1" | ||
| apiServerPort: 48366 | ||
| nodes: | ||
| - role: control-plane | ||
| """ | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "config", [SINGLE_NODE_CONFIG, LEGACY_TWO_NODE_CONFIG], ids=["single-node", "legacy-two-node"] | ||
| ) | ||
| @patch("airflow_breeze.utils.kubernetes_utils._get_kind_cluster_config_content", autospec=True) | ||
| def test_get_kubernetes_port_numbers_extracts_ports_from_any_node(mock_get_content, config): | ||
| mock_get_content.return_value = yaml.safe_load(config) | ||
| assert get_kubernetes_port_numbers(python="3.10", kubernetes_version="v1.30.13") == (48366, 18150) | ||
|
|
||
|
|
||
| @patch("airflow_breeze.utils.kubernetes_utils._get_kind_cluster_config_content", autospec=True) | ||
| def test_get_kubernetes_port_numbers_raises_when_forwarded_port_mapping_is_missing(mock_get_content): | ||
| mock_get_content.return_value = yaml.safe_load(NO_FORWARDED_PORT_CONFIG) | ||
| with pytest.raises(ValueError, match="extraPortMappings"): | ||
| get_kubernetes_port_numbers(python="3.10", kubernetes_version="v1.30.13") | ||
|
|
||
|
|
||
| def test_forwarded_node_port_matches_kind_config_and_nodeport_service(): | ||
| # The template placeholders are not valid YAML scalars, so render them before parsing. | ||
| kind_conf = yaml.safe_load( | ||
| (KUBERNETES_CI_PATH / "kind-cluster-conf.yaml") | ||
| .read_text() | ||
| .replace("{{FORWARDED_PORT_NUMBER}}", "18150") | ||
| .replace("{{API_SERVER_PORT}}", "48366") | ||
| ) | ||
| container_ports = [ | ||
| port_mapping["containerPort"] | ||
| for node in kind_conf["nodes"] | ||
| for port_mapping in node.get("extraPortMappings", []) | ||
| ] | ||
| node_port_service = yaml.safe_load((KUBERNETES_CI_PATH / "nodeport.yaml").read_text()) | ||
|
|
||
| assert container_ports == [FORWARDED_NODE_PORT] | ||
| assert [port["nodePort"] for port in node_port_service["spec"]["ports"]] == [FORWARDED_NODE_PORT] |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.