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

View File

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

View File

@ -8,9 +8,11 @@ import (
commonModels "github.com/goharbor/harbor/src/common/models" commonModels "github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/controller/event" "github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/controller/event/handler/util" "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/core/config"
"github.com/goharbor/harbor/src/jobservice/job" "github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/lib/log" "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/notification"
"github.com/goharbor/harbor/src/pkg/notifier/model" "github.com/goharbor/harbor/src/pkg/notifier/model"
notifyModel "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} 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 { if err != nil {
log.Errorf("failed to get project %s, error: %v", prjName, err) log.Errorf("failed to get project %s, error: %v", prjName, err)
return nil, nil, 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) { func getMetadataFromResource(resource string) (namespace, nameAndTag string) {

View File

@ -1,10 +1,15 @@
package artifact package artifact
import ( 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/common/models"
"github.com/goharbor/harbor/src/controller/event" "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/config"
"github.com/goharbor/harbor/src/core/promgr/metamgr"
"github.com/goharbor/harbor/src/lib/q" "github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/notification" "github.com/goharbor/harbor/src/pkg/notification"
"github.com/goharbor/harbor/src/replication" "github.com/goharbor/harbor/src/replication"
@ -12,9 +17,6 @@ import (
"github.com/goharbor/harbor/src/replication/model" "github.com/goharbor/harbor/src/replication/model"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"testing"
"time"
) )
type fakedNotificationPolicyMgr struct { type fakedNotificationPolicyMgr struct {
@ -29,7 +31,7 @@ type fakedReplicationMgr struct {
type fakedReplicationRegistryMgr struct { type fakedReplicationRegistryMgr struct {
} }
type fakedProjectMgr struct { type fakedProjectCtl struct {
} }
func (f *fakedNotificationPolicyMgr) Create(*models.NotificationPolicy) (int64, error) { func (f *fakedNotificationPolicyMgr) Create(*models.NotificationPolicy) (int64, error) {
@ -194,63 +196,38 @@ func (f *fakedReplicationRegistryMgr) HealthCheck() error {
return nil 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 return &models.Project{ProjectID: 1}, nil
} }
func (f *fakedProjectMgr) Create(*models.Project) (int64, error) { func (f *fakedProjectCtl) GetByName(ctx context.Context, projectName string, options ...project.Option) (*models.Project, error) {
return 0, nil return &models.Project{ProjectID: 1}, nil
} }
func (f *fakedProjectMgr) Delete(projectIDOrName interface{}) error { func (f *fakedProjectCtl) List(ctx context.Context, query *models.ProjectQueryParam, options ...project.Option) ([]*models.Project, 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) {
return nil, nil 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) { func TestReplicationHandler_Handle(t *testing.T) {
common_dao.PrepareTestForPostgresSQL()
config.Init() config.Init()
PolicyMgr := notification.PolicyMgr PolicyMgr := notification.PolicyMgr
execution := replication.OperationCtl execution := replication.OperationCtl
rpPolicy := replication.PolicyCtl rpPolicy := replication.PolicyCtl
rpRegistry := replication.RegistryMgr rpRegistry := replication.RegistryMgr
project := config.GlobalProjectMgr prj := project.Ctl
defer func() { defer func() {
notification.PolicyMgr = PolicyMgr notification.PolicyMgr = PolicyMgr
replication.OperationCtl = execution replication.OperationCtl = execution
replication.PolicyCtl = rpPolicy replication.PolicyCtl = rpPolicy
replication.RegistryMgr = rpRegistry replication.RegistryMgr = rpRegistry
config.GlobalProjectMgr = project project.Ctl = prj
}() }()
notification.PolicyMgr = &fakedNotificationPolicyMgr{} notification.PolicyMgr = &fakedNotificationPolicyMgr{}
replication.OperationCtl = &fakedReplicationMgr{} replication.OperationCtl = &fakedReplicationMgr{}
replication.PolicyCtl = &fakedReplicationPolicyMgr{} replication.PolicyCtl = &fakedReplicationPolicyMgr{}
replication.RegistryMgr = &fakedReplicationRegistryMgr{} replication.RegistryMgr = &fakedReplicationRegistryMgr{}
config.GlobalProjectMgr = &fakedProjectMgr{} project.Ctl = &fakedProjectCtl{}
handler := &ReplicationHandler{} handler := &ReplicationHandler{}

View File

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

View File

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

View File

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