-
Notifications
You must be signed in to change notification settings - Fork 11
/
reporter.go
285 lines (239 loc) · 9.58 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
package internal
import (
"os"
"strings"
s2hv1 "github.com/agoda-com/samsahai/api/v1"
"github.com/agoda-com/samsahai/pkg/samsahai/rpc"
)
// EventType represents an event type of reporter
type EventType string
const (
ComponentUpgradeType EventType = "ComponentUpgrade"
ActivePromotionType EventType = "ActivePromotion"
ImageMissingType EventType = "ImageMissing"
PullRequestTriggerType EventType = "PullRequestTrigger"
PullRequestQueueType EventType = "PullRequestQueue"
ActiveEnvironmentDeletedType EventType = "ActiveEnvironmentDeleted"
)
// ComponentUpgradeOption allows specifying various configuration
type ComponentUpgradeOption func(*ComponentUpgradeReporter)
// WithTestRunner specifies test runner to override when creating component upgrade reporter object
func WithTestRunner(tr s2hv1.TestRunner) ComponentUpgradeOption {
return func(c *ComponentUpgradeReporter) {
c.TestRunner = tr
}
}
// WithQueueHistoryName specifies queuehistory name to override when creating component upgrade reporter object
// QueueHistoryName will be the latest failure of component upgrade
// if reverification is success, QueueHistoryName will be the history of queue before running reverification
func WithQueueHistoryName(qHist string) ComponentUpgradeOption {
return func(c *ComponentUpgradeReporter) {
c.QueueHistoryName = qHist
}
}
// WithNamespace specifies namespace to override when creating component upgrade reporter object
func WithNamespace(ns string) ComponentUpgradeOption {
return func(c *ComponentUpgradeReporter) {
if ns != "" {
c.Namespace = ns
}
}
}
// WithComponentUpgradeOptCredential specifies credential to override when create component upgrade reporter object
func WithComponentUpgradeOptCredential(creds s2hv1.Credential) ComponentUpgradeOption {
return func(c *ComponentUpgradeReporter) {
c.Credential = creds
}
}
// ComponentUpgradeReporter manages component upgrade report
type ComponentUpgradeReporter struct {
IssueTypeStr IssueType `json:"issueTypeStr,omitempty"`
StatusStr StatusType `json:"statusStr,omitempty"`
StatusInt int32 `json:"statusInt,omitempty"`
TestRunner s2hv1.TestRunner `json:"testRunner,omitempty"`
Credential s2hv1.Credential `json:"credential,omitempty"`
Envs map[string]string
*rpc.ComponentUpgrade
SamsahaiConfig
}
// NewComponentUpgradeReporter creates component upgrade reporter from rpc object
func NewComponentUpgradeReporter(comp *rpc.ComponentUpgrade, s2hConfig SamsahaiConfig, opts ...ComponentUpgradeOption) *ComponentUpgradeReporter {
c := &ComponentUpgradeReporter{
ComponentUpgrade: comp,
SamsahaiConfig: s2hConfig,
IssueTypeStr: convertIssueType(comp.IssueType),
StatusStr: convertStatusType(comp.Status),
StatusInt: int32(comp.Status),
Envs: listEnv(),
}
// apply the new options
for _, opt := range opts {
opt(c)
}
return c
}
// StatusType represents an active promotion type
type StatusType string
const (
StatusSuccess StatusType = "Success"
StatusFailure StatusType = "Failure"
StatusCanceled StatusType = "Canceled"
)
// IssueType represents an issue type of component upgrade failure
type IssueType string
const (
IssueUnknown IssueType = "Unknown issue"
IssueDesiredVersionFailed IssueType = "Desired component failed"
IssueImageMissing IssueType = "Image missing"
IssueEnvironment IssueType = "Environment issue - Verification failed"
)
// ActivePromotionOption allows specifying various configuration
type ActivePromotionOption func(*ActivePromotionReporter)
// TODO: should override tc credential per team
// WithActivePromotionOptCredential specifies credential to override when create active promotion reporter object
func WithActivePromotionOptCredential(creds s2hv1.Credential) ActivePromotionOption {
return func(c *ActivePromotionReporter) {
c.Credential = creds
}
}
// ActivePromotionReporter manages active promotion report
type ActivePromotionReporter struct {
TeamName string `json:"teamName,omitempty"`
CurrentActiveNamespace string `json:"currentActiveNamespace,omitempty"`
Runs int `json:"runs,omitempty"`
Credential s2hv1.Credential `json:"credential,omitempty"`
Envs map[string]string
s2hv1.ActivePromotionStatus
SamsahaiConfig
}
// NewActivePromotionReporter creates active promotion reporter object
func NewActivePromotionReporter(status s2hv1.ActivePromotionStatus, s2hConfig SamsahaiConfig,
teamName, currentNs string, runs int, opts ...ActivePromotionOption) *ActivePromotionReporter {
c := &ActivePromotionReporter{
SamsahaiConfig: s2hConfig,
TeamName: teamName,
CurrentActiveNamespace: currentNs,
Runs: runs,
ActivePromotionStatus: status,
Envs: listEnv(),
}
// apply the new options
for _, opt := range opts {
opt(c)
}
return c
}
// ImageMissingReporter manages image missing report
type ImageMissingReporter struct {
TeamName string `json:"teamName,omitempty"`
ComponentName string `json:"componentName,omitempty"`
// Reason represents error reason
Reason string `json:"reason,omitempty"`
Envs map[string]string
s2hv1.Image
SamsahaiConfig
}
// NewImageMissingReporter creates image missing reporter object
func NewImageMissingReporter(image s2hv1.Image, s2hConfig SamsahaiConfig,
teamName, compName, reason string) *ImageMissingReporter {
c := &ImageMissingReporter{
SamsahaiConfig: s2hConfig,
TeamName: teamName,
ComponentName: compName,
Image: image,
Envs: listEnv(),
Reason: reason,
}
return c
}
// PullRequestTriggerReporter manages pull request trigger report
type PullRequestTriggerReporter struct {
TeamName string `json:"teamName,omitempty"`
BundleName string `json:"bundleName,omitempty"`
PRNumber string `json:"prNumber,omitempty"`
Result string `json:"result,omitempty"`
Components []*s2hv1.PullRequestTriggerComponent `json:"components,omitempty"`
NoOfRetry int `json:"noOfRetry,omitempty"`
s2hv1.PullRequestTriggerStatus
SamsahaiConfig
}
// NewPullRequestTriggerResultReporter creates pull request trigger result reporter object
func NewPullRequestTriggerResultReporter(status s2hv1.PullRequestTriggerStatus, s2hConfig SamsahaiConfig,
teamName, bundleName, prNumber, result string, noOfRetry int,
comps []*s2hv1.PullRequestTriggerComponent) *PullRequestTriggerReporter {
c := &PullRequestTriggerReporter{
PullRequestTriggerStatus: status,
TeamName: teamName,
BundleName: bundleName,
PRNumber: prNumber,
Result: result,
Components: comps,
SamsahaiConfig: s2hConfig,
NoOfRetry: noOfRetry,
}
return c
}
// ActiveEnvironmentDeletedReporter manages active namespace deletion report
type ActiveEnvironmentDeletedReporter struct {
TeamName string `json:"teamName,omitempty"`
ActiveNamespace string `json:"activeNamespace,omitempty"`
DeletedBy string `json:"deletedBy,omitempty"`
DeletedAt string `json:"deletedAt,omitempty"`
}
// NewActiveEnvironmentDeletedReporter creates deleted active namespace reporter object
func NewActiveEnvironmentDeletedReporter(teamname, activeNs, deletedBy, deleteAt string) *ActiveEnvironmentDeletedReporter {
c := &ActiveEnvironmentDeletedReporter{
TeamName: teamname,
ActiveNamespace: activeNs,
DeletedBy: deletedBy,
DeletedAt: deleteAt,
}
return c
}
func convertIssueType(issueType rpc.ComponentUpgrade_IssueType) IssueType {
switch issueType {
case rpc.ComponentUpgrade_IssueType_DESIRED_VERSION_FAILED:
return IssueDesiredVersionFailed
case rpc.ComponentUpgrade_IssueType_ENVIRONMENT_ISSUE:
return IssueEnvironment
case rpc.ComponentUpgrade_IssueType_IMAGE_MISSING:
return IssueImageMissing
default:
return IssueUnknown
}
}
func convertStatusType(statusType rpc.ComponentUpgrade_UpgradeStatus) StatusType {
switch statusType {
case rpc.ComponentUpgrade_UpgradeStatus_SUCCESS:
return StatusSuccess
case rpc.ComponentUpgrade_UpgradeStatus_CANCELED:
return StatusCanceled
default:
return StatusFailure
}
}
func listEnv() map[string]string {
env := make(map[string]string)
for _, setting := range os.Environ() {
pair := strings.SplitN(setting, "=", 2)
env[pair[0]] = pair[1]
}
return env
}
// Reporter is the interface of reporter
type Reporter interface {
// GetName returns type of reporter
GetName() string
// SendComponentUpgrade sends details of component upgrade
SendComponentUpgrade(configCtrl ConfigController, comp *ComponentUpgradeReporter) error
// SendPullRequestQueue sends details of pull request deployment queue
SendPullRequestQueue(configCtrl ConfigController, comp *ComponentUpgradeReporter) error
// SendActivePromotionStatus sends active promotion status
SendActivePromotionStatus(configCtrl ConfigController, atpRpt *ActivePromotionReporter) error
// SendImageMissing sends image missing
SendImageMissing(configCtrl ConfigController, imageMissingRpt *ImageMissingReporter) error
// SendPullRequestTriggerResult sends pull request trigger result information
SendPullRequestTriggerResult(configCtrl ConfigController, prTriggerRpt *PullRequestTriggerReporter) error
// SendActiveEnvironmentDeleted send active namespace deleted information
SendActiveEnvironmentDeleted(configCtrl ConfigController, activeNsDeletedRpt *ActiveEnvironmentDeletedReporter) error
}