Skip to content

Commit

Permalink
fix(metadb): set LastUpdated field also for indexes (#2088)
Browse files Browse the repository at this point in the history
Signed-off-by: Andreea-Lupu <[email protected]>
  • Loading branch information
Andreea-Lupu authored Nov 29, 2023
1 parent 3c8da6e commit e59d8da
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pkg/meta/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,24 @@ func AddImageMetaToRepoMeta(repoMeta *proto_go.RepoMeta, repoBlobs *proto_go.Rep
}
case ispec.MediaTypeImageIndex:
subBlobs := []string{}
lastUpdated := time.Time{}

for _, manifest := range imageMeta.Index.Manifests {
subBlobs = append(subBlobs, manifest.Digest.String())

blobInfo := repoBlobs.Blobs[manifest.Digest.String()]

if blobInfo != nil && blobInfo.LastUpdated != nil {
if lastUpdated.Before(blobInfo.LastUpdated.AsTime()) {
lastUpdated = blobInfo.LastUpdated.AsTime()
}
}
}

repoBlobs.Blobs[imageMeta.Digest.String()] = &proto_go.BlobInfo{
Size: imageMeta.Size,
SubBlobs: subBlobs,
Size: imageMeta.Size,
SubBlobs: subBlobs,
LastUpdated: mConvert.GetProtoTime(&lastUpdated),
}
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/meta/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package convert_test

import (
"testing"
"time"

ispec "github.com/opencontainers/image-spec/specs-go/v1"
. "github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -70,3 +71,31 @@ func TestConvertErrors(t *testing.T) {
})
})
}

func TestGetProtoEarlierUpdatedImage(t *testing.T) {
Convey("GetProtoEarlierUpdatedImage with nil params", t, func() {
// repoLastImage is nil
lastImage := gen.RepoLastUpdatedImage{}

repoLastUpdatedImage := convert.GetProtoEarlierUpdatedImage(nil, &lastImage)
So(repoLastUpdatedImage, ShouldNotBeNil)
So(repoLastUpdatedImage.LastUpdated, ShouldBeNil)

// lastImage is nil
repoLastImage := gen.RepoLastUpdatedImage{}

repoLastUpdatedImage = convert.GetProtoEarlierUpdatedImage(&repoLastImage, nil)
So(repoLastUpdatedImage, ShouldNotBeNil)
So(repoLastUpdatedImage.LastUpdated, ShouldBeNil)

// lastImage.LastUpdated is not nil, but repoLastImage.LastUpdated is nil
lastUpdated := time.Time{}
lastImage = gen.RepoLastUpdatedImage{
LastUpdated: convert.GetProtoTime(&lastUpdated),
}

repoLastUpdatedImage = convert.GetProtoEarlierUpdatedImage(&repoLastImage, &lastImage)
So(repoLastUpdatedImage, ShouldNotBeNil)
So(repoLastUpdatedImage.LastUpdated, ShouldNotBeNil)
})
}
44 changes: 44 additions & 0 deletions pkg/meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,50 @@ func RunMetaDBTests(t *testing.T, metaDB mTypes.MetaDB, preparationFuncs ...func
err = metaDB.SetRepoReference(ctx, "", "tag", imgData1)
So(err, ShouldNotBeNil)
})

Convey("Check last updated for indexes", func() {
config1 := GetDefaultConfig()
config1.Created = DateRef(2009, 2, 1, 12, 0, 0, 0, time.UTC)

config2 := GetDefaultConfig()
config2.Created = DateRef(2011, 2, 1, 12, 0, 0, 0, time.UTC)

config3 := GetDefaultConfig()
config3.Created = DateRef(2011, 3, 1, 12, 0, 0, 0, time.UTC)

image1 := CreateMultiarchWith().Images([]Image{
CreateImageWith().RandomLayers(1, 10).ImageConfig(config1).Build(),
}).Build()
image2 := CreateMultiarchWith().Images([]Image{
CreateImageWith().RandomLayers(1, 10).ImageConfig(config2).Build(),
CreateImageWith().RandomLayers(1, 10).ImageConfig(config3).Build(),
}).Build()

_, err := metaDB.GetRepoMeta(ctx, repo1)
So(err, ShouldNotBeNil)

for i := range image1.Images {
err := metaDB.SetRepoReference(ctx, repo1, image1.Images[i].DigestStr(),
image1.Images[i].AsImageMeta())
So(err, ShouldBeNil)
}

err = metaDB.SetRepoReference(ctx, repo1, tag1, image1.AsImageMeta())
So(err, ShouldBeNil)

for i := range image2.Images {
err := metaDB.SetRepoReference(ctx, repo1, image2.Images[i].DigestStr(),
image2.Images[i].AsImageMeta())
So(err, ShouldBeNil)
}

err = metaDB.SetRepoReference(ctx, repo1, tag2, image2.AsImageMeta())
So(err, ShouldBeNil)

repoMeta, err := metaDB.GetRepoMeta(ctx, repo1)
So(err, ShouldBeNil)
So(*repoMeta.LastUpdatedImage.LastUpdated, ShouldEqual, time.Date(2011, 3, 1, 12, 0, 0, 0, time.UTC))
})
})

Convey("Test RemoveRepoReference", func() {
Expand Down

0 comments on commit e59d8da

Please sign in to comment.