From e8784de5febd644dcc9b7d5ddcb59c21e394d5e8 Mon Sep 17 00:00:00 2001 From: Wang Yan Date: Fri, 3 Jul 2020 11:50:53 +0800 Subject: [PATCH] support list blobs by update time (#12385) Add support list blob with update time. As introduces the time window in GC, it wants to list the blobs less than specific time. Signed-off-by: wang yan --- src/pkg/blob/dao/dao.go | 4 ++++ src/pkg/blob/dao/dao_test.go | 13 +++++++++++++ src/pkg/blob/manager_test.go | 6 ++++++ src/pkg/blob/models/blob.go | 9 +++++---- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/pkg/blob/dao/dao.go b/src/pkg/blob/dao/dao.go index 2a53a878a..0c805624a 100644 --- a/src/pkg/blob/dao/dao.go +++ b/src/pkg/blob/dao/dao.go @@ -239,6 +239,10 @@ func (d *dao) ListBlobs(ctx context.Context, params models.ListParams) ([]*model qs = qs.Filter("digest__in", params.BlobDigests) } + if !params.UpdateTime.IsZero() { + qs = qs.Filter("update_time__lte", params.UpdateTime) + } + if params.ArtifactDigest != "" { params.ArtifactDigests = append(params.ArtifactDigests, params.ArtifactDigest) } diff --git a/src/pkg/blob/dao/dao_test.go b/src/pkg/blob/dao/dao_test.go index 324ea8e0e..f3afb397e 100644 --- a/src/pkg/blob/dao/dao_test.go +++ b/src/pkg/blob/dao/dao_test.go @@ -20,6 +20,7 @@ import ( htesting "github.com/goharbor/harbor/src/testing" "github.com/stretchr/testify/suite" "testing" + "time" ) type DaoTestSuite struct { @@ -203,6 +204,18 @@ func (suite *DaoTestSuite) TestListBlobs() { if suite.Nil(err) { suite.Len(blobs, 2) } + + blobs, err = suite.dao.ListBlobs(ctx, models.ListParams{UpdateTime: time.Now().Add(-time.Hour)}) + if suite.Nil(err) { + suite.Len(blobs, 0) + } + + digest3 := suite.DigestString() + suite.dao.CreateBlob(ctx, &models.Blob{Digest: digest3, UpdateTime: time.Now().Add(-time.Hour * 2)}) + blobs, err = suite.dao.ListBlobs(ctx, models.ListParams{UpdateTime: time.Now().Add(-time.Hour)}) + if suite.Nil(err) { + suite.Len(blobs, 1) + } } func (suite *DaoTestSuite) TestListBlobsAssociatedWithArtifact() { diff --git a/src/pkg/blob/manager_test.go b/src/pkg/blob/manager_test.go index 3c6c76678..4120486ca 100644 --- a/src/pkg/blob/manager_test.go +++ b/src/pkg/blob/manager_test.go @@ -19,6 +19,7 @@ import ( htesting "github.com/goharbor/harbor/src/testing" "github.com/stretchr/testify/suite" "testing" + "time" "github.com/goharbor/harbor/src/pkg/blob/models" ) @@ -222,6 +223,11 @@ func (suite *ManagerTestSuite) TestList() { blobs, err = Mgr.List(ctx, ListParams{BlobDigests: []string{digest1, digest2}}) suite.Nil(err) suite.Len(blobs, 2) + + blobs, err = Mgr.List(ctx, models.ListParams{UpdateTime: time.Now().Add(-time.Hour)}) + if suite.Nil(err) { + suite.Len(blobs, 0) + } } func (suite *ManagerTestSuite) TestListByArtifact() { diff --git a/src/pkg/blob/models/blob.go b/src/pkg/blob/models/blob.go index bb9a191db..0c2d073b6 100644 --- a/src/pkg/blob/models/blob.go +++ b/src/pkg/blob/models/blob.go @@ -88,8 +88,9 @@ type ProjectBlob = models.ProjectBlob // ListParams list params type ListParams struct { - ArtifactDigest string // list blobs which associated with the artifact - ArtifactDigests []string // list blobs which associated with these artifacts - BlobDigests []string // list blobs which digest in the digests - ProjectID int64 // list blobs which associated with the project + ArtifactDigest string // list blobs which associated with the artifact + ArtifactDigests []string // list blobs which associated with these artifacts + BlobDigests []string // list blobs which digest in the digests + ProjectID int64 // list blobs which associated with the project + UpdateTime time.Time // list blobs which update time less than updatetime }