Merge pull request #8457 from bitsf/tag_retention_api_fix

fix tag retention api bug
This commit is contained in:
Wenkai Yin(尹文开) 2019-07-30 10:24:28 +08:00 committed by GitHub
commit b90874b06a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 19 deletions

View File

@ -41,16 +41,10 @@ type RetentionExecution struct {
PolicyID int64 `orm:"column(policy_id)"`
DryRun bool
// manual, scheduled
Trigger string
StartTime time.Time
EndTime time.Time `orm:"-"`
Status string `orm:"-"`
Total int `orm:"-"`
Succeed int `orm:"-"`
Failed int `orm:"-"`
InProgress int `orm:"-"`
Stopped int `orm:"-"`
Pending int `orm:"-"`
Trigger string
StartTime time.Time
EndTime time.Time `orm:"-"`
Status string `orm:"-"`
}
// RetentionTask ...

View File

@ -3,7 +3,9 @@ package dao
import (
"errors"
"fmt"
"github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/common/dao"
jobmodels "github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/pkg/retention/dao/models"
"github.com/goharbor/harbor/src/pkg/retention/q"
)
@ -93,21 +95,45 @@ func GetExecution(id int64) (*models.RetentionExecution, error) {
// fillStatus the priority is InProgress Stopped Failed Succeed
func fillStatus(exec *models.RetentionExecution) error {
o := dao.GetOrmer()
var r orm.Params
if _, err := o.Raw("select status, count(*) num from retention_task where execution_id = ? group by status", exec.ID).
RowsToStruct(exec, "status", "num"); err != nil {
RowsToMap(&r, "status", "num"); err != nil {
return err
}
exec.Total = exec.Pending + exec.InProgress + exec.Succeed + exec.Failed + exec.Stopped
if exec.Total == 0 {
var (
total, running, succeed, failed, stopped int
)
for k, v := range r {
total += v.(int)
switch k {
case jobmodels.JobScheduled:
running += v.(int)
case jobmodels.JobPending:
running += v.(int)
case jobmodels.JobRunning:
running += v.(int)
case jobmodels.JobRetrying:
running += v.(int)
case jobmodels.JobFinished:
succeed += v.(int)
case jobmodels.JobCanceled:
stopped += v.(int)
case jobmodels.JobStopped:
stopped += v.(int)
case jobmodels.JobError:
failed += v.(int)
}
}
if total == 0 {
exec.Status = models.ExecutionStatusSucceed
exec.EndTime = exec.StartTime
return nil
}
if exec.Pending+exec.InProgress > 0 {
if running > 0 {
exec.Status = models.ExecutionStatusInProgress
} else if exec.Stopped > 0 {
} else if stopped > 0 {
exec.Status = models.ExecutionStatusStopped
} else if exec.Failed > 0 {
} else if failed > 0 {
exec.Status = models.ExecutionStatusFailed
} else {
exec.Status = models.ExecutionStatusSucceed
@ -127,6 +153,7 @@ func ListExecutions(policyID int64, query *q.Query) ([]*models.RetentionExecutio
qs := o.QueryTable(new(models.RetentionExecution))
qs = qs.Filter("policy_id", policyID)
qs = qs.OrderBy("-id")
if query != nil {
qs = qs.Limit(query.PageSize, (query.PageNumber-1)*query.PageSize)
}

View File

@ -159,10 +159,8 @@ func TestExecution(t *testing.T) {
e := &models.RetentionExecution{
PolicyID: policyID,
Status: "Running",
DryRun: false,
Trigger: "manual",
Total: 10,
StartTime: time.Now(),
}
id, err := CreateExecution(e)

View File

@ -66,6 +66,7 @@ type DefaultManager struct {
func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) {
p1 := &models.RetentionPolicy{}
p1.ScopeLevel = p.Scope.Level
p1.ScopeReference = p.Scope.Reference
p1.TriggerKind = p.Trigger.Kind
data, _ := json.Marshal(p)
p1.Data = string(data)
@ -79,6 +80,7 @@ func (d *DefaultManager) UpdatePolicy(p *policy.Metadata) error {
p1 := &models.RetentionPolicy{}
p1.ID = p.ID
p1.ScopeLevel = p.Scope.Level
p1.ScopeReference = p.Scope.Reference
p1.TriggerKind = p.Trigger.Kind
p.ID = 0
data, _ := json.Marshal(p)
@ -142,6 +144,7 @@ func (d *DefaultManager) ListExecutions(policyID int64, query *q.Query) ([]*Exec
e1.Status = e.Status
e1.StartTime = e.StartTime
e1.EndTime = e.EndTime
e1.DryRun = e.DryRun
execs1 = append(execs1, e1)
}
return execs1, nil
@ -159,6 +162,7 @@ func (d *DefaultManager) GetExecution(eid int64) (*Execution, error) {
e1.Status = e.Status
e1.StartTime = e.StartTime
e1.EndTime = e.EndTime
e1.DryRun = e.DryRun
return e1, nil
}

View File

@ -59,7 +59,7 @@ type Metadata struct {
// Valid Valid
func (m *Metadata) Valid(v *validation.Validation) {
if m.Trigger.Kind == TriggerKindSchedule {
if m.Trigger != nil && m.Trigger.Kind == TriggerKindSchedule {
if m.Trigger.Settings == nil {
_ = v.SetError("Trigger.Settings", "Trigger.Settings is required")
} else {