2023-01-09 15:37:44 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
|
|
|
|
zerr "zotregistry.io/zot/errors"
|
2023-10-30 15:06:04 -05:00
|
|
|
zcommon "zotregistry.io/zot/pkg/common"
|
|
|
|
mConvert "zotregistry.io/zot/pkg/meta/convert"
|
|
|
|
proto_go "zotregistry.io/zot/pkg/meta/proto/gen"
|
2023-07-18 12:27:26 -05:00
|
|
|
mTypes "zotregistry.io/zot/pkg/meta/types"
|
2023-01-09 15:37:44 -05:00
|
|
|
)
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func SignatureAlreadyExists(signatureSlice []mTypes.SignatureInfo, sm mTypes.SignatureMetadata) bool {
|
|
|
|
for _, sigInfo := range signatureSlice {
|
|
|
|
if sm.SignatureDigest == sigInfo.SignatureManifestDigest {
|
|
|
|
return true
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return false
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func ProtoSignatureAlreadyExists(signatureSlice []*proto_go.SignatureInfo, sm mTypes.SignatureMetadata) bool {
|
2023-01-09 15:37:44 -05:00
|
|
|
for _, sigInfo := range signatureSlice {
|
|
|
|
if sm.SignatureDigest == sigInfo.SignatureManifestDigest {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReferenceIsDigest(reference string) bool {
|
|
|
|
_, err := godigest.Parse(reference)
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
func ReferenceIsTag(reference string) bool {
|
|
|
|
return !ReferenceIsDigest(reference)
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:41:48 -05:00
|
|
|
func ValidateRepoReferenceInput(repo, reference string, manifestDigest godigest.Digest) error {
|
2023-01-09 15:37:44 -05:00
|
|
|
if repo == "" {
|
|
|
|
return zerr.ErrEmptyRepoName
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:41:48 -05:00
|
|
|
if reference == "" {
|
2023-01-09 15:37:44 -05:00
|
|
|
return zerr.ErrEmptyTag
|
|
|
|
}
|
|
|
|
|
|
|
|
if manifestDigest == "" {
|
|
|
|
return zerr.ErrEmptyDigest
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:31:53 -05:00
|
|
|
// These constants are meant used to describe how high or low in rank a match is.
|
|
|
|
// Note that the "higher rank" relates to a lower number so ranks are sorted in a
|
|
|
|
// ascending order.
|
|
|
|
const (
|
|
|
|
lowPriority = 100
|
|
|
|
mediumPriority = 10
|
|
|
|
highPriority = 1
|
|
|
|
perfectMatchPriority = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
// RankRepoName associates a rank to a given repoName given a searchText.
|
2023-10-30 15:06:04 -05:00
|
|
|
// The importance of the value grows inversely proportional to the int value it has.
|
2023-03-22 12:31:53 -05:00
|
|
|
// For example: rank(1) > rank(10) > rank(100)...
|
|
|
|
func RankRepoName(searchText string, repoName string) int {
|
|
|
|
searchText = strings.Trim(searchText, "/")
|
2023-01-09 15:37:44 -05:00
|
|
|
searchTextSlice := strings.Split(searchText, "/")
|
|
|
|
repoNameSlice := strings.Split(repoName, "/")
|
|
|
|
|
|
|
|
if len(searchTextSlice) > len(repoNameSlice) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:31:53 -05:00
|
|
|
if searchText == repoName {
|
|
|
|
return perfectMatchPriority
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
// searchText contains just 1 directory name
|
2023-01-09 15:37:44 -05:00
|
|
|
if len(searchTextSlice) == 1 {
|
2023-03-22 12:31:53 -05:00
|
|
|
lastNameInRepoPath := repoNameSlice[len(repoNameSlice)-1]
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-03-22 12:31:53 -05:00
|
|
|
// searchText: "bar" | repoName: "foo/bar" lastNameInRepoPath: "bar"
|
|
|
|
if index := strings.Index(lastNameInRepoPath, searchText); index != -1 {
|
|
|
|
return (index + 1) * highPriority
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-03-22 12:31:53 -05:00
|
|
|
firstNameInRepoPath := repoNameSlice[0]
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-03-22 12:31:53 -05:00
|
|
|
// searchText: "foo" | repoName: "foo/bar" firstNameInRepoPath: "foo"
|
|
|
|
if index := strings.Index(firstNameInRepoPath, searchText); index != -1 {
|
|
|
|
return (index + 1) * mediumPriority
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-03-22 12:31:53 -05:00
|
|
|
foundPrefixInRepoName := true
|
|
|
|
|
|
|
|
// searchText: "foo/bar/rep" | repoName: "foo/bar/baz/repo" foundPrefixInRepoName: true
|
|
|
|
// searchText: "foo/baz/rep" | repoName: "foo/bar/baz/repo" foundPrefixInRepoName: false
|
2023-01-09 15:37:44 -05:00
|
|
|
for i := 0; i < len(searchTextSlice)-1; i++ {
|
|
|
|
if searchTextSlice[i] != repoNameSlice[i] {
|
2023-03-22 12:31:53 -05:00
|
|
|
foundPrefixInRepoName = false
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundPrefixInRepoName {
|
|
|
|
lastNameInRepoPath := repoNameSlice[len(repoNameSlice)-1]
|
|
|
|
lastNameInSearchText := searchTextSlice[len(searchTextSlice)-1]
|
|
|
|
|
|
|
|
// searchText: "foo/bar/epo" | repoName: "foo/bar/baz/repo" -> Index(repo, epo) = 1
|
|
|
|
if index := strings.Index(lastNameInRepoPath, lastNameInSearchText); index != -1 {
|
|
|
|
return (index + 1) * highPriority
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:31:53 -05:00
|
|
|
// searchText: "foo/bar/b" | repoName: "foo/bar/baz/repo"
|
|
|
|
if strings.HasPrefix(repoName, searchText) {
|
|
|
|
return mediumPriority
|
|
|
|
}
|
|
|
|
|
|
|
|
// searchText: "bar/ba" | repoName: "foo/bar/baz/repo"
|
|
|
|
if index := strings.Index(repoName, searchText); index != -1 {
|
|
|
|
return (index + 1) * lowPriority
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-03-22 12:31:53 -05:00
|
|
|
// no match
|
2023-01-09 15:37:44 -05:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRepoTag(searchText string) (string, string, error) {
|
|
|
|
const repoTagCount = 2
|
|
|
|
|
|
|
|
splitSlice := strings.Split(searchText, ":")
|
|
|
|
|
|
|
|
if len(splitSlice) != repoTagCount {
|
2023-07-06 03:36:26 -05:00
|
|
|
return "", "", zerr.ErrInvalidRepoRefFormat
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
repo := strings.TrimSpace(splitSlice[0])
|
|
|
|
tag := strings.TrimSpace(splitSlice[1])
|
|
|
|
|
|
|
|
return repo, tag, nil
|
|
|
|
}
|
|
|
|
|
2023-03-10 13:37:29 -05:00
|
|
|
func MatchesArtifactTypes(descriptorMediaType string, artifactTypes []string) bool {
|
|
|
|
if len(artifactTypes) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, artifactType := range artifactTypes {
|
|
|
|
if artifactType != "" && descriptorMediaType != artifactType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return found
|
|
|
|
}
|
2023-04-03 12:53:09 -05:00
|
|
|
|
|
|
|
// CheckImageLastUpdated check if the given image is updated earlier than the current repoLastUpdated value
|
|
|
|
//
|
|
|
|
// It returns updated values for: repoLastUpdated, noImageChecked, isSigned.
|
|
|
|
func CheckImageLastUpdated(repoLastUpdated time.Time, isSigned bool, noImageChecked bool,
|
2023-07-18 12:27:26 -05:00
|
|
|
manifestFilterData mTypes.FilterData,
|
2023-04-03 12:53:09 -05:00
|
|
|
) (time.Time, bool, bool) {
|
|
|
|
if noImageChecked || repoLastUpdated.Before(manifestFilterData.LastUpdated) {
|
|
|
|
repoLastUpdated = manifestFilterData.LastUpdated
|
|
|
|
noImageChecked = false
|
|
|
|
|
|
|
|
isSigned = manifestFilterData.IsSigned
|
|
|
|
}
|
|
|
|
|
|
|
|
return repoLastUpdated, noImageChecked, isSigned
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func AddImageMetaToRepoMeta(repoMeta *proto_go.RepoMeta, repoBlobs *proto_go.RepoBlobs, reference string,
|
|
|
|
imageMeta mTypes.ImageMeta,
|
2023-11-08 16:37:52 -05:00
|
|
|
) (*proto_go.RepoMeta, *proto_go.RepoBlobs) {
|
2023-10-30 15:06:04 -05:00
|
|
|
switch imageMeta.MediaType {
|
|
|
|
case ispec.MediaTypeImageManifest:
|
|
|
|
manifestData := imageMeta.Manifests[0]
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
vendor := GetVendor(manifestData.Manifest.Annotations)
|
|
|
|
if vendor == "" {
|
|
|
|
vendor = GetVendor(manifestData.Manifest.Annotations)
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
vendors := []string{}
|
|
|
|
if vendor != "" {
|
|
|
|
vendors = append(vendors, vendor)
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
platforms := []*proto_go.Platform{GetProtoPlatform(&manifestData.Config.Platform)}
|
2023-10-30 15:06:04 -05:00
|
|
|
if platforms[0].OS == "" && platforms[0].Architecture == "" {
|
|
|
|
platforms = []*proto_go.Platform{}
|
|
|
|
}
|
|
|
|
|
|
|
|
subBlobs := []string{manifestData.Manifest.Config.Digest.String()}
|
|
|
|
repoBlobs.Blobs[manifestData.Manifest.Config.Digest.String()] = &proto_go.BlobInfo{
|
|
|
|
Size: manifestData.Manifest.Config.Size,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, layer := range manifestData.Manifest.Layers {
|
|
|
|
subBlobs = append(subBlobs, layer.Digest.String())
|
|
|
|
repoBlobs.Blobs[layer.Digest.String()] = &proto_go.BlobInfo{Size: layer.Size}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastUpdated := zcommon.GetImageLastUpdated(manifestData.Config)
|
|
|
|
|
|
|
|
repoBlobs.Blobs[imageMeta.Digest.String()] = &proto_go.BlobInfo{
|
|
|
|
Size: imageMeta.Size,
|
|
|
|
Vendors: vendors,
|
|
|
|
Platforms: platforms,
|
|
|
|
SubBlobs: subBlobs,
|
|
|
|
LastUpdated: mConvert.GetProtoTime(&lastUpdated),
|
|
|
|
}
|
|
|
|
case ispec.MediaTypeImageIndex:
|
|
|
|
subBlobs := []string{}
|
2023-11-29 08:15:39 -05:00
|
|
|
lastUpdated := time.Time{}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for _, manifest := range imageMeta.Index.Manifests {
|
|
|
|
subBlobs = append(subBlobs, manifest.Digest.String())
|
2023-11-29 08:15:39 -05:00
|
|
|
|
|
|
|
blobInfo := repoBlobs.Blobs[manifest.Digest.String()]
|
|
|
|
|
|
|
|
if blobInfo != nil && blobInfo.LastUpdated != nil {
|
|
|
|
if lastUpdated.Before(blobInfo.LastUpdated.AsTime()) {
|
|
|
|
lastUpdated = blobInfo.LastUpdated.AsTime()
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
|
|
|
|
repoBlobs.Blobs[imageMeta.Digest.String()] = &proto_go.BlobInfo{
|
2023-11-29 08:15:39 -05:00
|
|
|
Size: imageMeta.Size,
|
|
|
|
SubBlobs: subBlobs,
|
|
|
|
LastUpdated: mConvert.GetProtoTime(&lastUpdated),
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update info only when a tag is added
|
|
|
|
if zcommon.IsDigest(reference) {
|
2023-11-08 16:37:52 -05:00
|
|
|
return repoMeta, repoBlobs
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
size, platforms, vendors := recalculateAggregateFields(repoMeta, repoBlobs)
|
|
|
|
repoMeta.Vendors = vendors
|
|
|
|
repoMeta.Platforms = platforms
|
2023-12-08 14:38:30 -05:00
|
|
|
repoMeta.Size = size
|
2023-10-30 15:06:04 -05:00
|
|
|
|
|
|
|
imageBlobInfo := repoBlobs.Blobs[imageMeta.Digest.String()]
|
|
|
|
repoMeta.LastUpdatedImage = mConvert.GetProtoEarlierUpdatedImage(repoMeta.LastUpdatedImage,
|
|
|
|
&proto_go.RepoLastUpdatedImage{
|
|
|
|
LastUpdated: imageBlobInfo.LastUpdated,
|
|
|
|
MediaType: imageMeta.MediaType,
|
|
|
|
Digest: imageMeta.Digest.String(),
|
|
|
|
Tag: reference,
|
|
|
|
})
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
return repoMeta, repoBlobs
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func RemoveImageFromRepoMeta(repoMeta *proto_go.RepoMeta, repoBlobs *proto_go.RepoBlobs, ref string,
|
2023-11-08 16:37:52 -05:00
|
|
|
) (*proto_go.RepoMeta, *proto_go.RepoBlobs) {
|
2023-10-30 15:06:04 -05:00
|
|
|
var updatedLastImage *proto_go.RepoLastUpdatedImage
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
updatedBlobs := map[string]*proto_go.BlobInfo{}
|
|
|
|
updatedSize := int64(0)
|
|
|
|
updatedVendors := []string{}
|
|
|
|
updatedPlatforms := []*proto_go.Platform{}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for tag, descriptor := range repoMeta.Tags {
|
|
|
|
if descriptor.Digest == "" {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
queue := []string{descriptor.Digest}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-11-08 16:35:51 -05:00
|
|
|
updatedLastImage = mConvert.GetProtoEarlierUpdatedImage(updatedLastImage, &proto_go.RepoLastUpdatedImage{
|
2023-10-30 15:06:04 -05:00
|
|
|
LastUpdated: repoBlobs.Blobs[descriptor.Digest].LastUpdated,
|
|
|
|
MediaType: descriptor.MediaType,
|
|
|
|
Digest: descriptor.Digest,
|
|
|
|
Tag: tag,
|
|
|
|
})
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for len(queue) > 0 {
|
|
|
|
currentBlob := queue[0]
|
|
|
|
queue = queue[1:]
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if _, found := updatedBlobs[currentBlob]; !found {
|
|
|
|
blobInfo := repoBlobs.Blobs[currentBlob]
|
|
|
|
|
|
|
|
updatedBlobs[currentBlob] = blobInfo
|
|
|
|
updatedSize += blobInfo.Size
|
|
|
|
updatedVendors = mConvert.AddVendors(updatedVendors, blobInfo.Vendors)
|
|
|
|
updatedPlatforms = mConvert.AddProtoPlatforms(updatedPlatforms, blobInfo.Platforms)
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
queue = append(queue, blobInfo.SubBlobs...)
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:38:30 -05:00
|
|
|
repoMeta.Size = updatedSize
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMeta.Vendors = updatedVendors
|
|
|
|
repoMeta.Platforms = updatedPlatforms
|
|
|
|
repoMeta.LastUpdatedImage = updatedLastImage
|
|
|
|
|
|
|
|
repoBlobs.Blobs = updatedBlobs
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
return repoMeta, repoBlobs
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
2023-07-18 12:27:26 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func recalculateAggregateFields(repoMeta *proto_go.RepoMeta, repoBlobs *proto_go.RepoBlobs,
|
|
|
|
) (int64, []*proto_go.Platform, []string) {
|
|
|
|
size := int64(0)
|
|
|
|
platforms := []*proto_go.Platform{}
|
|
|
|
vendors := []string{}
|
|
|
|
blobsMap := map[string]struct{}{}
|
2023-07-18 12:27:26 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for _, descriptor := range repoMeta.Tags {
|
|
|
|
if descriptor.Digest == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
queue := []string{descriptor.Digest}
|
|
|
|
|
|
|
|
for len(queue) > 0 {
|
|
|
|
currentBlob := queue[0]
|
|
|
|
queue = queue[1:]
|
|
|
|
|
|
|
|
if _, found := blobsMap[currentBlob]; !found {
|
|
|
|
blobInfo := repoBlobs.Blobs[currentBlob]
|
|
|
|
if blobInfo == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
blobsMap[currentBlob] = struct{}{}
|
|
|
|
size += blobInfo.Size
|
|
|
|
vendors = mConvert.AddVendors(vendors, blobInfo.Vendors)
|
|
|
|
platforms = mConvert.AddProtoPlatforms(platforms, blobInfo.Platforms)
|
|
|
|
|
|
|
|
queue = append(queue, blobInfo.SubBlobs...)
|
|
|
|
}
|
|
|
|
}
|
2023-07-18 12:27:26 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return size, platforms, vendors
|
2023-07-18 12:27:26 -05:00
|
|
|
}
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
func GetProtoPlatform(platform *ispec.Platform) *proto_go.Platform {
|
2023-10-30 15:06:04 -05:00
|
|
|
if platform == nil {
|
|
|
|
return nil
|
2023-07-18 12:27:26 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return &proto_go.Platform{
|
|
|
|
Architecture: getArch(platform.Architecture, platform.Variant),
|
|
|
|
OS: platform.OS,
|
2023-07-18 12:27:26 -05:00
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
2023-07-18 12:27:26 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func getArch(arch string, variant string) string {
|
|
|
|
if variant != "" {
|
|
|
|
arch = arch + "/" + variant
|
|
|
|
}
|
|
|
|
|
|
|
|
return arch
|
2023-07-18 12:27:26 -05:00
|
|
|
}
|
2023-07-31 14:16:09 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func GetVendor(annotations map[string]string) string {
|
|
|
|
return GetAnnotationValue(annotations, ispec.AnnotationVendor, "org.label-schema.vendor")
|
|
|
|
}
|
2023-07-31 14:16:09 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func GetAnnotationValue(annotations map[string]string, annotationKey, labelKey string) string {
|
|
|
|
value, ok := annotations[annotationKey]
|
|
|
|
if !ok || value == "" {
|
|
|
|
value, ok = annotations[labelKey]
|
|
|
|
if !ok {
|
|
|
|
value = ""
|
|
|
|
}
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return value
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
2023-11-27 11:52:52 -05:00
|
|
|
|
|
|
|
func GetPartialImageMeta(imageIndexMeta mTypes.ImageMeta, imageMeta mTypes.ImageMeta) mTypes.ImageMeta {
|
|
|
|
partialImageMeta := imageIndexMeta
|
|
|
|
partialImageMeta.Manifests = imageMeta.Manifests
|
|
|
|
|
|
|
|
partialIndex := deref(imageIndexMeta.Index, ispec.Index{})
|
|
|
|
partialIndex.Manifests = getPartialManifestList(partialIndex.Manifests, imageMeta.Digest.String())
|
|
|
|
|
|
|
|
partialImageMeta.Index = &partialIndex
|
|
|
|
|
|
|
|
return partialImageMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPartialManifestList(descriptors []ispec.Descriptor, manifestDigest string) []ispec.Descriptor {
|
|
|
|
result := []ispec.Descriptor{}
|
|
|
|
|
|
|
|
for i := range descriptors {
|
|
|
|
if descriptors[i].Digest.String() == manifestDigest {
|
|
|
|
result = append(result, descriptors[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func deref[T any](pointer *T, defaultVal T) T {
|
|
|
|
if pointer != nil {
|
|
|
|
return *pointer
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultVal
|
|
|
|
}
|