Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add webhook events of members #195

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
89 changes: 42 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,38 @@ 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) {
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
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) {
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
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, 0, len(members))
for _, m := range members {
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
events = append(events, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceMember,
ResourceID: m.ID,
EventType: eventmodels.MemberCreated,
},
})
}
if len(events) > 0 {
if _, err := c.eventMgr.CreateEvent(ctx, events...); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
}
}
6 changes: 6 additions & 0 deletions core/controller/application/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
tmodels "github.com/horizoncd/horizon/pkg/template/models"
trmodels "github.com/horizoncd/horizon/pkg/templaterelease/models"
trschema "github.com/horizoncd/horizon/pkg/templaterelease/schema"
usermodel "github.com/horizoncd/horizon/pkg/user/models"
userservice "github.com/horizoncd/horizon/pkg/user/service"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -293,6 +294,9 @@ func TestMain(m *testing.M) {
if err := db.AutoMigrate(&eventmodels.Event{}); err != nil {
panic(err)
}
if err := db.AutoMigrate(&usermodel.User{}); err != nil {
panic(err)
}
ctx = context.TODO()
ctx = context.WithValue(ctx, common.UserContextKey(), &userauth.DefaultInfo{
Name: "Tony",
Expand Down Expand Up @@ -363,6 +367,7 @@ func Test(t *testing.T) {
clusterMgr: manager.ClusterMgr,
userSvc: userservice.NewService(manager),
eventMgr: manager.EventMgr,
memberManager: manager.MemberMgr,
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
}

group, err := manager.GroupMgr.Create(ctx, &groupmodels.Group{
Expand Down Expand Up @@ -518,6 +523,7 @@ func TestV2(t *testing.T) {
clusterMgr: manager.ClusterMgr,
userSvc: userservice.NewService(manager),
eventMgr: manager.EventMgr,
memberManager: manager.MemberMgr,
}

group, err := manager.GroupMgr.Create(ctx, &groupmodels.Group{
Expand Down
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
58 changes: 39 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,41 @@ func (c *controller) createPipelineRun(ctx context.Context, clusterID uint,
}, nil
}

func (c *controller) recordClusterEvent(ctx context.Context, clusterID uint, eventType string) {
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
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) {
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
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, 0, len(members))
for _, m := range members {
events = append(events, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceMember,
ResourceID: m.ID,
EventType: eventmodels.MemberCreated,
},
})
}
if len(events) > 0 {
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 {
xuzhu-591 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading