Merge pull request #11333 from ywk253100/200325_copy

Update the existence checking logic when copying artifact
This commit is contained in:
Wenkai Yin(尹文开) 2020-04-01 18:09:20 +08:00 committed by GitHub
commit d187a8e69e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 9 deletions

View File

@ -415,18 +415,33 @@ func (c *controller) copyDeeply(ctx context.Context, srcRepo, reference, dstRepo
digest := srcArt.Digest digest := srcArt.Digest
// check the existence of artifact in the destination repository // check the existence of artifact in the destination repository
dstArt, err := c.GetByReference(ctx, dstRepo, digest, option) if isRoot {
if err == nil { // for the root artifact, check the existence by calling "Count()"
// return conflict error if the root parent artifact already exists under the destination repository // which finds the artifact based on all parent artifacts to avoid
if isRoot { // the issue: https://github.com/goharbor/harbor/issues/11222
n, err := c.artMgr.Count(ctx, &q.Query{
Keywords: map[string]interface{}{
"RepositoryName": dstRepo,
"Digest": digest,
},
})
if err != nil {
return 0, err
}
if n > 0 {
return 0, errors.New(nil).WithCode(errors.ConflictCode). return 0, errors.New(nil).WithCode(errors.ConflictCode).
WithMessage("the artifact %s@%s already exists", dstRepo, digest) WithMessage("the artifact %s@%s already exists", dstRepo, digest)
} }
// the child artifact already under the destination repository, skip } else {
return dstArt.ID, nil // for the child artifact, check the existence by calling "GetByReference" directly
} dstArt, err := c.GetByReference(ctx, dstRepo, digest, option)
if !errors.IsErr(err, errors.NotFoundCode) { if err == nil {
return 0, err // the child artifact already under the destination repository, skip
return dstArt.ID, nil
}
if !errors.IsErr(err, errors.NotFoundCode) {
return 0, err
}
} }
// the artifact doesn't exist under the destination repository, continue to copy // the artifact doesn't exist under the destination repository, continue to copy

View File

@ -414,6 +414,7 @@ func (c *controllerTestSuite) TestCopy() {
RepositoryID: 1, RepositoryID: 1,
Name: "library/hello-world", Name: "library/hello-world",
}, nil) }, nil)
c.artMgr.On("Count").Return(0, nil)
c.artMgr.On("GetByDigest").Return(nil, errors.NotFoundError(nil)) c.artMgr.On("GetByDigest").Return(nil, errors.NotFoundError(nil))
c.tagCtl.On("List").Return([]*tag.Tag{ c.tagCtl.On("List").Return([]*tag.Tag{
{ {