Skip to content

Commit 2a97acc

Browse files
committed
Improve image version detection
1 parent bb10958 commit 2a97acc

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

app/main.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def strip_image_tag(image_name):
204204

205205
return f"{prefix}/{last}" if prefix else last
206206

207-
def get_image_version(container, image_name):
207+
def get_image_version(container, image_name, docker_client=None):
208208
"""Best-effort version from image labels, falling back to version-like tags."""
209209
labels = {}
210210
try:
@@ -239,6 +239,33 @@ def get_image_version(container, image_name):
239239
except Exception:
240240
pass
241241

242+
if docker_client:
243+
try:
244+
image_id = container.image.id if container.image else None
245+
image_obj = docker_client.images.get(image_id) if image_id else None
246+
image_labels = image_obj.attrs.get('Config', {}).get('Labels', {}) if image_obj else {}
247+
for key in VERSION_LABEL_KEYS:
248+
value = (image_labels or {}).get(key)
249+
if value:
250+
return value.strip(), 'label'
251+
ref_name = (image_labels or {}).get('org.opencontainers.image.ref.name', '')
252+
if ref_name:
253+
match = VERSION_IN_TEXT_RE.search(ref_name)
254+
if match:
255+
return match.group(0), 'label'
256+
tags = image_obj.tags if image_obj else []
257+
for repo_tag in tags:
258+
if ':' not in repo_tag:
259+
continue
260+
tag_value = repo_tag.rsplit(':', 1)[1].strip()
261+
if tag_value.lower() in NON_VERSION_TAGS:
262+
continue
263+
match = VERSION_IN_TEXT_RE.search(tag_value)
264+
if match:
265+
return match.group(0), 'tag'
266+
except Exception:
267+
pass
268+
242269
tag = None
243270
if image_name and ':' in image_name:
244271
tag = image_name.rsplit(':', 1)[1].strip()
@@ -1361,7 +1388,7 @@ def index():
13611388
pass
13621389

13631390
image_display = strip_image_tag(image_name)
1364-
image_version, version_source = get_image_version(container, image_name)
1391+
image_version, version_source = get_image_version(container, image_name, docker_client)
13651392
host_color = host_color_map.get(host_id, HOST_DEFAULT_COLOR)
13661393
host_text_color = host_text_color_map.get(host_id, get_contrast_text_color(host_color))
13671394

@@ -1499,7 +1526,7 @@ def get_containers():
14991526
pass
15001527

15011528
image_display = strip_image_tag(image_name)
1502-
image_version, version_source = get_image_version(container, image_name)
1529+
image_version, version_source = get_image_version(container, image_name, docker_client)
15031530
host_color = host_color_map.get(host_id, HOST_DEFAULT_COLOR)
15041531
host_text_color = host_text_color_map.get(host_id, get_contrast_text_color(host_color))
15051532

0 commit comments

Comments
 (0)