From 42a5db83b2a47ada67432861bd2cf5b8d80f331e Mon Sep 17 00:00:00 2001 From: wang yan Date: Tue, 17 Sep 2019 18:37:53 +0800 Subject: [PATCH] refactor selector of retention extract select from pkg/retention, move it to pkg/artselector to make it usable by immutable tag Signed-off-by: wang yan --- .../job/impl/replication/replication_test.go | 4 +- src/pkg/{retention/res => art}/candidate.go | 2 +- src/pkg/{retention/res => art}/result.go | 2 +- src/pkg/{retention/res => art}/selector.go | 2 +- .../selectors/doublestar/selector.go | 6 +-- .../selectors/doublestar/selector_test.go | 14 +++--- .../res => art}/selectors/index/index.go | 10 ++--- .../res => art}/selectors/label/selector.go | 6 +-- .../selectors/label/selector_test.go | 12 ++--- src/pkg/retention/dep/client.go | 44 +++++++++---------- src/pkg/retention/dep/client_test.go | 22 +++++----- src/pkg/retention/job.go | 14 +++--- src/pkg/retention/job_test.go | 16 +++---- src/pkg/retention/launcher.go | 32 +++++++------- src/pkg/retention/launcher_test.go | 2 +- .../policy/action/index/index_test.go | 10 ++--- src/pkg/retention/policy/action/performer.go | 26 +++++------ .../retention/policy/action/performer_test.go | 14 +++--- src/pkg/retention/policy/alg/or/processor.go | 26 +++++------ .../retention/policy/alg/or/processor_test.go | 22 +++++----- src/pkg/retention/policy/alg/processor.go | 10 ++--- src/pkg/retention/policy/builder.go | 10 ++--- src/pkg/retention/policy/builder_test.go | 20 ++++----- .../retention/policy/rule/always/evaluator.go | 4 +- .../policy/rule/always/evaluator_test.go | 4 +- .../retention/policy/rule/dayspl/evaluator.go | 4 +- .../policy/rule/dayspl/evaluator_test.go | 4 +- .../retention/policy/rule/daysps/evaluator.go | 4 +- .../policy/rule/daysps/evaluator_test.go | 4 +- src/pkg/retention/policy/rule/evaluator.go | 8 ++-- .../retention/policy/rule/index/index_test.go | 6 +-- .../retention/policy/rule/lastx/evaluator.go | 4 +- .../policy/rule/lastx/evaluator_test.go | 4 +- .../policy/rule/latestk/evaluator.go | 6 +-- .../policy/rule/latestk/evaluator_test.go | 6 +-- .../policy/rule/latestpl/evaluator.go | 4 +- .../policy/rule/latestpl/evaluator_test.go | 4 +- .../policy/rule/latestps/evaluator.go | 4 +- .../policy/rule/latestps/evaluator_test.go | 4 +- .../policy/rule/nothing/evaluator.go | 4 +- .../policy/rule/nothing/evaluator_test.go | 4 +- 41 files changed, 204 insertions(+), 204 deletions(-) rename src/pkg/{retention/res => art}/candidate.go (99%) rename src/pkg/{retention/res => art}/result.go (98%) rename src/pkg/{retention/res => art}/selector.go (98%) rename src/pkg/{retention/res => art}/selectors/doublestar/selector.go (92%) rename src/pkg/{retention/res => art}/selectors/doublestar/selector_test.go (96%) rename src/pkg/{retention/res => art}/selectors/index/index.go (89%) rename src/pkg/{retention/res => art}/selectors/label/selector.go (89%) rename src/pkg/{retention/res => art}/selectors/label/selector_test.go (94%) diff --git a/src/jobservice/job/impl/replication/replication_test.go b/src/jobservice/job/impl/replication/replication_test.go index 8abcd65dc..81c8d36bc 100644 --- a/src/jobservice/job/impl/replication/replication_test.go +++ b/src/jobservice/job/impl/replication/replication_test.go @@ -88,10 +88,10 @@ func (f *fakedTransfer) Transfer(src *model.Resource, dst *model.Resource) error } func TestRun(t *testing.T) { - err := transfer.RegisterFactory("res", fakedTransferFactory) + err := transfer.RegisterFactory("art", fakedTransferFactory) require.Nil(t, err) params := map[string]interface{}{ - "src_resource": `{"type":"res"}`, + "src_resource": `{"type":"art"}`, "dst_resource": `{}`, } rep := &Replication{} diff --git a/src/pkg/retention/res/candidate.go b/src/pkg/art/candidate.go similarity index 99% rename from src/pkg/retention/res/candidate.go rename to src/pkg/art/candidate.go index 15f5e8088..f44e22b99 100644 --- a/src/pkg/retention/res/candidate.go +++ b/src/pkg/art/candidate.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package res +package art import ( "encoding/base64" diff --git a/src/pkg/retention/res/result.go b/src/pkg/art/result.go similarity index 98% rename from src/pkg/retention/res/result.go rename to src/pkg/art/result.go index be91be04a..43d09b29d 100644 --- a/src/pkg/retention/res/result.go +++ b/src/pkg/art/result.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package res +package art // Result keeps the action result type Result struct { diff --git a/src/pkg/retention/res/selector.go b/src/pkg/art/selector.go similarity index 98% rename from src/pkg/retention/res/selector.go rename to src/pkg/art/selector.go index de0d34836..4e7bbcdb0 100644 --- a/src/pkg/retention/res/selector.go +++ b/src/pkg/art/selector.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package res +package art // Selector is used to filter the inputting list type Selector interface { diff --git a/src/pkg/retention/res/selectors/doublestar/selector.go b/src/pkg/art/selectors/doublestar/selector.go similarity index 92% rename from src/pkg/retention/res/selectors/doublestar/selector.go rename to src/pkg/art/selectors/doublestar/selector.go index fcbb628b9..274dae730 100644 --- a/src/pkg/retention/res/selectors/doublestar/selector.go +++ b/src/pkg/art/selectors/doublestar/selector.go @@ -16,7 +16,7 @@ package doublestar import ( "github.com/bmatcuk/doublestar" - "github.com/goharbor/harbor/src/pkg/retention/res" + "github.com/goharbor/harbor/src/pkg/art" ) const ( @@ -46,7 +46,7 @@ type selector struct { } // Select candidates by regular expressions -func (s *selector) Select(artifacts []*res.Candidate) (selected []*res.Candidate, err error) { +func (s *selector) Select(artifacts []*art.Candidate) (selected []*art.Candidate, err error) { value := "" excludes := false @@ -86,7 +86,7 @@ func (s *selector) Select(artifacts []*res.Candidate) (selected []*res.Candidate } // New is factory method for doublestar selector -func New(decoration string, pattern string) res.Selector { +func New(decoration string, pattern string) art.Selector { return &selector{ decoration: decoration, pattern: pattern, diff --git a/src/pkg/retention/res/selectors/doublestar/selector_test.go b/src/pkg/art/selectors/doublestar/selector_test.go similarity index 96% rename from src/pkg/retention/res/selectors/doublestar/selector_test.go rename to src/pkg/art/selectors/doublestar/selector_test.go index 23c8dd377..f511f8ebe 100644 --- a/src/pkg/retention/res/selectors/doublestar/selector_test.go +++ b/src/pkg/art/selectors/doublestar/selector_test.go @@ -16,7 +16,7 @@ package doublestar import ( "fmt" - "github.com/goharbor/harbor/src/pkg/retention/res" + "github.com/goharbor/harbor/src/pkg/art" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -28,7 +28,7 @@ import ( type RegExpSelectorTestSuite struct { suite.Suite - artifacts []*res.Candidate + artifacts []*art.Candidate } // TestRegExpSelector is entrance for RegExpSelectorTestSuite @@ -38,13 +38,13 @@ func TestRegExpSelector(t *testing.T) { // SetupSuite to do preparation work func (suite *RegExpSelectorTestSuite) SetupSuite() { - suite.artifacts = []*res.Candidate{ + suite.artifacts = []*art.Candidate{ { NamespaceID: 1, Namespace: "library", Repository: "harbor", Tag: "latest", - Kind: res.Image, + Kind: art.Image, PushedTime: time.Now().Unix() - 3600, PulledTime: time.Now().Unix(), CreationTime: time.Now().Unix() - 7200, @@ -55,7 +55,7 @@ func (suite *RegExpSelectorTestSuite) SetupSuite() { Namespace: "retention", Repository: "redis", Tag: "4.0", - Kind: res.Image, + Kind: art.Image, PushedTime: time.Now().Unix() - 3600, PulledTime: time.Now().Unix(), CreationTime: time.Now().Unix() - 7200, @@ -66,7 +66,7 @@ func (suite *RegExpSelectorTestSuite) SetupSuite() { Namespace: "retention", Repository: "redis", Tag: "4.1", - Kind: res.Image, + Kind: art.Image, PushedTime: time.Now().Unix() - 3600, PulledTime: time.Now().Unix(), CreationTime: time.Now().Unix() - 7200, @@ -235,7 +235,7 @@ func (suite *RegExpSelectorTestSuite) TestNSExcludes() { } // Check whether the returned result matched the expected ones (only check repo:tag) -func expect(expected []string, candidates []*res.Candidate) bool { +func expect(expected []string, candidates []*art.Candidate) bool { hash := make(map[string]bool) for _, art := range candidates { diff --git a/src/pkg/retention/res/selectors/index/index.go b/src/pkg/art/selectors/index/index.go similarity index 89% rename from src/pkg/retention/res/selectors/index/index.go rename to src/pkg/art/selectors/index/index.go index 690beef2d..8387de7cd 100644 --- a/src/pkg/retention/res/selectors/index/index.go +++ b/src/pkg/art/selectors/index/index.go @@ -17,8 +17,8 @@ package index import ( "sync" - "github.com/goharbor/harbor/src/pkg/retention/res" - "github.com/goharbor/harbor/src/pkg/retention/res/selectors/doublestar" + "github.com/goharbor/harbor/src/pkg/art" + "github.com/goharbor/harbor/src/pkg/art/selectors/doublestar" "github.com/pkg/errors" ) @@ -49,11 +49,11 @@ type IndexedMeta struct { // indexedItem defined item kept in the index type indexedItem struct { Meta *IndexedMeta - Factory res.SelectorFactory + Factory art.SelectorFactory } // Register the selector with the corresponding selector kind and decoration -func Register(kind string, decorations []string, factory res.SelectorFactory) { +func Register(kind string, decorations []string, factory art.SelectorFactory) { if len(kind) == 0 || factory == nil { // do nothing return @@ -69,7 +69,7 @@ func Register(kind string, decorations []string, factory res.SelectorFactory) { } // Get selector with the provided kind and decoration -func Get(kind, decoration, pattern string) (res.Selector, error) { +func Get(kind, decoration, pattern string) (art.Selector, error) { if len(kind) == 0 || len(decoration) == 0 { return nil, errors.New("empty selector kind or decoration") } diff --git a/src/pkg/retention/res/selectors/label/selector.go b/src/pkg/art/selectors/label/selector.go similarity index 89% rename from src/pkg/retention/res/selectors/label/selector.go rename to src/pkg/art/selectors/label/selector.go index 2fa788a5a..c43616fd6 100644 --- a/src/pkg/retention/res/selectors/label/selector.go +++ b/src/pkg/art/selectors/label/selector.go @@ -17,7 +17,7 @@ package label import ( "strings" - "github.com/goharbor/harbor/src/pkg/retention/res" + "github.com/goharbor/harbor/src/pkg/art" ) const ( @@ -39,7 +39,7 @@ type selector struct { } // Select candidates by the labels -func (s *selector) Select(artifacts []*res.Candidate) (selected []*res.Candidate, err error) { +func (s *selector) Select(artifacts []*art.Candidate) (selected []*art.Candidate, err error) { for _, art := range artifacts { if isMatched(s.labels, art.Labels, s.decoration) { selected = append(selected, art) @@ -50,7 +50,7 @@ func (s *selector) Select(artifacts []*res.Candidate) (selected []*res.Candidate } // New is factory method for list selector -func New(decoration string, pattern string) res.Selector { +func New(decoration string, pattern string) art.Selector { labels := make([]string, 0) if len(pattern) > 0 { labels = append(labels, strings.Split(pattern, ",")...) diff --git a/src/pkg/retention/res/selectors/label/selector_test.go b/src/pkg/art/selectors/label/selector_test.go similarity index 94% rename from src/pkg/retention/res/selectors/label/selector_test.go rename to src/pkg/art/selectors/label/selector_test.go index 6bf58118a..6e028b62d 100644 --- a/src/pkg/retention/res/selectors/label/selector_test.go +++ b/src/pkg/art/selectors/label/selector_test.go @@ -16,7 +16,7 @@ package label import ( "fmt" - "github.com/goharbor/harbor/src/pkg/retention/res" + "github.com/goharbor/harbor/src/pkg/art" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -28,7 +28,7 @@ import ( type LabelSelectorTestSuite struct { suite.Suite - artifacts []*res.Candidate + artifacts []*art.Candidate } // TestLabelSelector is entrance for LabelSelectorTestSuite @@ -38,13 +38,13 @@ func TestLabelSelector(t *testing.T) { // SetupSuite to do preparation work func (suite *LabelSelectorTestSuite) SetupSuite() { - suite.artifacts = []*res.Candidate{ + suite.artifacts = []*art.Candidate{ { NamespaceID: 1, Namespace: "library", Repository: "harbor", Tag: "1.9", - Kind: res.Image, + Kind: art.Image, PushedTime: time.Now().Unix() - 3600, PulledTime: time.Now().Unix(), CreationTime: time.Now().Unix() - 7200, @@ -55,7 +55,7 @@ func (suite *LabelSelectorTestSuite) SetupSuite() { Namespace: "library", Repository: "harbor", Tag: "dev", - Kind: res.Image, + Kind: art.Image, PushedTime: time.Now().Unix() - 3600, PulledTime: time.Now().Unix(), CreationTime: time.Now().Unix() - 7200, @@ -131,7 +131,7 @@ func (suite *LabelSelectorTestSuite) TestWithoutNoneExistingLabels() { } // Check whether the returned result matched the expected ones (only check repo:tag) -func expect(expected []string, candidates []*res.Candidate) bool { +func expect(expected []string, candidates []*art.Candidate) bool { hash := make(map[string]bool) for _, art := range candidates { diff --git a/src/pkg/retention/dep/client.go b/src/pkg/retention/dep/client.go index c51d427e8..871b8a924 100644 --- a/src/pkg/retention/dep/client.go +++ b/src/pkg/retention/dep/client.go @@ -21,8 +21,8 @@ import ( "github.com/goharbor/harbor/src/common/http/modifier/auth" "github.com/goharbor/harbor/src/jobservice/config" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/clients/core" - "github.com/goharbor/harbor/src/pkg/retention/res" ) // DefaultClient for the retention @@ -33,30 +33,30 @@ type Client interface { // Get the tag candidates under the repository // // Arguments: - // repo *res.Repository : repository info + // repo *art.Repository : repository info // // Returns: - // []*res.Candidate : candidates returned + // []*art.Candidate : candidates returned // error : common error if any errors occurred - GetCandidates(repo *res.Repository) ([]*res.Candidate, error) + GetCandidates(repo *art.Repository) ([]*art.Candidate, error) // Delete the given repository // // Arguments: - // repo *res.Repository : repository info + // repo *art.Repository : repository info // // Returns: // error : common error if any errors occurred - DeleteRepository(repo *res.Repository) error + DeleteRepository(repo *art.Repository) error // Delete the specified candidate // // Arguments: - // candidate *res.Candidate : the deleting candidate + // candidate *art.Candidate : the deleting candidate // // Returns: // error : common error if any errors occurred - Delete(candidate *res.Candidate) error + Delete(candidate *art.Candidate) error } // NewClient new a basic client @@ -88,13 +88,13 @@ type basicClient struct { } // GetCandidates gets the tag candidates under the repository -func (bc *basicClient) GetCandidates(repository *res.Repository) ([]*res.Candidate, error) { +func (bc *basicClient) GetCandidates(repository *art.Repository) ([]*art.Candidate, error) { if repository == nil { return nil, errors.New("repository is nil") } - candidates := make([]*res.Candidate, 0) + candidates := make([]*art.Candidate, 0) switch repository.Kind { - case res.Image: + case art.Image: images, err := bc.coreClient.ListAllImages(repository.Namespace, repository.Name) if err != nil { return nil, err @@ -104,8 +104,8 @@ func (bc *basicClient) GetCandidates(repository *res.Repository) ([]*res.Candida for _, label := range image.Labels { labels = append(labels, label.Name) } - candidate := &res.Candidate{ - Kind: res.Image, + candidate := &art.Candidate{ + Kind: art.Image, Namespace: repository.Namespace, Repository: repository.Name, Tag: image.Name, @@ -118,7 +118,7 @@ func (bc *basicClient) GetCandidates(repository *res.Repository) ([]*res.Candida candidates = append(candidates, candidate) } /* - case res.Chart: + case art.Chart: charts, err := bc.coreClient.ListAllCharts(repository.Namespace, repository.Name) if err != nil { return nil, err @@ -128,8 +128,8 @@ func (bc *basicClient) GetCandidates(repository *res.Repository) ([]*res.Candida for _, label := range chart.Labels { labels = append(labels, label.Name) } - candidate := &res.Candidate{ - Kind: res.Chart, + candidate := &art.Candidate{ + Kind: art.Chart, Namespace: repository.Namespace, Repository: repository.Name, Tag: chart.Name, @@ -148,15 +148,15 @@ func (bc *basicClient) GetCandidates(repository *res.Repository) ([]*res.Candida } // DeleteRepository deletes the specified repository -func (bc *basicClient) DeleteRepository(repo *res.Repository) error { +func (bc *basicClient) DeleteRepository(repo *art.Repository) error { if repo == nil { return errors.New("repository is nil") } switch repo.Kind { - case res.Image: + case art.Image: return bc.coreClient.DeleteImageRepository(repo.Namespace, repo.Name) /* - case res.Chart: + case art.Chart: return bc.coreClient.DeleteChartRepository(repo.Namespace, repo.Name) */ default: @@ -165,15 +165,15 @@ func (bc *basicClient) DeleteRepository(repo *res.Repository) error { } // Deletes the specified candidate -func (bc *basicClient) Delete(candidate *res.Candidate) error { +func (bc *basicClient) Delete(candidate *art.Candidate) error { if candidate == nil { return errors.New("candidate is nil") } switch candidate.Kind { - case res.Image: + case art.Image: return bc.coreClient.DeleteImage(candidate.Namespace, candidate.Repository, candidate.Tag) /* - case res.Chart: + case art.Chart: return bc.coreClient.DeleteChart(candidate.Namespace, candidate.Repository, candidate.Tag) */ default: diff --git a/src/pkg/retention/dep/client_test.go b/src/pkg/retention/dep/client_test.go index 071cc230c..90c0e38c8 100644 --- a/src/pkg/retention/dep/client_test.go +++ b/src/pkg/retention/dep/client_test.go @@ -21,7 +21,7 @@ import ( jmodels "github.com/goharbor/harbor/src/common/job/models" "github.com/goharbor/harbor/src/common/models" "github.com/goharbor/harbor/src/jobservice/job" - "github.com/goharbor/harbor/src/pkg/retention/res" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/testing/clients" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -73,33 +73,33 @@ type clientTestSuite struct { func (c *clientTestSuite) TestGetCandidates() { client := &basicClient{} client.coreClient = &fakeCoreClient{} - var repository *res.Repository + var repository *art.Repository // nil repository candidates, err := client.GetCandidates(repository) require.NotNil(c.T(), err) // image repository - repository = &res.Repository{} - repository.Kind = res.Image + repository = &art.Repository{} + repository.Kind = art.Image repository.Namespace = "library" repository.Name = "hello-world" candidates, err = client.GetCandidates(repository) require.Nil(c.T(), err) assert.Equal(c.T(), 1, len(candidates)) - assert.Equal(c.T(), res.Image, candidates[0].Kind) + assert.Equal(c.T(), art.Image, candidates[0].Kind) assert.Equal(c.T(), "library", candidates[0].Namespace) assert.Equal(c.T(), "hello-world", candidates[0].Repository) assert.Equal(c.T(), "latest", candidates[0].Tag) /* // chart repository - repository.Kind = res.Chart + repository.Kind = art.Chart repository.Namespace = "goharbor" repository.Name = "harbor" candidates, err = client.GetCandidates(repository) require.Nil(c.T(), err) assert.Equal(c.T(), 1, len(candidates)) - assert.Equal(c.T(), res.Chart, candidates[0].Kind) + assert.Equal(c.T(), art.Chart, candidates[0].Kind) assert.Equal(c.T(), "goharbor", candidates[0].Namespace) assert.Equal(c.T(), "1.0", candidates[0].Tag) */ @@ -109,20 +109,20 @@ func (c *clientTestSuite) TestDelete() { client := &basicClient{} client.coreClient = &fakeCoreClient{} - var candidate *res.Candidate + var candidate *art.Candidate // nil candidate err := client.Delete(candidate) require.NotNil(c.T(), err) // image - candidate = &res.Candidate{} - candidate.Kind = res.Image + candidate = &art.Candidate{} + candidate.Kind = art.Image err = client.Delete(candidate) require.Nil(c.T(), err) /* // chart - candidate.Kind = res.Chart + candidate.Kind = art.Chart err = client.Delete(candidate) require.Nil(c.T(), err) */ diff --git a/src/pkg/retention/job.go b/src/pkg/retention/job.go index 4839b7002..0944aa0c1 100644 --- a/src/pkg/retention/job.go +++ b/src/pkg/retention/job.go @@ -23,10 +23,10 @@ import ( "github.com/goharbor/harbor/src/jobservice/job" "github.com/goharbor/harbor/src/jobservice/logger" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/dep" "github.com/goharbor/harbor/src/pkg/retention/policy" "github.com/goharbor/harbor/src/pkg/retention/policy/lwp" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/olekukonko/tablewriter" "github.com/pkg/errors" ) @@ -116,7 +116,7 @@ func (pj *Job) Run(ctx job.Context, params job.Parameters) error { return saveRetainNum(ctx, results, allCandidates) } -func saveRetainNum(ctx job.Context, retained []*res.Result, allCandidates []*res.Candidate) error { +func saveRetainNum(ctx job.Context, retained []*art.Result, allCandidates []*art.Candidate) error { var delNum int for _, r := range retained { if r.Error == nil { @@ -138,7 +138,7 @@ func saveRetainNum(ctx job.Context, retained []*res.Result, allCandidates []*res return nil } -func logResults(logger logger.Interface, all []*res.Candidate, results []*res.Result) { +func logResults(logger logger.Interface, all []*art.Candidate, results []*art.Result) { hash := make(map[string]error, len(results)) for _, r := range results { if r.Target != nil { @@ -146,7 +146,7 @@ func logResults(logger logger.Interface, all []*res.Candidate, results []*res.Re } } - op := func(art *res.Candidate) string { + op := func(art *art.Candidate) string { if e, exists := hash[art.Hash()]; exists { if e != nil { return actionMarkError @@ -194,7 +194,7 @@ func logResults(logger logger.Interface, all []*res.Candidate, results []*res.Re } } -func arn(art *res.Candidate) string { +func arn(art *art.Candidate) string { return fmt.Sprintf("%s/%s:%s", art.Namespace, art.Repository, art.Tag) } @@ -237,7 +237,7 @@ func getParamDryRun(params job.Parameters) (bool, error) { return dryRun, nil } -func getParamRepo(params job.Parameters) (*res.Repository, error) { +func getParamRepo(params job.Parameters) (*art.Repository, error) { v, ok := params[ParamRepo] if !ok { return nil, errors.Errorf("missing parameter: %s", ParamRepo) @@ -248,7 +248,7 @@ func getParamRepo(params job.Parameters) (*res.Repository, error) { return nil, errors.Errorf("invalid parameter: %s", ParamRepo) } - repo := &res.Repository{} + repo := &art.Repository{} if err := repo.FromJSON(repoJSON); err != nil { return nil, errors.Wrap(err, "parse repository from JSON") } diff --git a/src/pkg/retention/job_test.go b/src/pkg/retention/job_test.go index 6d960ae64..27ab7410d 100644 --- a/src/pkg/retention/job_test.go +++ b/src/pkg/retention/job_test.go @@ -22,14 +22,14 @@ import ( "github.com/goharbor/harbor/src/jobservice/job" "github.com/goharbor/harbor/src/jobservice/logger" + "github.com/goharbor/harbor/src/pkg/art" + "github.com/goharbor/harbor/src/pkg/art/selectors/doublestar" "github.com/goharbor/harbor/src/pkg/retention/dep" "github.com/goharbor/harbor/src/pkg/retention/policy" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/lwp" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" "github.com/goharbor/harbor/src/pkg/retention/policy/rule/latestps" - "github.com/goharbor/harbor/src/pkg/retention/res" - "github.com/goharbor/harbor/src/pkg/retention/res/selectors/doublestar" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -60,10 +60,10 @@ func (suite *JobTestSuite) TearDownSuite() { func (suite *JobTestSuite) TestRunSuccess() { params := make(job.Parameters) params[ParamDryRun] = false - repository := &res.Repository{ + repository := &art.Repository{ Namespace: "library", Name: "harbor", - Kind: res.Image, + Kind: art.Image, } repoJSON, err := repository.ToJSON() require.Nil(suite.T(), err) @@ -112,8 +112,8 @@ func (suite *JobTestSuite) TestRunSuccess() { type fakeRetentionClient struct{} // GetCandidates ... -func (frc *fakeRetentionClient) GetCandidates(repo *res.Repository) ([]*res.Candidate, error) { - return []*res.Candidate{ +func (frc *fakeRetentionClient) GetCandidates(repo *art.Repository) ([]*art.Candidate, error) { + return []*art.Candidate{ { Namespace: "library", Repository: "harbor", @@ -140,12 +140,12 @@ func (frc *fakeRetentionClient) GetCandidates(repo *res.Repository) ([]*res.Cand } // Delete ... -func (frc *fakeRetentionClient) Delete(candidate *res.Candidate) error { +func (frc *fakeRetentionClient) Delete(candidate *art.Candidate) error { return nil } // SubmitTask ... -func (frc *fakeRetentionClient) DeleteRepository(repo *res.Repository) error { +func (frc *fakeRetentionClient) DeleteRepository(repo *art.Repository) error { return nil } diff --git a/src/pkg/retention/launcher.go b/src/pkg/retention/launcher.go index c9f6d4655..09d264ee7 100644 --- a/src/pkg/retention/launcher.go +++ b/src/pkg/retention/launcher.go @@ -19,7 +19,7 @@ import ( "time" "github.com/goharbor/harbor/src/jobservice/job" - "github.com/goharbor/harbor/src/pkg/retention/res/selectors/index" + "github.com/goharbor/harbor/src/pkg/art/selectors/index" cjob "github.com/goharbor/harbor/src/common/job" "github.com/goharbor/harbor/src/common/job/models" @@ -27,12 +27,12 @@ import ( "github.com/goharbor/harbor/src/common/utils" "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/core/config" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/project" "github.com/goharbor/harbor/src/pkg/repository" "github.com/goharbor/harbor/src/pkg/retention/policy" "github.com/goharbor/harbor/src/pkg/retention/policy/lwp" "github.com/goharbor/harbor/src/pkg/retention/q" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/pkg/errors" ) @@ -84,7 +84,7 @@ func NewLauncher(projectMgr project.Manager, repositoryMgr repository.Manager, type jobData struct { TaskID int64 - Repository res.Repository + Repository art.Repository JobName string JobParams map[string]interface{} } @@ -111,9 +111,9 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64, isDryRun bool if scope == nil { return 0, launcherError(fmt.Errorf("the scope of policy is nil")) } - repositoryRules := make(map[res.Repository]*lwp.Metadata, 0) + repositoryRules := make(map[art.Repository]*lwp.Metadata, 0) level := scope.Level - var allProjects []*res.Candidate + var allProjects []*art.Candidate var err error if level == "system" { // get projects @@ -144,12 +144,12 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64, isDryRun bool } } case "project": - projectCandidates = append(projectCandidates, &res.Candidate{ + projectCandidates = append(projectCandidates, &art.Candidate{ NamespaceID: scope.Reference, }) } - var repositoryCandidates []*res.Candidate + var repositoryCandidates []*art.Candidate // get repositories of projects for _, projectCandidate := range projectCandidates { repositories, err := getRepositories(l.projectMgr, l.repositoryMgr, projectCandidate.NamespaceID, l.chartServerEnabled) @@ -174,7 +174,7 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64, isDryRun bool } for _, repositoryCandidate := range repositoryCandidates { - reposit := res.Repository{ + reposit := art.Repository{ Namespace: repositoryCandidate.Namespace, Name: repositoryCandidate.Repository, Kind: repositoryCandidate.Kind, @@ -214,7 +214,7 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64, isDryRun bool return int64(len(jobDatas)), nil } -func createJobs(repositoryRules map[res.Repository]*lwp.Metadata, isDryRun bool) ([]*jobData, error) { +func createJobs(repositoryRules map[art.Repository]*lwp.Metadata, isDryRun bool) ([]*jobData, error) { jobDatas := []*jobData{} for repository, policy := range repositoryRules { jobData := &jobData{ @@ -320,14 +320,14 @@ func launcherError(err error) error { return errors.Wrap(err, "launcher") } -func getProjects(projectMgr project.Manager) ([]*res.Candidate, error) { +func getProjects(projectMgr project.Manager) ([]*art.Candidate, error) { projects, err := projectMgr.List() if err != nil { return nil, err } - var candidates []*res.Candidate + var candidates []*art.Candidate for _, pro := range projects { - candidates = append(candidates, &res.Candidate{ + candidates = append(candidates, &art.Candidate{ NamespaceID: pro.ProjectID, Namespace: pro.Name, }) @@ -336,8 +336,8 @@ func getProjects(projectMgr project.Manager) ([]*res.Candidate, error) { } func getRepositories(projectMgr project.Manager, repositoryMgr repository.Manager, - projectID int64, chartServerEnabled bool) ([]*res.Candidate, error) { - var candidates []*res.Candidate + projectID int64, chartServerEnabled bool) ([]*art.Candidate, error) { + var candidates []*art.Candidate /* pro, err := projectMgr.Get(projectID) if err != nil { @@ -351,7 +351,7 @@ func getRepositories(projectMgr project.Manager, repositoryMgr repository.Manage } for _, r := range imageRepositories { namespace, repo := utils.ParseRepository(r.Name) - candidates = append(candidates, &res.Candidate{ + candidates = append(candidates, &art.Candidate{ Namespace: namespace, Repository: repo, Kind: "image", @@ -366,7 +366,7 @@ func getRepositories(projectMgr project.Manager, repositoryMgr repository.Manage return nil, err } for _, r := range chartRepositories { - candidates = append(candidates, &res.Candidate{ + candidates = append(candidates, &art.Candidate{ Namespace: pro.Name, Repository: r.Name, Kind: "chart", diff --git a/src/pkg/retention/launcher_test.go b/src/pkg/retention/launcher_test.go index c63b7bf28..3048b15a9 100644 --- a/src/pkg/retention/launcher_test.go +++ b/src/pkg/retention/launcher_test.go @@ -21,12 +21,12 @@ import ( "github.com/goharbor/harbor/src/chartserver" "github.com/goharbor/harbor/src/common/job" "github.com/goharbor/harbor/src/common/models" + _ "github.com/goharbor/harbor/src/pkg/art/selectors/doublestar" "github.com/goharbor/harbor/src/pkg/project" "github.com/goharbor/harbor/src/pkg/repository" "github.com/goharbor/harbor/src/pkg/retention/policy" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" "github.com/goharbor/harbor/src/pkg/retention/q" - _ "github.com/goharbor/harbor/src/pkg/retention/res/selectors/doublestar" hjob "github.com/goharbor/harbor/src/testing/job" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/src/pkg/retention/policy/action/index/index_test.go b/src/pkg/retention/policy/action/index/index_test.go index f9d4f57e5..a873fed3c 100644 --- a/src/pkg/retention/policy/action/index/index_test.go +++ b/src/pkg/retention/policy/action/index/index_test.go @@ -18,8 +18,8 @@ import ( "testing" "time" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -29,7 +29,7 @@ import ( type IndexTestSuite struct { suite.Suite - candidates []*res.Candidate + candidates []*art.Candidate } // TestIndexEntry is entry of IndexTestSuite @@ -41,7 +41,7 @@ func TestIndexEntry(t *testing.T) { func (suite *IndexTestSuite) SetupSuite() { Register("fakeAction", newFakePerformer) - suite.candidates = []*res.Candidate{{ + suite.candidates = []*art.Candidate{{ Namespace: "library", Repository: "harbor", Kind: "image", @@ -77,9 +77,9 @@ type fakePerformer struct { } // Perform the artifacts -func (p *fakePerformer) Perform(candidates []*res.Candidate) (results []*res.Result, err error) { +func (p *fakePerformer) Perform(candidates []*art.Candidate) (results []*art.Result, err error) { for _, c := range candidates { - results = append(results, &res.Result{ + results = append(results, &art.Result{ Target: c, }) } diff --git a/src/pkg/retention/policy/action/performer.go b/src/pkg/retention/policy/action/performer.go index 72d34d612..2461d0945 100644 --- a/src/pkg/retention/policy/action/performer.go +++ b/src/pkg/retention/policy/action/performer.go @@ -15,8 +15,8 @@ package action import ( + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/dep" - "github.com/goharbor/harbor/src/pkg/retention/res" ) const ( @@ -29,12 +29,12 @@ type Performer interface { // Perform the action // // Arguments: - // candidates []*res.Candidate : the targets to perform + // candidates []*art.Candidate : the targets to perform // // Returns: - // []*res.Result : result infos + // []*art.Result : result infos // error : common error if any errors occurred - Perform(candidates []*res.Candidate) ([]*res.Result, error) + Perform(candidates []*art.Candidate) ([]*art.Result, error) } // PerformerFactory is factory method for creating Performer @@ -42,13 +42,13 @@ type PerformerFactory func(params interface{}, isDryRun bool) Performer // retainAction make sure all the candidates will be retained and others will be cleared type retainAction struct { - all []*res.Candidate + all []*art.Candidate // Indicate if it is a dry run isDryRun bool } // Perform the action -func (ra *retainAction) Perform(candidates []*res.Candidate) (results []*res.Result, err error) { +func (ra *retainAction) Perform(candidates []*art.Candidate) (results []*art.Result, err error) { retained := make(map[string]bool) for _, c := range candidates { retained[c.Hash()] = true @@ -56,14 +56,14 @@ func (ra *retainAction) Perform(candidates []*res.Candidate) (results []*res.Res // start to delete if len(ra.all) > 0 { - for _, art := range ra.all { - if _, ok := retained[art.Hash()]; !ok { - result := &res.Result{ - Target: art, + for _, c := range ra.all { + if _, ok := retained[c.Hash()]; !ok { + result := &art.Result{ + Target: c, } if !ra.isDryRun { - if err := dep.DefaultClient.Delete(art); err != nil { + if err := dep.DefaultClient.Delete(c); err != nil { result.Error = err } } @@ -79,7 +79,7 @@ func (ra *retainAction) Perform(candidates []*res.Candidate) (results []*res.Res // NewRetainAction is factory method for RetainAction func NewRetainAction(params interface{}, isDryRun bool) Performer { if params != nil { - if all, ok := params.([]*res.Candidate); ok { + if all, ok := params.([]*art.Candidate); ok { return &retainAction{ all: all, isDryRun: isDryRun, @@ -88,7 +88,7 @@ func NewRetainAction(params interface{}, isDryRun bool) Performer { } return &retainAction{ - all: make([]*res.Candidate, 0), + all: make([]*art.Candidate, 0), isDryRun: isDryRun, } } diff --git a/src/pkg/retention/policy/action/performer_test.go b/src/pkg/retention/policy/action/performer_test.go index 0f07c0433..868bb4c93 100644 --- a/src/pkg/retention/policy/action/performer_test.go +++ b/src/pkg/retention/policy/action/performer_test.go @@ -18,8 +18,8 @@ import ( "testing" "time" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/dep" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -31,7 +31,7 @@ type TestPerformerSuite struct { suite.Suite oldClient dep.Client - all []*res.Candidate + all []*art.Candidate } // TestPerformer is the entry of the TestPerformerSuite @@ -41,7 +41,7 @@ func TestPerformer(t *testing.T) { // SetupSuite ... func (suite *TestPerformerSuite) SetupSuite() { - suite.all = []*res.Candidate{ + suite.all = []*art.Candidate{ { Namespace: "library", Repository: "harbor", @@ -77,7 +77,7 @@ func (suite *TestPerformerSuite) TestPerform() { all: suite.all, } - candidates := []*res.Candidate{ + candidates := []*art.Candidate{ { Namespace: "library", Repository: "harbor", @@ -100,16 +100,16 @@ func (suite *TestPerformerSuite) TestPerform() { type fakeRetentionClient struct{} // GetCandidates ... -func (frc *fakeRetentionClient) GetCandidates(repo *res.Repository) ([]*res.Candidate, error) { +func (frc *fakeRetentionClient) GetCandidates(repo *art.Repository) ([]*art.Candidate, error) { return nil, errors.New("not implemented") } // Delete ... -func (frc *fakeRetentionClient) Delete(candidate *res.Candidate) error { +func (frc *fakeRetentionClient) Delete(candidate *art.Candidate) error { return nil } // DeleteRepository ... -func (frc *fakeRetentionClient) DeleteRepository(repo *res.Repository) error { +func (frc *fakeRetentionClient) DeleteRepository(repo *art.Repository) error { panic("implement me") } diff --git a/src/pkg/retention/policy/alg/or/processor.go b/src/pkg/retention/policy/alg/or/processor.go index 623e4f050..a940299dd 100644 --- a/src/pkg/retention/policy/alg/or/processor.go +++ b/src/pkg/retention/policy/alg/or/processor.go @@ -18,10 +18,10 @@ import ( "sync" "github.com/goharbor/harbor/src/common/utils/log" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/alg" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/pkg/errors" ) @@ -29,7 +29,7 @@ import ( type processor struct { // keep evaluator and its related selector if existing // attentions here, the selectors can be empty/nil, that means match all "**" - evaluators map[*rule.Evaluator][]res.Selector + evaluators map[*rule.Evaluator][]art.Selector // action performer performers map[string]action.Performer } @@ -37,7 +37,7 @@ type processor struct { // New processor func New(parameters []*alg.Parameter) alg.Processor { p := &processor{ - evaluators: make(map[*rule.Evaluator][]res.Selector), + evaluators: make(map[*rule.Evaluator][]art.Selector), performers: make(map[string]action.Performer), } @@ -59,10 +59,10 @@ func New(parameters []*alg.Parameter) alg.Processor { } // Process the candidates with the rules -func (p *processor) Process(artifacts []*res.Candidate) ([]*res.Result, error) { +func (p *processor) Process(artifacts []*art.Candidate) ([]*art.Result, error) { if len(artifacts) == 0 { log.Debug("no artifacts to retention") - return make([]*res.Result, 0), nil + return make([]*art.Result, 0), nil } var ( @@ -75,7 +75,7 @@ func (p *processor) Process(artifacts []*res.Candidate) ([]*res.Result, error) { // for sync type chanItem struct { action string - processed []*res.Candidate + processed []*art.Candidate } resChan := make(chan *chanItem, 1) @@ -124,9 +124,9 @@ func (p *processor) Process(artifacts []*res.Candidate) ([]*res.Result, error) { for eva, selectors := range p.evaluators { var evaluator = *eva - go func(evaluator rule.Evaluator, selectors []res.Selector) { + go func(evaluator rule.Evaluator, selectors []art.Selector) { var ( - processed []*res.Candidate + processed []*art.Candidate err error ) @@ -173,7 +173,7 @@ func (p *processor) Process(artifacts []*res.Candidate) ([]*res.Result, error) { return nil, err } - results := make([]*res.Result, 0) + results := make([]*art.Result, 0) // Perform actions for act, hash := range processedCandidates { var attachedErr error @@ -192,7 +192,7 @@ func (p *processor) Process(artifacts []*res.Candidate) ([]*res.Result, error) { if attachedErr != nil { for _, c := range cl { - results = append(results, &res.Result{ + results = append(results, &art.Result{ Target: c, Error: attachedErr, }) @@ -203,10 +203,10 @@ func (p *processor) Process(artifacts []*res.Candidate) ([]*res.Result, error) { return results, nil } -type cHash map[string]*res.Candidate +type cHash map[string]*art.Candidate -func (ch cHash) toList() []*res.Candidate { - l := make([]*res.Candidate, 0) +func (ch cHash) toList() []*art.Candidate { + l := make([]*art.Candidate, 0) for _, v := range ch { l = append(l, v) diff --git a/src/pkg/retention/policy/alg/or/processor_test.go b/src/pkg/retention/policy/alg/or/processor_test.go index 8d09966e5..54e5233f5 100644 --- a/src/pkg/retention/policy/alg/or/processor_test.go +++ b/src/pkg/retention/policy/alg/or/processor_test.go @@ -19,6 +19,9 @@ import ( "testing" "time" + "github.com/goharbor/harbor/src/pkg/art" + "github.com/goharbor/harbor/src/pkg/art/selectors/doublestar" + "github.com/goharbor/harbor/src/pkg/art/selectors/label" "github.com/goharbor/harbor/src/pkg/retention/dep" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/alg" @@ -26,9 +29,6 @@ import ( "github.com/goharbor/harbor/src/pkg/retention/policy/rule/always" "github.com/goharbor/harbor/src/pkg/retention/policy/rule/lastx" "github.com/goharbor/harbor/src/pkg/retention/policy/rule/latestps" - "github.com/goharbor/harbor/src/pkg/retention/res" - "github.com/goharbor/harbor/src/pkg/retention/res/selectors/doublestar" - "github.com/goharbor/harbor/src/pkg/retention/res/selectors/label" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -38,7 +38,7 @@ import ( type ProcessorTestSuite struct { suite.Suite - all []*res.Candidate + all []*art.Candidate oldClient dep.Client } @@ -50,7 +50,7 @@ func TestProcessor(t *testing.T) { // SetupSuite ... func (suite *ProcessorTestSuite) SetupSuite() { - suite.all = []*res.Candidate{ + suite.all = []*art.Candidate{ { Namespace: "library", Repository: "harbor", @@ -90,7 +90,7 @@ func (suite *ProcessorTestSuite) TestProcess() { lastxParams[lastx.ParameterX] = 10 params = append(params, &alg.Parameter{ Evaluator: lastx.New(lastxParams), - Selectors: []res.Selector{ + Selectors: []art.Selector{ doublestar.New(doublestar.Matches, "*dev*"), label.New(label.With, "L1,L2"), }, @@ -101,7 +101,7 @@ func (suite *ProcessorTestSuite) TestProcess() { latestKParams[latestps.ParameterK] = 10 params = append(params, &alg.Parameter{ Evaluator: latestps.New(latestKParams), - Selectors: []res.Selector{ + Selectors: []art.Selector{ label.New(label.With, "L3"), }, Performer: perf, @@ -131,7 +131,7 @@ func (suite *ProcessorTestSuite) TestProcess2() { alwaysParams := make(map[string]rule.Parameter) params = append(params, &alg.Parameter{ Evaluator: always.New(alwaysParams), - Selectors: []res.Selector{ + Selectors: []art.Selector{ doublestar.New(doublestar.Matches, "latest"), label.New(label.With, ""), }, @@ -163,16 +163,16 @@ func (suite *ProcessorTestSuite) TestProcess2() { type fakeRetentionClient struct{} // GetCandidates ... -func (frc *fakeRetentionClient) GetCandidates(repo *res.Repository) ([]*res.Candidate, error) { +func (frc *fakeRetentionClient) GetCandidates(repo *art.Repository) ([]*art.Candidate, error) { return nil, errors.New("not implemented") } // Delete ... -func (frc *fakeRetentionClient) Delete(candidate *res.Candidate) error { +func (frc *fakeRetentionClient) Delete(candidate *art.Candidate) error { return nil } // DeleteRepository ... -func (frc *fakeRetentionClient) DeleteRepository(repo *res.Repository) error { +func (frc *fakeRetentionClient) DeleteRepository(repo *art.Repository) error { panic("implement me") } diff --git a/src/pkg/retention/policy/alg/processor.go b/src/pkg/retention/policy/alg/processor.go index 4f7103a5f..a057b6fee 100644 --- a/src/pkg/retention/policy/alg/processor.go +++ b/src/pkg/retention/policy/alg/processor.go @@ -15,9 +15,9 @@ package alg import ( + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" ) // Processor processing the whole policy targeting a repository. @@ -27,12 +27,12 @@ type Processor interface { // Process the artifact candidates // // Arguments: - // artifacts []*res.Candidate : process the retention candidates + // artifacts []*art.Candidate : process the retention candidates // // Returns: - // []*res.Result : the processed results + // []*art.Result : the processed results // error : common error object if any errors occurred - Process(artifacts []*res.Candidate) ([]*res.Result, error) + Process(artifacts []*art.Candidate) ([]*art.Result, error) } // Parameter for constructing a processor @@ -42,7 +42,7 @@ type Parameter struct { Evaluator rule.Evaluator // Selectors for the rule - Selectors []res.Selector + Selectors []art.Selector // Performer for the rule evaluator Performer action.Performer diff --git a/src/pkg/retention/policy/builder.go b/src/pkg/retention/policy/builder.go index 88443fb6b..59884c86c 100644 --- a/src/pkg/retention/policy/builder.go +++ b/src/pkg/retention/policy/builder.go @@ -21,13 +21,13 @@ import ( index3 "github.com/goharbor/harbor/src/pkg/retention/policy/alg/index" - index2 "github.com/goharbor/harbor/src/pkg/retention/res/selectors/index" + index2 "github.com/goharbor/harbor/src/pkg/art/selectors/index" "github.com/goharbor/harbor/src/pkg/retention/policy/rule/index" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/alg" "github.com/goharbor/harbor/src/pkg/retention/policy/lwp" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/pkg/errors" ) @@ -46,7 +46,7 @@ type Builder interface { } // NewBuilder news a basic builder -func NewBuilder(all []*res.Candidate) Builder { +func NewBuilder(all []*art.Candidate) Builder { return &basicBuilder{ allCandidates: all, } @@ -54,7 +54,7 @@ func NewBuilder(all []*res.Candidate) Builder { // basicBuilder is default implementation of Builder interface type basicBuilder struct { - allCandidates []*res.Candidate + allCandidates []*art.Candidate } // Build policy processor from the raw policy @@ -76,7 +76,7 @@ func (bb *basicBuilder) Build(policy *lwp.Metadata, isDryRun bool) (alg.Processo return nil, errors.Wrap(err, "get action performer by metadata") } - sl := make([]res.Selector, 0) + sl := make([]art.Selector, 0) for _, s := range r.TagSelectors { sel, err := index2.Get(s.Kind, s.Decoration, s.Pattern) if err != nil { diff --git a/src/pkg/retention/policy/builder_test.go b/src/pkg/retention/policy/builder_test.go index cd12b9494..60ba74e0e 100644 --- a/src/pkg/retention/policy/builder_test.go +++ b/src/pkg/retention/policy/builder_test.go @@ -22,7 +22,7 @@ import ( index2 "github.com/goharbor/harbor/src/pkg/retention/policy/alg/index" - "github.com/goharbor/harbor/src/pkg/retention/res/selectors/index" + "github.com/goharbor/harbor/src/pkg/art/selectors/index" "github.com/goharbor/harbor/src/pkg/retention/dep" @@ -30,9 +30,9 @@ import ( "github.com/goharbor/harbor/src/pkg/retention/policy/alg/or" - "github.com/goharbor/harbor/src/pkg/retention/res/selectors/label" + "github.com/goharbor/harbor/src/pkg/art/selectors/label" - "github.com/goharbor/harbor/src/pkg/retention/res/selectors/doublestar" + "github.com/goharbor/harbor/src/pkg/art/selectors/doublestar" "github.com/goharbor/harbor/src/pkg/retention/policy/rule/latestps" @@ -46,7 +46,7 @@ import ( "github.com/goharbor/harbor/src/pkg/retention/policy/lwp" - "github.com/goharbor/harbor/src/pkg/retention/res" + "github.com/goharbor/harbor/src/pkg/art" "github.com/stretchr/testify/suite" ) @@ -55,7 +55,7 @@ import ( type TestBuilderSuite struct { suite.Suite - all []*res.Candidate + all []*art.Candidate oldClient dep.Client } @@ -66,7 +66,7 @@ func TestBuilder(t *testing.T) { // SetupSuite prepares the testing content if needed func (suite *TestBuilderSuite) SetupSuite() { - suite.all = []*res.Candidate{ + suite.all = []*art.Candidate{ { NamespaceID: 1, Namespace: "library", @@ -163,21 +163,21 @@ func (suite *TestBuilderSuite) TestBuild() { type fakeRetentionClient struct{} -func (frc *fakeRetentionClient) DeleteRepository(repo *res.Repository) error { +func (frc *fakeRetentionClient) DeleteRepository(repo *art.Repository) error { panic("implement me") } // GetCandidates ... -func (frc *fakeRetentionClient) GetCandidates(repo *res.Repository) ([]*res.Candidate, error) { +func (frc *fakeRetentionClient) GetCandidates(repo *art.Repository) ([]*art.Candidate, error) { return nil, errors.New("not implemented") } // Delete ... -func (frc *fakeRetentionClient) Delete(candidate *res.Candidate) error { +func (frc *fakeRetentionClient) Delete(candidate *art.Candidate) error { return nil } // SubmitTask ... -func (frc *fakeRetentionClient) SubmitTask(taskID int64, repository *res.Repository, meta *lwp.Metadata) (string, error) { +func (frc *fakeRetentionClient) SubmitTask(taskID int64, repository *art.Repository, meta *lwp.Metadata) (string, error) { return "", errors.New("not implemented") } diff --git a/src/pkg/retention/policy/rule/always/evaluator.go b/src/pkg/retention/policy/rule/always/evaluator.go index 1cd4f4eb4..7155b5a99 100644 --- a/src/pkg/retention/policy/rule/always/evaluator.go +++ b/src/pkg/retention/policy/rule/always/evaluator.go @@ -15,9 +15,9 @@ package always import ( + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" ) const ( @@ -28,7 +28,7 @@ const ( type evaluator struct{} // Process for the "always" Evaluator simply returns the input with no error -func (e *evaluator) Process(artifacts []*res.Candidate) ([]*res.Candidate, error) { +func (e *evaluator) Process(artifacts []*art.Candidate) ([]*art.Candidate, error) { return artifacts, nil } diff --git a/src/pkg/retention/policy/rule/always/evaluator_test.go b/src/pkg/retention/policy/rule/always/evaluator_test.go index 9e7c53b77..52bb142b9 100644 --- a/src/pkg/retention/policy/rule/always/evaluator_test.go +++ b/src/pkg/retention/policy/rule/always/evaluator_test.go @@ -17,8 +17,8 @@ package always import ( "testing" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -36,7 +36,7 @@ func (e *EvaluatorTestSuite) TestNew() { func (e *EvaluatorTestSuite) TestProcess() { sut := New(rule.Parameters{}) - input := []*res.Candidate{{PushedTime: 0}, {PushedTime: 1}, {PushedTime: 2}, {PushedTime: 3}} + input := []*art.Candidate{{PushedTime: 0}, {PushedTime: 1}, {PushedTime: 2}, {PushedTime: 3}} result, err := sut.Process(input) diff --git a/src/pkg/retention/policy/rule/dayspl/evaluator.go b/src/pkg/retention/policy/rule/dayspl/evaluator.go index 7face2b73..962555257 100644 --- a/src/pkg/retention/policy/rule/dayspl/evaluator.go +++ b/src/pkg/retention/policy/rule/dayspl/evaluator.go @@ -20,9 +20,9 @@ import ( "time" "github.com/goharbor/harbor/src/common/utils/log" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" ) const ( @@ -41,7 +41,7 @@ type evaluator struct { n int } -func (e *evaluator) Process(artifacts []*res.Candidate) (result []*res.Candidate, err error) { +func (e *evaluator) Process(artifacts []*art.Candidate) (result []*art.Candidate, err error) { minPullTime := time.Now().UTC().Add(time.Duration(-1*24*e.n) * time.Hour).Unix() for _, a := range artifacts { if a.PulledTime >= minPullTime { diff --git a/src/pkg/retention/policy/rule/dayspl/evaluator_test.go b/src/pkg/retention/policy/rule/dayspl/evaluator_test.go index 49a98d2cb..1d4c19282 100644 --- a/src/pkg/retention/policy/rule/dayspl/evaluator_test.go +++ b/src/pkg/retention/policy/rule/dayspl/evaluator_test.go @@ -20,8 +20,8 @@ import ( "testing" "time" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -54,7 +54,7 @@ func (e *EvaluatorTestSuite) TestNew() { func (e *EvaluatorTestSuite) TestProcess() { now := time.Now().UTC() - data := []*res.Candidate{ + data := []*art.Candidate{ {PulledTime: daysAgo(now, 1)}, {PulledTime: daysAgo(now, 2)}, {PulledTime: daysAgo(now, 3)}, diff --git a/src/pkg/retention/policy/rule/daysps/evaluator.go b/src/pkg/retention/policy/rule/daysps/evaluator.go index 58cf73d2b..58ee79a57 100644 --- a/src/pkg/retention/policy/rule/daysps/evaluator.go +++ b/src/pkg/retention/policy/rule/daysps/evaluator.go @@ -20,9 +20,9 @@ import ( "time" "github.com/goharbor/harbor/src/common/utils/log" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" ) const ( @@ -41,7 +41,7 @@ type evaluator struct { n int } -func (e *evaluator) Process(artifacts []*res.Candidate) (result []*res.Candidate, err error) { +func (e *evaluator) Process(artifacts []*art.Candidate) (result []*art.Candidate, err error) { minPushTime := time.Now().UTC().Add(time.Duration(-1*24*e.n) * time.Hour).Unix() for _, a := range artifacts { if a.PushedTime >= minPushTime { diff --git a/src/pkg/retention/policy/rule/daysps/evaluator_test.go b/src/pkg/retention/policy/rule/daysps/evaluator_test.go index a40c2c5a2..7d4e6995c 100644 --- a/src/pkg/retention/policy/rule/daysps/evaluator_test.go +++ b/src/pkg/retention/policy/rule/daysps/evaluator_test.go @@ -20,8 +20,8 @@ import ( "testing" "time" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -54,7 +54,7 @@ func (e *EvaluatorTestSuite) TestNew() { func (e *EvaluatorTestSuite) TestProcess() { now := time.Now().UTC() - data := []*res.Candidate{ + data := []*art.Candidate{ {PushedTime: daysAgo(now, 1)}, {PushedTime: daysAgo(now, 2)}, {PushedTime: daysAgo(now, 3)}, diff --git a/src/pkg/retention/policy/rule/evaluator.go b/src/pkg/retention/policy/rule/evaluator.go index 18e641986..f36d0f344 100644 --- a/src/pkg/retention/policy/rule/evaluator.go +++ b/src/pkg/retention/policy/rule/evaluator.go @@ -14,19 +14,19 @@ package rule -import "github.com/goharbor/harbor/src/pkg/retention/res" +import "github.com/goharbor/harbor/src/pkg/art" // Evaluator defines method of executing rule type Evaluator interface { // Filter the inputs and return the filtered outputs // // Arguments: - // artifacts []*res.Candidate : candidates for processing + // artifacts []*art.Candidate : candidates for processing // // Returns: - // []*res.Candidate : matched candidates for next stage + // []*art.Candidate : matched candidates for next stage // error : common error object if any errors occurred - Process(artifacts []*res.Candidate) ([]*res.Candidate, error) + Process(artifacts []*art.Candidate) ([]*art.Candidate, error) // Specify what action is performed to the candidates processed by this evaluator Action() string diff --git a/src/pkg/retention/policy/rule/index/index_test.go b/src/pkg/retention/policy/rule/index/index_test.go index fd8268f18..9f2f1f3ef 100644 --- a/src/pkg/retention/policy/rule/index/index_test.go +++ b/src/pkg/retention/policy/rule/index/index_test.go @@ -22,8 +22,8 @@ import ( "github.com/stretchr/testify/require" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/suite" ) @@ -63,7 +63,7 @@ func (suite *IndexTestSuite) TestGet() { require.NoError(suite.T(), err) require.NotNil(suite.T(), evaluator) - candidates := []*res.Candidate{{ + candidates := []*art.Candidate{{ Namespace: "library", Repository: "harbor", Kind: "image", @@ -102,7 +102,7 @@ type fakeEvaluator struct { } // Process rule -func (e *fakeEvaluator) Process(artifacts []*res.Candidate) ([]*res.Candidate, error) { +func (e *fakeEvaluator) Process(artifacts []*art.Candidate) ([]*art.Candidate, error) { return artifacts, nil } diff --git a/src/pkg/retention/policy/rule/lastx/evaluator.go b/src/pkg/retention/policy/rule/lastx/evaluator.go index 7457c0db3..ad0447f65 100644 --- a/src/pkg/retention/policy/rule/lastx/evaluator.go +++ b/src/pkg/retention/policy/rule/lastx/evaluator.go @@ -19,9 +19,9 @@ import ( "time" "github.com/goharbor/harbor/src/common/utils/log" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" ) const ( @@ -40,7 +40,7 @@ type evaluator struct { } // Process the candidates based on the rule definition -func (e *evaluator) Process(artifacts []*res.Candidate) (retain []*res.Candidate, err error) { +func (e *evaluator) Process(artifacts []*art.Candidate) (retain []*art.Candidate, err error) { cutoff := time.Now().Add(time.Duration(e.x*-24) * time.Hour) for _, a := range artifacts { if time.Unix(a.PushedTime, 0).UTC().After(cutoff) { diff --git a/src/pkg/retention/policy/rule/lastx/evaluator_test.go b/src/pkg/retention/policy/rule/lastx/evaluator_test.go index becd79234..ee42425e8 100644 --- a/src/pkg/retention/policy/rule/lastx/evaluator_test.go +++ b/src/pkg/retention/policy/rule/lastx/evaluator_test.go @@ -5,8 +5,8 @@ import ( "testing" "time" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -38,7 +38,7 @@ func (e *EvaluatorTestSuite) TestNew() { func (e *EvaluatorTestSuite) TestProcess() { now := time.Now().UTC() - data := []*res.Candidate{ + data := []*art.Candidate{ {PushedTime: now.Add(time.Duration(1*-24) * time.Hour).Unix()}, {PushedTime: now.Add(time.Duration(2*-24) * time.Hour).Unix()}, {PushedTime: now.Add(time.Duration(3*-24) * time.Hour).Unix()}, diff --git a/src/pkg/retention/policy/rule/latestk/evaluator.go b/src/pkg/retention/policy/rule/latestk/evaluator.go index 9f9610f55..3405841cc 100644 --- a/src/pkg/retention/policy/rule/latestk/evaluator.go +++ b/src/pkg/retention/policy/rule/latestk/evaluator.go @@ -19,9 +19,9 @@ import ( "sort" "github.com/goharbor/harbor/src/common/utils/log" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" ) const ( @@ -40,7 +40,7 @@ type evaluator struct { } // Process the candidates based on the rule definition -func (e *evaluator) Process(artifacts []*res.Candidate) ([]*res.Candidate, error) { +func (e *evaluator) Process(artifacts []*art.Candidate) ([]*art.Candidate, error) { // Sort artifacts by their "active time" // // Active time is defined as the selection of c.PulledTime or c.PushedTime, @@ -81,7 +81,7 @@ func New(params rule.Parameters) rule.Evaluator { } } -func activeTime(c *res.Candidate) int64 { +func activeTime(c *art.Candidate) int64 { if c.PulledTime > c.PushedTime { return c.PulledTime } diff --git a/src/pkg/retention/policy/rule/latestk/evaluator_test.go b/src/pkg/retention/policy/rule/latestk/evaluator_test.go index 24b04fb9e..2fb09a5b9 100644 --- a/src/pkg/retention/policy/rule/latestk/evaluator_test.go +++ b/src/pkg/retention/policy/rule/latestk/evaluator_test.go @@ -22,18 +22,18 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/goharbor/harbor/src/pkg/retention/res" + "github.com/goharbor/harbor/src/pkg/art" "github.com/stretchr/testify/suite" ) type EvaluatorTestSuite struct { suite.Suite - artifacts []*res.Candidate + artifacts []*art.Candidate } func (e *EvaluatorTestSuite) SetupSuite() { - e.artifacts = []*res.Candidate{ + e.artifacts = []*art.Candidate{ {PulledTime: 1, PushedTime: 2}, {PulledTime: 3, PushedTime: 4}, {PulledTime: 6, PushedTime: 5}, diff --git a/src/pkg/retention/policy/rule/latestpl/evaluator.go b/src/pkg/retention/policy/rule/latestpl/evaluator.go index 21381759c..7f63a3896 100644 --- a/src/pkg/retention/policy/rule/latestpl/evaluator.go +++ b/src/pkg/retention/policy/rule/latestpl/evaluator.go @@ -21,9 +21,9 @@ import ( "sort" "github.com/goharbor/harbor/src/common/utils/log" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" ) const ( @@ -41,7 +41,7 @@ type evaluator struct { n int } -func (e *evaluator) Process(artifacts []*res.Candidate) ([]*res.Candidate, error) { +func (e *evaluator) Process(artifacts []*art.Candidate) ([]*art.Candidate, error) { sort.Slice(artifacts, func(i, j int) bool { return artifacts[i].PulledTime > artifacts[j].PulledTime }) diff --git a/src/pkg/retention/policy/rule/latestpl/evaluator_test.go b/src/pkg/retention/policy/rule/latestpl/evaluator_test.go index ebd1679ae..1c33b94ea 100644 --- a/src/pkg/retention/policy/rule/latestpl/evaluator_test.go +++ b/src/pkg/retention/policy/rule/latestpl/evaluator_test.go @@ -20,8 +20,8 @@ import ( "math/rand" "testing" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -52,7 +52,7 @@ func (e *EvaluatorTestSuite) TestNew() { } func (e *EvaluatorTestSuite) TestProcess() { - data := []*res.Candidate{{PulledTime: 0}, {PulledTime: 1}, {PulledTime: 2}, {PulledTime: 3}, {PulledTime: 4}} + data := []*art.Candidate{{PulledTime: 0}, {PulledTime: 1}, {PulledTime: 2}, {PulledTime: 3}, {PulledTime: 4}} rand.Shuffle(len(data), func(i, j int) { data[i], data[j] = data[j], data[i] }) diff --git a/src/pkg/retention/policy/rule/latestps/evaluator.go b/src/pkg/retention/policy/rule/latestps/evaluator.go index f672aa1c6..96a6bf19a 100644 --- a/src/pkg/retention/policy/rule/latestps/evaluator.go +++ b/src/pkg/retention/policy/rule/latestps/evaluator.go @@ -21,9 +21,9 @@ import ( "sort" "github.com/goharbor/harbor/src/common/utils/log" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" ) const ( @@ -42,7 +42,7 @@ type evaluator struct { } // Process the candidates based on the rule definition -func (e *evaluator) Process(artifacts []*res.Candidate) ([]*res.Candidate, error) { +func (e *evaluator) Process(artifacts []*art.Candidate) ([]*art.Candidate, error) { // The updated proposal does not guarantee the order artifacts are provided, so we have to sort them first sort.Slice(artifacts, func(i, j int) bool { return artifacts[i].PushedTime > artifacts[j].PushedTime diff --git a/src/pkg/retention/policy/rule/latestps/evaluator_test.go b/src/pkg/retention/policy/rule/latestps/evaluator_test.go index 38fa64570..a0e727c08 100644 --- a/src/pkg/retention/policy/rule/latestps/evaluator_test.go +++ b/src/pkg/retention/policy/rule/latestps/evaluator_test.go @@ -8,8 +8,8 @@ import ( "github.com/stretchr/testify/suite" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/require" ) @@ -39,7 +39,7 @@ func (e *EvaluatorTestSuite) TestNew() { } func (e *EvaluatorTestSuite) TestProcess() { - data := []*res.Candidate{{PushedTime: 0}, {PushedTime: 1}, {PushedTime: 2}, {PushedTime: 3}, {PushedTime: 4}} + data := []*art.Candidate{{PushedTime: 0}, {PushedTime: 1}, {PushedTime: 2}, {PushedTime: 3}, {PushedTime: 4}} rand.Shuffle(len(data), func(i, j int) { data[i], data[j] = data[j], data[i] }) diff --git a/src/pkg/retention/policy/rule/nothing/evaluator.go b/src/pkg/retention/policy/rule/nothing/evaluator.go index 8bc4b9063..f926c20c7 100644 --- a/src/pkg/retention/policy/rule/nothing/evaluator.go +++ b/src/pkg/retention/policy/rule/nothing/evaluator.go @@ -15,9 +15,9 @@ package nothing import ( + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/action" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" ) const ( @@ -28,7 +28,7 @@ const ( type evaluator struct{} // Process for the "nothing" Evaluator simply returns the input with no error -func (e *evaluator) Process(artifacts []*res.Candidate) (processed []*res.Candidate, err error) { +func (e *evaluator) Process(artifacts []*art.Candidate) (processed []*art.Candidate, err error) { return processed, err } diff --git a/src/pkg/retention/policy/rule/nothing/evaluator_test.go b/src/pkg/retention/policy/rule/nothing/evaluator_test.go index 1432db651..db4cea68a 100644 --- a/src/pkg/retention/policy/rule/nothing/evaluator_test.go +++ b/src/pkg/retention/policy/rule/nothing/evaluator_test.go @@ -17,8 +17,8 @@ package nothing import ( "testing" + "github.com/goharbor/harbor/src/pkg/art" "github.com/goharbor/harbor/src/pkg/retention/policy/rule" - "github.com/goharbor/harbor/src/pkg/retention/res" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -36,7 +36,7 @@ func (e *EvaluatorTestSuite) TestNew() { func (e *EvaluatorTestSuite) TestProcess() { sut := New(rule.Parameters{}) - input := []*res.Candidate{{PushedTime: 0}, {PushedTime: 1}, {PushedTime: 2}, {PushedTime: 3}} + input := []*art.Candidate{{PushedTime: 0}, {PushedTime: 1}, {PushedTime: 2}, {PushedTime: 3}} result, err := sut.Process(input)