-
Notifications
You must be signed in to change notification settings - Fork 11
/
reporter.go
196 lines (162 loc) · 6.07 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
package internal
import (
"os"
"strings"
s2hv1beta1 "github.com/agoda-com/samsahai/api/v1beta1"
"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"
)
// ComponentUpgradeOption allows specifying various configuration
type ComponentUpgradeOption func(*ComponentUpgradeReporter)
// WithTestRunner specifies test runner to override when creating component upgrade reporter object
func WithTestRunner(tr s2hv1beta1.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
}
}
// ComponentUpgradeReporter manages component upgrade report
type ComponentUpgradeReporter struct {
IssueTypeStr IssueType `json:"issueTypeStr,omitempty"`
StatusStr StatusType `json:"statusStr,omitempty"`
StatusInt int32 `json:"statusInt,omitempty"`
TestRunner s2hv1beta1.TestRunner `json:"testRunner,omitempty"`
Credential s2hv1beta1.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"
)
// IssueType represents an issue type of component upgrade failure
type IssueType string
const (
IssueUnknown IssueType = "Unknown issue"
IssueDesiredVersionFailed IssueType = "Desired component failed - Please check your test"
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
// WithCredential specifies credential to override when create active promotion reporter object
func WithCredential(creds s2hv1beta1.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"`
Credential s2hv1beta1.Credential `json:"credential,omitempty"`
Envs map[string]string
s2hv1beta1.ActivePromotionStatus
SamsahaiConfig
}
// NewActivePromotionReporter creates active promotion reporter object
func NewActivePromotionReporter(status *s2hv1beta1.ActivePromotionStatus, s2hConfig SamsahaiConfig, teamName, currentNs string, opts ...ActivePromotionOption) *ActivePromotionReporter {
c := &ActivePromotionReporter{
SamsahaiConfig: s2hConfig,
TeamName: teamName,
CurrentActiveNamespace: currentNs,
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"`
Envs map[string]string
*rpc.Image
SamsahaiConfig
}
// NewImageMissingReporter creates image missing reporter object
func NewImageMissingReporter(image *rpc.Image, s2hConfig SamsahaiConfig, teamName, compName string) *ImageMissingReporter {
c := &ImageMissingReporter{
SamsahaiConfig: s2hConfig,
TeamName: teamName,
ComponentName: compName,
Image: image,
Envs: listEnv(),
}
return c
}
// 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
// SendActivePromotionStatus sends active promotion status
SendActivePromotionStatus(configCtrl ConfigController, atpRpt *ActivePromotionReporter) error
// SendImageMissing sends image missing
SendImageMissing(configCtrl ConfigController, imageMissingRpt *ImageMissingReporter) error
}
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
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
}