@@ -22,7 +22,6 @@ import (
2222 "embed"
2323 "fmt"
2424 "io"
25- "log"
2625 "math"
2726 "net/http"
2827 "os"
@@ -91,7 +90,7 @@ func getConfig() (*Config, error) {
9190 return & c , nil
9291}
9392
94- func New (ctx context.Context ) (* Options , error ) {
93+ func NewOptions (ctx context.Context ) (* Options , error ) {
9594 config , err := getConfig ()
9695 if err != nil {
9796 return nil , fmt .Errorf ("failed to get config: %w" , err )
@@ -102,7 +101,6 @@ func New(ctx context.Context) (*Options, error) {
102101 Region : aws .String (config .AWSRegion ),
103102 })
104103 if err != nil {
105- log .Println ("Error occurred while creating aws session" , err )
106104 return nil , err
107105 }
108106
@@ -114,20 +112,21 @@ func New(ctx context.Context) (*Options, error) {
114112}
115113
116114func handler (ctx context.Context , request events.APIGatewayProxyRequest ) (events.APIGatewayProxyResponse , error ) { //nolint: gocritic
117- o , err := New (ctx )
115+ o , err := NewOptions (ctx )
118116 if err != nil {
119117 return events.APIGatewayProxyResponse {
120118 Body : `{"status": "nok"}` ,
121119 StatusCode : http .StatusInternalServerError ,
122120 }, err
123121 }
124122
123+ logrus .Infof ("Will pull the patch release schedule from: %s" , o .Config .SchedulePath )
125124 data , err := loadFileOrURL (o .Config .SchedulePath )
126125 if err != nil {
127126 return events.APIGatewayProxyResponse {
128127 Body : `{"status": "nok"}` ,
129128 StatusCode : http .StatusInternalServerError ,
130- }, fmt .Errorf ("failed to read the file: %w" , err )
129+ }, fmt .Errorf ("reading the file: %w" , err )
131130 }
132131
133132 patchSchedule := & model.PatchSchedule {}
@@ -138,7 +137,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
138137 return events.APIGatewayProxyResponse {
139138 Body : `{"status": "nok"}` ,
140139 StatusCode : http .StatusInternalServerError ,
141- }, fmt .Errorf ("failed to decode the file: %w" , err )
140+ }, fmt .Errorf ("decoding the file: %w" , err )
142141 }
143142
144143 output := & Template {}
@@ -157,7 +156,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
157156 currentTime := time .Now ().UTC ()
158157 days := t .Sub (currentTime ).Hours () / 24
159158 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 )
161160 if int (intDay ) == o .Config .DaysToAlert {
162161 output .Releases = append (output .Releases , TemplateRelease {
163162 Release : patch .Release ,
@@ -167,6 +166,14 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
167166 }
168167 }
169168
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+
170177 tmpl , err := template .ParseFS (tpls , "templates/email.tmpl" )
171178 if err != nil {
172179 return events.APIGatewayProxyResponse {
@@ -181,19 +188,11 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
181188 return events.APIGatewayProxyResponse {
182189 Body : `{"status": "nok"}` ,
183190 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 )
193192 }
194193
195194 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 "
197196 fromEmail := o .Config .FromEmail
198197
199198 recipient := Recipient {
@@ -212,7 +211,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
212211 return events.APIGatewayProxyResponse {
213212 Body : `{"status": "nok"}` ,
214213 StatusCode : http .StatusInternalServerError ,
215- }, fmt .Errorf ("parsing values to the template : %w" , err )
214+ }, fmt .Errorf ("sending the email : %w" , err )
216215 }
217216
218217 return events.APIGatewayProxyResponse {
@@ -240,9 +239,6 @@ func (o *Options) SendEmailRawSES(messageBody, subject, fromEmail string, recipi
240239 recipients = append (recipients , & recipient )
241240 }
242241
243- // Set to emails
244- msg .SetHeader ("To" , recipient .toEmails ... )
245-
246242 // cc mails mentioned
247243 if len (recipient .ccEmails ) != 0 {
248244 // Need to add cc mail IDs also in recipient list
@@ -275,7 +271,7 @@ func (o *Options) SendEmailRawSES(messageBody, subject, fromEmail string, recipi
275271 var emailRaw bytes.Buffer
276272 _ , err := msg .WriteTo (& emailRaw )
277273 if err != nil {
278- log . Printf ( "failed to write mail: %v\n " , err )
274+ logrus . Errorf ( "Failed to write mail: %v" , err )
279275 return err
280276 }
281277
@@ -287,11 +283,11 @@ func (o *Options) SendEmailRawSES(messageBody, subject, fromEmail string, recipi
287283 // send raw email
288284 _ , err = svc .SendRawEmail (input )
289285 if err != nil {
290- log . Println ("Error sending mail - " , err )
286+ logrus . Errorf ("Error sending mail - %v " , err )
291287 return err
292288 }
293289
294- log . Println ("Email sent successfully to: " , recipient .toEmails )
290+ logrus . Infof ("Email sent successfully to: %q " , recipient .toEmails )
295291 return nil
296292}
297293
@@ -314,5 +310,6 @@ func loadFileOrURL(fileRef string) ([]byte, error) {
314310 return nil , err
315311 }
316312 }
313+
317314 return raw , nil
318315}
0 commit comments