Skip to content

Commit 91edda9

Browse files
committed
Refactor codefresh images caching check
1 parent e362f15 commit 91edda9

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

tools/deployment-cli-tools/ch_cli_tools/codefresh.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .models import HarnessMainConfig, ApplicationTestConfig, ApplicationHarnessConfig
1212
from cloudharness_utils.constants import *
1313
from .helm import KEY_APPS, KEY_TASK_IMAGES, KEY_TEST_IMAGES, generate_tag_from_content
14-
from .utils import find_dockerfiles_paths, get_app_relative_to_base_path, guess_build_dependencies_from_dockerfile, \
14+
from .utils import check_docker_manifest_exists, find_dockerfiles_paths, get_app_relative_to_base_path, guess_build_dependencies_from_dockerfile, \
1515
get_image_name, get_template, dict_merge, app_name_from_path, clean_path
1616
from cloudharness_utils.testing.api import get_api_filename, get_schemathesis_command, get_urls_from_api_file
1717

@@ -33,7 +33,7 @@ def literal_presenter(dumper, data):
3333

3434
yaml.add_representer(str, literal_presenter)
3535

36-
def write_env_file(helm_values: HarnessMainConfig, filename):
36+
def write_env_file(helm_values: HarnessMainConfig, filename, registry_secret=None):
3737
env = {}
3838
logging.info("Create env file with image info %s", filename)
3939

@@ -45,14 +45,15 @@ def check_image_exists(name, image):
4545
chunks = image.split(":")[0].split("/")
4646
registry = chunks[0] if "." in chunks[0] else "docker.io"
4747
image_name = "/".join(chunks[1::] if "." in chunks[0] else chunks[0::])
48-
api_url = f"https://{registry}/v2/{image_name}/manifests/{tag}"
49-
resp = requests.get(api_url)
50-
if resp.status_code == 200:
48+
exists = check_docker_manifest_exists(registry, image_name, tag, registry_secret=registry_secret)
49+
if exists:
5150
# TODO the hash might be the same but not the parent's hash
5251
env[app_specific_tag_variable(name) + "_EXISTS"] = 1
5352
else:
5453
env[app_specific_tag_variable(name) + "_NEW"] = 1
5554

55+
56+
5657
for app in helm_values.apps.values():
5758
if app.harness and app.harness.deployment.image:
5859
env[app_specific_tag_variable(app.name)] = extract_tag(app.harness.deployment.image)

tools/deployment-cli-tools/ch_cli_tools/utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import socket
33
import glob
44
import subprocess
5+
import requests
56
import os
67
from functools import cache
78
from os.path import join, dirname, isdir, basename, exists, relpath, sep, dirname as dn
@@ -33,11 +34,14 @@ def image_name_from_dockerfile_path(dockerfile_path, base_name=None):
3334
def app_name_from_path(dockerfile_path):
3435
return "-".join(p for p in dockerfile_path.split("/") if p not in NEUTRAL_PATHS)
3536

37+
3638
def clean_path(path):
3739
return "/".join(p for p in path.split("/") if p not in NEUTRAL_PATHS)
38-
40+
41+
3942
def get_app_relative_to_base_path(base_path, dockerfile_path):
40-
return clean_path(relpath(dockerfile_path, base_path))
43+
return clean_path(relpath(dockerfile_path, base_path))
44+
4145

4246
def get_sub_paths(base_path):
4347
return tuple(path for path in glob.glob(base_path + "/*") if isdir(path))
@@ -351,6 +355,7 @@ def merge_app_directories(root_paths, destination) -> None:
351355
def to_python_module(name):
352356
return name.replace('-', '_')
353357

358+
354359
@cache
355360
def guess_build_dependencies_from_dockerfile(filename):
356361
dependencies = []
@@ -367,4 +372,7 @@ def guess_build_dependencies_from_dockerfile(filename):
367372
return dependencies
368373

369374

370-
375+
def check_docker_manifest_exists(registry, image_name, tag, registry_secret=None):
376+
api_url = f"https://{registry}/v2/{image_name}/manifests/{tag}"
377+
resp = requests.get(api_url)
378+
return resp.status_code == 200

tools/deployment-cli-tools/tests/test_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,9 @@ def test_guess_build_dependencies_from_dockerfile():
5959

6060
deps = guess_build_dependencies_from_dockerfile(os.path.join(HERE, "resources/applications/myapp/tasks/mytask"))
6161
assert len(deps) == 0
62+
63+
64+
def test_check_docker_manifest_exists():
65+
assert check_docker_manifest_exists("gcr.io/metacellllc", "cloudharness/cloudharness-base", "latest")
66+
assert not check_docker_manifest_exists("gcr.io/metacellllc", "cloudharness/cloudharness-base", "RANDOM_TAG")
67+

0 commit comments

Comments
 (0)