-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathEventClient.go
275 lines (255 loc) · 10.5 KB
/
EventClient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright (c) 2020-2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/caarlos0/env"
pubsub "github.com/devtron-labs/common-lib/pubsub-lib"
"github.com/devtron-labs/devtron/api/bean"
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
bean2 "github.com/devtron-labs/devtron/pkg/attributes/bean"
buildBean "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
"github.com/devtron-labs/devtron/pkg/module"
bean3 "github.com/devtron-labs/devtron/pkg/module/bean"
util "github.com/devtron-labs/devtron/util/event"
"go.uber.org/zap"
"net/http"
)
type EventClientConfig struct {
DestinationURL string `env:"EVENT_URL" envDefault:"http://localhost:3000/notify"`
NotificationMedium NotificationMedium `env:"NOTIFICATION_MEDIUM" envDefault:"rest"`
}
type NotificationMedium string
const PUB_SUB NotificationMedium = "nats"
func GetEventClientConfig() (*EventClientConfig, error) {
cfg := &EventClientConfig{}
err := env.Parse(cfg)
if err != nil {
return nil, errors.New("could not get event service url")
}
return cfg, err
}
type EventClient interface {
WriteNotificationEvent(event Event) (bool, error)
WriteNatsEvent(channel string, payload interface{}) error
}
type Event struct {
EventTypeId int `json:"eventTypeId"`
EventName string `json:"eventName"`
PipelineId int `json:"pipelineId"`
PipelineType string `json:"pipelineType"`
CorrelationId string `json:"correlationId"`
Payload *Payload `json:"payload"`
EventTime string `json:"eventTime"`
TeamId int `json:"teamId"`
AppId int `json:"appId"`
EnvId int `json:"envId"`
IsProdEnv bool `json:"isProdEnv"`
ClusterId int `json:"clusterId"`
CdWorkflowType bean.WorkflowType `json:"cdWorkflowType,omitempty"`
CdWorkflowRunnerId int `json:"cdWorkflowRunnerId"`
CiWorkflowRunnerId int `json:"ciWorkflowRunnerId"`
CiArtifactId int `json:"ciArtifactId"`
BaseUrl string `json:"baseUrl"`
UserId int `json:"-"`
}
type Payload struct {
AppName string `json:"appName"`
EnvName string `json:"envName"`
PipelineName string `json:"pipelineName"`
Source string `json:"source"`
DockerImageUrl string `json:"dockerImageUrl"`
TriggeredBy string `json:"triggeredBy"`
Stage string `json:"stage"`
DeploymentHistoryLink string `json:"deploymentHistoryLink"`
AppDetailLink string `json:"appDetailLink"`
DownloadLink string `json:"downloadLink"`
BuildHistoryLink string `json:"buildHistoryLink"`
MaterialTriggerInfo *buildBean.MaterialTriggerInfo `json:"material"`
FailureReason string `json:"failureReason"`
}
type EventRESTClientImpl struct {
logger *zap.SugaredLogger
client *http.Client
config *EventClientConfig
pubsubClient *pubsub.PubSubClientServiceImpl
ciPipelineRepository pipelineConfig.CiPipelineRepository
pipelineRepository pipelineConfig.PipelineRepository
attributesRepository repository.AttributesRepository
moduleService module.ModuleService
}
func NewEventRESTClientImpl(logger *zap.SugaredLogger, client *http.Client, config *EventClientConfig, pubsubClient *pubsub.PubSubClientServiceImpl,
ciPipelineRepository pipelineConfig.CiPipelineRepository, pipelineRepository pipelineConfig.PipelineRepository,
attributesRepository repository.AttributesRepository, moduleService module.ModuleService) *EventRESTClientImpl {
return &EventRESTClientImpl{logger: logger, client: client, config: config, pubsubClient: pubsubClient,
ciPipelineRepository: ciPipelineRepository, pipelineRepository: pipelineRepository,
attributesRepository: attributesRepository, moduleService: moduleService}
}
func (impl *EventRESTClientImpl) buildFinalPayload(event Event, cdPipeline *pipelineConfig.Pipeline, ciPipeline *pipelineConfig.CiPipeline) *Payload {
payload := event.Payload
if payload == nil {
payload = &Payload{}
}
if event.PipelineType == string(util.CD) {
if cdPipeline != nil {
payload.AppName = cdPipeline.App.AppName
payload.EnvName = cdPipeline.Environment.Name
payload.PipelineName = cdPipeline.Name
}
payload.Stage = string(event.CdWorkflowType)
payload.DeploymentHistoryLink = fmt.Sprintf("/dashboard/app/%d/cd-details/%d/%d/%d/source-code", event.AppId, event.EnvId, event.PipelineId, event.CdWorkflowRunnerId)
payload.AppDetailLink = fmt.Sprintf("/dashboard/app/%d/details/%d/pod", event.AppId, event.EnvId)
if event.CdWorkflowType != bean.CD_WORKFLOW_TYPE_DEPLOY {
payload.DownloadLink = fmt.Sprintf("/orchestrator/app/cd-pipeline/workflow/download/%d/%d/%d/%d", event.AppId, event.EnvId, event.PipelineId, event.CdWorkflowRunnerId)
}
} else if event.PipelineType == string(util.CI) {
if ciPipeline != nil {
payload.AppName = ciPipeline.App.AppName
payload.PipelineName = ciPipeline.Name
}
payload.BuildHistoryLink = fmt.Sprintf("/dashboard/app/%d/ci-details/%d/%d/artifacts", event.AppId, event.PipelineId, event.CiWorkflowRunnerId)
}
return payload
}
func (impl *EventRESTClientImpl) WriteNotificationEvent(event Event) (bool, error) {
// if notification integration is not installed then do not send the notification
moduleInfo, err := impl.moduleService.GetModuleInfo(bean3.ModuleNameNotification)
if err != nil {
impl.logger.Errorw("error while getting notification module status", "err", err)
return false, err
}
if moduleInfo.Status != bean3.ModuleStatusInstalled {
impl.logger.Warnw("Notification module is not installed, hence skipping sending notification", "currentModuleStatus", moduleInfo.Status)
return false, nil
}
var cdPipeline *pipelineConfig.Pipeline
var ciPipeline *pipelineConfig.CiPipeline
if event.PipelineId > 0 {
if event.PipelineType == string(util.CD) {
cdPipeline, err = impl.pipelineRepository.FindById(event.PipelineId)
if err != nil {
impl.logger.Errorw("error while fetching pipeline", "err", err)
return false, err
}
if cdPipeline != nil {
event.TeamId = cdPipeline.App.TeamId
}
} else if event.PipelineType == string(util.CI) {
ciPipeline, err = impl.ciPipelineRepository.FindById(event.PipelineId)
if err != nil {
impl.logger.Errorw("error while fetching pipeline", "err", err)
return false, err
}
if ciPipeline != nil {
event.TeamId = ciPipeline.App.TeamId
}
}
}
payload := impl.buildFinalPayload(event, cdPipeline, ciPipeline)
event.Payload = payload
isPreStageExist := false
isPostStageExist := false
if cdPipeline != nil && len(cdPipeline.PreStageConfig) > 0 {
isPreStageExist = true
}
if cdPipeline != nil && len(cdPipeline.PostStageConfig) > 0 {
isPostStageExist = true
}
attribute, err := impl.attributesRepository.FindByKey(bean2.HostUrlKey)
if err != nil {
impl.logger.Errorw("there is host url configured", "ci pipeline", ciPipeline)
return false, err
}
if attribute != nil {
event.BaseUrl = attribute.Value
}
if event.CdWorkflowType == "" {
_, err = impl.sendEvent(event)
} else if event.CdWorkflowType == bean.CD_WORKFLOW_TYPE_PRE {
if event.EventTypeId == int(util.Success) {
impl.logger.Debug("skip - will send from deployment or post stage")
} else {
_, err = impl.sendEvent(event)
}
} else if event.CdWorkflowType == bean.CD_WORKFLOW_TYPE_DEPLOY {
if isPreStageExist && event.EventTypeId == int(util.Trigger) {
impl.logger.Debug("skip - already sent from pre stage")
} else if isPostStageExist && event.EventTypeId == int(util.Success) {
impl.logger.Debug("skip - will send from post stage")
} else {
_, err = impl.sendEvent(event)
}
} else if event.CdWorkflowType == bean.CD_WORKFLOW_TYPE_POST {
if event.EventTypeId == int(util.Trigger) {
impl.logger.Debug("skip - already sent from pre or deployment stage")
} else {
_, err = impl.sendEvent(event)
}
}
return true, err
}
func (impl *EventRESTClientImpl) sendEventsOnNats(body []byte) error {
err := impl.pubsubClient.Publish(pubsub.NOTIFICATION_EVENT_TOPIC, string(body))
if err != nil {
impl.logger.Errorw("err while publishing msg for testing topic", "msg", body, "err", err)
return err
}
return nil
}
// do not call this method if notification module is not installed
func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
impl.logger.Debugw("event before send", "event", event)
body, err := json.Marshal(event)
if err != nil {
impl.logger.Errorw("error while marshaling event request ", "err", err)
return false, err
}
if impl.config.NotificationMedium == PUB_SUB {
err = impl.sendEventsOnNats(body)
if err != nil {
impl.logger.Errorw("error while publishing event ", "err", err)
return false, err
}
return true, nil
}
var reqBody = []byte(body)
req, err := http.NewRequest(http.MethodPost, impl.config.DestinationURL, bytes.NewBuffer(reqBody))
if err != nil {
impl.logger.Errorw("error while writing event", "err", err)
return false, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := impl.client.Do(req)
if err != nil {
impl.logger.Errorw("error while UpdateJiraTransition request ", "err", err)
return false, err
}
defer resp.Body.Close()
impl.logger.Debugw("event completed", "event resp", resp)
return true, err
}
func (impl *EventRESTClientImpl) WriteNatsEvent(topic string, payload interface{}) error {
body, err := json.Marshal(payload)
if err != nil {
return err
}
err = impl.pubsubClient.Publish(topic, string(body))
return err
}