0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

fix(convert): fix the update rule of download count for images (#1802)

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
This commit is contained in:
LaurentiuNiculae 2023-09-14 20:48:23 +03:00 committed by GitHub
parent 3d8d47d601
commit c210e3f377
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 32 deletions

View file

@ -980,3 +980,95 @@ func TestGetOneManifestAnnotations(t *testing.T) {
So(*imageSummary.Source, ShouldBeBlank)
})
}
func TestDownloadCount(t *testing.T) {
Convey("manifest", t, func() {
repoMeta, manifestMetaMap, indexDataMap := test.GetMetadataForRepos(
test.Repo{
Name: "repo",
Images: []test.RepoImage{
{
Image: test.CreateRandomImage(),
Tag: "10-downloads",
Statistics: mTypes.DescriptorStatistics{
DownloadCount: 10,
},
},
},
},
)
repoSummary := convert.RepoMeta2RepoSummary(context.Background(), repoMeta[0], manifestMetaMap, indexDataMap,
convert.SkipQGLField{}, nil)
So(*repoSummary.DownloadCount, ShouldEqual, 10)
So(*repoSummary.NewestImage.DownloadCount, ShouldEqual, 10)
})
Convey("index", t, func() {
img1, img2, img3 := test.CreateRandomImage(), test.CreateRandomImage(), test.CreateRandomImage()
multiArch := test.CreateMultiarchWith().Images([]test.Image{img1, img2, img3}).Build()
repoMeta, manifestMetaMap, indexDataMap := test.GetMetadataForRepos(
test.Repo{
Name: "repo",
MultiArchImages: []test.RepoMultiArchImage{
{
MultiarchImage: multiArch,
Tag: "160-multiarch",
ImageStatistics: map[string]mTypes.DescriptorStatistics{
img1.DigestStr(): {DownloadCount: 10},
img2.DigestStr(): {DownloadCount: 20},
img3.DigestStr(): {DownloadCount: 30},
multiArch.DigestStr(): {DownloadCount: 100},
},
},
},
},
)
repoSummary := convert.RepoMeta2RepoSummary(context.Background(), repoMeta[0], manifestMetaMap, indexDataMap,
convert.SkipQGLField{}, nil)
So(*repoSummary.DownloadCount, ShouldEqual, 100)
So(*repoSummary.NewestImage.DownloadCount, ShouldEqual, 100)
})
Convey("index + manifest mixed", t, func() {
img1 := test.CreateRandomImage()
img2 := test.CreateRandomImage()
img3 := test.CreateImageWith().DefaultLayers().ImageConfig(
ispec.Image{Created: test.DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)},
).Build()
multiArch := test.CreateMultiarchWith().Images([]test.Image{img1, img2, img3}).Build()
repoMeta, manifestMetaMap, indexDataMap := test.GetMetadataForRepos(
test.Repo{
Name: "repo",
Images: []test.RepoImage{
{
Image: test.CreateRandomImage(),
Tag: "5-downloads",
Statistics: mTypes.DescriptorStatistics{DownloadCount: 5},
},
},
MultiArchImages: []test.RepoMultiArchImage{
{
MultiarchImage: multiArch,
Tag: "160-multiarch",
ImageStatistics: map[string]mTypes.DescriptorStatistics{
img1.DigestStr(): {DownloadCount: 10},
img2.DigestStr(): {DownloadCount: 20},
img3.DigestStr(): {DownloadCount: 30},
multiArch.DigestStr(): {DownloadCount: 100},
},
},
},
},
)
repoSummary := convert.RepoMeta2RepoSummary(context.Background(), repoMeta[0], manifestMetaMap, indexDataMap,
convert.SkipQGLField{}, nil)
So(*repoSummary.DownloadCount, ShouldEqual, 105)
So(*repoSummary.NewestImage.DownloadCount, ShouldEqual, 100)
})
}

View file

@ -70,17 +70,15 @@ func RepoMeta2RepoSummary(ctx context.Context, repoMeta mTypes.RepoMetadata,
platformString := strings.TrimSpace(fmt.Sprintf("%s %s", opSys, arch))
repoPlatformsSet[platformString] = &gql_generated.Platform{Os: &opSys, Arch: &arch}
}
repoDownloadCount += manifestMetaMap[*manifestSummary.Digest].DownloadCount
}
repoDownloadCount += *imageSummary.DownloadCount
if *imageSummary.Vendor != "" {
repoVendorsSet[*imageSummary.Vendor] = true
}
lastUpdatedImageSummary = UpdateLastUpdatedTimestamp(&repoLastUpdatedTimestamp, lastUpdatedImageSummary, imageSummary)
repoDownloadCount += repoMeta.Statistics[descriptor.Digest].DownloadCount
}
// calculate repo size = sum all manifest, config and layer blobs sizes
@ -248,6 +246,8 @@ func ImageIndex2ImageSummary(ctx context.Context, repo, tag string, indexDigest
manifestSummaries = append(manifestSummaries, manifestSummary)
}
totalDownloadCount += repoMeta.Statistics[indexDigestStr].DownloadCount
for _, signatures := range repoMeta.Signatures[indexDigest.String()] {
if len(signatures) > 0 {
isSigned = true
@ -511,7 +511,7 @@ func ImageManifest2ManifestSummary(ctx context.Context, repo, tag string, descri
configSize = manifestContent.Config.Size
artifactType = zcommon.GetManifestArtifactType(manifestContent)
imageLastUpdated = zcommon.GetImageLastUpdated(configContent)
downloadCount = manifestMeta.DownloadCount
downloadCount = repoMeta.Statistics[digest.String()].DownloadCount
isSigned = false
)

View file

@ -9,10 +9,12 @@ import (
type RepoImage struct {
Image
Tag string
Statistics mTypes.DescriptorStatistics
}
type RepoMultiArchImage struct {
MultiarchImage
ImageStatistics map[string]mTypes.DescriptorStatistics
Tag string
}
@ -38,11 +40,48 @@ func GetMetadataForRepos(repos ...Repo) ([]mTypes.RepoMetadata, map[string]mType
Name: repo.Name,
Tags: map[string]mTypes.Descriptor{},
Signatures: map[string]mTypes.ManifestSignatures{},
Statistics: map[string]mTypes.DescriptorStatistics{},
IsStarred: repo.IsStarred,
IsBookmarked: repo.IsBookmarked,
}
for _, image := range repo.Images {
addImageMetaToMetaDB(image, repoMeta, manifestMetadataMap)
}
for _, multiArch := range repo.MultiArchImages {
if multiArch.ImageStatistics == nil {
multiArch.ImageStatistics = map[string]mTypes.DescriptorStatistics{}
}
repoMeta.Tags[multiArch.Tag] = mTypes.Descriptor{
MediaType: ispec.MediaTypeImageIndex,
Digest: multiArch.DigestStr(),
}
repoMeta.Statistics[multiArch.DigestStr()] = multiArch.ImageStatistics[multiArch.DigestStr()]
for _, image := range multiArch.Images {
addImageMetaToMetaDB(RepoImage{
Image: image,
Statistics: multiArch.ImageStatistics[image.DigestStr()],
}, repoMeta, manifestMetadataMap)
}
indexDataMap[multiArch.indexDescriptor.Digest.String()] = mTypes.IndexData{
IndexBlob: multiArch.indexDescriptor.Data,
}
}
reposMetadata = append(reposMetadata, repoMeta)
}
return reposMetadata, manifestMetadataMap, indexDataMap
}
func addImageMetaToMetaDB(image RepoImage, repoMeta mTypes.RepoMetadata,
manifestMetadataMap map[string]mTypes.ManifestMetadata,
) {
if image.Tag != "" {
repoMeta.Tags[image.Tag] = mTypes.Descriptor{
MediaType: ispec.MediaTypeImageManifest,
@ -60,32 +99,11 @@ func GetMetadataForRepos(repos ...Repo) ([]mTypes.RepoMetadata, map[string]mType
}
}
manifestMetadataMap[image.ManifestDescriptor.Digest.String()] = mTypes.ManifestMetadata{
repoMeta.Statistics[image.DigestStr()] = image.Statistics
manifestMetadataMap[image.DigestStr()] = mTypes.ManifestMetadata{
ManifestBlob: image.ManifestDescriptor.Data,
ConfigBlob: image.ConfigDescriptor.Data,
DownloadCount: image.Statistics.DownloadCount,
}
}
for _, multiArch := range repo.MultiArchImages {
repoMeta.Tags[multiArch.Tag] = mTypes.Descriptor{
MediaType: ispec.MediaTypeImageIndex,
Digest: multiArch.DigestStr(),
}
for _, image := range multiArch.Images {
manifestMetadataMap[image.ManifestDescriptor.Digest.String()] = mTypes.ManifestMetadata{
ManifestBlob: image.ManifestDescriptor.Data,
ConfigBlob: image.ConfigDescriptor.Data,
}
}
indexDataMap[multiArch.indexDescriptor.Digest.String()] = mTypes.IndexData{
IndexBlob: multiArch.indexDescriptor.Data,
}
}
reposMetadata = append(reposMetadata, repoMeta)
}
return reposMetadata, manifestMetadataMap, indexDataMap
}