Filter the events triggerred by replication

Filter the events triggerred by replication pull

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2019-04-04 15:42:26 +08:00
parent 3ed8d87406
commit 1c735a7464
3 changed files with 12 additions and 10 deletions

View File

@ -20,6 +20,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/goharbor/harbor/src/replication/ng/adapter"
"github.com/goharbor/harbor/src/common/dao" "github.com/goharbor/harbor/src/common/dao"
clairdao "github.com/goharbor/harbor/src/common/dao/clair" clairdao "github.com/goharbor/harbor/src/common/dao/clair"
"github.com/goharbor/harbor/src/common/models" "github.com/goharbor/harbor/src/common/models"
@ -179,15 +181,16 @@ func filterEvents(notification *models.Notification) ([]*models.Event, error) {
} }
func checkEvent(event *models.Event) bool { func checkEvent(event *models.Event) bool {
// pull and push manifest // push action
if strings.ToLower(strings.TrimSpace(event.Request.UserAgent)) != "harbor-registry-client" && (event.Action == "pull" || event.Action == "push") { if event.Action == "push" {
return true return true
} }
// push manifest by job-service // if it is pull action, check the user-agent
if strings.ToLower(strings.TrimSpace(event.Request.UserAgent)) == "harbor-registry-client" && event.Action == "push" { userAgent := strings.ToLower(strings.TrimSpace(event.Request.UserAgent))
return true if userAgent == "harbor-registry-client" || userAgent == strings.ToLower(adapter.UserAgentReplication) {
return false
} }
return false return true
} }
func autoScanEnabled(project *models.Project) bool { func autoScanEnabled(project *models.Project) bool {

View File

@ -51,7 +51,7 @@ func newAdapter(registry *model.Registry) *adapter {
transport := util.GetHTTPTransport(registry.Insecure) transport := util.GetHTTPTransport(registry.Insecure)
modifiers := []modifier.Modifier{ modifiers := []modifier.Modifier{
&auth.UserAgentModifier{ &auth.UserAgentModifier{
UserAgent: adp.UserAgentReplicator, UserAgent: adp.UserAgentReplication,
}, },
} }
if registry.Credential != nil { if registry.Credential != nil {

View File

@ -35,8 +35,7 @@ import (
// const definition // const definition
const ( const (
// TODO: add filter for the agent in registry webhook handler UserAgentReplication = "harbor-replication-service"
UserAgentReplicator = "harbor-replicator"
) )
// ImageRegistry defines the capabilities that an image registry should have // ImageRegistry defines the capabilities that an image registry should have
@ -65,7 +64,7 @@ func NewDefaultImageRegistry(registry *model.Registry) *DefaultImageRegistry {
transport := util.GetHTTPTransport(registry.Insecure) transport := util.GetHTTPTransport(registry.Insecure)
modifiers := []modifier.Modifier{ modifiers := []modifier.Modifier{
&auth.UserAgentModifier{ &auth.UserAgentModifier{
UserAgent: UserAgentReplicator, UserAgent: UserAgentReplication,
}, },
} }
if registry.Credential != nil { if registry.Credential != nil {