RepoInfo structure now includes new field representing RepoSummary

ExpandedRepoInfo currently returns RepoInfo that is a list of Manifests.
To comply with the newest UI requirements, a new field called Summary,
referring to RepoSummary structure, was added.

Signed-off-by: Alex Stan <alexandrustan96@yahoo.ro>
This commit is contained in:
Alex Stan
2022-07-29 17:51:10 +03:00
committed by Andrei Aaron
parent ae73290929
commit 0c70ae8a4e
6 changed files with 192 additions and 1 deletions
+16 -1
View File
@@ -517,7 +517,7 @@ func TestExpandedRepoInfo(t *testing.T) {
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 422)
query := "{ExpandedRepoInfo(repo:\"zot-cve-test\"){Manifests%20{Digest%20IsSigned%20Tag%20Layers%20{Size%20Digest}}}}"
query := "{ExpandedRepoInfo(repo:\"zot-cve-test\"){Summary%20{Name%20LastUpdated%20Size%20Platforms%20{Os%20Arch}%20Vendors%20Score}}}" // nolint: lll
resp, err = resty.R().Get(baseURL + graphqlQueryPrefix + "?query=" + query)
So(resp, ShouldNotBeNil)
@@ -526,6 +526,21 @@ func TestExpandedRepoInfo(t *testing.T) {
responseStruct := &ExpandedRepoInfoResp{}
err = json.Unmarshal(resp.Body(), responseStruct)
So(err, ShouldBeNil)
So(responseStruct.ExpandedRepoInfo.RepoInfo.Summary, ShouldNotBeEmpty)
So(responseStruct.ExpandedRepoInfo.RepoInfo.Summary.Name, ShouldEqual, "zot-cve-test")
So(responseStruct.ExpandedRepoInfo.RepoInfo.Summary.Score, ShouldEqual, -1)
query = "{ExpandedRepoInfo(repo:\"zot-cve-test\"){Manifests%20{Digest%20IsSigned%20Tag%20Layers%20{Size%20Digest}}}}"
resp, err = resty.R().Get(baseURL + graphqlQueryPrefix + "?query=" + query)
So(resp, ShouldNotBeNil)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 200)
responseStruct = &ExpandedRepoInfoResp{}
err = json.Unmarshal(resp.Body(), responseStruct)
So(err, ShouldBeNil)
So(len(responseStruct.ExpandedRepoInfo.RepoInfo.Manifests), ShouldNotEqual, 0)
@@ -43,6 +43,7 @@ type BaseOciLayoutUtils struct {
type RepoInfo struct {
Manifests []Manifest `json:"manifests"`
Summary RepoSummary
}
type Manifest struct {
@@ -52,6 +53,20 @@ type Manifest struct {
Layers []Layer `json:"layers"`
}
type RepoSummary struct {
Name string `json:"name"`
LastUpdated time.Time `json:"lastUpdated"`
Size string `json:"size"`
Platforms []OsArch `json:"platforms"`
Vendors []string `json:"vendors"`
Score int `json:"score"`
}
type OsArch struct {
Os string `json:"os"`
Arch string `json:"arch"`
}
type Layer struct {
Size string `json:"size"`
Digest string `json:"digest"`
@@ -337,8 +352,18 @@ func (olu BaseOciLayoutUtils) GetRepoLastUpdated(repo string) (TagInfo, error) {
func (olu BaseOciLayoutUtils) GetExpandedRepoInfo(name string) (RepoInfo, error) {
repo := RepoInfo{}
repoBlob2Size := make(map[string]int64, 10)
// made up of all manifests, configs and image layers
repoSize := int64(0)
manifests := make([]Manifest, 0)
tagsInfo, err := olu.GetImageTagsWithTimestamp(name)
if err != nil {
olu.Log.Error().Err(err).Msgf("can't get tags info for repo: %s", name)
}
manifestList, err := olu.GetImageManifests(name)
if err != nil {
olu.Log.Error().Err(err).Msg("error getting image manifests")
@@ -346,6 +371,9 @@ func (olu BaseOciLayoutUtils) GetExpandedRepoInfo(name string) (RepoInfo, error)
return RepoInfo{}, err
}
repoPlatforms := make([]OsArch, 0, len(tagsInfo))
repoVendors := make([]string, 0, len(manifestList))
for _, man := range manifestList {
manifestInfo := Manifest{}
@@ -369,6 +397,30 @@ func (olu BaseOciLayoutUtils) GetExpandedRepoInfo(name string) (RepoInfo, error)
manifestInfo.IsSigned = olu.checkManifestSignature(name, man.Digest)
manifestSize := olu.GetImageManifestSize(name, man.Digest)
olu.Log.Debug().Msg(fmt.Sprintf("%v", man.Digest))
configSize := manifest.Config.Size
repoBlob2Size[man.Digest.String()] = manifestSize
repoBlob2Size[manifest.Config.Digest.Hex] = configSize
imageConfigInfo, err := olu.GetImageConfigInfo(name, man.Digest)
if err != nil {
olu.Log.Error().Err(err).Msgf("can't retrieve config info for the image %s %s", name, man.Digest)
continue
}
vendor := olu.GetImageVendor(imageConfigInfo)
os, arch := olu.GetImagePlatform(imageConfigInfo)
osArch := OsArch{
Os: os,
Arch: arch,
}
repoPlatforms = append(repoPlatforms, osArch)
repoVendors = append(repoVendors, vendor)
layers := make([]Layer, 0)
for _, layer := range manifest.Layers {
@@ -376,6 +428,8 @@ func (olu BaseOciLayoutUtils) GetExpandedRepoInfo(name string) (RepoInfo, error)
layerInfo.Digest = layer.Digest.Hex
repoBlob2Size[layerInfo.Digest] = layer.Size
layerInfo.Size = strconv.FormatInt(layer.Size, 10)
layers = append(layers, layerInfo)
@@ -388,6 +442,28 @@ func (olu BaseOciLayoutUtils) GetExpandedRepoInfo(name string) (RepoInfo, error)
repo.Manifests = manifests
lastUpdate, err := olu.GetRepoLastUpdated(name)
if err != nil {
olu.Log.Error().Err(err).Msgf("can't find latest update timestamp for repo: %s", name)
}
for blob := range repoBlob2Size {
repoSize += repoBlob2Size[blob]
}
size := strconv.FormatInt(repoSize, 10)
summary := RepoSummary{
Name: name,
LastUpdated: lastUpdate.Timestamp,
Size: size,
Platforms: repoPlatforms,
Vendors: repoVendors,
Score: -1,
}
repo.Summary = summary
return repo, nil
}