Merge pull request #12698 from kofj/fix_p2p_policy_delete

[FIX] Delete executions of the P2P policy.
This commit is contained in:
Steven Zou 2020-08-17 22:18:16 +08:00 committed by GitHub
commit 3364f76d99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 21 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/goharbor/harbor/src/pkg/p2p/preheat/policy" "github.com/goharbor/harbor/src/pkg/p2p/preheat/policy"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider" "github.com/goharbor/harbor/src/pkg/p2p/preheat/provider"
"github.com/goharbor/harbor/src/pkg/scheduler" "github.com/goharbor/harbor/src/pkg/scheduler"
"github.com/goharbor/harbor/src/pkg/task"
) )
const ( const (
@ -126,16 +127,18 @@ type controller struct {
// For instance // For instance
iManager instance.Manager iManager instance.Manager
// For policy // For policy
pManager policy.Manager pManager policy.Manager
scheduler scheduler.Scheduler scheduler scheduler.Scheduler
executionMgr task.ExecutionManager
} }
// NewController is constructor of controller // NewController is constructor of controller
func NewController() Controller { func NewController() Controller {
return &controller{ return &controller{
iManager: instance.Mgr, iManager: instance.Mgr,
pManager: policy.Mgr, pManager: policy.Mgr,
scheduler: scheduler.Sched, scheduler: scheduler.Sched,
executionMgr: task.NewExecutionManager(),
} }
} }
@ -341,8 +344,7 @@ func (c *controller) UpdatePolicy(ctx context.Context, schema *policyModels.Sche
} }
} else { } else {
// not change trigger type // not change trigger type
if schema.Trigger.Type == policyModels.TriggerTypeScheduled && if schema.Trigger.Type == policyModels.TriggerTypeScheduled && oldCron != cron {
s0.Trigger.Settings.Cron != cron {
// unschedule old // unschedule old
if len(oldCron) > 0 { if len(oldCron) > 0 {
needUn = true needUn = true
@ -370,11 +372,6 @@ func (c *controller) UpdatePolicy(ctx context.Context, schema *policyModels.Sche
TriggerParam{PolicyID: schema.ID}); err != nil { TriggerParam{PolicyID: schema.ID}); err != nil {
return err return err
} }
if err := schema.Encode(); err != nil {
// Possible
// TODO: Refactor
return err // whether update or not has been not important as the jon reference will be lost
}
} }
// Update timestamp // Update timestamp
@ -396,9 +393,35 @@ func (c *controller) DeletePolicy(ctx context.Context, id int64) error {
} }
} }
if err = c.deleteExecs(ctx, id); err != nil {
return err
}
return c.pManager.Delete(ctx, id) return c.pManager.Delete(ctx, id)
} }
// deleteExecs delete executions
func (c *controller) deleteExecs(ctx context.Context, vendorID int64) error {
executions, err := c.executionMgr.List(ctx, &q.Query{
Keywords: map[string]interface{}{
"VendorType": job.P2PPreheat,
"VendorID": vendorID,
},
})
if err != nil {
return err
}
for _, execution := range executions {
if err = c.executionMgr.Delete(ctx, execution.ID); err != nil {
return err
}
}
return nil
}
// ListPolicies lists policies by query. // ListPolicies lists policies by query.
func (c *controller) ListPolicies(ctx context.Context, query *q.Query) ([]*policyModels.Schema, error) { func (c *controller) ListPolicies(ctx context.Context, query *q.Query) ([]*policyModels.Schema, error) {
return c.pManager.ListPolicies(ctx, query) return c.pManager.ListPolicies(ctx, query)

View File

@ -15,10 +15,12 @@ import (
providerModel "github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider" providerModel "github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider" "github.com/goharbor/harbor/src/pkg/p2p/preheat/provider"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider/auth" "github.com/goharbor/harbor/src/pkg/p2p/preheat/provider/auth"
taskModel "github.com/goharbor/harbor/src/pkg/task"
ormtesting "github.com/goharbor/harbor/src/testing/lib/orm" ormtesting "github.com/goharbor/harbor/src/testing/lib/orm"
"github.com/goharbor/harbor/src/testing/pkg/p2p/preheat/instance" "github.com/goharbor/harbor/src/testing/pkg/p2p/preheat/instance"
pmocks "github.com/goharbor/harbor/src/testing/pkg/p2p/preheat/policy" pmocks "github.com/goharbor/harbor/src/testing/pkg/p2p/preheat/policy"
smocks "github.com/goharbor/harbor/src/testing/pkg/scheduler" smocks "github.com/goharbor/harbor/src/testing/pkg/scheduler"
tmocks "github.com/goharbor/harbor/src/testing/pkg/task"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -32,6 +34,7 @@ type preheatSuite struct {
fakePolicyMgr *pmocks.FakeManager fakePolicyMgr *pmocks.FakeManager
fakeScheduler *smocks.Scheduler fakeScheduler *smocks.Scheduler
mockInstanceServer *httptest.Server mockInstanceServer *httptest.Server
fakeExecutionMgr *tmocks.FakeExecutionManager
} }
func TestPreheatSuite(t *testing.T) { func TestPreheatSuite(t *testing.T) {
@ -39,21 +42,24 @@ func TestPreheatSuite(t *testing.T) {
fakeInstanceMgr := &instance.FakeManager{} fakeInstanceMgr := &instance.FakeManager{}
fakePolicyMgr := &pmocks.FakeManager{} fakePolicyMgr := &pmocks.FakeManager{}
fakeScheduler := &smocks.Scheduler{} fakeScheduler := &smocks.Scheduler{}
fakeExecutionMgr := &tmocks.FakeExecutionManager{}
var c = &controller{ var c = &controller{
iManager: fakeInstanceMgr, iManager: fakeInstanceMgr,
pManager: fakePolicyMgr, pManager: fakePolicyMgr,
scheduler: fakeScheduler, scheduler: fakeScheduler,
executionMgr: fakeExecutionMgr,
} }
assert.NotNil(t, c) assert.NotNil(t, c)
ctx := orm.NewContext(context.TODO(), &ormtesting.FakeOrmer{}) ctx := orm.NewContext(context.TODO(), &ormtesting.FakeOrmer{})
suite.Run(t, &preheatSuite{ suite.Run(t, &preheatSuite{
ctx: ctx, ctx: ctx,
controller: c, controller: c,
fakeInstanceMgr: fakeInstanceMgr, fakeInstanceMgr: fakeInstanceMgr,
fakePolicyMgr: fakePolicyMgr, fakePolicyMgr: fakePolicyMgr,
fakeScheduler: fakeScheduler, fakeScheduler: fakeScheduler,
fakeExecutionMgr: fakeExecutionMgr,
}) })
} }
@ -291,6 +297,13 @@ func (s *preheatSuite) TestUpdatePolicy() {
func (s *preheatSuite) TestDeletePolicy() { func (s *preheatSuite) TestDeletePolicy() {
var p0 = &policy.Schema{Name: "test", Trigger: &policy.Trigger{Type: policy.TriggerTypeScheduled}} var p0 = &policy.Schema{Name: "test", Trigger: &policy.Trigger{Type: policy.TriggerTypeScheduled}}
s.fakePolicyMgr.On("Get", s.ctx, int64(1)).Return(p0, nil) s.fakePolicyMgr.On("Get", s.ctx, int64(1)).Return(p0, nil)
s.fakeExecutionMgr.On("List", s.ctx, mock.AnythingOfType("*q.Query")).Return(
[]*taskModel.Execution{
{ID: 1},
{ID: 2},
}, nil,
)
s.fakeExecutionMgr.On("Delete", mock.Anything, mock.Anything).Return(nil)
s.fakePolicyMgr.On("Delete", s.ctx, int64(1)).Return(nil) s.fakePolicyMgr.On("Delete", s.ctx, int64(1)).Return(nil)
err := s.controller.DeletePolicy(s.ctx, 1) err := s.controller.DeletePolicy(s.ctx, 1)
s.NoError(err) s.NoError(err)

View File

@ -35,7 +35,7 @@ type Manager interface {
Update(ctx context.Context, schema *policy.Schema, props ...string) (err error) Update(ctx context.Context, schema *policy.Schema, props ...string) (err error)
// Get the policy schema by id // Get the policy schema by id
Get(ctx context.Context, id int64) (schema *policy.Schema, err error) Get(ctx context.Context, id int64) (schema *policy.Schema, err error)
// GetByName the policy schema by id // GetByName the policy schema by project ID and name
GetByName(ctx context.Context, projectID int64, name string) (schema *policy.Schema, err error) GetByName(ctx context.Context, projectID int64, name string) (schema *policy.Schema, err error)
// Delete the policy schema by id // Delete the policy schema by id
Delete(ctx context.Context, id int64) (err error) Delete(ctx context.Context, id int64) (err error)