Fix bugs found in running the retention workflow

Fix bugs found in running the retention workflow

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2019-07-27 11:39:17 +08:00
parent 3a7066346b
commit ab792019db
7 changed files with 83 additions and 24 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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