-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathEventClient.go
378 lines (342 loc) · 14.4 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* 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"
"context"
"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" description:"Notifier service url"`
NotificationMedium NotificationMedium `env:"NOTIFICATION_MEDIUM" envDefault:"rest" description:"notification medium"`
EnableNotifierV2 bool `env:"ENABLE_NOTIFIER_V2" envDefault:"false" description:"enable notifier v2"`
}
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"`
EnvIdsForCiPipeline []int `json:"envIdsForCiPipeline"`
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
notificationSettingsRepository repository.NotificationSettingsRepository
}
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,
notificationSettingsRepository repository.NotificationSettingsRepository) *EventRESTClientImpl {
return &EventRESTClientImpl{logger: logger, client: client, config: config, pubsubClient: pubsubClient,
ciPipelineRepository: ciPipelineRepository, pipelineRepository: pipelineRepository,
attributesRepository: attributesRepository, moduleService: moduleService,
notificationSettingsRepository: notificationSettingsRepository}
}
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)
// Step 1: Create payload and destination URL based on config
bodyBytes, destinationUrl, err := impl.createPayloadAndDestination(event)
if err != nil {
return false, err
}
// Step 2: Send via appropriate medium (NATS or REST)
return impl.deliverEvent(bodyBytes, destinationUrl)
}
func (impl *EventRESTClientImpl) createPayloadAndDestination(event Event) ([]byte, string, error) {
if impl.config.EnableNotifierV2 {
return impl.createV2PayloadAndDestination(event)
}
return impl.createDefaultPayloadAndDestination(event)
}
func (impl *EventRESTClientImpl) createV2PayloadAndDestination(event Event) ([]byte, string, error) {
destinationUrl := impl.config.DestinationURL + "/v2"
// Fetch notification settings
req := repository.GetRulesRequest{
TeamId: event.TeamId,
EnvId: event.EnvId,
AppId: event.AppId,
PipelineId: event.PipelineId,
PipelineType: event.PipelineType,
IsProdEnv: &event.IsProdEnv,
ClusterId: event.ClusterId,
EnvIdsForCiPipeline: event.EnvIdsForCiPipeline,
}
notificationSettings, err := impl.notificationSettingsRepository.FindNotificationSettingsWithRules(
context.Background(), event.EventTypeId, req,
)
if err != nil {
impl.logger.Errorw("error while fetching notification settings", "err", err)
return nil, "", err
}
// Process notification settings into beans
notificationSettingsBean, err := impl.processNotificationSettings(notificationSettings)
if err != nil {
return nil, "", err
}
// Create combined payload
combinedPayload := map[string]interface{}{
"event": event,
"notificationSettings": notificationSettingsBean,
}
bodyBytes, err := json.Marshal(combinedPayload)
if err != nil {
impl.logger.Errorw("error while marshaling combined event request", "err", err)
return nil, "", err
}
return bodyBytes, destinationUrl, nil
}
func (impl *EventRESTClientImpl) createDefaultPayloadAndDestination(event Event) ([]byte, string, error) {
bodyBytes, err := json.Marshal(event)
if err != nil {
impl.logger.Errorw("error while marshaling event request", "err", err)
return nil, "", err
}
return bodyBytes, impl.config.DestinationURL, nil
}
func (impl *EventRESTClientImpl) processNotificationSettings(notificationSettings []repository.NotificationSettings) ([]*repository.NotificationSettingsBean, error) {
notificationSettingsBean := make([]*repository.NotificationSettingsBean, 0)
for _, item := range notificationSettings {
config := make([]repository.ConfigEntry, 0)
if item.Config != "" {
if err := json.Unmarshal([]byte(item.Config), &config); err != nil {
impl.logger.Errorw("error while unmarshaling config", "err", err)
return nil, err
}
}
notificationSettingsBean = append(notificationSettingsBean, &repository.NotificationSettingsBean{
Id: item.Id,
TeamId: item.TeamId,
AppId: item.AppId,
EnvId: item.EnvId,
PipelineId: item.PipelineId,
PipelineType: item.PipelineType,
EventTypeId: item.EventTypeId,
Config: config,
ViewId: item.ViewId,
})
}
return notificationSettingsBean, nil
}
func (impl *EventRESTClientImpl) deliverEvent(bodyBytes []byte, destinationUrl string) (bool, error) {
if impl.config.NotificationMedium == PUB_SUB {
if err := impl.sendEventsOnNats(bodyBytes); err != nil {
impl.logger.Errorw("error while publishing event", "err", err)
return false, err
}
return true, nil
}
req, err := http.NewRequest(http.MethodPost, destinationUrl, bytes.NewBuffer(bodyBytes))
if err != nil {
impl.logger.Errorw("error while creating HTTP request", "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 sending HTTP request", "err", err)
return false, err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
impl.logger.Errorw("unexpected response from notifier", "status", resp.StatusCode)
return false, fmt.Errorf("unexpected response code: %d", resp.StatusCode)
}
impl.logger.Debugw("event successfully delivered", "status", resp.StatusCode)
return true, nil
}
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
}