Skip to content

Commit

Permalink
fix(code): code clean-up
Browse files Browse the repository at this point in the history
Signed-off-by: Laurentiu Niculae <[email protected]>
  • Loading branch information
laurentiuNiculae committed Oct 6, 2023
1 parent ba37e33 commit 97b37b8
Show file tree
Hide file tree
Showing 34 changed files with 2,599 additions and 3,068 deletions.
37 changes: 5 additions & 32 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,41 +104,14 @@ gen-protobuf: $(PROTOC)
$(PROTOC) --experimental_allow_proto3_optional \
--proto_path=$(TOP_LEVEL)/pkg/meta/proto \
--go_out=$(TOP_LEVEL)/pkg/meta/ \
--go_opt='Mdescriptor.proto=./proto_go' \
$(TOP_LEVEL)/pkg/meta/proto/descriptor.proto
--go_opt='Moci.proto=./proto_go' \
--go_opt='Mmeta.proto=./proto_go' \
$(TOP_LEVEL)/pkg/meta/proto/meta.proto
$(PROTOC) --experimental_allow_proto3_optional \
--proto_path=$(TOP_LEVEL)/pkg/meta/proto \
--go_out=$(TOP_LEVEL)/pkg/meta/ \
--go_opt='Mconfig.proto=./proto_go' \
--go_opt='Mdescriptor.proto=./proto_go' \
$(TOP_LEVEL)/pkg/meta/proto/config.proto
$(PROTOC) --experimental_allow_proto3_optional \
--proto_path=$(TOP_LEVEL)/pkg/meta/proto \
--go_out=$(TOP_LEVEL)/pkg/meta/ \
--go_opt='Mversioned.proto=./proto_go' \
$(TOP_LEVEL)/pkg/meta/proto/versioned.proto
$(PROTOC) --experimental_allow_proto3_optional \
--proto_path=$(TOP_LEVEL)/pkg/meta/proto \
--go_out=$(TOP_LEVEL)/pkg/meta/ \
--go_opt='Mmanifest.proto=./proto_go' \
--go_opt='Mdescriptor.proto=./proto_go' \
--go_opt='Mversioned.proto=./proto_go' \
$(TOP_LEVEL)/pkg/meta/proto/manifest.proto
$(PROTOC) --experimental_allow_proto3_optional \
--proto_path=$(TOP_LEVEL)/pkg/meta/proto \
--go_out=$(TOP_LEVEL)/pkg/meta/ \
--go_opt='Mindex.proto=./proto_go' \
--go_opt='Mdescriptor.proto=./proto_go' \
--go_opt='Mversioned.proto=./proto_go' \
$(TOP_LEVEL)/pkg/meta/proto/index.proto
$(PROTOC) --experimental_allow_proto3_optional \
--proto_path=$(TOP_LEVEL)/pkg/meta/proto \
--go_out=$(TOP_LEVEL)/pkg/meta/ \
--go_opt='MimageData.proto=./proto_go' \
--go_opt='Mdescriptor.proto=./proto_go' \
--go_opt='Mversioned.proto=./proto_go' \
--go_opt='Mconfig.proto=./proto_go' \
$(TOP_LEVEL)/pkg/meta/proto/imageData.proto
--go_opt='Moci.proto=./proto_go' \
$(TOP_LEVEL)/pkg/meta/proto/oci.proto

.PHONY: binary-minimal
binary-minimal: EXTENSIONS=
Expand Down
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ var (
ErrManifestConflict = errors.New("manifest: multiple manifests found")
ErrManifestMetaNotFound = errors.New("metadb: image metadata not found for given manifest reference")
ErrManifestDataNotFound = errors.New("metadb: image data not found for given manifest digest")
ErrImageDataNotFound = errors.New("metadb: image data not found for")
ErrIndexDataNotFount = errors.New("metadb: index data not found for given digest")
ErrRepoMetaNotFound = errors.New("metadb: repo metadata not found for given repo name")
ErrTagMetaNotFound = errors.New("metadb: tag metadata not found for given repo and tag names")
Expand Down
1 change: 0 additions & 1 deletion pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ func (c *Controller) InitMetaDB(reloadCtx context.Context) error {
return err
}

// TODO: undo, commented for testing
err = meta.ParseStorage(driver, c.StoreController, c.Log)
if err != nil {
return err
Expand Down
72 changes: 37 additions & 35 deletions pkg/extensions/search/convert/metadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,8 @@ func FullGetSignaturesInfo(isSigned bool, signatures mTypes.ManifestSignatures,
}

func PaginatedProtoRepoMeta2RepoSummaries(ctx context.Context, repoMetaList []mTypes.RepoMetadata2,
imageDataMap map[string]mTypes.ImageData2, filter mTypes.Filter, pageInput pagination.PageInput, cveInfo cveinfo.CveInfo,
skip SkipQGLField,
imageDataMap map[string]mTypes.ImageData2, filter mTypes.Filter, pageInput pagination.PageInput,
cveInfo cveinfo.CveInfo, skip SkipQGLField,
) ([]*gql_generated.RepoSummary, zcommon.PageInfo, error) {
reposPageFinder, err := pagination.NewRepoSumPageFinder(pageInput.Limit, pageInput.Offset, pageInput.SortBy)
if err != nil {
Expand All @@ -1051,8 +1051,8 @@ func PaginatedProtoRepoMeta2RepoSummaries(ctx context.Context, repoMetaList []mT
}

func PaginatedFullRepoMeta2RepoSummaries(ctx context.Context, repoMetaList []mTypes.FullRepoMetadata,
imageDataMap map[string]mTypes.ImageData2, filter mTypes.Filter, pageInput pagination.PageInput, cveInfo cveinfo.CveInfo,
skip SkipQGLField,
imageDataMap map[string]mTypes.ImageData2, filter mTypes.Filter, pageInput pagination.PageInput,
cveInfo cveinfo.CveInfo, skip SkipQGLField,
) ([]*gql_generated.RepoSummary, zcommon.PageInfo, error) {
reposPageFinder, err := pagination.NewRepoSumPageFinder(pageInput.Limit, pageInput.Offset, pageInput.SortBy)
if err != nil {
Expand All @@ -1077,7 +1077,8 @@ func PaginatedFullRepoMeta2RepoSummaries(ctx context.Context, repoMetaList []mTy
return page, pageInfo, nil
}

func ProtoRepoMeta2RepoSummary(ctx context.Context, repoMeta mTypes.RepoMetadata2, imageDataMap map[string]mTypes.ImageData2,
func ProtoRepoMeta2RepoSummary(ctx context.Context, repoMeta mTypes.RepoMetadata2,
imageDataMap map[string]mTypes.ImageData2,
) *gql_generated.RepoSummary {
var (
repoName = repoMeta.Name
Expand All @@ -1086,7 +1087,7 @@ func ProtoRepoMeta2RepoSummary(ctx context.Context, repoMeta mTypes.RepoMetadata
repoVendorsSet = map[string]bool{}
lastUpdatedImageSummary *gql_generated.ImageSummary
repoDownloadCount = 0
repoStarCount = int(repoMeta.Stars) // total number of stars
repoStarCount = repoMeta.Stars // total number of stars
repoIsUserStarred = repoMeta.IsStarred // value specific to the current user
repoIsUserBookMarked = repoMeta.IsBookmarked // value specific to the current user

Expand Down Expand Up @@ -1159,11 +1160,12 @@ func ProtoRepoMeta2RepoSummary(ctx context.Context, repoMeta mTypes.RepoMetadata
StarCount: &repoStarCount,
IsBookmarked: &repoIsUserBookMarked,
IsStarred: &repoIsUserStarred,
Rank: ref(int(repoMeta.Rank)),
Rank: ref(repoMeta.Rank),
}
}

func FullRepoMeta2RepoSummary(ctx context.Context, repoMeta mTypes.FullRepoMetadata, imageDataMap map[string]mTypes.ImageData2,
func FullRepoMeta2RepoSummary(ctx context.Context, repoMeta mTypes.FullRepoMetadata,
imageDataMap map[string]mTypes.ImageData2,
) *gql_generated.RepoSummary {
var (
repoName = repoMeta.Name
Expand All @@ -1175,9 +1177,9 @@ func FullRepoMeta2RepoSummary(ctx context.Context, repoMeta mTypes.FullRepoMetad
repoIsUserStarred = repoMeta.IsStarred // value specific to the current user
repoIsUserBookMarked = repoMeta.IsBookmarked // value specific to the current user
repoSize = repoMeta.Size
lastUpdatedImageData = imageDataMap[repoMeta.LastUpdatedImage.Digest]
)

lastUpdatedImageData := imageDataMap[repoMeta.LastUpdatedImage.Digest]
if repoLastUpdatedTimestamp == nil {
repoLastUpdatedTimestamp = &time.Time{}
}
Expand Down Expand Up @@ -1208,7 +1210,7 @@ func FullRepoMeta2RepoSummary(ctx context.Context, repoMeta mTypes.FullRepoMetad
StarCount: &repoStarCount,
IsBookmarked: &repoIsUserBookMarked,
IsStarred: &repoIsUserStarred,
Rank: ref(int(repoMeta.Rank)),
Rank: ref(repoMeta.Rank),
}
}

Expand Down Expand Up @@ -1264,8 +1266,7 @@ func ProtoDescriptor2ImageSummary(ctx context.Context, descriptor mTypes.Descrip
case ispec.MediaTypeImageIndex:
return ProtoImageIndex2ImageSummary(ctx, repo, tag, descriptor.Digest, repoMeta, imageDataMap)
default:
// TODO:
return nil, nil, nil
return nil, nil, zerr.ErrMediaTypeNotSupported
}
}

Expand All @@ -1277,8 +1278,7 @@ func FullImageData2ImageSummary(ctx context.Context, imageData mTypes.FullImageD
case ispec.MediaTypeImageIndex:
return FullImageIndex2ImageSummary(ctx, imageData)
default:
// TODO:
return nil, nil, nil
return nil, nil, zerr.ErrMediaTypeNotSupported
}
}

Expand All @@ -1300,12 +1300,13 @@ func ProtoImageIndex2ImageSummary(ctx context.Context, repo, tag, digest string,
)

for _, imageManifest := range imageIndex.Manifests {
imageManifestSummary, manifestBlobs, err := ProtoImageManifest2ImageSummary(ctx, repo, tag, repoMeta, mTypes.ImageData2{
MediaType: ispec.MediaTypeImageManifest,
Digest: imageManifest.Digest,
Size: imageIndex.Size,
Manifests: []mTypes.ManifestData2{imageManifest},
})
imageManifestSummary, manifestBlobs, err := ProtoImageManifest2ImageSummary(ctx, repo, tag, repoMeta,
mTypes.ImageData2{
MediaType: ispec.MediaTypeImageManifest,
Digest: imageManifest.Digest,
Size: imageIndex.Size,
Manifests: []mTypes.ManifestData2{imageManifest},
})
if err != nil {
return &gql_generated.ImageSummary{}, map[string]int64{}, err
}
Expand Down Expand Up @@ -1351,7 +1352,7 @@ func ProtoImageIndex2ImageSummary(ctx context.Context, repo, tag, digest string,
IsSigned: &isSigned,
SignatureInfo: signaturesInfo,
Size: ref(strconv.FormatInt(indexSize, 10)),
DownloadCount: ref(int(repoMeta.Statistics[indexDigestStr].DownloadCount)),
DownloadCount: ref(repoMeta.Statistics[indexDigestStr].DownloadCount),
Description: &annotations.Description,
Title: &annotations.Title,
Documentation: &annotations.Documentation,
Expand Down Expand Up @@ -1399,6 +1400,7 @@ func FullImageIndex2ImageSummary(ctx context.Context, imageData mTypes.FullImage
}

manifestSize := int64(0)

for digest, size := range manifestBlobs {
indexBlobs[digest] = size
manifestSize += size
Expand All @@ -1418,7 +1420,7 @@ func FullImageIndex2ImageSummary(ctx context.Context, imageData mTypes.FullImage
manifestSummaries = append(manifestSummaries, imageManifestSummary.Manifests[0])
}

signaturesInfo := FullGetSignaturesInfo(isSigned, imageData.Signatures, godigest.Digest(imageData.Digest))
signaturesInfo := FullGetSignaturesInfo(isSigned, imageData.Signatures, imageData.Digest)

if manifestAnnotations == nil {
manifestAnnotations = &ImageAnnotations{}
Expand All @@ -1436,7 +1438,7 @@ func FullImageIndex2ImageSummary(ctx context.Context, imageData mTypes.FullImage
IsSigned: &isSigned,
SignatureInfo: signaturesInfo,
Size: ref(strconv.FormatInt(indexSize, 10)),
DownloadCount: ref(int(imageData.Statistics.DownloadCount)),
DownloadCount: ref(imageData.Statistics.DownloadCount),
Description: &annotations.Description,
Title: &annotations.Title,
Documentation: &annotations.Documentation,
Expand All @@ -1461,25 +1463,25 @@ func ProtoImageManifest2ImageSummary(ctx context.Context, repo, tag string, repo
configDigest = manifest.Config.Digest.String()
configSize = manifest.Config.Size
manifestDigest = manifest.Digest.String()
manifestSize = int64(manifest.Size)
manifestSize = manifest.Size
mediaType = manifest.MediaType
artifactType = zcommon.GetManifestArtifactType(imageData.Manifests[0].Manifest)
platform = getPlatform(manifest.ConfigContent.Platform)
imageLastUpdated = zcommon.GetImageLastUpdated(manifest.ConfigContent) // TODO: we can cache this
downloadCount = int(repoMeta.Statistics[manifest.Digest.String()].DownloadCount)
imageLastUpdated = zcommon.GetImageLastUpdated(manifest.ConfigContent)
downloadCount = repoMeta.Statistics[manifest.Digest.String()].DownloadCount
isSigned = isImageSigned(repoMeta.Signatures[manifest.Digest.String()])
)

imageSize, imageBlobsMap := getImageBlobsInfo(manifestDigest, manifestSize, configDigest, configSize, manifest.Layers)
imageSizeStr := strconv.FormatInt(imageSize, 10)
annotations := GetAnnotations(manifest.Annotations, manifest.ConfigContent.Config.Labels) // TODO: This can be cached
annotations := GetAnnotations(manifest.Annotations, manifest.ConfigContent.Config.Labels)

authors := annotations.Authors
if authors == "" {
authors = manifest.ConfigContent.Author
}

historyEntries, err := getAllHistory(manifest.Manifest, manifest.ConfigContent) // TODO: this can be cached
historyEntries, err := getAllHistory(manifest.Manifest, manifest.ConfigContent)
if err != nil {
graphql.AddError(ctx, gqlerror.Errorf("error generating history on tag %s in repo %s: "+
"manifest digest: %s, error: %s", tag, repo, manifest.Digest, err.Error()))
Expand Down Expand Up @@ -1537,25 +1539,25 @@ func FullImageManifest2ImageSummary(ctx context.Context, imageData mTypes.FullIm
configDigest = manifest.Config.Digest.String()
configSize = manifest.Config.Size
manifestDigest = manifest.Digest.String()
manifestSize = int64(manifest.Size)
manifestSize = manifest.Size
mediaType = manifest.MediaType
artifactType = zcommon.GetManifestArtifactType(imageData.Manifests[0].Manifest)
platform = getPlatform(manifest.ConfigContent.Platform)
imageLastUpdated = zcommon.GetImageLastUpdated(manifest.ConfigContent) // TODO: we can cache this
imageLastUpdated = zcommon.GetImageLastUpdated(manifest.ConfigContent)
downloadCount = imageData.Statistics.DownloadCount
isSigned = isImageSigned(imageData.Signatures)
)

imageSize, imageBlobsMap := getImageBlobsInfo(manifestDigest, manifestSize, configDigest, configSize, manifest.Layers)
imageSizeStr := strconv.FormatInt(imageSize, 10)
annotations := GetAnnotations(manifest.Annotations, manifest.ConfigContent.Config.Labels) // TODO: This can be cached
annotations := GetAnnotations(manifest.Annotations, manifest.ConfigContent.Config.Labels)

authors := annotations.Authors
if authors == "" {
authors = manifest.ConfigContent.Author
}

historyEntries, err := getAllHistory(manifest.Manifest, manifest.ConfigContent) // TODO: this can be cached
historyEntries, err := getAllHistory(manifest.Manifest, manifest.ConfigContent)
if err != nil {
graphql.AddError(ctx, gqlerror.Errorf("error generating history on tag %s in repo %s: "+
"manifest digest: %s, error: %s", tag, repoName, manifest.Digest, err.Error()))
Expand Down Expand Up @@ -1646,14 +1648,14 @@ func getAllProtoHistory(manifest *proto_go.ManifestData) ([]*gql_generated.Layer
allHistory = append(allHistory, &gql_generated.LayerHistory{
HistoryDescription: &gql_generated.HistoryDescription{
Created: ref(history[i].GetCreated().AsTime()),
CreatedBy: history[i].Createdby,
CreatedBy: history[i].CreatedBy,
Author: history[i].Author,
Comment: history[i].Comment,
EmptyLayer: history[i].Emptylayer,
EmptyLayer: history[i].EmptyLayer,
},
})

if history[i].Emptylayer != nil && *history[i].Emptylayer {
if history[i].EmptyLayer != nil && *history[i].EmptyLayer {
continue
}

Expand Down
13 changes: 9 additions & 4 deletions pkg/extensions/search/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,18 +913,22 @@ func globalSearch(ctx context.Context, query string, metaDB mTypes.MetaDB, filte

repoMetaList, err := metaDB.ProtoSearchRepos(ctx, query)
if err != nil {
return &gql_generated.PaginatedReposResult{}, []*gql_generated.ImageSummary{}, []*gql_generated.LayerSummary{}, err
return &gql_generated.PaginatedReposResult{}, []*gql_generated.ImageSummary{},
[]*gql_generated.LayerSummary{}, err
}

imageDataMap, err := metaDB.ProtoFilterImageData(ctx, getLatestImageDigest(repoMetaList))
if err != nil {
return &gql_generated.PaginatedReposResult{}, []*gql_generated.ImageSummary{}, []*gql_generated.LayerSummary{}, err
return &gql_generated.PaginatedReposResult{}, []*gql_generated.ImageSummary{},
[]*gql_generated.LayerSummary{}, err
}

repos, pageInfo, err := convert.PaginatedFullRepoMeta2RepoSummaries(ctx, repoMetaList, imageDataMap, localFilter, pageInput, cveInfo,
repos, pageInfo, err := convert.PaginatedFullRepoMeta2RepoSummaries(ctx, repoMetaList, imageDataMap, localFilter,
pageInput, cveInfo,
skip)
if err != nil {
return &gql_generated.PaginatedReposResult{}, []*gql_generated.ImageSummary{}, []*gql_generated.LayerSummary{}, err
return &gql_generated.PaginatedReposResult{}, []*gql_generated.ImageSummary{},
[]*gql_generated.LayerSummary{}, err
}

paginatedRepos.Page = &gql_generated.PageInfo{
Expand Down Expand Up @@ -1412,6 +1416,7 @@ func expandedRepoInfo(ctx context.Context, repo string, metaDB mTypes.MetaDB, cv
}

tagsDigests := []string{}

for i := range repoMeta.Tags {
if i == "" {
continue
Expand Down
13 changes: 0 additions & 13 deletions pkg/extensions/search/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,19 +1385,6 @@ func TestGetImageSummary(t *testing.T) {
})
}

func TestFilterBaseImagesFn(t *testing.T) {
Convey("FilterBaseImages", t, func() {
filterFunc := filterBaseImages(&gql_generated.ImageSummary{})
ok := filterFunc(
mTypes.RepoMetadata{},
mTypes.ManifestMetadata{
ManifestBlob: []byte("bad json"),
},
)
So(ok, ShouldBeFalse)
})
}

func TestImageList(t *testing.T) {
Convey("getImageList", t, func() {
testLogger := log.NewLogger("debug", "")
Expand Down
Loading

0 comments on commit 97b37b8

Please sign in to comment.