Skip to content

Commit

Permalink
removed notifications/slack package
Browse files Browse the repository at this point in the history
 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.
 Also added a unit test for flagCheck(), this
 function also checks the (slack)URL syntax.
  • Loading branch information
atighineanu committed Oct 7, 2021
1 parent 4e1c05c commit 2588b6c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 70 deletions.
32 changes: 16 additions & 16 deletions cmd/kured/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
Expand All @@ -30,7 +32,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"
)
Expand Down Expand Up @@ -173,12 +174,22 @@ 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 != "" {
log.Warnf("slack-* flag(s) are being deprecated. Please use --notify-url flag instead.")
if slackHookURL != "" {
log.Warnf("Deprecated flag(s). Please use --notify-url flag instead.")
if regexp.MustCompile(`https://hooks.slack.com/services/\w{5,15}\/\w{5,15}\/\w{5,20}[^\/]`).FindString(slackHookURL) == "" {
log.Fatalf("ERROR: %v\n", errors.New("slack-hook-url is not properly formatted...no notification will be sent"))
}
trataURL, err := url.Parse(slackHookURL)
if err != nil {
log.Fatalf("slack-hook-url is not properly formatted...no notification will be sent: %v\n", err)
}
if len(strings.Split(strings.Trim(trataURL.Path, "/services/"), "/")) != 3 {
log.Fatalf("slack-hook-url is not properly formatted...no notification will be sent: %v\n", err)
} else {
notifyURL = fmt.Sprintf("slack://%s", strings.Trim(trataURL.Path, "/services/"))
}
}

}

// newCommand creates a new Command with stdout/stderr wired to our standard logger
Expand Down Expand Up @@ -353,11 +364,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)
Expand Down Expand Up @@ -411,12 +417,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)
Expand Down
10 changes: 10 additions & 0 deletions cmd/kured/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/weaveworks/kured/pkg/alerts"
assert "gotest.tools/v3/assert"

Expand All @@ -22,6 +23,15 @@ func (fbc BlockingChecker) isBlocked() bool {
var _ RebootBlocker = BlockingChecker{} // Verify that Type implements Interface.
var _ RebootBlocker = (*BlockingChecker)(nil) // Verify that *Type implements Interface.

func Test_flagCheck(t *testing.T) {
var cmd *cobra.Command
var args []string
slackHookURL = "https://hooks.slack.com/services/BLABLABA12345/IAM931A0VERY/COMPLICATED711854TOKEN1SET"
flagCheck(cmd, args)
if notifyURL != "slack://BLABLABA12345/IAM931A0VERY/COMPLICATED711854TOKEN1SET" {
t.Errorf("Slack URL Parsing is wrong: expecting %s but got %s\n", "slack://BLABLABA12345/IAM931A0VERY/COMPLICATED711854TOKEN1SET", notifyURL)
}
}
func Test_rebootBlocked(t *testing.T) {
noCheckers := []RebootBlocker{}
nonblockingChecker := BlockingChecker{blocking: false}
Expand Down
54 changes: 0 additions & 54 deletions pkg/notifications/slack/slack.go

This file was deleted.

0 comments on commit 2588b6c

Please sign in to comment.