Skip to content

Commit 7c7aeda

Browse files
authored
Merge pull request #27 from release-engineering/skopeo-list-tags
retrieve the tag list using "skopeo list-tags"
2 parents 7440e3a + dd69124 commit 7c7aeda

2 files changed

Lines changed: 123 additions & 111 deletions

File tree

repotracker/container.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,38 +75,22 @@ def inspect_image_repo(repo, token=None):
7575
"""
7676
results = {}
7777
# Use skopeo
78-
tags = []
79-
# Don't assume the repo has a :latest tag.
80-
# Record info for whatever skopeo wants to tell us when we don't specify a tag.
81-
default = inspect_tag(repo)
82-
tags = default["RepoTags"]
83-
for tag in tags:
78+
for tag in list_tags(repo):
8479
try:
85-
results[tag] = inspect_tag(repo, tag=tag)
80+
results[tag] = inspect_tag(repo, tag)
8681
except:
8782
log.error("Could not query %s:%s", repo, tag, exc_info=True)
8883
return results
8984

9085

91-
def inspect_tag(repo, tag=None):
86+
def inspect_tag(repo, tag):
9287
"""
93-
Inspect the contents of the tag within the given repo. If no tag is specified,
94-
skopeo should inspect the default tag.
88+
Inspect the contents of the tag within the given repo.
9589
Returns a dict describing the image referenced by the given tag.
9690
If the repo is not accessible, or the tag does not exist, raise an
9791
exception.
9892
"""
99-
cmd = ["/usr/bin/skopeo", "inspect"]
100-
if tag:
101-
tag = ":" + tag
102-
# If we're querying information about a specific tag, exclude the RepoTags list to improve performance.
103-
cmd.append("--no-tags")
104-
else:
105-
tag = ""
106-
cmd.append(f"docker://{repo}{tag}")
107-
proc = subprocess.run(
108-
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8"
109-
)
93+
proc = skopeo_run(f"{repo}:{tag}", "inspect", "--no-tags")
11094
if proc.returncode:
11195
if "manifest unknown" in proc.stderr:
11296
# This tag has been deleted, which is represented by an empty dict.
@@ -117,6 +101,28 @@ def inspect_tag(repo, tag=None):
117101
return json.loads(proc.stdout)
118102

119103

104+
def list_tags(repo):
105+
"""
106+
List the tags available in the given repo.
107+
If the repo is not available, raise an exception.
108+
"""
109+
proc = skopeo_run(repo, "list-tags")
110+
if proc.returncode:
111+
raise RuntimeError(f"Error listing tags for {repo}: {proc.stderr}")
112+
return json.loads(proc.stdout)["Tags"]
113+
114+
115+
def skopeo_run(reporef, *args):
116+
"""
117+
Run skopeo with the given args, against the given repo reference.
118+
Return the CompletedProcess object associated with the skopeo command.
119+
"""
120+
cmd = ["/usr/bin/skopeo", *args, f"docker://{reporef}"]
121+
return subprocess.run(
122+
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8"
123+
)
124+
125+
120126
def gen_result(repo, tag, tagdata):
121127
"""
122128
Generate a dict containing info about the specified repo.

0 commit comments

Comments
 (0)