mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-22 18:25:56 +01:00
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:
parent
b3012ec08d
commit
ff937e90d3
@ -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
|
// PullArtifactEventMetadata is the metadata from which the pull artifact event can be resolved
|
||||||
type PullArtifactEventMetadata struct {
|
type PullArtifactEventMetadata struct {
|
||||||
Ctx context.Context
|
|
||||||
Artifact *artifact.Artifact
|
Artifact *artifact.Artifact
|
||||||
Tag string
|
Tag string
|
||||||
|
Operator string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve to the event from the metadata
|
// Resolve to the event from the metadata
|
||||||
@ -74,10 +74,7 @@ func (p *PullArtifactEventMetadata) Resolve(event *event.Event) error {
|
|||||||
data := &event2.PullArtifactEvent{
|
data := &event2.PullArtifactEvent{
|
||||||
ArtifactEvent: ae,
|
ArtifactEvent: ae,
|
||||||
}
|
}
|
||||||
ctx, exist := security.FromContext(p.Ctx)
|
data.Operator = p.Operator
|
||||||
if exist {
|
|
||||||
data.Operator = ctx.GetUsername()
|
|
||||||
}
|
|
||||||
event.Topic = event2.TopicPullArtifact
|
event.Topic = event2.TopicPullArtifact
|
||||||
event.Data = data
|
event.Data = data
|
||||||
return nil
|
return nil
|
||||||
|
@ -47,9 +47,9 @@ func (a *artifactEventTestSuite) TestResolveOfPushArtifactEventMetadata() {
|
|||||||
func (a *artifactEventTestSuite) TestResolveOfPullArtifactEventMetadata() {
|
func (a *artifactEventTestSuite) TestResolveOfPullArtifactEventMetadata() {
|
||||||
e := &event.Event{}
|
e := &event.Event{}
|
||||||
metadata := &PullArtifactEventMetadata{
|
metadata := &PullArtifactEventMetadata{
|
||||||
Ctx: context.Background(),
|
|
||||||
Artifact: &artifact.Artifact{ID: 1},
|
Artifact: &artifact.Artifact{ID: 1},
|
||||||
Tag: "latest",
|
Tag: "latest",
|
||||||
|
Operator: "admin",
|
||||||
}
|
}
|
||||||
err := metadata.Resolve(e)
|
err := metadata.Resolve(e)
|
||||||
a.Require().Nil(err)
|
a.Require().Nil(err)
|
||||||
|
@ -16,6 +16,8 @@ package proxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/goharbor/harbor/src/common/security"
|
||||||
|
"github.com/goharbor/harbor/src/lib/orm"
|
||||||
"github.com/opencontainers/go-digest"
|
"github.com/opencontainers/go-digest"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
@ -120,9 +122,14 @@ func (c *controller) ProxyManifest(ctx context.Context, p *models.Project, art l
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return man, err
|
return man, err
|
||||||
}
|
}
|
||||||
|
operator := ""
|
||||||
|
if secCtx, ok := security.FromContext(ctx); ok {
|
||||||
|
operator = secCtx.GetUsername()
|
||||||
|
}
|
||||||
// Push manifest in background
|
// Push manifest in background
|
||||||
go func() {
|
go func() {
|
||||||
a, err := c.local.GetManifest(ctx, art)
|
bCtx := orm.Context()
|
||||||
|
a, err := c.local.GetManifest(bCtx, art)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to get manifest, error %v", err)
|
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 {
|
if a != nil {
|
||||||
SendPullEvent(ctx, a, art.Tag)
|
SendPullEvent(a, art.Tag, operator)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -210,11 +210,11 @@ func (l *localHelper) CheckDependencies(ctx context.Context, repo string, man di
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SendPullEvent send a pull image event
|
// 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{
|
e := &metadata.PullArtifactEventMetadata{
|
||||||
Ctx: ctx,
|
|
||||||
Artifact: &a.Artifact,
|
Artifact: &a.Artifact,
|
||||||
Tag: tag,
|
Tag: tag,
|
||||||
|
Operator: operator,
|
||||||
}
|
}
|
||||||
event.BuildAndPublish(e)
|
event.BuildAndPublish(e)
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package registry
|
package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/goharbor/harbor/src/common/security"
|
||||||
"github.com/goharbor/harbor/src/controller/artifact"
|
"github.com/goharbor/harbor/src/controller/artifact"
|
||||||
"github.com/goharbor/harbor/src/controller/event/metadata"
|
"github.com/goharbor/harbor/src/controller/event/metadata"
|
||||||
"github.com/goharbor/harbor/src/controller/repository"
|
"github.com/goharbor/harbor/src/controller/repository"
|
||||||
@ -54,9 +55,13 @@ func getManifest(w http.ResponseWriter, req *http.Request) {
|
|||||||
req.UserAgent() == registry.UserAgent {
|
req.UserAgent() == registry.UserAgent {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
operator := ""
|
||||||
|
if secCtx, exist := security.FromContext(req.Context()); exist {
|
||||||
|
operator = secCtx.GetUsername()
|
||||||
|
}
|
||||||
e := &metadata.PullArtifactEventMetadata{
|
e := &metadata.PullArtifactEventMetadata{
|
||||||
Ctx: req.Context(),
|
|
||||||
Artifact: &art.Artifact,
|
Artifact: &art.Artifact,
|
||||||
|
Operator: operator,
|
||||||
}
|
}
|
||||||
// the reference is tag
|
// the reference is tag
|
||||||
if _, err = digest.Parse(reference); err != nil {
|
if _, err = digest.Parse(reference); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user