Merge pull request #11107 from ywk253100/filter_replication_pull

Filter the pulling manifest request from replication service
This commit is contained in:
Wenkai Yin(尹文开) 2020-03-18 14:36:29 +08:00 committed by GitHub
commit eb2af6095e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 13 deletions

View File

@ -56,6 +56,11 @@ var (
}
)
// const definition
const (
UserAgent = "harbor-registry-client"
)
// Client defines the methods that a registry client should implements
type Client interface {
// Ping the base API endpoint "/v2/"
@ -504,6 +509,7 @@ func (c *client) do(req *http.Request) (*http.Response, error) {
return nil, err
}
}
req.Header.Set(http.CanonicalHeaderKey("User-Agent"), UserAgent)
resp, err := c.client.Do(req)
if err != nil {
return nil, err

View File

@ -22,6 +22,7 @@ import (
"github.com/goharbor/harbor/src/internal"
ierror "github.com/goharbor/harbor/src/internal/error"
"github.com/goharbor/harbor/src/pkg/notification"
"github.com/goharbor/harbor/src/pkg/registry"
serror "github.com/goharbor/harbor/src/server/error"
"github.com/goharbor/harbor/src/server/router"
"github.com/opencontainers/go-digest"
@ -48,20 +49,20 @@ func getManifest(w http.ResponseWriter, req *http.Request) {
recorder := internal.NewResponseRecorder(w)
proxy.ServeHTTP(recorder, req)
// fire event
if recorder.Success() {
// TODO don't fire event for the pulling from replication
e := &metadata.PullArtifactEventMetadata{
Ctx: req.Context(),
Artifact: &artifact.Artifact,
}
// TODO provide a util function to determine whether the reference is tag or not
// the reference is tag
if _, err = digest.Parse(reference); err != nil {
e.Tag = reference
}
notification.AddEvent(req.Context(), e)
// fire event, ignore the HEAD request and pulling request from replication service
if !recorder.Success() || req.Method == http.MethodHead ||
req.UserAgent() == registry.UserAgent {
return
}
e := &metadata.PullArtifactEventMetadata{
Ctx: req.Context(),
Artifact: &artifact.Artifact,
}
// the reference is tag
if _, err = digest.Parse(reference); err != nil {
e.Tag = reference
}
notification.AddEvent(req.Context(), e)
}
// just delete the artifact from database