Skip to content

Commit

Permalink
fix(metadb): populate image pushTimestamp if it's 0 value (project-zo…
Browse files Browse the repository at this point in the history
…t#2003)

in the case of an already existing meta db without pushTimestamp field
its value would be 0 until image is updated, check for zero values and update them
with time.Now() so that retention logic won't remove them.

Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
  • Loading branch information
eusebiu-constantin-petu-dbk authored Nov 9, 2023
1 parent 16def78 commit 4ed4661
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 116 deletions.
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

0 comments on commit 4ed4661

Please sign in to comment.