From df8150d47b8a004342843de9ba18da446fdda287 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Mon, 7 Jun 2021 12:20:57 +0100 Subject: [PATCH 1/4] Return an error when request status code is not 200 Signed-off-by: Somtochi Onyekwere --- internal/notifier/client.go | 12 +++++++++++- internal/server/event_handlers.go | 9 ++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/internal/notifier/client.go b/internal/notifier/client.go index 6d6deba2b..68a7755b1 100644 --- a/internal/notifier/client.go +++ b/internal/notifier/client.go @@ -21,6 +21,7 @@ import ( "crypto/x509" "encoding/json" "fmt" + "io/ioutil" "net" "net/http" "net/url" @@ -87,9 +88,18 @@ func postMessage(address, proxy string, certPool *x509.CertPool, payload interfa for _, o := range reqOpts { o(req) } - if _, err := httpClient.Do(req); err != nil { + resp, err := httpClient.Do(req) + if err != nil { return fmt.Errorf("failed to execute request: %w", err) } + if resp.StatusCode != http.StatusOK { + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("unable to read response body, %s", err) + } + return fmt.Errorf("request not successful, %s", string(b)) + } + return nil } diff --git a/internal/server/event_handlers.go b/internal/server/event_handlers.go index 1d7767c30..c9255b6aa 100644 --- a/internal/server/event_handlers.go +++ b/internal/server/event_handlers.go @@ -224,9 +224,12 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request) go func(n notifier.Interface, e events.Event) { if err := n.Post(e); err != nil { - redacted := strings.ReplaceAll(err.Error(), token, "*****") - redactedErr := errors.New(redacted) - s.logger.Error(redactedErr, "failed to send notification", + if token != "" { + redacted := strings.ReplaceAll(err.Error(), token, "*****") + err = errors.New(redacted) + } + + s.logger.Error(err, "failed to send notification", "reconciler kind", event.InvolvedObject.Kind, "name", event.InvolvedObject.Name, "namespace", event.InvolvedObject.Namespace) From fd90d4ec35d1e2bc1dc9041dc8cda8a6bcf7df53 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Mon, 7 Jun 2021 12:48:47 +0100 Subject: [PATCH 2/4] Adds other 200 status code Signed-off-by: Somtochi Onyekwere --- internal/notifier/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/notifier/client.go b/internal/notifier/client.go index 68a7755b1..bb6230916 100644 --- a/internal/notifier/client.go +++ b/internal/notifier/client.go @@ -93,7 +93,7 @@ func postMessage(address, proxy string, certPool *x509.CertPool, payload interfa return fmt.Errorf("failed to execute request: %w", err) } - if resp.StatusCode != http.StatusOK { + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusCreated { b, err := ioutil.ReadAll(resp.Body) if err != nil { return fmt.Errorf("unable to read response body, %s", err) From fb66d685621f68ad234d0a55d1afd66ec6c1a370 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Mon, 7 Jun 2021 13:06:02 +0100 Subject: [PATCH 3/4] Better error message Signed-off-by: Somtochi Onyekwere --- internal/notifier/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/notifier/client.go b/internal/notifier/client.go index bb6230916..2bb1a15e1 100644 --- a/internal/notifier/client.go +++ b/internal/notifier/client.go @@ -98,7 +98,7 @@ func postMessage(address, proxy string, certPool *x509.CertPool, payload interfa if err != nil { return fmt.Errorf("unable to read response body, %s", err) } - return fmt.Errorf("request not successful, %s", string(b)) + return fmt.Errorf("request failed with status code %d, %s", resp.StatusCode, string(b)) } return nil From c2d9b1e85ed495e2396476b5363c92ac63ad2848 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Mon, 7 Jun 2021 13:21:42 +0100 Subject: [PATCH 4/4] Make slack channel optional Signed-off-by: Somtochi Onyekwere --- internal/notifier/slack.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/internal/notifier/slack.go b/internal/notifier/slack.go index 9d683c6e6..d0322b271 100644 --- a/internal/notifier/slack.go +++ b/internal/notifier/slack.go @@ -18,7 +18,6 @@ package notifier import ( "crypto/x509" - "errors" "fmt" "net/url" "strings" @@ -67,10 +66,6 @@ func NewSlack(hookURL string, proxyURL string, certPool *x509.CertPool, username return nil, fmt.Errorf("invalid Slack hook URL %s", hookURL) } - if channel == "" { - return nil, errors.New("empty Slack channel") - } - return &Slack{ Channel: channel, Username: username, @@ -88,9 +83,13 @@ func (s *Slack) Post(event events.Event) error { } payload := SlackPayload{ - Channel: s.Channel, Username: s.Username, } + + if s.Channel != "" { + payload.Channel = s.Channel + } + if payload.Username == "" { payload.Username = event.ReportingController }