Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(metadb): populate image pushTimestamp if it's 0 value #2003

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/meta/boltdb/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ func (bdw *BoltDB) SetRepoReference(ctx context.Context, repo string, reference
PushTimestamp: timestamppb.Now(),
PushedBy: userid,
}
} else if protoRepoMeta.Statistics[imageMeta.Digest.String()].PushTimestamp.AsTime().IsZero() {
protoRepoMeta.Statistics[imageMeta.Digest.String()].PushTimestamp = timestamppb.Now()
}

if _, ok := protoRepoMeta.Signatures[imageMeta.Digest.String()]; !ok {
Expand Down
2 changes: 2 additions & 0 deletions pkg/meta/convert/convert_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func GetProtoRepoMeta(repo mTypes.RepoMeta) *proto_go.RepoMeta {
Vendors: repo.Vendors,
Platforms: GetProtoPlatforms(repo.Platforms),
LastUpdatedImage: GetProtoLastUpdatedImage(repo.LastUpdatedImage),
Stars: int32(repo.StarCount),
Downloads: int32(repo.DownloadCount),
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/meta/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ func (dwr *DynamoDB) SetRepoReference(ctx context.Context, repo string, referenc
PushTimestamp: timestamppb.Now(),
PushedBy: userid,
}
} else if repoMeta.Statistics[imageMeta.Digest.String()].PushTimestamp.AsTime().IsZero() {
repoMeta.Statistics[imageMeta.Digest.String()].PushTimestamp = timestamppb.Now()
}

if _, ok := repoMeta.Signatures[imageMeta.Digest.String()]; !ok {
Expand Down
46 changes: 46 additions & 0 deletions pkg/meta/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,52 @@ func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB) {
So(repoMeta.Statistics[image.DigestStr()].DownloadCount, ShouldEqual, 3)
So(repoMeta.StarCount, ShouldEqual, 1)
})

// make sure pushTimestamp is always populated to not interfere with retention logic
Convey("Always update pushTimestamp if its value is 0(time.Time{})", func() {
imageStore := local.NewImageStore(rootDir, false, false,
log.NewLogger("debug", ""), monitoring.NewMetricsServer(false, log.NewLogger("debug", "")), nil, nil)

storeController := storage.StoreController{DefaultStore: imageStore}
// add an image
image := CreateRandomImage() //nolint:staticcheck

err := WriteImageToFileSystem(image, repo, "tag", storeController)
So(err, ShouldBeNil)

err = metaDB.SetRepoReference(context.Background(), repo, "tag", image.AsImageMeta())
So(err, ShouldBeNil)

err = metaDB.UpdateStatsOnDownload(repo, "tag")
So(err, ShouldBeNil)

repoMeta, err := metaDB.GetRepoMeta(context.Background(), repo)
So(err, ShouldBeNil)

So(repoMeta.Statistics[image.DigestStr()].DownloadCount, ShouldEqual, 1)
So(time.Now(), ShouldHappenAfter, repoMeta.Statistics[image.DigestStr()].LastPullTimestamp)
So(time.Now(), ShouldHappenAfter, repoMeta.Statistics[image.DigestStr()].PushTimestamp)

// update statistics (simulate that a metaDB has statistics, but pushTimestamp is 0)
stats := repoMeta.Statistics[image.DigestStr()]
oldPushTimestamp := stats.PushTimestamp
stats.PushTimestamp = time.Time{}
repoMeta.Statistics[image.DigestStr()] = stats

err = metaDB.SetRepoMeta(repo, repoMeta)
So(err, ShouldBeNil)

// metaDB should detect that pushTimestamp is 0 and update it.
err = meta.ParseStorage(metaDB, storeController, log.NewLogger("debug", ""))
So(err, ShouldBeNil)

repoMeta, err = metaDB.GetRepoMeta(context.Background(), repo)
So(err, ShouldBeNil)

So(repoMeta.Statistics[image.DigestStr()].DownloadCount, ShouldEqual, 1)
So(repoMeta.DownloadCount, ShouldEqual, 1)
So(repoMeta.Statistics[image.DigestStr()].PushTimestamp, ShouldHappenAfter, oldPushTimestamp)
})
}

func TestGetSignatureLayersInfo(t *testing.T) {
Expand Down
Loading
Loading