Skip to content

Commit 8f7d891

Browse files
potiukephraimbuddy
authored andcommitted
Add hatch_build.py to k8s test venv cache calculation (#39473)
With dynamic project depdencies, the requirements for airlfow are calculated dymamically from hatch_build.py, however cache invalidation key for k8s tests only included pyproject.toml so when colorlog has been bumped in @39453, main build continued to use cache from old colorlog. This PR adds also hatch_build.py to cache id calculation (cherry picked from commit f5c86ed)
1 parent 2ea4836 commit 8f7d891

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

.github/workflows/k8s-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ jobs:
9595
path: ".build/.k8s-env"
9696
key: "\
9797
k8s-env-${{ steps.breeze.outputs.host-python-version }}-\
98-
${{ hashFiles('scripts/ci/kubernetes/k8s_requirements.txt','pyproject.toml') }}"
98+
${{ hashFiles('scripts/ci/kubernetes/k8s_requirements.txt','hatch_build.py') }}"
9999
- name: Run complete K8S tests ${{ inputs.kubernetes-combos-list-as-string }}
100100
run: breeze k8s run-complete-tests --run-in-parallel --upgrade --no-copy-local-sources
101101
env:

dev/breeze/src/airflow_breeze/utils/kubernetes_utils.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717
from __future__ import annotations
1818

19+
import hashlib
1920
import itertools
2021
import os
2122
import random
@@ -53,8 +54,9 @@
5354
HELM_BIN_PATH = K8S_BIN_BASE_PATH / "helm"
5455
PYTHON_BIN_PATH = K8S_BIN_BASE_PATH / "python"
5556
SCRIPTS_CI_KUBERNETES_PATH = AIRFLOW_SOURCES_ROOT / "scripts" / "ci" / "kubernetes"
56-
K8S_REQUIREMENTS = SCRIPTS_CI_KUBERNETES_PATH / "k8s_requirements.txt"
57-
CACHED_K8S_REQUIREMENTS = K8S_ENV_PATH / "k8s_requirements.txt"
57+
K8S_REQUIREMENTS_PATH = SCRIPTS_CI_KUBERNETES_PATH / "k8s_requirements.txt"
58+
HATCH_BUILD_PY_PATH = AIRFLOW_SOURCES_ROOT / "hatch_build.py"
59+
CACHED_K8S_DEPS_HASH_PATH = K8S_ENV_PATH / "k8s_deps_hash.txt"
5860
CHART_PATH = AIRFLOW_SOURCES_ROOT / "chart"
5961

6062
# In case of parallel runs those ports will be quickly allocated by multiple threads and closed, which
@@ -272,15 +274,21 @@ def make_sure_kubernetes_tools_are_installed():
272274
)
273275

274276

277+
def _get_k8s_deps_hash():
278+
md5_hash = hashlib.md5()
279+
content = K8S_REQUIREMENTS_PATH.read_text() + HATCH_BUILD_PY_PATH.read_text()
280+
md5_hash.update(content.encode("utf-8"))
281+
k8s_deps_hash = md5_hash.hexdigest()
282+
return k8s_deps_hash
283+
284+
275285
def _requirements_changed() -> bool:
276-
if not CACHED_K8S_REQUIREMENTS.exists():
286+
if not CACHED_K8S_DEPS_HASH_PATH.exists():
277287
get_console().print(
278288
f"\n[warning]The K8S venv in {K8S_ENV_PATH} has never been created. Installing it.\n"
279289
)
280290
return True
281-
requirements_file_content = K8S_REQUIREMENTS.read_text()
282-
cached_requirements_content = CACHED_K8S_REQUIREMENTS.read_text()
283-
if cached_requirements_content != requirements_file_content:
291+
if CACHED_K8S_DEPS_HASH_PATH.read_text() != _get_k8s_deps_hash():
284292
get_console().print(
285293
f"\n[warning]Requirements changed for the K8S venv in {K8S_ENV_PATH}. "
286294
f"Reinstalling the venv.\n"
@@ -296,7 +304,7 @@ def _install_packages_in_k8s_virtualenv():
296304
"pip",
297305
"install",
298306
"-r",
299-
str(K8S_REQUIREMENTS.resolve()),
307+
str(K8S_REQUIREMENTS_PATH.resolve()),
300308
]
301309
env = os.environ.copy()
302310
capture_output = True
@@ -306,7 +314,9 @@ def _install_packages_in_k8s_virtualenv():
306314
install_command, check=False, capture_output=capture_output, text=True, env=env
307315
)
308316
if install_packages_result.returncode != 0:
309-
get_console().print(f"[error]Error when installing packages from : {K8S_REQUIREMENTS.resolve()}[/]\n")
317+
get_console().print(
318+
f"[error]Error when installing packages from : {K8S_REQUIREMENTS_PATH.resolve()}[/]\n"
319+
)
310320
if not get_verbose():
311321
get_console().print(install_packages_result.stdout)
312322
get_console().print(install_packages_result.stderr)
@@ -382,9 +392,9 @@ def create_virtualenv(force_venv_setup: bool) -> RunCommandResult:
382392
install_packages_result = _install_packages_in_k8s_virtualenv()
383393
if install_packages_result.returncode == 0:
384394
if get_dry_run():
385-
get_console().print(f"[info]Dry run - would be saving {K8S_REQUIREMENTS} to cache")
395+
get_console().print(f"[info]Dry run - would be saving {K8S_REQUIREMENTS_PATH} to cache")
386396
else:
387-
CACHED_K8S_REQUIREMENTS.write_text(K8S_REQUIREMENTS.read_text())
397+
CACHED_K8S_DEPS_HASH_PATH.write_text(_get_k8s_deps_hash())
388398
return install_packages_result
389399

390400

0 commit comments

Comments
 (0)