diff --git a/src/pkg/retention/job.go b/src/pkg/retention/job.go index bce40e427..5f3254ac8 100644 --- a/src/pkg/retention/job.go +++ b/src/pkg/retention/job.go @@ -20,10 +20,9 @@ import ( "strings" "time" - "github.com/goharbor/harbor/src/pkg/retention/dep" - "github.com/goharbor/harbor/src/jobservice/job" "github.com/goharbor/harbor/src/jobservice/logger" + "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" @@ -215,11 +214,17 @@ func getParamRepo(params job.Parameters) (*res.Repository, error) { return nil, errors.Errorf("missing parameter: %s", ParamRepo) } - repo, ok := v.(*res.Repository) + fmt.Printf("%T", v) + repoMap, ok := v.(map[string]interface{}) if !ok { return nil, errors.Errorf("invalid parameter: %s", ParamRepo) } + repo := &res.Repository{} + if err := repo.FromMap(repoMap); err != nil { + return nil, fmt.Errorf("failed to convert map to repository: %v", err) + } + return repo, nil } @@ -229,10 +234,15 @@ func getParamMeta(params job.Parameters) (*lwp.Metadata, error) { return nil, errors.Errorf("missing parameter: %s", ParamMeta) } - meta, ok := v.(*lwp.Metadata) + metaMap, ok := v.(map[string]interface{}) if !ok { return nil, errors.Errorf("invalid parameter: %s", ParamMeta) } + meta := &lwp.Metadata{} + if err := meta.FromMap(metaMap); err != nil { + return nil, fmt.Errorf("failed to convert map to metadata: %v", err) + } + return meta, nil } diff --git a/src/pkg/retention/job_test.go b/src/pkg/retention/job_test.go index 70a690687..56902b2a5 100644 --- a/src/pkg/retention/job_test.go +++ b/src/pkg/retention/job_test.go @@ -16,6 +16,7 @@ package retention import ( "context" + "encoding/json" "errors" "fmt" "testing" @@ -77,11 +78,14 @@ func (suite *JobTestSuite) TearDownSuite() { func (suite *JobTestSuite) TestRunSuccess() { params := make(job.Parameters) params[ParamDryRun] = false - params[ParamRepo] = &res.Repository{ + repository := &res.Repository{ Namespace: "library", Name: "harbor", Kind: res.Image, } + repoMap, err := toMap(repository) + require.Nil(suite.T(), err) + params[ParamRepo] = repoMap scopeSelectors := make(map[string][]*rule.Selector) scopeSelectors["project"] = []*rule.Selector{{ @@ -93,7 +97,7 @@ func (suite *JobTestSuite) TestRunSuccess() { ruleParams := make(rule.Parameters) ruleParams[latestk.ParameterK] = 10 - params[ParamMeta] = &lwp.Metadata{ + meta := &lwp.Metadata{ Algorithm: policy.AlgorithmOR, Rules: []*rule.Metadata{ { @@ -115,9 +119,12 @@ func (suite *JobTestSuite) TestRunSuccess() { }, }, } + metaMap, err := toMap(meta) + require.Nil(suite.T(), err) + params[ParamMeta] = metaMap j := &Job{} - err := j.Validate(params) + err = j.Validate(params) require.NoError(suite.T(), err) err = j.Run(&fakeJobContext{}, params) @@ -232,3 +239,15 @@ func (c *fakeJobContext) GetLogger() logger.Interface { func (c *fakeJobContext) Tracker() job.Tracker { return nil } + +func toMap(v interface{}) (map[string]interface{}, error) { + data, err := json.Marshal(v) + if err != nil { + return nil, err + } + m := map[string]interface{}{} + if err = json.Unmarshal(data, &m); err != nil { + return nil, err + } + return m, nil +} diff --git a/src/pkg/retention/launcher.go b/src/pkg/retention/launcher.go index 92006ce63..8d5c13265 100644 --- a/src/pkg/retention/launcher.go +++ b/src/pkg/retention/launcher.go @@ -205,7 +205,7 @@ func (l *launcher) Launch(ply *policy.Metadata, executionID int64, isDryRun bool Metadata: &models.JobMetadata{ JobKind: job.KindGeneric, }, - StatusHook: fmt.Sprintf("%s/service/notifications/jobs/retention/tasks/%d", l.internalCoreURL, jobData.taskID), + StatusHook: fmt.Sprintf("%s/service/notifications/jobs/retention/task/%d", l.internalCoreURL, jobData.taskID), } j.Name = job.Retention j.Parameters = map[string]interface{}{ diff --git a/src/pkg/retention/policy/lwp/models.go b/src/pkg/retention/policy/lwp/models.go index 4066e68b5..9a0c80ba6 100644 --- a/src/pkg/retention/policy/lwp/models.go +++ b/src/pkg/retention/policy/lwp/models.go @@ -15,7 +15,11 @@ // Package lwp = lightweight policy package lwp -import "github.com/goharbor/harbor/src/pkg/retention/policy/rule" +import ( + "encoding/json" + + "github.com/goharbor/harbor/src/pkg/retention/policy/rule" +) // Metadata contains partial metadata of policy // It's a lightweight version of policy.Metadata @@ -27,3 +31,15 @@ type Metadata struct { // Rule collection Rules []*rule.Metadata `json:"rules"` } + +// FromMap constructs the metadata struct from map +func (meta *Metadata) FromMap(m map[string]interface{}) error { + mdata, err := json.Marshal(&m) + if err != nil { + return err + } + if err := json.Unmarshal(mdata, meta); err != nil { + return err + } + return nil +} diff --git a/src/pkg/retention/res/candidate.go b/src/pkg/retention/res/candidate.go index 4e177cb85..002b73892 100644 --- a/src/pkg/retention/res/candidate.go +++ b/src/pkg/retention/res/candidate.go @@ -16,6 +16,7 @@ package res import ( "encoding/base64" + "encoding/json" "fmt" ) @@ -37,6 +38,18 @@ type Repository struct { Kind string } +// FromMap constructs the repository struct from map +func (r *Repository) FromMap(m map[string]interface{}) error { + mdata, err := json.Marshal(&m) + if err != nil { + return err + } + if err := json.Unmarshal(mdata, r); err != nil { + return err + } + return nil +} + // Candidate for retention processor to match type Candidate struct { // Namespace(project) ID diff --git a/src/pkg/retention/res/selectors/doublestar/selector.go b/src/pkg/retention/res/selectors/doublestar/selector.go index 1a2051338..fcbb628b9 100644 --- a/src/pkg/retention/res/selectors/doublestar/selector.go +++ b/src/pkg/retention/res/selectors/doublestar/selector.go @@ -17,7 +17,6 @@ package doublestar import ( "github.com/bmatcuk/doublestar" "github.com/goharbor/harbor/src/pkg/retention/res" - "github.com/goharbor/harbor/src/pkg/retention/res/selectors" ) const ( @@ -101,15 +100,3 @@ func match(pattern, str string) (bool, error) { } return doublestar.Match(pattern, str) } - -func init() { - // Register doublestar selector - selectors.Register(Kind, []string{ - Matches, - Excludes, - RepoMatches, - RepoExcludes, - NSMatches, - NSExcludes, - }, New) -} diff --git a/src/pkg/retention/res/selectors/index.go b/src/pkg/retention/res/selectors/index.go index 073a7ca25..06a86293b 100644 --- a/src/pkg/retention/res/selectors/index.go +++ b/src/pkg/retention/res/selectors/index.go @@ -15,11 +15,25 @@ package selectors import ( - "github.com/goharbor/harbor/src/pkg/retention/res" - "github.com/pkg/errors" "sync" + + "github.com/goharbor/harbor/src/pkg/retention/res" + "github.com/goharbor/harbor/src/pkg/retention/res/selectors/doublestar" + "github.com/pkg/errors" ) +func init() { + // Register doublestar selector + Register(doublestar.Kind, []string{ + doublestar.Matches, + doublestar.Excludes, + doublestar.RepoMatches, + doublestar.RepoExcludes, + doublestar.NSMatches, + doublestar.NSExcludes, + }, doublestar.New) +} + // index for keeping the mapping between selector meta and its implementation var index sync.Map