fix event log issue

fixes #12554
Add string method for each event, and the detail can be logged in the core.log

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2020-08-26 15:02:21 +08:00
parent 37e0aa0798
commit b51aaac26e
25 changed files with 201 additions and 6 deletions

View File

@ -33,6 +33,11 @@ type AuditResolver interface {
ResolveToAuditLog() (*am.AuditLog, error) ResolveToAuditLog() (*am.AuditLog, error)
} }
// Name ...
func (h *Handler) Name() string {
return "AuditLog"
}
// Handle ... // Handle ...
func (h *Handler) Handle(value interface{}) error { func (h *Handler) Handle(value interface{}) error {
ctx := orm.NewContext(context.Background(), beegorm.NewOrm()) ctx := orm.NewContext(context.Background(), beegorm.NewOrm())

View File

@ -91,6 +91,10 @@ func (suite *AuditLogHandlerTestSuite) TestSubscribeTagEvent() {
} }
func (suite *AuditLogHandlerTestSuite) TestName() {
suite.Equal("AuditLog", suite.auditLogHandler.Name())
}
func TestAuditLogHandlerTestSuite(t *testing.T) { func TestAuditLogHandlerTestSuite(t *testing.T) {
suite.Run(t, &AuditLogHandlerTestSuite{}) suite.Run(t, &AuditLogHandlerTestSuite{})
} }

View File

@ -31,6 +31,11 @@ type Handler struct {
Context func() context.Context Context func() context.Context
} }
// Name ...
func (a *Handler) Name() string {
return "InternalArtifact"
}
// Handle ... // Handle ...
func (a *Handler) Handle(value interface{}) error { func (a *Handler) Handle(value interface{}) error {
switch v := value.(type) { switch v := value.(type) {

View File

@ -32,6 +32,11 @@ type Handler struct {
Context func() context.Context Context func() context.Context
} }
// Name ...
func (p *Handler) Name() string {
return "P2PPreheat"
}
// Handle ... // Handle ...
func (p *Handler) Handle(value interface{}) error { func (p *Handler) Handle(value interface{}) error {
switch value.(type) { switch value.(type) {

View File

@ -75,6 +75,10 @@ func (suite *PreheatTestSuite) TestIsStateful() {
suite.False(b, "handler is stateful") suite.False(b, "handler is stateful")
} }
func (suite *PreheatTestSuite) TestName() {
suite.Equal("P2PPreheat", suite.handler.Name())
}
// TestHandle ... // TestHandle ...
func (suite *PreheatTestSuite) TestHandle() { func (suite *PreheatTestSuite) TestHandle() {
type args struct { type args struct {

View File

@ -30,6 +30,11 @@ import (
type Handler struct { type Handler struct {
} }
// Name ...
func (r *Handler) Name() string {
return "Replication"
}
// Handle ... // Handle ...
func (r *Handler) Handle(value interface{}) error { func (r *Handler) Handle(value interface{}) error {
pushArtEvent, ok := value.(*event.PushArtifactEvent) pushArtEvent, ok := value.(*event.PushArtifactEvent)

View File

@ -35,6 +35,11 @@ import (
type Handler struct { type Handler struct {
} }
// Name ...
func (a *Handler) Name() string {
return "ArtifactWebhook"
}
// Handle preprocess artifact event data and then publish hook event // Handle preprocess artifact event data and then publish hook event
func (a *Handler) Handle(value interface{}) error { func (a *Handler) Handle(value interface{}) error {
switch v := value.(type) { switch v := value.(type) {

View File

@ -24,6 +24,11 @@ import (
type ReplicationHandler struct { type ReplicationHandler struct {
} }
// Name ...
func (r *ReplicationHandler) Name() string {
return "ReplicationWebhook"
}
// Handle ... // Handle ...
func (r *ReplicationHandler) Handle(value interface{}) error { func (r *ReplicationHandler) Handle(value interface{}) error {
if !config.NotificationEnable() { if !config.NotificationEnable() {

View File

@ -288,3 +288,8 @@ func TestReplicationHandler_IsStateful(t *testing.T) {
handler := &ReplicationHandler{} handler := &ReplicationHandler{}
assert.False(t, handler.IsStateful()) assert.False(t, handler.IsStateful())
} }
func TestReplicationHandler_Name(t *testing.T) {
handler := &ReplicationHandler{}
assert.Equal(t, "ReplicationWebhook", handler.Name())
}

View File

@ -29,6 +29,11 @@ func NewRetentionController() retention.APIController {
return api.GetRetentionController() return api.GetRetentionController()
} }
// Name ...
func (r *RetentionHandler) Name() string {
return "RetentionWebhook"
}
// Handle ... // Handle ...
func (r *RetentionHandler) Handle(value interface{}) error { func (r *RetentionHandler) Handle(value interface{}) error {
if !config.NotificationEnable() { if !config.NotificationEnable() {

View File

@ -34,6 +34,11 @@ import (
type Handler struct { type Handler struct {
} }
// Name ...
func (cph *Handler) Name() string {
return "ChartWebhook"
}
// Handle preprocess chart event data and then publish hook event // Handle preprocess chart event data and then publish hook event
func (cph *Handler) Handle(value interface{}) error { func (cph *Handler) Handle(value interface{}) error {
chartEvent, ok := value.(*event.ChartEvent) chartEvent, ok := value.(*event.ChartEvent)

View File

@ -134,3 +134,8 @@ func TestChartPreprocessHandler_IsStateful(t *testing.T) {
handler := &Handler{} handler := &Handler{}
assert.False(t, handler.IsStateful()) assert.False(t, handler.IsStateful())
} }
func TestChartPreprocessHandler_Name(t *testing.T) {
handler := &Handler{}
assert.Equal(t, "ChartWebhook", handler.Name())
}

View File

@ -33,6 +33,11 @@ import (
type Handler struct { type Handler struct {
} }
// Name ...
func (qp *Handler) Name() string {
return "QuotaWebhook"
}
// Handle ... // Handle ...
func (qp *Handler) Handle(value interface{}) error { func (qp *Handler) Handle(value interface{}) error {
quotaEvent, ok := value.(*event.QuotaEvent) quotaEvent, ok := value.(*event.QuotaEvent)

View File

@ -93,6 +93,11 @@ func (suite *QuotaPreprocessHandlerSuite) TestHandle() {
// MockHandler ... // MockHandler ...
type MockHandler struct{} type MockHandler struct{}
// Name ...
func (m *MockHandler) Name() string {
return "Mock"
}
// Handle ... // Handle ...
func (m *MockHandler) Handle(value interface{}) error { func (m *MockHandler) Handle(value interface{}) error {
return nil return nil

View File

@ -30,6 +30,11 @@ import (
type DelArtHandler struct { type DelArtHandler struct {
} }
// Name ...
func (o *DelArtHandler) Name() string {
return "DeleteArtifactWebhook"
}
// Handle ... // Handle ...
func (o *DelArtHandler) Handle(value interface{}) error { func (o *DelArtHandler) Handle(value interface{}) error {
if value == nil { if value == nil {

View File

@ -37,6 +37,11 @@ import (
type Handler struct { type Handler struct {
} }
// Name ...
func (si *Handler) Name() string {
return "ScanWebhook"
}
// Handle preprocess chart event data and then publish hook event // Handle preprocess chart event data and then publish hook event
func (si *Handler) Handle(value interface{}) error { func (si *Handler) Handle(value interface{}) error {
if value == nil { if value == nil {

View File

@ -137,6 +137,11 @@ func (suite *ScanImagePreprocessHandlerSuite) TestHandle() {
// MockHTTPHandler ... // MockHTTPHandler ...
type MockHTTPHandler struct{} type MockHTTPHandler struct{}
// Name ...
func (m *MockHTTPHandler) Name() string {
return "MockHTTP"
}
// Handle ... // Handle ...
func (m *MockHTTPHandler) Handle(value interface{}) error { func (m *MockHTTPHandler) Handle(value interface{}) error {
return nil return nil

View File

@ -71,6 +71,11 @@ func (c *CreateProjectEvent) ResolveToAuditLog() (*model.AuditLog, error) {
return auditLog, nil return auditLog, nil
} }
func (c *CreateProjectEvent) String() string {
return fmt.Sprintf("ID-%d Name-%s Operator-%s OccurAt-%s",
c.ProjectID, c.Project, c.Operator, c.OccurAt.Format("2006-01-02 15:04:05"))
}
// DeleteProjectEvent is the deleting project event // DeleteProjectEvent is the deleting project event
type DeleteProjectEvent struct { type DeleteProjectEvent struct {
EventType string EventType string
@ -92,6 +97,11 @@ func (d *DeleteProjectEvent) ResolveToAuditLog() (*model.AuditLog, error) {
return auditLog, nil return auditLog, nil
} }
func (d *DeleteProjectEvent) String() string {
return fmt.Sprintf("ID-%d Name-%s Operator-%s OccurAt-%s",
d.ProjectID, d.Project, d.Operator, d.OccurAt.Format("2006-01-02 15:04:05"))
}
// DeleteRepositoryEvent is the deleting repository event // DeleteRepositoryEvent is the deleting repository event
type DeleteRepositoryEvent struct { type DeleteRepositoryEvent struct {
EventType string EventType string
@ -114,6 +124,11 @@ func (d *DeleteRepositoryEvent) ResolveToAuditLog() (*model.AuditLog, error) {
return auditLog, nil return auditLog, nil
} }
func (d *DeleteRepositoryEvent) String() string {
return fmt.Sprintf("ID-%d Repository-%s Operator-%s OccurAt-%s",
d.ProjectID, d.Repository, d.Operator, d.OccurAt.Format("2006-01-02 15:04:05"))
}
// ArtifactEvent is the pushing/pulling artifact event // ArtifactEvent is the pushing/pulling artifact event
type ArtifactEvent struct { type ArtifactEvent struct {
EventType string EventType string
@ -124,6 +139,12 @@ type ArtifactEvent struct {
OccurAt time.Time OccurAt time.Time
} }
func (a *ArtifactEvent) String() string {
return fmt.Sprintf("ID-%d, Repository-%s Tags-%s Digest-%s Operator-%s OccurAt-%s",
a.Artifact.ID, a.Repository, a.Tags, a.Artifact.Digest, a.Operator,
a.OccurAt.Format("2006-01-02 15:04:05"))
}
// PushArtifactEvent is the pushing artifact event // PushArtifactEvent is the pushing artifact event
type PushArtifactEvent struct { type PushArtifactEvent struct {
*ArtifactEvent *ArtifactEvent
@ -149,6 +170,10 @@ func (p *PushArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) {
return auditLog, nil return auditLog, nil
} }
func (p *PushArtifactEvent) String() string {
return p.ArtifactEvent.String()
}
// PullArtifactEvent is the pulling artifact event // PullArtifactEvent is the pulling artifact event
type PullArtifactEvent struct { type PullArtifactEvent struct {
*ArtifactEvent *ArtifactEvent
@ -181,23 +206,31 @@ func (p *PullArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) {
return auditLog, nil return auditLog, nil
} }
func (p *PullArtifactEvent) String() string {
return p.ArtifactEvent.String()
}
// DeleteArtifactEvent is the deleting artifact event // DeleteArtifactEvent is the deleting artifact event
type DeleteArtifactEvent struct { type DeleteArtifactEvent struct {
*ArtifactEvent *ArtifactEvent
} }
// ResolveToAuditLog ... // ResolveToAuditLog ...
func (p *DeleteArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) { func (d *DeleteArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) {
auditLog := &model.AuditLog{ auditLog := &model.AuditLog{
ProjectID: p.Artifact.ProjectID, ProjectID: d.Artifact.ProjectID,
OpTime: p.OccurAt, OpTime: d.OccurAt,
Operation: "delete", Operation: "delete",
Username: p.Operator, Username: d.Operator,
ResourceType: "artifact", ResourceType: "artifact",
Resource: fmt.Sprintf("%s:%s", p.Artifact.RepositoryName, p.Artifact.Digest)} Resource: fmt.Sprintf("%s:%s", d.Artifact.RepositoryName, d.Artifact.Digest)}
return auditLog, nil return auditLog, nil
} }
func (d *DeleteArtifactEvent) String() string {
return d.ArtifactEvent.String()
}
// CreateTagEvent is the creating tag event // CreateTagEvent is the creating tag event
type CreateTagEvent struct { type CreateTagEvent struct {
EventType string EventType string
@ -220,6 +253,12 @@ func (c *CreateTagEvent) ResolveToAuditLog() (*model.AuditLog, error) {
return auditLog, nil return auditLog, nil
} }
func (c *CreateTagEvent) String() string {
return fmt.Sprintf("ArtifactID-%d, Repository-%s Tag-%s Digest-%s Operator-%s OccurAt-%s",
c.AttachedArtifact.ID, c.Repository, c.Tag, c.AttachedArtifact.Digest, c.Operator,
c.OccurAt.Format("2006-01-02 15:04:05"))
}
// DeleteTagEvent is the deleting tag event // DeleteTagEvent is the deleting tag event
type DeleteTagEvent struct { type DeleteTagEvent struct {
EventType string EventType string
@ -242,6 +281,12 @@ func (d *DeleteTagEvent) ResolveToAuditLog() (*model.AuditLog, error) {
return auditLog, nil return auditLog, nil
} }
func (d *DeleteTagEvent) String() string {
return fmt.Sprintf("ArtifactID-%d, Repository-%s Tag-%s Digest-%s Operator-%s OccurAt-%s",
d.AttachedArtifact.ID, d.Repository, d.Tag, d.AttachedArtifact.Digest, d.Operator,
d.OccurAt.Format("2006-01-02 15:04:05"))
}
// ScanImageEvent is scanning image related event data to publish // ScanImageEvent is scanning image related event data to publish
type ScanImageEvent struct { type ScanImageEvent struct {
EventType string EventType string
@ -250,6 +295,11 @@ type ScanImageEvent struct {
Operator string Operator string
} }
func (s *ScanImageEvent) String() string {
return fmt.Sprintf("Artifact-%+v Operator-%s OccurAt-%s",
s.Artifact, s.Operator, s.OccurAt.Format("2006-01-02 15:04:05"))
}
// ChartEvent is chart related event data to publish // ChartEvent is chart related event data to publish
type ChartEvent struct { type ChartEvent struct {
EventType string EventType string
@ -260,6 +310,11 @@ type ChartEvent struct {
Operator string Operator string
} }
func (c *ChartEvent) String() string {
return fmt.Sprintf("ProjectName-%s ChartName-%s Versions-%s Operator-%s OccurAt-%s",
c.ProjectName, c.ChartName, c.Versions, c.Operator, c.OccurAt.Format("2006-01-02 15:04:05"))
}
// QuotaEvent is project quota related event data to publish // QuotaEvent is project quota related event data to publish
type QuotaEvent struct { type QuotaEvent struct {
EventType string EventType string
@ -270,6 +325,11 @@ type QuotaEvent struct {
Msg string Msg string
} }
func (q *QuotaEvent) String() string {
return fmt.Sprintf("ProjectID-%d RepoName-%s Resource-%+v Msg-%s OccurAt-%s",
q.Project.ProjectID, q.RepoName, q.Resource, q.Msg, q.OccurAt.Format("2006-01-02 15:04:05"))
}
// ImgResource include image digest and tag // ImgResource include image digest and tag
type ImgResource struct { type ImgResource struct {
Digest string Digest string
@ -284,6 +344,11 @@ type ReplicationEvent struct {
Status string Status string
} }
func (r *ReplicationEvent) String() string {
return fmt.Sprintf("ReplicationTaskID-%d Status-%s OccurAt-%s",
r.ReplicationTaskID, r.Status, r.OccurAt.Format("2006-01-02 15:04:05"))
}
// ArtifactLabeledEvent is event data of artifact labeled // ArtifactLabeledEvent is event data of artifact labeled
type ArtifactLabeledEvent struct { type ArtifactLabeledEvent struct {
ArtifactID int64 ArtifactID int64
@ -292,6 +357,11 @@ type ArtifactLabeledEvent struct {
Operator string Operator string
} }
func (al *ArtifactLabeledEvent) String() string {
return fmt.Sprintf("ArtifactID-%d LabelID-%d Operator-%s OccurAt-%s",
al.ArtifactID, al.LabelID, al.Operator, al.OccurAt.Format("2006-01-02 15:04:05"))
}
// RetentionEvent is tag retention related event data to publish // RetentionEvent is tag retention related event data to publish
type RetentionEvent struct { type RetentionEvent struct {
TaskID int64 TaskID int64
@ -300,3 +370,14 @@ type RetentionEvent struct {
Status string Status string
Deleted []*selector.Result Deleted []*selector.Result
} }
func (r *RetentionEvent) String() string {
candidates := []string{}
for _, candidate := range r.Deleted {
candidates = append(candidates, fmt.Sprintf("%s:%s:%s", candidate.Target.Namespace,
candidate.Target.Repository, candidate.Target.Tags))
}
return fmt.Sprintf("TaskID-%d Status-%s Deleted-%s OccurAt-%s",
r.TaskID, r.Status, candidates, r.OccurAt.Format("2006-01-02 15:04:05"))
}

View File

@ -15,6 +15,11 @@ import (
type HTTPHandler struct { type HTTPHandler struct {
} }
// Name ...
func (h *HTTPHandler) Name() string {
return "HTTP"
}
// Handle handles http event // Handle handles http event
func (h *HTTPHandler) Handle(value interface{}) error { func (h *HTTPHandler) Handle(value interface{}) error {
if value == nil { if value == nil {

View File

@ -95,3 +95,8 @@ func TestHTTPHandler_IsStateful(t *testing.T) {
handler := &HTTPHandler{} handler := &HTTPHandler{}
assert.False(t, handler.IsStateful()) assert.False(t, handler.IsStateful())
} }
func TestHTTPHandler_Name(t *testing.T) {
handler := &HTTPHandler{}
assert.Equal(t, "HTTP", handler.Name())
}

View File

@ -64,6 +64,11 @@ const (
type SlackHandler struct { type SlackHandler struct {
} }
// Name ...
func (s *SlackHandler) Name() string {
return "Slack"
}
// Handle handles event to slack // Handle handles event to slack
func (s *SlackHandler) Handle(value interface{}) error { func (s *SlackHandler) Handle(value interface{}) error {
if value == nil { if value == nil {

View File

@ -99,3 +99,8 @@ func TestSlackHandler_IsStateful(t *testing.T) {
handler := &SlackHandler{} handler := &SlackHandler{}
assert.False(t, handler.IsStateful()) assert.False(t, handler.IsStateful())
} }
func TestSlackHandler_Name(t *testing.T) {
handler := &SlackHandler{}
assert.Equal(t, "Slack", handler.Name())
}

View File

@ -3,6 +3,9 @@ package notifier
// NotificationHandler defines what operations a notification handler // NotificationHandler defines what operations a notification handler
// should have. // should have.
type NotificationHandler interface { type NotificationHandler interface {
// The name of the Handler
Name() string
// Handle the event when it coming. // Handle the event when it coming.
// value might be optional, it depends on usages. // value might be optional, it depends on usages.
Handle(value interface{}) error Handle(value interface{}) error

View File

@ -201,7 +201,7 @@ func (nw *NotificationWatcher) Notify(notification Notification) error {
// Currently, we just log the error // Currently, we just log the error
log.Errorf("Error occurred when triggering handler %s of topic %s: %s\n", reflect.TypeOf(hd).String(), notification.Topic, err.Error()) log.Errorf("Error occurred when triggering handler %s of topic %s: %s\n", reflect.TypeOf(hd).String(), notification.Topic, err.Error())
} else { } else {
log.Infof("Handle notification with topic '%s': %#v\n", notification.Topic, notification.Value) log.Infof("Handle notification with Handler '%s' on topic '%s': %s\n", hd.Name(), notification.Topic, notification.Value)
} }
}() }()
}(h, handlerChan) }(h, handlerChan)

View File

@ -13,6 +13,10 @@ type fakeStatefulHandler struct {
number int number int
} }
func (fsh *fakeStatefulHandler) Name() string {
return "fakeStateful"
}
func (fsh *fakeStatefulHandler) IsStateful() bool { func (fsh *fakeStatefulHandler) IsStateful() bool {
return true return true
} }
@ -32,6 +36,10 @@ func (fsh *fakeStatelessHandler) IsStateful() bool {
return false return false
} }
func (fsh *fakeStatelessHandler) Name() string {
return "fakeStateless"
}
func (fsh *fakeStatelessHandler) Handle(v interface{}) error { func (fsh *fakeStatelessHandler) Handle(v interface{}) error {
return nil return nil
} }