Merge pull request #12883 from wy65701436/fixes-12254

fix event log issue
This commit is contained in:
Daniel Jiang 2020-08-27 16:52:24 +08:00 committed by GitHub
commit a651eb0949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 201 additions and 6 deletions

View File

@ -33,6 +33,11 @@ type AuditResolver interface {
ResolveToAuditLog() (*am.AuditLog, error)
}
// Name ...
func (h *Handler) Name() string {
return "AuditLog"
}
// Handle ...
func (h *Handler) Handle(value interface{}) error {
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) {
suite.Run(t, &AuditLogHandlerTestSuite{})
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -288,3 +288,8 @@ func TestReplicationHandler_IsStateful(t *testing.T) {
handler := &ReplicationHandler{}
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()
}
// Name ...
func (r *RetentionHandler) Name() string {
return "RetentionWebhook"
}
// Handle ...
func (r *RetentionHandler) Handle(value interface{}) error {
if !config.NotificationEnable() {

View File

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

View File

@ -134,3 +134,8 @@ func TestChartPreprocessHandler_IsStateful(t *testing.T) {
handler := &Handler{}
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 {
}
// Name ...
func (qp *Handler) Name() string {
return "QuotaWebhook"
}
// Handle ...
func (qp *Handler) Handle(value interface{}) error {
quotaEvent, ok := value.(*event.QuotaEvent)

View File

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

View File

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

View File

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

View File

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

View File

@ -71,6 +71,11 @@ func (c *CreateProjectEvent) ResolveToAuditLog() (*model.AuditLog, error) {
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
type DeleteProjectEvent struct {
EventType string
@ -92,6 +97,11 @@ func (d *DeleteProjectEvent) ResolveToAuditLog() (*model.AuditLog, error) {
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
type DeleteRepositoryEvent struct {
EventType string
@ -114,6 +124,11 @@ func (d *DeleteRepositoryEvent) ResolveToAuditLog() (*model.AuditLog, error) {
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
type ArtifactEvent struct {
EventType string
@ -124,6 +139,12 @@ type ArtifactEvent struct {
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
type PushArtifactEvent struct {
*ArtifactEvent
@ -149,6 +170,10 @@ func (p *PushArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) {
return auditLog, nil
}
func (p *PushArtifactEvent) String() string {
return p.ArtifactEvent.String()
}
// PullArtifactEvent is the pulling artifact event
type PullArtifactEvent struct {
*ArtifactEvent
@ -181,23 +206,31 @@ func (p *PullArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) {
return auditLog, nil
}
func (p *PullArtifactEvent) String() string {
return p.ArtifactEvent.String()
}
// DeleteArtifactEvent is the deleting artifact event
type DeleteArtifactEvent struct {
*ArtifactEvent
}
// ResolveToAuditLog ...
func (p *DeleteArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) {
func (d *DeleteArtifactEvent) ResolveToAuditLog() (*model.AuditLog, error) {
auditLog := &model.AuditLog{
ProjectID: p.Artifact.ProjectID,
OpTime: p.OccurAt,
ProjectID: d.Artifact.ProjectID,
OpTime: d.OccurAt,
Operation: "delete",
Username: p.Operator,
Username: d.Operator,
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
}
func (d *DeleteArtifactEvent) String() string {
return d.ArtifactEvent.String()
}
// CreateTagEvent is the creating tag event
type CreateTagEvent struct {
EventType string
@ -220,6 +253,12 @@ func (c *CreateTagEvent) ResolveToAuditLog() (*model.AuditLog, error) {
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
type DeleteTagEvent struct {
EventType string
@ -242,6 +281,12 @@ func (d *DeleteTagEvent) ResolveToAuditLog() (*model.AuditLog, error) {
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
type ScanImageEvent struct {
EventType string
@ -250,6 +295,11 @@ type ScanImageEvent struct {
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
type ChartEvent struct {
EventType string
@ -260,6 +310,11 @@ type ChartEvent struct {
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
type QuotaEvent struct {
EventType string
@ -270,6 +325,11 @@ type QuotaEvent struct {
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
type ImgResource struct {
Digest string
@ -284,6 +344,11 @@ type ReplicationEvent struct {
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
type ArtifactLabeledEvent struct {
ArtifactID int64
@ -292,6 +357,11 @@ type ArtifactLabeledEvent struct {
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
type RetentionEvent struct {
TaskID int64
@ -300,3 +370,14 @@ type RetentionEvent struct {
Status string
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 {
}
// Name ...
func (h *HTTPHandler) Name() string {
return "HTTP"
}
// Handle handles http event
func (h *HTTPHandler) Handle(value interface{}) error {
if value == nil {

View File

@ -95,3 +95,8 @@ func TestHTTPHandler_IsStateful(t *testing.T) {
handler := &HTTPHandler{}
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 {
}
// Name ...
func (s *SlackHandler) Name() string {
return "Slack"
}
// Handle handles event to slack
func (s *SlackHandler) Handle(value interface{}) error {
if value == nil {

View File

@ -99,3 +99,8 @@ func TestSlackHandler_IsStateful(t *testing.T) {
handler := &SlackHandler{}
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
// should have.
type NotificationHandler interface {
// The name of the Handler
Name() string
// Handle the event when it coming.
// value might be optional, it depends on usages.
Handle(value interface{}) error

View File

@ -201,7 +201,7 @@ func (nw *NotificationWatcher) Notify(notification Notification) 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())
} 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)

View File

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