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 <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2020-07-03 11:50:53 +08:00 committed by GitHub
parent cf07ff0052
commit e8784de5fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 4 deletions

View File

@ -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)
}

View File

@ -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() {

View File

@ -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() {

View File

@ -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
}