Skip to content

Commit

Permalink
test: [skip e2e] optimize get_image_tag_by_short_name function (milvu…
Browse files Browse the repository at this point in the history
…s-io#33780)

Refactor the function to improve performance and readability. Instead of
making API requests to Docker Hub, the function now retrieves tags from
the Harbor registry. It also filters the tags based on the provided
architecture and selects the latest tag that matches the prefix. This
change enhances the efficiency of retrieving image tags by short name.

Signed-off-by: zhuwenxing <wenxing.zhu@zilliz.com>
  • Loading branch information
zhuwenxing committed Jun 12, 2024
1 parent 8cb3505 commit 1697706
Showing 1 changed file with 22 additions and 32 deletions.
54 changes: 22 additions & 32 deletions tests/scripts/get_image_tag_by_short_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,34 @@
from tenacity import retry, stop_after_attempt

@retry(stop=stop_after_attempt(7))
def get_image_tag_by_short_name(repository, tag, arch):
def get_image_tag_by_short_name(tag, arch):

# Send API request to get all tags start with prefix
# ${branch}-latest means the tag is a dev build
# master-latest -> master-$date-$commit
# 2.3.0-latest -> 2.3.0-$date-$commit
# latest means the tag is a release build
# latest -> v$version
splits = tag.split("-")
prefix = f"{splits[0]}-" if len(splits) > 1 else "v"
url = f"https://hub.docker.com/v2/repositories/{repository}/tags?name={prefix}&ordering=last_updated"
response = requests.get(url)
data = response.json()

# Get the latest tag with the same arch and prefix
sorted_images = sorted(data["results"], key=lambda x: x["last_updated"], reverse=True)
# print(sorted_images)
candidate_tag = None
for tag_info in sorted_images:
# print(tag_info)
if tag == "2.2.0-latest": # special case for 2.2.0-latest, for 2.2.0 branch, there is no arm amd and gpu as suffix
candidate_tag = tag_info["name"]
else:
if arch in tag_info["name"]:
candidate_tag = tag_info["name"]
else:
continue
if candidate_tag == tag:
continue
else:
return candidate_tag
prefix = tag.split("-")[0]
url = f"https://harbor.milvus.io/api/v2.0/projects/milvus/repositories/milvus/artifacts?with_tag=true&q=tags%253D~{prefix}-&page_size=100&page=1"

payload = {}
response = requests.request("GET", url, data=payload)
rsp = response.json()
tag_list = []
for r in rsp:
tags = r["tags"]
for tag in tags:
tag_list.append(tag["name"])
tag_candidates = []
for t in tag_list:
r = t.split("-")
if len(r) == 4 and arch in t:
tag_candidates.append(t)
tag_candidates.sort()
if len(tag_candidates) == 0:
return tag
else:
return tag_candidates[-1]

if __name__ == "__main__":
argparse = argparse.ArgumentParser()
argparse.add_argument("--repository", type=str, default="milvusdb/milvus")
argparse.add_argument("--tag", type=str, default="master-latest")
argparse.add_argument("--arch", type=str, default="amd64")
args = argparse.parse_args()
res = get_image_tag_by_short_name(args.repository, args.tag, args.arch)
res = get_image_tag_by_short_name(args.tag, args.arch)
print(res)

0 comments on commit 1697706

Please sign in to comment.