2023-09-27 13:34:48 -05:00
|
|
|
package ociutils
|
2023-07-31 14:16:09 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
|
|
|
|
mTypes "zotregistry.io/zot/pkg/meta/types"
|
2023-09-15 11:53:15 -05:00
|
|
|
imageUtil "zotregistry.io/zot/pkg/test/image-utils"
|
2023-07-31 14:16:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type RepoImage struct {
|
2023-09-15 11:53:15 -05:00
|
|
|
imageUtil.Image
|
2023-09-14 12:48:23 -05:00
|
|
|
Tag string
|
|
|
|
Statistics mTypes.DescriptorStatistics
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type RepoMultiArchImage struct {
|
2023-09-15 11:53:15 -05:00
|
|
|
imageUtil.MultiarchImage
|
2023-09-14 12:48:23 -05:00
|
|
|
ImageStatistics map[string]mTypes.DescriptorStatistics
|
|
|
|
Tag string
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Repo struct {
|
|
|
|
Name string
|
|
|
|
Images []RepoImage
|
|
|
|
MultiArchImages []RepoMultiArchImage
|
|
|
|
IsBookmarked bool
|
|
|
|
IsStarred bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMetadataForRepos(repos ...Repo) ([]mTypes.RepoMetadata, map[string]mTypes.ManifestMetadata,
|
|
|
|
map[string]mTypes.IndexData,
|
|
|
|
) {
|
|
|
|
var (
|
|
|
|
reposMetadata = []mTypes.RepoMetadata{}
|
|
|
|
manifestMetadataMap = map[string]mTypes.ManifestMetadata{}
|
|
|
|
indexDataMap = map[string]mTypes.IndexData{}
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
|
|
|
repoMeta := mTypes.RepoMetadata{
|
|
|
|
Name: repo.Name,
|
|
|
|
Tags: map[string]mTypes.Descriptor{},
|
|
|
|
Signatures: map[string]mTypes.ManifestSignatures{},
|
2023-09-14 12:48:23 -05:00
|
|
|
Statistics: map[string]mTypes.DescriptorStatistics{},
|
2023-07-31 14:16:09 -05:00
|
|
|
IsStarred: repo.IsStarred,
|
|
|
|
IsBookmarked: repo.IsBookmarked,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, image := range repo.Images {
|
2023-09-14 12:48:23 -05:00
|
|
|
addImageMetaToMetaDB(image, repoMeta, manifestMetadataMap)
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, multiArch := range repo.MultiArchImages {
|
2023-09-14 12:48:23 -05:00
|
|
|
if multiArch.ImageStatistics == nil {
|
|
|
|
multiArch.ImageStatistics = map[string]mTypes.DescriptorStatistics{}
|
|
|
|
}
|
|
|
|
|
2023-07-31 14:16:09 -05:00
|
|
|
repoMeta.Tags[multiArch.Tag] = mTypes.Descriptor{
|
|
|
|
MediaType: ispec.MediaTypeImageIndex,
|
|
|
|
Digest: multiArch.DigestStr(),
|
|
|
|
}
|
|
|
|
|
2023-09-14 12:48:23 -05:00
|
|
|
repoMeta.Statistics[multiArch.DigestStr()] = multiArch.ImageStatistics[multiArch.DigestStr()]
|
|
|
|
|
2023-07-31 14:16:09 -05:00
|
|
|
for _, image := range multiArch.Images {
|
2023-09-14 12:48:23 -05:00
|
|
|
addImageMetaToMetaDB(RepoImage{
|
|
|
|
Image: image,
|
|
|
|
Statistics: multiArch.ImageStatistics[image.DigestStr()],
|
|
|
|
}, repoMeta, manifestMetadataMap)
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
|
|
|
|
2023-09-15 11:53:15 -05:00
|
|
|
indexDataMap[multiArch.IndexDescriptor.Digest.String()] = mTypes.IndexData{
|
|
|
|
IndexBlob: multiArch.IndexDescriptor.Data,
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reposMetadata = append(reposMetadata, repoMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
return reposMetadata, manifestMetadataMap, indexDataMap
|
|
|
|
}
|
2023-09-14 12:48:23 -05:00
|
|
|
|
|
|
|
func addImageMetaToMetaDB(image RepoImage, repoMeta mTypes.RepoMetadata,
|
|
|
|
manifestMetadataMap map[string]mTypes.ManifestMetadata,
|
|
|
|
) {
|
|
|
|
if image.Tag != "" {
|
|
|
|
repoMeta.Tags[image.Tag] = mTypes.Descriptor{
|
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
|
|
|
Digest: image.DigestStr(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// here we can do many more checks about the images like check for referrers, signatures but it's not needed yet
|
|
|
|
// I need just the tags for now and the fake signature.
|
|
|
|
|
|
|
|
// This is done just to mark a manifest as signed in the resulted RepoMeta
|
2023-09-15 11:53:15 -05:00
|
|
|
if image.Manifest.ArtifactType == imageUtil.TestFakeSignatureArtType && image.Manifest.Subject != nil {
|
2023-09-14 12:48:23 -05:00
|
|
|
signedManifestDig := image.Manifest.Subject.Digest.String()
|
|
|
|
repoMeta.Signatures[signedManifestDig] = mTypes.ManifestSignatures{
|
|
|
|
"fakeSignature": []mTypes.SignatureInfo{{SignatureManifestDigest: image.ManifestDescriptor.Digest.String()}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repoMeta.Statistics[image.DigestStr()] = image.Statistics
|
|
|
|
|
|
|
|
manifestMetadataMap[image.DigestStr()] = mTypes.ManifestMetadata{
|
|
|
|
ManifestBlob: image.ManifestDescriptor.Data,
|
|
|
|
ConfigBlob: image.ConfigDescriptor.Data,
|
|
|
|
DownloadCount: image.Statistics.DownloadCount,
|
|
|
|
}
|
|
|
|
}
|