Fix for project metadata (#12410)

Signed-off-by: guanxiatao <guanxiatao@corp.netease.com>
This commit is contained in:
Ted Guan 2020-07-15 18:46:45 +08:00 committed by GitHub
parent cadcd4b877
commit 9e7edb7a6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 85 deletions

View File

@ -15,13 +15,15 @@
package replication
import (
"strconv"
"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/project"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/replication"
repevent "github.com/goharbor/harbor/src/replication/event"
"github.com/goharbor/harbor/src/replication/model"
"strconv"
)
// Handler ...
@ -57,13 +59,13 @@ func (r *Handler) IsStateful() bool {
func (r *Handler) handlePushArtifact(event *event.PushArtifactEvent) error {
art := event.Artifact
public := false
project, err := project.Mgr.Get(art.ProjectID)
if err == nil && project != nil {
public = project.IsPublic()
} else {
log.Error(err)
prj, err := project.Ctl.Get(orm.Context(), art.ProjectID, project.Metadata(true))
if err != nil {
log.Errorf("failed to get project: %d, error: %v", art.ProjectID, err)
return err
}
project.IsPublic()
public = prj.IsPublic()
e := &repevent.Event{
Type: repevent.EventTypeArtifactPush,
Resource: &model.Resource{
@ -113,13 +115,13 @@ func (r *Handler) handleDeleteArtifact(event *event.DeleteArtifactEvent) error {
func (r *Handler) handleCreateTag(event *event.CreateTagEvent) error {
art := event.AttachedArtifact
public := false
project, err := project.Mgr.Get(art.ProjectID)
if err == nil && project != nil {
public = project.IsPublic()
} else {
log.Error(err)
prj, err := project.Ctl.Get(orm.Context(), art.ProjectID, project.Metadata(true))
if err != nil {
log.Errorf("failed to get project: %d, error: %v", art.ProjectID, err)
return err
}
project.IsPublic()
public = prj.IsPublic()
e := &repevent.Event{
Type: repevent.EventTypeArtifactPush,
Resource: &model.Resource{

View File

@ -17,22 +17,22 @@ package artifact
import (
"context"
"fmt"
beegorm "github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/controller/event/handler/util"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/pkg/notification"
"github.com/goharbor/harbor/src/pkg/notifier/model"
notifyModel "github.com/goharbor/harbor/src/pkg/notifier/model"
"github.com/goharbor/harbor/src/pkg/project"
"github.com/goharbor/harbor/src/pkg/repository"
)
// Handler preprocess artifact event data
type Handler struct {
project *models.Project
}
// Handle preprocess artifact event data and then publish hook event
@ -56,13 +56,13 @@ func (a *Handler) IsStateful() bool {
}
func (a *Handler) handle(event *event.ArtifactEvent) error {
var err error
a.project, err = project.Mgr.Get(event.Artifact.ProjectID)
prj, err := project.Ctl.Get(orm.Context(), event.Artifact.ProjectID, project.Metadata(true))
if err != nil {
log.Errorf("failed to get project:%d, error: %v", event.Artifact.ProjectID, err)
log.Errorf("failed to get project: %d, error: %v", event.Artifact.ProjectID, err)
return err
}
policies, err := notification.PolicyMgr.GetRelatedPolices(a.project.ProjectID, event.EventType)
policies, err := notification.PolicyMgr.GetRelatedPolices(prj.ProjectID, event.EventType)
if err != nil {
log.Errorf("failed to find policy for %s event: %v", event.EventType, err)
return err
@ -72,7 +72,7 @@ func (a *Handler) handle(event *event.ArtifactEvent) error {
return nil
}
payload, err := a.constructArtifactPayload(event)
payload, err := a.constructArtifactPayload(event, prj)
if err != nil {
return err
}
@ -84,14 +84,14 @@ func (a *Handler) handle(event *event.ArtifactEvent) error {
return nil
}
func (a *Handler) constructArtifactPayload(event *event.ArtifactEvent) (*model.Payload, error) {
func (a *Handler) constructArtifactPayload(event *event.ArtifactEvent, project *models.Project) (*model.Payload, error) {
repoName := event.Repository
if repoName == "" {
return nil, fmt.Errorf("invalid %s event with empty repo name", event.EventType)
}
repoType := models.ProjectPrivate
if a.project.IsPublic() {
if project.IsPublic() {
repoType = models.ProjectPublic
}
@ -103,7 +103,7 @@ func (a *Handler) constructArtifactPayload(event *event.ArtifactEvent) (*model.P
EventData: &notifyModel.EventData{
Repository: &notifyModel.Repository{
Name: imageName,
Namespace: a.project.Name,
Namespace: project.Name,
RepoFullName: repoName,
RepoType: repoType,
},

View File

@ -8,9 +8,11 @@ import (
commonModels "github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/controller/event/handler/util"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/pkg/notification"
"github.com/goharbor/harbor/src/pkg/notifier/model"
notifyModel "github.com/goharbor/harbor/src/pkg/notifier/model"
@ -188,16 +190,13 @@ func constructReplicationPayload(event *event.ReplicationEvent) (*model.Payload,
payload.EventData.Replication.FailedArtifact = []*model.ArtifactInfo{failedArtifact}
}
project, err := config.GlobalProjectMgr.Get(prjName)
prj, err := project.Ctl.GetByName(orm.Context(), prjName, project.Metadata(true))
if err != nil {
log.Errorf("failed to get project %s, error: %v", prjName, err)
return nil, nil, err
}
if project == nil {
return nil, nil, fmt.Errorf("project %s not found of replication event", prjName)
}
return payload, project, nil
return payload, prj, nil
}
func getMetadataFromResource(resource string) (namespace, nameAndTag string) {

View File

@ -1,10 +1,15 @@
package artifact
import (
"context"
"testing"
"time"
common_dao "github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/core/promgr/metamgr"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/notification"
"github.com/goharbor/harbor/src/replication"
@ -12,9 +17,6 @@ import (
"github.com/goharbor/harbor/src/replication/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
type fakedNotificationPolicyMgr struct {
@ -29,7 +31,7 @@ type fakedReplicationMgr struct {
type fakedReplicationRegistryMgr struct {
}
type fakedProjectMgr struct {
type fakedProjectCtl struct {
}
func (f *fakedNotificationPolicyMgr) Create(*models.NotificationPolicy) (int64, error) {
@ -194,63 +196,38 @@ func (f *fakedReplicationRegistryMgr) HealthCheck() error {
return nil
}
func (f *fakedProjectMgr) Get(projectIDOrName interface{}) (*models.Project, error) {
func (f *fakedProjectCtl) Get(ctx context.Context, projectID int64, options ...project.Option) (*models.Project, error) {
return &models.Project{ProjectID: 1}, nil
}
func (f *fakedProjectMgr) Create(*models.Project) (int64, error) {
return 0, nil
func (f *fakedProjectCtl) GetByName(ctx context.Context, projectName string, options ...project.Option) (*models.Project, error) {
return &models.Project{ProjectID: 1}, nil
}
func (f *fakedProjectMgr) Delete(projectIDOrName interface{}) error {
return nil
}
func (f *fakedProjectMgr) Update(projectIDOrName interface{}, project *models.Project) error {
return nil
}
func (f *fakedProjectMgr) List(query *models.ProjectQueryParam) (*models.ProjectQueryResult, error) {
func (f *fakedProjectCtl) List(ctx context.Context, query *models.ProjectQueryParam, options ...project.Option) ([]*models.Project, error) {
return nil, nil
}
func (f *fakedProjectMgr) IsPublic(projectIDOrName interface{}) (bool, error) {
return true, nil
}
func (f *fakedProjectMgr) Exists(projectIDOrName interface{}) (bool, error) {
return false, nil
}
// get all public project
func (f *fakedProjectMgr) GetPublic() ([]*models.Project, error) {
return nil, nil
}
func (f *fakedProjectMgr) GetAuthorized(user *models.User) ([]*models.Project, error) {
return nil, nil
}
// if the project manager uses a metadata manager, return it, otherwise return nil
func (f *fakedProjectMgr) GetMetadataManager() metamgr.ProjectMetadataManager {
return nil
}
func TestReplicationHandler_Handle(t *testing.T) {
common_dao.PrepareTestForPostgresSQL()
config.Init()
PolicyMgr := notification.PolicyMgr
execution := replication.OperationCtl
rpPolicy := replication.PolicyCtl
rpRegistry := replication.RegistryMgr
project := config.GlobalProjectMgr
prj := project.Ctl
defer func() {
notification.PolicyMgr = PolicyMgr
replication.OperationCtl = execution
replication.PolicyCtl = rpPolicy
replication.RegistryMgr = rpRegistry
config.GlobalProjectMgr = project
project.Ctl = prj
}()
notification.PolicyMgr = &fakedNotificationPolicyMgr{}
replication.OperationCtl = &fakedReplicationMgr{}
replication.PolicyCtl = &fakedReplicationPolicyMgr{}
replication.RegistryMgr = &fakedReplicationRegistryMgr{}
config.GlobalProjectMgr = &fakedProjectMgr{}
project.Ctl = &fakedProjectCtl{}
handler := &ReplicationHandler{}

View File

@ -17,14 +17,17 @@ package chart
import (
"errors"
"fmt"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/controller/event/handler/util"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/notification"
"github.com/goharbor/harbor/src/pkg/notifier/model"
"github.com/goharbor/harbor/src/pkg/project"
)
// Handler preprocess chart event data
@ -42,15 +45,12 @@ func (cph *Handler) Handle(value interface{}) error {
return fmt.Errorf("data miss in chart event: %v", chartEvent)
}
project, err := project.Mgr.Get(chartEvent.ProjectName)
prj, err := project.Ctl.GetByName(orm.Context(), chartEvent.ProjectName, project.Metadata(true))
if err != nil {
log.Errorf("failed to find project[%s] for chart event: %v", chartEvent.ProjectName, err)
return err
}
if project == nil {
return fmt.Errorf("project not found for chart event: %s", chartEvent.ProjectName)
}
policies, err := notification.PolicyMgr.GetRelatedPolices(project.ProjectID, chartEvent.EventType)
policies, err := notification.PolicyMgr.GetRelatedPolices(prj.ProjectID, chartEvent.EventType)
if err != nil {
log.Errorf("failed to find policy for %s event: %v", chartEvent.EventType, err)
return err
@ -61,7 +61,7 @@ func (cph *Handler) Handle(value interface{}) error {
return nil
}
payload, err := constructChartPayload(chartEvent, project)
payload, err := constructChartPayload(chartEvent, prj)
if err != nil {
return err
}

View File

@ -21,11 +21,12 @@ import (
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/controller/event/handler/util"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/pkg/notification"
"github.com/goharbor/harbor/src/pkg/notifier/model"
notifyModel "github.com/goharbor/harbor/src/pkg/notifier/model"
"github.com/goharbor/harbor/src/pkg/project"
)
// Handler preprocess image event data
@ -42,12 +43,13 @@ func (qp *Handler) Handle(value interface{}) error {
return fmt.Errorf("nil quota event")
}
project, err := project.Mgr.Get(quotaEvent.Project.Name)
prj, err := project.Ctl.GetByName(orm.Context(), quotaEvent.Project.Name, project.Metadata(true))
if err != nil {
log.Errorf("failed to get project:%s, error: %v", quotaEvent.Project.Name, err)
return err
}
policies, err := notification.PolicyMgr.GetRelatedPolices(project.ProjectID, quotaEvent.EventType)
policies, err := notification.PolicyMgr.GetRelatedPolices(prj.ProjectID, quotaEvent.EventType)
if err != nil {
log.Errorf("failed to find policy for %s event: %v", quotaEvent.EventType, err)
return err

View File

@ -16,20 +16,20 @@ package scan
import (
"context"
"time"
o "github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/controller/event/handler/util"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/pkg/notifier/model"
"time"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/controller/scan"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/pkg/notification"
"github.com/goharbor/harbor/src/pkg/project"
"github.com/goharbor/harbor/src/pkg/notifier/model"
v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1"
)
@ -60,12 +60,12 @@ func (si *Handler) Handle(value interface{}) error {
}
// Get project
project, err := project.Mgr.Get(e.Artifact.NamespaceID)
prj, err := project.Ctl.Get(orm.Context(), e.Artifact.NamespaceID, project.Metadata(true))
if err != nil {
return errors.Wrap(err, "scan preprocess handler")
}
payload, err := constructScanImagePayload(e, project)
payload, err := constructScanImagePayload(e, prj)
if err != nil {
return errors.Wrap(err, "scan preprocess handler")
}