16
16
# under the License.
17
17
from __future__ import annotations
18
18
19
+ import hashlib
19
20
import itertools
20
21
import os
21
22
import random
53
54
HELM_BIN_PATH = K8S_BIN_BASE_PATH / "helm"
54
55
PYTHON_BIN_PATH = K8S_BIN_BASE_PATH / "python"
55
56
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"
58
60
CHART_PATH = AIRFLOW_SOURCES_ROOT / "chart"
59
61
60
62
# 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():
272
274
)
273
275
274
276
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
+
275
285
def _requirements_changed () -> bool :
276
- if not CACHED_K8S_REQUIREMENTS .exists ():
286
+ if not CACHED_K8S_DEPS_HASH_PATH .exists ():
277
287
get_console ().print (
278
288
f"\n [warning]The K8S venv in { K8S_ENV_PATH } has never been created. Installing it.\n "
279
289
)
280
290
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 ():
284
292
get_console ().print (
285
293
f"\n [warning]Requirements changed for the K8S venv in { K8S_ENV_PATH } . "
286
294
f"Reinstalling the venv.\n "
@@ -296,7 +304,7 @@ def _install_packages_in_k8s_virtualenv():
296
304
"pip" ,
297
305
"install" ,
298
306
"-r" ,
299
- str (K8S_REQUIREMENTS .resolve ()),
307
+ str (K8S_REQUIREMENTS_PATH .resolve ()),
300
308
]
301
309
env = os .environ .copy ()
302
310
capture_output = True
@@ -306,7 +314,9 @@ def _install_packages_in_k8s_virtualenv():
306
314
install_command , check = False , capture_output = capture_output , text = True , env = env
307
315
)
308
316
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
+ )
310
320
if not get_verbose ():
311
321
get_console ().print (install_packages_result .stdout )
312
322
get_console ().print (install_packages_result .stderr )
@@ -382,9 +392,9 @@ def create_virtualenv(force_venv_setup: bool) -> RunCommandResult:
382
392
install_packages_result = _install_packages_in_k8s_virtualenv ()
383
393
if install_packages_result .returncode == 0 :
384
394
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" )
386
396
else :
387
- CACHED_K8S_REQUIREMENTS .write_text (K8S_REQUIREMENTS . read_text ())
397
+ CACHED_K8S_DEPS_HASH_PATH .write_text (_get_k8s_deps_hash ())
388
398
return install_packages_result
389
399
390
400
0 commit comments