-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10753 from wy65701436/artifact-trash
add artifact trash manager
- Loading branch information
Showing
11 changed files
with
425 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package dao | ||
|
||
import ( | ||
"context" | ||
ierror "github.com/goharbor/harbor/src/internal/error" | ||
"github.com/goharbor/harbor/src/internal/orm" | ||
"github.com/goharbor/harbor/src/pkg/artifactrash/model" | ||
"time" | ||
) | ||
|
||
// DAO is the data access object interface for artifact trash | ||
type DAO interface { | ||
// Create the artifact trash | ||
Create(ctx context.Context, artifactrsh *model.ArtifactTrash) (id int64, err error) | ||
// Delete the artifact trash specified by ID | ||
Delete(ctx context.Context, id int64) (err error) | ||
// Filter lists the artifact that needs to be cleaned | ||
Filter(ctx context.Context) (arts []model.ArtifactTrash, err error) | ||
} | ||
|
||
// New returns an instance of the default DAO | ||
func New() DAO { | ||
return &dao{} | ||
} | ||
|
||
type dao struct{} | ||
|
||
// Create ... | ||
func (d *dao) Create(ctx context.Context, artifactrsh *model.ArtifactTrash) (id int64, err error) { | ||
ormer, err := orm.FromContext(ctx) | ||
if err != nil { | ||
return 0, err | ||
} | ||
artifactrsh.CreationTime = time.Now() | ||
id, err = ormer.Insert(artifactrsh) | ||
if err != nil { | ||
if e := orm.AsConflictError(err, "artifact trash %s already exists under the repository %s", | ||
artifactrsh.Digest, artifactrsh.RepositoryName); e != nil { | ||
err = e | ||
} | ||
} | ||
return id, err | ||
} | ||
|
||
// Delete ... | ||
func (d *dao) Delete(ctx context.Context, id int64) (err error) { | ||
ormer, err := orm.FromContext(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
n, err := ormer.Delete(&model.ArtifactTrash{ | ||
ID: id, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
if n == 0 { | ||
return ierror.NotFoundError(nil).WithMessage("artifact trash %d not found", id) | ||
} | ||
return nil | ||
} | ||
|
||
// Filter ... | ||
// ToDo replace artifact_2 with artifact | ||
func (d *dao) Filter(ctx context.Context) (arts []model.ArtifactTrash, err error) { | ||
var deletedAfs []model.ArtifactTrash | ||
ormer, err := orm.FromContext(ctx) | ||
if err != nil { | ||
return deletedAfs, err | ||
} | ||
|
||
sql := `SELECT * FROM artifact_trash where artifact_trash.digest NOT IN (select digest from artifact_2)` | ||
|
||
if err := ormer.Raw(sql).QueryRow(&deletedAfs); err != nil { | ||
return deletedAfs, err | ||
} | ||
return deletedAfs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package dao | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
beegoorm "github.com/astaxie/beego/orm" | ||
common_dao "github.com/goharbor/harbor/src/common/dao" | ||
ierror "github.com/goharbor/harbor/src/internal/error" | ||
"github.com/goharbor/harbor/src/internal/orm" | ||
artdao "github.com/goharbor/harbor/src/pkg/artifact/dao" | ||
"github.com/goharbor/harbor/src/pkg/artifactrash/model" | ||
v1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type daoTestSuite struct { | ||
suite.Suite | ||
dao DAO | ||
afDao artdao.DAO | ||
id int64 | ||
ctx context.Context | ||
} | ||
|
||
func (d *daoTestSuite) SetupSuite() { | ||
d.dao = New() | ||
common_dao.PrepareTestForPostgresSQL() | ||
d.ctx = orm.NewContext(nil, beegoorm.NewOrm()) | ||
} | ||
|
||
func (d *daoTestSuite) SetupTest() { | ||
art1 := &artdao.Artifact{ | ||
Type: "image", | ||
ManifestMediaType: v1.MediaTypeImageManifest, | ||
ProjectID: 10, | ||
RepositoryID: 10, | ||
Digest: "1234", | ||
} | ||
id, err := d.afDao.Create(d.ctx, art1) | ||
d.Require().Nil(err) | ||
err = d.afDao.Delete(d.ctx, id) | ||
d.Require().Nil(err) | ||
art2 := &artdao.Artifact{ | ||
Type: "image", | ||
ManifestMediaType: v1.MediaTypeImageManifest, | ||
ProjectID: 10, | ||
RepositoryID: 10, | ||
Digest: "5678", | ||
} | ||
_, err = d.afDao.Create(d.ctx, art2) | ||
d.Require().Nil(err) | ||
|
||
aft := &model.ArtifactTrash{ | ||
ManifestMediaType: v1.MediaTypeImageManifest, | ||
RepositoryName: "test/hello-world", | ||
Digest: "1234", | ||
} | ||
id, err = d.dao.Create(d.ctx, aft) | ||
d.Require().Nil(err) | ||
d.id = id | ||
} | ||
|
||
func (d *daoTestSuite) TearDownTest() { | ||
err := d.dao.Delete(d.ctx, d.id) | ||
d.Require().Nil(err) | ||
} | ||
|
||
func (d *daoTestSuite) TestCreate() { | ||
// conflict | ||
aft := &model.ArtifactTrash{ | ||
ManifestMediaType: v1.MediaTypeImageManifest, | ||
RepositoryName: "test/hello-world", | ||
} | ||
|
||
_, err := d.dao.Create(d.ctx, aft) | ||
d.Require().NotNil(err) | ||
d.True(ierror.IsErr(err, ierror.ConflictCode)) | ||
} | ||
|
||
func (d *daoTestSuite) TestDelete() { | ||
err := d.dao.Delete(d.ctx, 100021) | ||
d.Require().NotNil(err) | ||
var e *ierror.Error | ||
d.Require().True(errors.As(err, &e)) | ||
d.Equal(ierror.NotFoundCode, e.Code) | ||
} | ||
|
||
func (d *daoTestSuite) TestFilter() { | ||
afs, err := d.dao.Filter(d.ctx) | ||
d.Require().NotNil(err) | ||
d.Require().Equal(afs[0].Digest, "1234") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package artifactrash | ||
|
||
import ( | ||
"context" | ||
"github.com/goharbor/harbor/src/pkg/artifactrash/dao" | ||
"github.com/goharbor/harbor/src/pkg/artifactrash/model" | ||
) | ||
|
||
var ( | ||
// Mgr is a global artifact trash manager instance | ||
Mgr = NewManager() | ||
) | ||
|
||
// Manager is the only interface of artifact module to provide the management functions for artifacts | ||
type Manager interface { | ||
// Create ... | ||
Create(ctx context.Context, artifactrsh *model.ArtifactTrash) (id int64, err error) | ||
// Delete ... | ||
Delete(ctx context.Context, id int64) (err error) | ||
// Filter ... | ||
Filter(ctx context.Context) (arts []model.ArtifactTrash, err error) | ||
} | ||
|
||
// NewManager returns an instance of the default manager | ||
func NewManager() Manager { | ||
return &manager{ | ||
dao.New(), | ||
} | ||
} | ||
|
||
var _ Manager = &manager{} | ||
|
||
type manager struct { | ||
dao dao.DAO | ||
} | ||
|
||
func (m *manager) Create(ctx context.Context, artifactrsh *model.ArtifactTrash) (id int64, err error) { | ||
return m.dao.Create(ctx, artifactrsh) | ||
} | ||
func (m *manager) Delete(ctx context.Context, id int64) error { | ||
return m.dao.Delete(ctx, id) | ||
} | ||
func (m *manager) Filter(ctx context.Context) (arts []model.ArtifactTrash, err error) { | ||
return m.dao.Filter(ctx) | ||
} |
Oops, something went wrong.