Skip to content

Commit

Permalink
Merge pull request #8955 from madhavajay/madhava/fix_base_tag
Browse files Browse the repository at this point in the history
Fixed issue where tags in local repo are not returned in create order
  • Loading branch information
madhavajay authored Jun 24, 2024
2 parents 4501ca2 + 1e1882e commit b8f105c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
6 changes: 4 additions & 2 deletions notebooks/api/0.8/11-container-images-k8s.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,14 @@
"metadata": {},
"outputs": [],
"source": [
"# syft absolute\n",
"from syft.util.util import get_latest_tag\n",
"\n",
"registry = os.getenv(\"SYFT_BASE_IMAGE_REGISTRY\", \"docker.io\")\n",
"repo = \"openmined/grid-backend\"\n",
"\n",
"if \"k3d\" in registry:\n",
" res = requests.get(url=f\"http://{registry}/v2/{repo}/tags/list\")\n",
" tag = res.json()[\"tags\"][0]\n",
" tag = get_latest_tag(registry, repo)\n",
"else:\n",
" tag = sy.__version__"
]
Expand Down
32 changes: 32 additions & 0 deletions packages/syft/src/syft/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from copy import deepcopy
from datetime import datetime
import functools
import hashlib
from itertools import repeat
import json
import multiprocessing
import multiprocessing as mp
from multiprocessing import set_start_method
Expand Down Expand Up @@ -959,3 +961,33 @@ def sanitize_html(html: str) -> str:
clean_content_tags=policy["remove"],
attributes=attributes,
)


def parse_iso8601_date(date_string: str) -> datetime:
# Handle variable length of microseconds by trimming to 6 digits
if "." in date_string:
base_date, microseconds = date_string.split(".")
microseconds = microseconds.rstrip("Z") # Remove trailing 'Z'
microseconds = microseconds[:6] # Trim to 6 digits
date_string = f"{base_date}.{microseconds}Z"
return datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S.%fZ")


def get_latest_tag(registry: str, repo: str) -> str | None:
repo_url = f"http://{registry}/v2/{repo}"
res = requests.get(url=f"{repo_url}/tags/list", timeout=5)
tags = res.json().get("tags", [])

tag_times = []
for tag in tags:
manifest_response = requests.get(f"{repo_url}/manifests/{tag}", timeout=5)
manifest = manifest_response.json()
created_time = json.loads(manifest["history"][0]["v1Compatibility"])["created"]
created_datetime = parse_iso8601_date(created_time)
tag_times.append((tag, created_datetime))

# sort tags by datetime
tag_times.sort(key=lambda x: x[1], reverse=True)
if len(tag_times) > 0:
return tag_times[0][0]
return None
5 changes: 2 additions & 3 deletions tests/integration/container_workload/pool_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# third party
import numpy as np
import pytest
import requests

# syft absolute
import syft as sy
Expand All @@ -19,13 +18,13 @@
from syft.service.worker.worker_pool import SyftWorker
from syft.service.worker.worker_pool import WorkerPool
from syft.types.uid import UID
from syft.util.util import get_latest_tag

registry = os.getenv("SYFT_BASE_IMAGE_REGISTRY", "docker.io")
repo = "openmined/grid-backend"

if "k3d" in registry:
res = requests.get(url=f"http://{registry}/v2/{repo}/tags/list")
tag = res.json()["tags"][0]
tag = get_latest_tag(registry, repo)
else:
tag = sy.__version__

Expand Down

0 comments on commit b8f105c

Please sign in to comment.