From 5eafd796eab6f9291bb3f095edeab6afb27cd733 Mon Sep 17 00:00:00 2001 From: atighineanu Date: Thu, 1 Apr 2021 13:55:34 +0200 Subject: [PATCH] removed notifications/slack package In this PR the slack-hook-url is translated into shoutrrr syntax. Therefore, slack pack age as well as checks for slack-hook-url in drain and reboot functions are removed. --- cmd/kured/main.go | 26 +++++++-------- pkg/notifications/slack/slack.go | 54 -------------------------------- 2 files changed, 11 insertions(+), 69 deletions(-) delete mode 100644 pkg/notifications/slack/slack.go diff --git a/cmd/kured/main.go b/cmd/kured/main.go index 6f6cfebba..11d030127 100644 --- a/cmd/kured/main.go +++ b/cmd/kured/main.go @@ -29,7 +29,6 @@ import ( "github.com/weaveworks/kured/pkg/alerts" "github.com/weaveworks/kured/pkg/daemonsetlock" "github.com/weaveworks/kured/pkg/delaytick" - "github.com/weaveworks/kured/pkg/notifications/slack" "github.com/weaveworks/kured/pkg/taints" "github.com/weaveworks/kured/pkg/timewindow" ) @@ -166,12 +165,20 @@ func main() { func flagCheck(cmd *cobra.Command, args []string) { if slackHookURL != "" && notifyURL != "" { log.Warnf("Cannot use both --notify-url and --slack-hook-url flags. Kured will use --notify-url flag only...") - slackHookURL = "" } - if slackChannel != "" || slackHookURL != "" || slackUsername != "" { + + if slackHookURL != "" { log.Warnf("Deprecated flag(s). Please use --notify-url flag instead.") + tmpsplit := strings.Split(slackHookURL, "/") + if len(tmpsplit) < 4 { + log.Warnf("slack-hook-url is not properly formatted...\nno notification will be sent.") + } else { + tokenA := tmpsplit[len(tmpsplit)-3] + tokenB := tmpsplit[len(tmpsplit)-2] + tokenC := tmpsplit[len(tmpsplit)-1] + notifyURL = fmt.Sprintf("slack://%s/%s/%s", tokenA, tokenB, tokenC) + } } - } // newCommand creates a new Command with stdout/stderr wired to our standard logger @@ -342,11 +349,6 @@ func drain(client *kubernetes.Clientset, node *v1.Node) { log.Infof("Draining node %s", nodename) - if slackHookURL != "" { - if err := slack.NotifyDrain(slackHookURL, slackUsername, slackChannel, messageTemplateDrain, nodename); err != nil { - log.Warnf("Error notifying slack: %v", err) - } - } if notifyURL != "" { if err := shoutrrr.Send(notifyURL, fmt.Sprintf(messageTemplateDrain, nodename)); err != nil { log.Warnf("Error notifying: %v", err) @@ -399,12 +401,6 @@ func uncordon(client *kubernetes.Clientset, node *v1.Node) { func invokeReboot(nodeID string, rebootCommand []string) { log.Infof("Running command: %s for node: %s", rebootCommand, nodeID) - if slackHookURL != "" { - if err := slack.NotifyReboot(slackHookURL, slackUsername, slackChannel, messageTemplateReboot, nodeID); err != nil { - log.Warnf("Error notifying slack: %v", err) - } - } - if notifyURL != "" { if err := shoutrrr.Send(notifyURL, fmt.Sprintf(messageTemplateReboot, nodeID)); err != nil { log.Warnf("Error notifying: %v", err) diff --git a/pkg/notifications/slack/slack.go b/pkg/notifications/slack/slack.go deleted file mode 100644 index 4af05d93f..000000000 --- a/pkg/notifications/slack/slack.go +++ /dev/null @@ -1,54 +0,0 @@ -package slack - -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" - "time" -) - -var ( - httpClient = &http.Client{Timeout: 5 * time.Second} -) - -type body struct { - Text string `json:"text,omitempty"` - Username string `json:"username,omitempty"` - Channel string `json:"channel,omitempty"` -} - -func notify(hookURL, username, channel, message string) error { - msg := body{ - Text: message, - Username: username, - Channel: channel, - } - - var buf bytes.Buffer - if err := json.NewEncoder(&buf).Encode(&msg); err != nil { - return err - } - - resp, err := httpClient.Post(hookURL, "application/json", &buf) - if err != nil { - return err - } - defer resp.Body.Close() - - if resp.StatusCode < 200 || resp.StatusCode >= 300 { - return fmt.Errorf(resp.Status) - } - - return nil -} - -// NotifyDrain is the exposed way to notify of a drain event onto a slack chan -func NotifyDrain(hookURL, username, channel, messageTemplate, nodeID string) error { - return notify(hookURL, username, channel, fmt.Sprintf(messageTemplate, nodeID)) -} - -// NotifyReboot is the exposed way to notify of a reboot event onto a slack chan -func NotifyReboot(hookURL, username, channel, messageTemplate, nodeID string) error { - return notify(hookURL, username, channel, fmt.Sprintf(messageTemplate, nodeID)) -}