Use orm.Context instead request context in background go routing

Fixes #12741,
Avoid use ctx info in event handler because it is background go routine

Signed-off-by: stonezdj <stonezdj@gmail.com>
This commit is contained in:
stonezdj 2020-08-13 11:52:03 +08:00
parent b3012ec08d
commit ff937e90d3
5 changed files with 20 additions and 11 deletions

View File

@ -55,9 +55,9 @@ func (p *PushArtifactEventMetadata) Resolve(event *event.Event) error {
// PullArtifactEventMetadata is the metadata from which the pull artifact event can be resolved
type PullArtifactEventMetadata struct {
Ctx context.Context
Artifact *artifact.Artifact
Tag string
Operator string
}
// Resolve to the event from the metadata
@ -74,10 +74,7 @@ func (p *PullArtifactEventMetadata) Resolve(event *event.Event) error {
data := &event2.PullArtifactEvent{
ArtifactEvent: ae,
}
ctx, exist := security.FromContext(p.Ctx)
if exist {
data.Operator = ctx.GetUsername()
}
data.Operator = p.Operator
event.Topic = event2.TopicPullArtifact
event.Data = data
return nil

View File

@ -47,9 +47,9 @@ func (a *artifactEventTestSuite) TestResolveOfPushArtifactEventMetadata() {
func (a *artifactEventTestSuite) TestResolveOfPullArtifactEventMetadata() {
e := &event.Event{}
metadata := &PullArtifactEventMetadata{
Ctx: context.Background(),
Artifact: &artifact.Artifact{ID: 1},
Tag: "latest",
Operator: "admin",
}
err := metadata.Resolve(e)
a.Require().Nil(err)

View File

@ -16,6 +16,8 @@ package proxy
import (
"context"
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/opencontainers/go-digest"
"io"
"strings"
@ -120,9 +122,14 @@ func (c *controller) ProxyManifest(ctx context.Context, p *models.Project, art l
if err != nil {
return man, err
}
operator := ""
if secCtx, ok := security.FromContext(ctx); ok {
operator = secCtx.GetUsername()
}
// Push manifest in background
go func() {
a, err := c.local.GetManifest(ctx, art)
bCtx := orm.Context()
a, err := c.local.GetManifest(bCtx, art)
if err != nil {
log.Errorf("failed to get manifest, error %v", err)
}
@ -140,7 +147,7 @@ func (c *controller) ProxyManifest(ctx context.Context, p *models.Project, art l
}
}
if a != nil {
SendPullEvent(ctx, a, art.Tag)
SendPullEvent(a, art.Tag, operator)
}
}()

View File

@ -210,11 +210,11 @@ func (l *localHelper) CheckDependencies(ctx context.Context, repo string, man di
}
// SendPullEvent send a pull image event
func SendPullEvent(ctx context.Context, a *artifact.Artifact, tag string) {
func SendPullEvent(a *artifact.Artifact, tag, operator string) {
e := &metadata.PullArtifactEventMetadata{
Ctx: ctx,
Artifact: &a.Artifact,
Tag: tag,
Operator: operator,
}
event.BuildAndPublish(e)
}

View File

@ -15,6 +15,7 @@
package registry
import (
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/event/metadata"
"github.com/goharbor/harbor/src/controller/repository"
@ -54,9 +55,13 @@ func getManifest(w http.ResponseWriter, req *http.Request) {
req.UserAgent() == registry.UserAgent {
return
}
operator := ""
if secCtx, exist := security.FromContext(req.Context()); exist {
operator = secCtx.GetUsername()
}
e := &metadata.PullArtifactEventMetadata{
Ctx: req.Context(),
Artifact: &art.Artifact,
Operator: operator,
}
// the reference is tag
if _, err = digest.Parse(reference); err != nil {