Skip to content

Commit a5983ee

Browse files
committed
OCI: Fetch image metadata for highest version
OCI repos don't provide a way to fetch the metadata alongside the image tags. Each metadata is a separate API call per tag, and so it could be very slow to retrieve all the metadata if a image has a lot of tags. We only really care about the highest version, so just retrieve metadata for that one.
1 parent b7f94a1 commit a5983ee

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

pkg/client/oci/oci.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package oci
33
import (
44
"context"
55
"fmt"
6+
"sort"
67
"strings"
78

89
"github.com/google/go-containerregistry/pkg/name"
910
"github.com/google/go-containerregistry/pkg/v1/remote"
1011
"github.com/jetstack/version-checker/pkg/api"
12+
"github.com/jetstack/version-checker/pkg/version/semver"
1113
)
1214

1315
type Client struct{}
@@ -34,9 +36,36 @@ func (c *Client) Tags(ctx context.Context, host, repo, image string) ([]api.Imag
3436
return []api.ImageTag{}, err
3537
}
3638

39+
sort.SliceStable(bareTags, func(i, j int) bool {
40+
return semver.Parse(bareTags[i]).LessThan(semver.Parse(bareTags[j]))
41+
})
42+
3743
var tags []api.ImageTag
38-
for _, t := range bareTags {
39-
tags = append(tags, api.ImageTag{Tag: t})
44+
for i, t := range bareTags {
45+
tag := api.ImageTag{Tag: t}
46+
47+
// Only fetch metadata for the highest version
48+
// Could be very slow otherwise if there are a lot of tags, and
49+
// we only really care about the highest version.
50+
if i == len(bareTags)-1 {
51+
img, err := name.ParseReference(fmt.Sprintf("%s:%s", src, t))
52+
if err != nil {
53+
continue
54+
}
55+
image, err := remote.Image(img, remote.WithContext(ctx))
56+
if err != nil {
57+
continue
58+
}
59+
if digest, err := image.Digest(); err == nil {
60+
tag.SHA = digest.String()
61+
}
62+
if conf, err := image.ConfigFile(); err == nil {
63+
tag.Architecture = api.Architecture(conf.Architecture)
64+
tag.OS = api.OS(conf.OS)
65+
tag.Timestamp = conf.Created.Time
66+
}
67+
}
68+
tags = append(tags, tag)
4069
}
4170

4271
return tags, nil

0 commit comments

Comments
 (0)