@@ -22,7 +22,6 @@ import (
22
22
"embed"
23
23
"fmt"
24
24
"io"
25
- "log"
26
25
"math"
27
26
"net/http"
28
27
"os"
@@ -91,7 +90,7 @@ func getConfig() (*Config, error) {
91
90
return & c , nil
92
91
}
93
92
94
- func New (ctx context.Context ) (* Options , error ) {
93
+ func NewOptions (ctx context.Context ) (* Options , error ) {
95
94
config , err := getConfig ()
96
95
if err != nil {
97
96
return nil , fmt .Errorf ("failed to get config: %w" , err )
@@ -102,7 +101,6 @@ func New(ctx context.Context) (*Options, error) {
102
101
Region : aws .String (config .AWSRegion ),
103
102
})
104
103
if err != nil {
105
- log .Println ("Error occurred while creating aws session" , err )
106
104
return nil , err
107
105
}
108
106
@@ -114,20 +112,21 @@ func New(ctx context.Context) (*Options, error) {
114
112
}
115
113
116
114
func handler (ctx context.Context , request events.APIGatewayProxyRequest ) (events.APIGatewayProxyResponse , error ) { //nolint: gocritic
117
- o , err := New (ctx )
115
+ o , err := NewOptions (ctx )
118
116
if err != nil {
119
117
return events.APIGatewayProxyResponse {
120
118
Body : `{"status": "nok"}` ,
121
119
StatusCode : http .StatusInternalServerError ,
122
120
}, err
123
121
}
124
122
123
+ logrus .Infof ("Will pull the patch release schedule from: %s" , o .Config .SchedulePath )
125
124
data , err := loadFileOrURL (o .Config .SchedulePath )
126
125
if err != nil {
127
126
return events.APIGatewayProxyResponse {
128
127
Body : `{"status": "nok"}` ,
129
128
StatusCode : http .StatusInternalServerError ,
130
- }, fmt .Errorf ("failed to read the file: %w" , err )
129
+ }, fmt .Errorf ("reading the file: %w" , err )
131
130
}
132
131
133
132
patchSchedule := & model.PatchSchedule {}
@@ -138,7 +137,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
138
137
return events.APIGatewayProxyResponse {
139
138
Body : `{"status": "nok"}` ,
140
139
StatusCode : http .StatusInternalServerError ,
141
- }, fmt .Errorf ("failed to decode the file: %w" , err )
140
+ }, fmt .Errorf ("decoding the file: %w" , err )
142
141
}
143
142
144
143
output := & Template {}
@@ -157,7 +156,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
157
156
currentTime := time .Now ().UTC ()
158
157
days := t .Sub (currentTime ).Hours () / 24
159
158
intDay , _ := math .Modf (days )
160
- logrus .Infof ("cherry pick deadline: %d, days to alert: %d" , int (intDay ), o .Config .DaysToAlert )
159
+ logrus .Infof ("Cherry pick deadline: %d, days to alert: %d" , int (intDay ), o .Config .DaysToAlert )
161
160
if int (intDay ) == o .Config .DaysToAlert {
162
161
output .Releases = append (output .Releases , TemplateRelease {
163
162
Release : patch .Release ,
@@ -167,6 +166,14 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
167
166
}
168
167
}
169
168
169
+ if ! shouldSendEmail {
170
+ logrus .Info ("No email is needed to send" )
171
+ return events.APIGatewayProxyResponse {
172
+ Body : `{"status": "ok"}` ,
173
+ StatusCode : http .StatusOK ,
174
+ }, nil
175
+ }
176
+
170
177
tmpl , err := template .ParseFS (tpls , "templates/email.tmpl" )
171
178
if err != nil {
172
179
return events.APIGatewayProxyResponse {
@@ -181,19 +188,11 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
181
188
return events.APIGatewayProxyResponse {
182
189
Body : `{"status": "nok"}` ,
183
190
StatusCode : http .StatusInternalServerError ,
184
- }, fmt .Errorf ("parsing values to the template: %w" , err )
185
- }
186
-
187
- if ! shouldSendEmail {
188
- logrus .Info ("No email is needed to send" )
189
- return events.APIGatewayProxyResponse {
190
- Body : `{"status": "ok"}` ,
191
- StatusCode : http .StatusOK ,
192
- }, nil
191
+ }, fmt .Errorf ("executing the template: %w" , err )
193
192
}
194
193
195
194
logrus .Info ("Sending mail" )
196
- subject := "[Please Read] Patch Releases cherry-pick deadline "
195
+ subject := "[Please Read] Upcoming Patch Releases Cherry-Pick Deadline for Kubernetes "
197
196
fromEmail := o .Config .FromEmail
198
197
199
198
recipient := Recipient {
@@ -212,7 +211,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
212
211
return events.APIGatewayProxyResponse {
213
212
Body : `{"status": "nok"}` ,
214
213
StatusCode : http .StatusInternalServerError ,
215
- }, fmt .Errorf ("parsing values to the template : %w" , err )
214
+ }, fmt .Errorf ("sending the email : %w" , err )
216
215
}
217
216
218
217
return events.APIGatewayProxyResponse {
@@ -240,9 +239,6 @@ func (o *Options) SendEmailRawSES(messageBody, subject, fromEmail string, recipi
240
239
recipients = append (recipients , & recipient )
241
240
}
242
241
243
- // Set to emails
244
- msg .SetHeader ("To" , recipient .toEmails ... )
245
-
246
242
// cc mails mentioned
247
243
if len (recipient .ccEmails ) != 0 {
248
244
// Need to add cc mail IDs also in recipient list
@@ -275,7 +271,7 @@ func (o *Options) SendEmailRawSES(messageBody, subject, fromEmail string, recipi
275
271
var emailRaw bytes.Buffer
276
272
_ , err := msg .WriteTo (& emailRaw )
277
273
if err != nil {
278
- log . Printf ( "failed to write mail: %v\n " , err )
274
+ logrus . Errorf ( "Failed to write mail: %v" , err )
279
275
return err
280
276
}
281
277
@@ -287,11 +283,11 @@ func (o *Options) SendEmailRawSES(messageBody, subject, fromEmail string, recipi
287
283
// send raw email
288
284
_ , err = svc .SendRawEmail (input )
289
285
if err != nil {
290
- log . Println ("Error sending mail - " , err )
286
+ logrus . Errorf ("Error sending mail - %v " , err )
291
287
return err
292
288
}
293
289
294
- log . Println ("Email sent successfully to: " , recipient .toEmails )
290
+ logrus . Infof ("Email sent successfully to: %q " , recipient .toEmails )
295
291
return nil
296
292
}
297
293
@@ -314,5 +310,6 @@ func loadFileOrURL(fileRef string) ([]byte, error) {
314
310
return nil , err
315
311
}
316
312
}
313
+
317
314
return raw , nil
318
315
}
0 commit comments