mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-06 16:08:29 +01:00
Manual starting replication will be rejected if there are pending/running jobs
This commit is contained in:
parent
7639834c96
commit
87ce1c84d5
@ -1139,7 +1139,7 @@ func TestGetRepJobByPolicy(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterRepJobs(t *testing.T) {
|
func TestFilterRepJobs(t *testing.T) {
|
||||||
jobs, _, err := FilterRepJobs(policyID, "", "", nil, nil, 1000, 0)
|
jobs, _, err := FilterRepJobs(policyID, "", []string{}, nil, nil, 1000, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Error occurred in FilterRepJobs: %v, policy ID: %d", err, policyID)
|
t.Errorf("Error occurred in FilterRepJobs: %v, policy ID: %d", err, policyID)
|
||||||
return
|
return
|
||||||
|
@ -291,7 +291,7 @@ func GetRepJobByPolicy(policyID int64) ([]*models.RepJob, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FilterRepJobs ...
|
// FilterRepJobs ...
|
||||||
func FilterRepJobs(policyID int64, repository, status string, startTime,
|
func FilterRepJobs(policyID int64, repository string, status []string, startTime,
|
||||||
endTime *time.Time, limit, offset int64) ([]*models.RepJob, int64, error) {
|
endTime *time.Time, limit, offset int64) ([]*models.RepJob, int64, error) {
|
||||||
|
|
||||||
jobs := []*models.RepJob{}
|
jobs := []*models.RepJob{}
|
||||||
@ -305,7 +305,7 @@ func FilterRepJobs(policyID int64, repository, status string, startTime,
|
|||||||
qs = qs.Filter("Repository__icontains", repository)
|
qs = qs.Filter("Repository__icontains", repository)
|
||||||
}
|
}
|
||||||
if len(status) != 0 {
|
if len(status) != 0 {
|
||||||
qs = qs.Filter("Status__icontains", status)
|
qs = qs.Filter("Status__in", status)
|
||||||
}
|
}
|
||||||
if startTime != nil {
|
if startTime != nil {
|
||||||
qs = qs.Filter("CreationTime__gte", startTime)
|
qs = qs.Filter("CreationTime__gte", startTime)
|
||||||
|
@ -16,13 +16,16 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/vmware/harbor/src/common/dao"
|
||||||
|
"github.com/vmware/harbor/src/common/models"
|
||||||
"github.com/vmware/harbor/src/common/notifier"
|
"github.com/vmware/harbor/src/common/notifier"
|
||||||
"github.com/vmware/harbor/src/common/utils/log"
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
"github.com/vmware/harbor/src/replication/core"
|
"github.com/vmware/harbor/src/replication/core"
|
||||||
"github.com/vmware/harbor/src/replication/event/notification"
|
"github.com/vmware/harbor/src/replication/event/notification"
|
||||||
"github.com/vmware/harbor/src/replication/event/topic"
|
"github.com/vmware/harbor/src/replication/event/topic"
|
||||||
"github.com/vmware/harbor/src/ui/api/models"
|
api_models "github.com/vmware/harbor/src/ui/api/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ReplicationAPI handles API calls for replication
|
// ReplicationAPI handles API calls for replication
|
||||||
@ -46,7 +49,7 @@ func (r *ReplicationAPI) Prepare() {
|
|||||||
|
|
||||||
// Post trigger a replication according to the specified policy
|
// Post trigger a replication according to the specified policy
|
||||||
func (r *ReplicationAPI) Post() {
|
func (r *ReplicationAPI) Post() {
|
||||||
replication := &models.Replication{}
|
replication := &api_models.Replication{}
|
||||||
r.DecodeJSONReqAndValidate(replication)
|
r.DecodeJSONReqAndValidate(replication)
|
||||||
|
|
||||||
policy, err := core.GlobalController.GetPolicy(replication.PolicyID)
|
policy, err := core.GlobalController.GetPolicy(replication.PolicyID)
|
||||||
@ -60,6 +63,18 @@ func (r *ReplicationAPI) Post() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, count, err := dao.FilterRepJobs(replication.PolicyID, "",
|
||||||
|
[]string{models.JobRunning, models.JobPending}, nil, nil, 1, 0)
|
||||||
|
if err != nil {
|
||||||
|
r.HandleInternalServerError(fmt.Sprintf("failed to filter jobs of policy %d: %v",
|
||||||
|
replication.PolicyID, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
r.RenderError(http.StatusPreconditionFailed, "policy has running/pending jobs, new replication can not be triggered")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err = startReplication(replication.PolicyID); err != nil {
|
if err = startReplication(replication.PolicyID); err != nil {
|
||||||
r.HandleInternalServerError(fmt.Sprintf("failed to publish replication topic for policy %d: %v", replication.PolicyID, err))
|
r.HandleInternalServerError(fmt.Sprintf("failed to publish replication topic for policy %d: %v", replication.PolicyID, err))
|
||||||
return
|
return
|
||||||
|
@ -108,7 +108,7 @@ func (ra *RepJobAPI) List() {
|
|||||||
|
|
||||||
page, pageSize := ra.GetPaginationParams()
|
page, pageSize := ra.GetPaginationParams()
|
||||||
|
|
||||||
jobs, total, err := dao.FilterRepJobs(policyID, repository, status,
|
jobs, total, err := dao.FilterRepJobs(policyID, repository, []string{status},
|
||||||
startTime, endTime, pageSize, pageSize*(page-1))
|
startTime, endTime, pageSize, pageSize*(page-1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to filter jobs according policy ID %d, repository %s, status %s, start time %v, end time %v: %v",
|
log.Errorf("failed to filter jobs according policy ID %d, repository %s, status %s, start time %v, end time %v: %v",
|
||||||
|
@ -243,20 +243,15 @@ func (pa *RepPolicyAPI) Delete() {
|
|||||||
pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
|
pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
_, count, err := dao.FilterRepJobs(id, "",
|
||||||
jobs, err := dao.GetRepJobByPolicy(id)
|
[]string{models.JobRunning, models.JobRetrying, models.JobPending}, nil, nil, 1, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to get jobs of policy %d: %v", id, err)
|
log.Errorf("failed to filter jobs of policy %d: %v", id, err)
|
||||||
pa.CustomAbort(http.StatusInternalServerError, "")
|
pa.CustomAbort(http.StatusInternalServerError, "")
|
||||||
}
|
}
|
||||||
|
if count > 0 {
|
||||||
for _, job := range jobs {
|
|
||||||
if job.Status == models.JobRunning ||
|
|
||||||
job.Status == models.JobRetrying ||
|
|
||||||
job.Status == models.JobPending {
|
|
||||||
pa.CustomAbort(http.StatusPreconditionFailed, "policy has running/retrying/pending jobs, can not be deleted")
|
pa.CustomAbort(http.StatusPreconditionFailed, "policy has running/retrying/pending jobs, can not be deleted")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if err = core.GlobalController.RemovePolicy(id); err != nil {
|
if err = core.GlobalController.RemovePolicy(id); err != nil {
|
||||||
log.Errorf("failed to delete policy %d: %v", id, err)
|
log.Errorf("failed to delete policy %d: %v", id, err)
|
||||||
@ -302,7 +297,7 @@ func convertFromRepPolicy(projectMgr promgr.ProjectManager, policy rep_models.Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO call the method from replication controller
|
// TODO call the method from replication controller
|
||||||
_, errJobCount, err := dao.FilterRepJobs(policy.ID, "", "error", nil, nil, 0, 0)
|
_, errJobCount, err := dao.FilterRepJobs(policy.ID, "", []string{"error"}, nil, nil, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user