-
Notifications
You must be signed in to change notification settings - Fork 11
/
reporter.go
360 lines (297 loc) · 12.4 KB
/
reporter.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
package msteams
import (
"strings"
s2hv1beta1 "github.com/agoda-com/samsahai/api/v1beta1"
"github.com/agoda-com/samsahai/internal"
s2herrors "github.com/agoda-com/samsahai/internal/errors"
s2hlog "github.com/agoda-com/samsahai/internal/log"
"github.com/agoda-com/samsahai/internal/util/msteams"
"github.com/agoda-com/samsahai/internal/util/template"
"github.com/agoda-com/samsahai/pkg/samsahai/rpc"
)
var logger = s2hlog.Log.WithName(ReporterName)
const (
ReporterName = "msteams"
componentUpgradeInterval = s2hv1beta1.IntervalRetry
componentUpgradeCriteria = s2hv1beta1.CriteriaFailure
styleDanger = `style="color:#EE2828"`
styleWarning = `style="color:#EEA328"`
styleInfo = `style="color:#2EB44E"`
)
type reporter struct {
msTeams msteams.MSTeams
}
// NewOption allows specifying various configuration
type NewOption func(*reporter)
// WithMSTeamsClient specifies msteams client to override when creating Microsoft Teams reporter
func WithMSTeamsClient(msTeams msteams.MSTeams) NewOption {
if msTeams == nil {
panic("Microsoft Teams client should not be nil")
}
return func(r *reporter) {
r.msTeams = msTeams
}
}
// New creates a new Microsoft Teams reporter
func New(tenantID, clientID, clientSecret, username, password string, opts ...NewOption) internal.Reporter {
r := &reporter{
msTeams: newMSTeams(tenantID, clientID, clientSecret, username, password),
}
// apply the new options
for _, opt := range opts {
opt(r)
}
return r
}
// newMSTeams returns reporter for sending report via Microsoft Teams into specific groups and channels
func newMSTeams(tenantID, clientID, clientSecret, username, password string) msteams.MSTeams {
return msteams.NewClient(tenantID, clientID, clientSecret, username, password)
}
// GetName returns msteams type
func (r *reporter) GetName() string {
return ReporterName
}
// SendComponentUpgrade implements the reporter SendComponentUpgrade function
func (r *reporter) SendComponentUpgrade(configCtrl internal.ConfigController, comp *internal.ComponentUpgradeReporter) error {
msTeamsConfig, err := r.getMSTeamsConfig(comp.TeamName, configCtrl)
if err != nil {
return nil
}
if err := r.checkMatchingInterval(msTeamsConfig, comp.IsReverify); err != nil {
return nil
}
if err := r.checkMatchingCriteria(msTeamsConfig, comp.Status); err != nil {
return nil
}
message := r.makeComponentUpgradeReport(comp)
if len(comp.ImageMissingList) > 0 {
message += "<hr/>"
message += r.makeImageMissingListReport(comp.ImageMissingList)
}
return r.post(msTeamsConfig, message, internal.ComponentUpgradeType)
}
func (r *reporter) checkMatchingInterval(msTeamsConfig *s2hv1beta1.MSTeams, isReverify bool) error {
interval := componentUpgradeInterval
if msTeamsConfig.ComponentUpgrade != nil && msTeamsConfig.ComponentUpgrade.Interval != "" {
interval = msTeamsConfig.ComponentUpgrade.Interval
}
switch interval {
case s2hv1beta1.IntervalEveryTime:
default:
if !isReverify {
return s2herrors.New("interval was not matched")
}
}
return nil
}
func (r *reporter) checkMatchingCriteria(msTeamsConfig *s2hv1beta1.MSTeams, status rpc.ComponentUpgrade_UpgradeStatus) error {
criteria := componentUpgradeCriteria
if msTeamsConfig.ComponentUpgrade != nil && msTeamsConfig.ComponentUpgrade.Criteria != "" {
criteria = msTeamsConfig.ComponentUpgrade.Criteria
}
switch criteria {
case s2hv1beta1.CriteriaBoth:
case s2hv1beta1.CriteriaSuccess:
if status != rpc.ComponentUpgrade_UpgradeStatus_SUCCESS {
return s2herrors.New("criteria was not matched")
}
default:
if status != rpc.ComponentUpgrade_UpgradeStatus_FAILURE {
return s2herrors.New("criteria was not matched")
}
}
return nil
}
// SendActivePromotionStatus implements the reporter SendActivePromotionStatus function
func (r *reporter) SendActivePromotionStatus(configCtrl internal.ConfigController, atpRpt *internal.ActivePromotionReporter) error {
msTeamsConfig, err := r.getMSTeamsConfig(atpRpt.TeamName, configCtrl)
if err != nil {
return nil
}
message := r.makeActivePromotionStatusReport(atpRpt)
imageMissingList := atpRpt.ActivePromotionStatus.PreActiveQueue.ImageMissingList
if len(imageMissingList) > 0 {
message += "<hr/>"
message += r.makeImageMissingListReport(convertImageListToRPCImageList(imageMissingList))
}
if atpRpt.HasOutdatedComponent {
message += "<hr/>"
message += r.makeOutdatedComponentsReport(atpRpt.OutdatedComponents)
} else {
message += "<br/>"
message += r.makeNoOutdatedComponentsReport()
}
message += "<br/>"
isDemotionFailed := atpRpt.DemotionStatus == s2hv1beta1.ActivePromotionDemotionFailure
if isDemotionFailed {
message += "<br/>"
message += r.makeActiveDemotingFailureReport()
}
if atpRpt.RollbackStatus == s2hv1beta1.ActivePromotionRollbackFailure {
message += "<br/>"
message += r.makeActivePromotionRollbackFailureReport()
}
hasPreviousActiveNamespace := atpRpt.PreviousActiveNamespace != ""
if atpRpt.Result == s2hv1beta1.ActivePromotionSuccess && hasPreviousActiveNamespace && !isDemotionFailed {
message += "<br/>"
message += r.makeDestroyedPreviousActiveTimeReport(&atpRpt.ActivePromotionStatus)
}
return r.post(msTeamsConfig, message, internal.ActivePromotionType)
}
func convertImageListToRPCImageList(images []s2hv1beta1.Image) []*rpc.Image {
rpcImages := make([]*rpc.Image, 0)
for _, img := range images {
rpcImages = append(rpcImages, &rpc.Image{
Repository: img.Repository,
Tag: img.Tag,
})
}
return rpcImages
}
// SendImageMissing implements the reporter SendImageMissing function
func (r *reporter) SendImageMissing(teamName string, configCtrl internal.ConfigController, images *rpc.Image) error {
msTeamsConfig, err := r.getMSTeamsConfig(teamName, configCtrl)
if err != nil {
return nil
}
message := r.makeImageMissingListReport([]*rpc.Image{images})
return r.post(msTeamsConfig, message, internal.ImageMissingType)
}
func (r *reporter) makeComponentUpgradeReport(comp *internal.ComponentUpgradeReporter) string {
message := `
<b>Component Upgrade:</b><span {{ if eq .Status 1 }}` + styleInfo + `> Success {{ else }}` + styleDanger + `> Failure{{ end }}</span>
{{- if eq .Status 0 }}
<li><b>Issue type:</b> {{ .IssueTypeStr }}</li>
{{- end }}
<li><b>Run:</b>{{ if .IsReverify }} Reverify {{ else }} #{{ .Runs }} {{ end }}</li>
<li><b>Component:</b> {{ .Name }}</li>
<li><b>Version:</b> {{ .Image.Tag }}</li>
<li><b>Repository:</b> {{ .Image.Repository }}</li>
<li><b>Owner:</b> {{ .TeamName }}</li>
<li><b>Namespace:</b> {{ .Namespace }}</li>
{{- if eq .Status 0 }}
{{- if .TestRunner.Teamcity.BuildURL }}
<li><b>Teamcity URL:</b> <a href="{{ .TestRunner.Teamcity.BuildURL }}">Click here</a></li>
{{- end }}
<li><b>Deployment Logs:</b> <a href="{{ .SamsahaiExternalURL }}/teams/{{ .TeamName }}/queue/histories/{{ .QueueHistoryName }}/log">Download here</a></li>
<li><b>Deployment History:</b> <a href="{{ .SamsahaiExternalURL }}/teams/{{ .TeamName }}/queue/histories/{{ .QueueHistoryName }}">Click here</a></li>
{{- end}}
`
return strings.TrimSpace(template.TextRender("MSTeamsComponentUpgradeFailure", message, comp))
}
func (r *reporter) makeActivePromotionStatusReport(comp *internal.ActivePromotionReporter) string {
var message = `
<b>Active Promotion:</b> <span {{ if eq .Result "Success" }}` + styleInfo + `{{ else if eq .Result "Failure" }}` + styleDanger + `{{ end }}>{{ .Result }}</span>
{{- if ne .Result "Success" }}
{{- range .Conditions }}
{{- if eq .Type "` + string(s2hv1beta1.ActivePromotionCondActivePromoted) + `" }}
<br/><b>Reason:</b> {{ .Message }}
{{- end }}
{{- end }}
{{- end }}
<li><b>Current Active Namespace:</b> {{ .CurrentActiveNamespace }}</li>
<li><b>Owner:</b> {{ .TeamName }}</li>
{{- if and .PreActiveQueue.TestRunner (and .PreActiveQueue.TestRunner.Teamcity .PreActiveQueue.TestRunner.Teamcity.BuildURL) }}
<li><b>Teamcity URL:</b> <a href="{{ .PreActiveQueue.TestRunner.Teamcity.BuildURL }}">Click here</a></li>
{{- end }}
{{- if eq .Result "Failure" }}
<li><b>Deployment Logs:</b> <a href="{{ .SamsahaiExternalURL }}/teams/{{ .TeamName }}/activepromotions/histories/{{ .ActivePromotionHistoryName }}/log">Download here</a></li>
{{- end }}
<li><b>Active Promotion History:</b> <a href="{{ .SamsahaiExternalURL }}/teams/{{ .TeamName }}/activepromotions/histories/{{ .ActivePromotionHistoryName }}">Click here</a></li>
`
return strings.TrimSpace(template.TextRender("MSTeamsActivePromotionStatus", message, comp))
}
func (r *reporter) makeOutdatedComponentsReport(comps map[string]s2hv1beta1.OutdatedComponent) string {
var message = `
<b>Outdated Components:</b>
{{- range $name, $component := .Components }}
{{- if gt .OutdatedDuration 0 }}
<br/>
<b>{{ $name }}</b>
<li>Not update for {{ .OutdatedDuration | FmtDurationToStr }}</li>
<li>Current Version: <a href="{{ .CurrentImage.Repository | ConcatHTTPStr }}">{{ .CurrentImage.Tag }}</a></li>
<li>Latest Version: <a href="{{ .DesiredImage.Repository | ConcatHTTPStr }}">{{ .DesiredImage.Tag }}</a></li>
{{- end }}
{{- end }}
`
ocObj := struct {
Components map[string]s2hv1beta1.OutdatedComponent
}{Components: comps}
return strings.TrimSpace(template.TextRender("MSTeamsOutdatedComponents", message, ocObj))
}
func (r *reporter) makeNoOutdatedComponentsReport() string {
var message = `
<li><b>All components are up to date!</b></li>
`
return strings.TrimSpace(template.TextRender("MSTeamsNoOutdatedComponents", message, ""))
}
func (r *reporter) makeActivePromotionRollbackFailureReport() string {
var message = "<b " + styleDanger + ">ERROR:</b> cannot rollback an active promotion process due to timeout"
return strings.TrimSpace(template.TextRender("RollbackFailure", message, ""))
}
func (r *reporter) makeActiveDemotingFailureReport() string {
var message = "<b " + styleWarning + ">WARNING:</b> cannot demote a previous active environment, previous active namespace has been destroyed immediately"
return strings.TrimSpace(template.TextRender("DemotionFailure", message, ""))
}
func (r *reporter) makeDestroyedPreviousActiveTimeReport(status *s2hv1beta1.ActivePromotionStatus) string {
var message = "<b " + styleWarning + ">NOTES:</b> previous active namespace <code>{{ .PreviousActiveNamespace }}</code> will be destroyed at <code>{{ .DestroyedTime | TimeFormat }}</code>"
return strings.TrimSpace(template.TextRender("DestroyedTime", message, status))
}
// makeImageMissingListReport implements the reporter makeImageMissingListReport function
func (r *reporter) makeImageMissingListReport(images []*rpc.Image) string {
var message = `
<b>Image Missing List</b>
{{- range .Images }}
<li>{{ .Repository }}:{{ .Tag }}</li>
{{- end }}
`
imagesObj := struct{ Images []*rpc.Image }{Images: images}
return strings.TrimSpace(template.TextRender("MSTeamsImageMissingList", message, imagesObj))
}
func (r *reporter) post(msTeamsConfig *s2hv1beta1.MSTeams, message string, event internal.EventType) error {
logger.Debug("start sending message to Microsoft Teams groups and channels",
"event", event, "groups", msTeamsConfig.Groups)
accessToken, err := r.msTeams.GetAccessToken()
if err != nil {
logger.Error(err, "cannot get Microsoft access token",
"event", event, "groups", msTeamsConfig.Groups)
return err
}
var globalErr error
for _, group := range msTeamsConfig.Groups {
// get group id from group name
groupID, err := r.msTeams.GetGroupID(group.GroupNameOrID, accessToken)
if err != nil {
logger.Error(err, "cannot get group id",
"event", event, "group", group.GroupNameOrID)
continue
}
for _, channelNameOrID := range group.ChannelNameOrIDs {
// get channel id from channel name
channelID, err := r.msTeams.GetChannelID(groupID, channelNameOrID, accessToken)
if err != nil {
logger.Error(err, "cannot get channel id",
"event", event, "group", group.GroupNameOrID, "channel", channelNameOrID)
continue
}
if err := r.msTeams.PostMessage(groupID, channelID, message, accessToken, msteams.WithContentType(msteams.HTML)); err != nil {
logger.Error(err, "cannot post message to Microsoft Teams",
"event", event, "group", group.GroupNameOrID, "channel", channelNameOrID)
globalErr = err
continue
}
}
}
return globalErr
}
func (r *reporter) getMSTeamsConfig(teamName string, configCtrl internal.ConfigController) (*s2hv1beta1.MSTeams, error) {
config, err := configCtrl.Get(teamName)
if err != nil {
return nil, err
}
// no Microsoft Teams configuration
if config.Spec.Reporter == nil || config.Spec.Reporter.MSTeams == nil {
return nil, s2herrors.New("msTeams configuration not found")
}
return config.Spec.Reporter.MSTeams, nil
}