Skip to content

Commit

Permalink
feat: add webhook events of members
Browse files Browse the repository at this point in the history
Signed-off-by: xu.zhu <[email protected]>
  • Loading branch information
xuzhu-591 committed Sep 25, 2023
1 parent d09b910 commit 3bac5fa
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 152 deletions.
2 changes: 2 additions & 0 deletions core/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ const (

ResourceWebhook = "webhooks"
ResourceWebhookLog = "webhooklogs"

ResourceMember = "members"
)

const (
Expand Down
87 changes: 40 additions & 47 deletions core/controller/application/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,8 @@ func (c *controller) CreateApplication(ctx context.Context, groupID uint,
request.TemplateInput.Pipeline, request.TemplateInput.Application)

// 7. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationCreated,
ResourceID: ret.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}

c.recordApplicationEvent(ctx, ret.ID, eventmodels.ApplicationCreated)
c.recordMemberCreatedEvent(ctx, ret.ID)
return ret, nil
}

Expand Down Expand Up @@ -436,16 +428,8 @@ func (c *controller) CreateApplicationV2(ctx context.Context, groupID uint,
ret.Priority = *request.Priority
}

if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationCreated,
ResourceID: applicationDBModel.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}

c.recordApplicationEvent(ctx, applicationDBModel.ID, eventmodels.ApplicationCreated)
c.recordMemberCreatedEvent(ctx, ret.ID)
return ret, nil
}

Expand Down Expand Up @@ -498,15 +482,7 @@ func (c *controller) UpdateApplication(ctx context.Context, id uint,
}

// 5. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationUpdated,
ResourceID: applicationModel.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.recordApplicationEvent(ctx, applicationModel.ID, eventmodels.ApplicationUpdated)

// 6. get fullPath
group, err := c.groupSvc.GetChildByID(ctx, appExistsInDB.GroupID)
Expand Down Expand Up @@ -591,15 +567,7 @@ func (c *controller) UpdateApplicationV2(ctx context.Context, id uint,
}

// 6. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationUpdated,
ResourceID: appExistsInDB.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.recordApplicationEvent(ctx, appExistsInDB.ID, eventmodels.ApplicationUpdated)
return err
}

Expand Down Expand Up @@ -652,15 +620,7 @@ func (c *controller) DeleteApplication(ctx context.Context, id uint, hard bool)
}

// 4. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationDeleted,
ResourceID: id,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.recordApplicationEvent(ctx, id, eventmodels.ApplicationDeleted)

return nil
}
Expand Down Expand Up @@ -895,3 +855,36 @@ func (c *controller) GetApplicationPipelineStats(ctx context.Context, applicatio

return c.pipelinemanager.ListPipelineStats(ctx, app.Name, cluster, pageNumber, pageSize)
}

func (c *controller) recordApplicationEvent(ctx context.Context, applicationID uint, eventType string) {
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
ResourceID: applicationID,
EventType: eventType,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
}

func (c *controller) recordMemberCreatedEvent(ctx context.Context, applicationID uint) {
members, err := c.memberManager.ListDirectMember(ctx, membermodels.TypeApplication, applicationID)
if err != nil {
log.Warningf(ctx, "failed to list members of application, err: %s", err.Error())
return
}
events := make([]*eventmodels.Event, len(members))
for _, m := range members {
events = append(events, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceMember,
ResourceID: m.ID,
EventType: eventmodels.MemberCreated,
},
})
}
if _, err := c.eventMgr.CreateEvent(ctx, events...); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
}
43 changes: 5 additions & 38 deletions core/controller/cluster/controller_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,16 +553,8 @@ func (c *controller) CreateCluster(ctx context.Context, applicationID uint, envi
r.TemplateInput.Pipeline, r.TemplateInput.Application)

// 11. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterCreated,
ResourceID: ret.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}

c.recordClusterEvent(ctx, ret.ID, eventmodels.ClusterCreated)
c.recordMemberCreatedEvent(ctx, ret.ID)
return ret, nil
}

Expand Down Expand Up @@ -725,15 +717,7 @@ func (c *controller) UpdateCluster(ctx context.Context, clusterID uint,
}

// 6. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterUpdated,
ResourceID: cluster.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.recordClusterEvent(ctx, cluster.ID, eventmodels.ClusterUpdated)

// 7. get full path
group, err := c.groupSvc.GetChildByID(ctx, application.GroupID)
Expand Down Expand Up @@ -917,16 +901,7 @@ func (c *controller) DeleteCluster(ctx context.Context, clusterID uint, hard boo
}

// 5. record event
if _, err := c.eventMgr.CreateEvent(newctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterDeleted,
ResourceID: clusterID,
},
ReqID: rid,
}); err != nil {
log.Warningf(newctx, "failed to create event, err: %s", err.Error())
}
c.recordClusterEvent(newctx, clusterID, eventmodels.ClusterDeleted)
}()

return nil
Expand Down Expand Up @@ -997,15 +972,7 @@ func (c *controller) FreeCluster(ctx context.Context, clusterID uint) (err error
}

// 4. create event
if _, err := c.eventMgr.CreateEvent(newctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterFreed,
ResourceID: clusterID,
},
}); err != nil {
log.Warningf(newctx, "failed to create event, err: %s", err.Error())
}
c.recordClusterEvent(newctx, clusterID, eventmodels.ClusterFreed)
}()

return nil
Expand Down
56 changes: 37 additions & 19 deletions core/controller/cluster/controller_basic_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
collectionmodels "github.com/horizoncd/horizon/pkg/collection/models"
eventmodels "github.com/horizoncd/horizon/pkg/event/models"
"github.com/horizoncd/horizon/pkg/git"
membermodels "github.com/horizoncd/horizon/pkg/member/models"
prmodels "github.com/horizoncd/horizon/pkg/pr/models"
tagmodels "github.com/horizoncd/horizon/pkg/tag/models"
"github.com/horizoncd/horizon/pkg/templaterelease/models"
Expand Down Expand Up @@ -245,16 +246,8 @@ func (c *controller) CreateClusterV2(ctx context.Context,
}

// 12. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterCreated,
ResourceID: cluster.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}

c.recordClusterEvent(ctx, ret.ID, eventmodels.ClusterCreated)
c.recordMemberCreatedEvent(ctx, ret.ID)
// 13. customize response
return ret, nil
}
Expand Down Expand Up @@ -519,15 +512,7 @@ func (c *controller) UpdateClusterV2(ctx context.Context, clusterID uint,
}

// 7. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterUpdated,
ResourceID: cluster.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.recordClusterEvent(ctx, cluster.ID, eventmodels.ClusterUpdated)

// 8. update cluster in db
clusterModel, tags := r.toClusterModel(cluster, expireSeconds, environmentName,
Expand Down Expand Up @@ -831,6 +816,39 @@ func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
}, nil
}

func (c *controller) recordClusterEvent(ctx context.Context, clusterID uint, eventType string) {
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
ResourceID: clusterID,
EventType: eventType,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
}

func (c *controller) recordMemberCreatedEvent(ctx context.Context, clusterID uint) {
members, err := c.memberManager.ListDirectMember(ctx, membermodels.TypeApplicationCluster, clusterID)
if err != nil {
log.Warningf(ctx, "failed to list members of application, err: %s", err.Error())
return
}
events := make([]*eventmodels.Event, len(members))
for _, m := range members {
events = append(events, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceMember,
ResourceID: m.ID,
EventType: eventmodels.MemberCreated,
},
})
}
if _, err := c.eventMgr.CreateEvent(ctx, events...); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
}

func (c *controller) recordPipelinerunCreatedEvent(ctx context.Context, pr *prmodels.Pipelinerun) {
_, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
Expand Down
10 changes: 1 addition & 9 deletions core/controller/cluster/controller_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,7 @@ func (c *controller) InternalDeploy(ctx context.Context, clusterID uint,
}

// 10. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterBuildDeployed,
ResourceID: cluster.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.recordClusterEvent(ctx, cluster.ID, eventmodels.ClusterBuildDeployed)

return &InternalDeployResponse{
PipelinerunID: pr.ID,
Expand Down
27 changes: 7 additions & 20 deletions core/controller/cluster/controller_internal_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
userauth "github.com/horizoncd/horizon/pkg/authentication/user"
"github.com/horizoncd/horizon/pkg/cd"
"github.com/horizoncd/horizon/pkg/cluster/gitrepo"
"github.com/horizoncd/horizon/pkg/cluster/models"
perror "github.com/horizoncd/horizon/pkg/errors"
eventmodels "github.com/horizoncd/horizon/pkg/event/models"
prmodels "github.com/horizoncd/horizon/pkg/pr/models"
Expand Down Expand Up @@ -179,32 +178,20 @@ func (c *controller) InternalDeployV2(ctx context.Context, clusterID uint,
}

// 10. record cluster event
c.recordClusterEvent(ctx, pr, cluster)
eventType := func() string {
if pr.Action == prmodels.ActionBuildDeploy {
return eventmodels.ClusterBuildDeployed
}
return eventmodels.ClusterDeployed
}()
c.recordClusterEvent(ctx, cluster.ID, eventType)

return &InternalDeployResponseV2{
PipelinerunID: pr.ID,
Commit: configCommit.Gitops,
}, nil
}

func (c *controller) recordClusterEvent(ctx context.Context, pr *prmodels.Pipelinerun, cluster *models.Cluster) {
_, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: func() string {
if pr.Action == prmodels.ActionBuildDeploy {
return eventmodels.ClusterBuildDeployed
}
return eventmodels.ClusterDeployed
}(),
ResourceID: cluster.ID,
},
})
if err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
}

func (c *controller) retrieveClaimsAndUser(ctx context.Context) (*tokenservice.Claims, *usermodel.User, error) {
jwtTokenString, err := common.JWTTokenStringFromContext(ctx)
if err != nil {
Expand Down
20 changes: 2 additions & 18 deletions core/controller/cluster/controller_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,7 @@ func (c *controller) Restart(ctx context.Context, clusterID uint) (_ *Pipelineru
}

// 5. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterRestarted,
ResourceID: cluster.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.recordClusterEvent(ctx, cluster.ID, eventmodels.ClusterRestarted)

return &PipelinerunIDResponse{
PipelinerunID: prCreated.ID,
Expand Down Expand Up @@ -424,15 +416,7 @@ func (c *controller) Rollback(ctx context.Context,
}

// 10. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterRollbacked,
ResourceID: cluster.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.recordClusterEvent(ctx, cluster.ID, eventmodels.ClusterRollbacked)

return &PipelinerunIDResponse{
PipelinerunID: prCreated.ID,
Expand Down
Loading

0 comments on commit 3bac5fa

Please sign in to comment.