Skip to content

Commit

Permalink
Issue #16: Added cluster name in the messages from botkube
Browse files Browse the repository at this point in the history
  • Loading branch information
mugdha-adhav committed Feb 20, 2019
1 parent f0fbbfe commit 4b3696a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
13 changes: 7 additions & 6 deletions pkg/controller/controller.go
@@ -1,6 +1,7 @@
package controller

import (
"fmt"
"os"
"os/signal"
"strconv"
Expand All @@ -20,13 +21,13 @@ import (
"k8s.io/client-go/tools/cache"
)

var startTime time.Time

const (
controllerStartMsg = "...and now my watch begins! :crossed_swords:"
controllerStopMsg = "my watch has ended!"
controllerStartMsg = "...and now my watch begins for cluster '%s'! :crossed_swords:"
controllerStopMsg = "my watch has ended for cluster '%s'!"
)

var startTime time.Time

func findNamespace(ns string) string {
if ns == "all" {
return apiV1.NamespaceAll
Expand All @@ -39,7 +40,7 @@ func findNamespace(ns string) string {

// RegisterInformers creates new informer controllers to watch k8s resources
func RegisterInformers(c *config.Config) {
sendMessage(controllerStartMsg)
sendMessage(fmt.Sprintf(controllerStartMsg, c.Settings.ClusterName))
startTime = time.Now().Local()

// Get resync period
Expand Down Expand Up @@ -127,7 +128,7 @@ func RegisterInformers(c *config.Config) {
signal.Notify(sigterm, syscall.SIGTERM)
signal.Notify(sigterm, syscall.SIGINT)
<-sigterm
sendMessage(controllerStopMsg)
sendMessage(fmt.Sprintf(controllerStopMsg, c.Settings.ClusterName))
}

func registerEventHandlers(resourceType string, events []string) (handlerFns cache.ResourceEventHandlerFuncs) {
Expand Down
31 changes: 17 additions & 14 deletions pkg/execute/executor.go
@@ -1,6 +1,7 @@
package execute

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -34,10 +35,10 @@ var validNotifierCommands = map[string]bool{
var kubectlBinary = "/usr/local/bin/kubectl"

const (
notifierStartMsg = "Brace yourselves, notifications are coming."
notifierStopMsg = "Sure! I won't send you notifications anymore."
notifierStartMsg = "Brace yourselves, notifications are coming from cluster '%s'."
notifierStopMsg = "Sure! I won't send you notifications from cluster '%s' anymore."
unsupportedCmdMsg = "Command not supported. Please run '@BotKube help' to see supported commands."
kubectlDisabledMsg = "Sorry, the admin hasn't given me the permission to execute kubectl command."
kubectlDisabledMsg = "Sorry, the admin hasn't given me the permission to execute kubectl command on cluster '%s'."
)

// Executor is an interface for processes to execute commands
Expand All @@ -49,13 +50,15 @@ type Executor interface {
type DefaultExecutor struct {
Message string
AllowKubectl bool
ClusterName string
}

// NewDefaultExecutor returns new Executor object
func NewDefaultExecutor(msg string, allowkubectl bool) Executor {
func NewDefaultExecutor(msg string, allowkubectl bool, clustername string) Executor {
return &DefaultExecutor{
Message: msg,
AllowKubectl: allowkubectl,
ClusterName: clustername,
}
}

Expand All @@ -64,12 +67,12 @@ func (e *DefaultExecutor) Execute() string {
args := strings.Split(e.Message, " ")
if validKubectlCommands[args[0]] {
if !e.AllowKubectl {
return kubectlDisabledMsg
return fmt.Sprintf(kubectlDisabledMsg, e.ClusterName)
}
return runKubectlCommand(args)
return fmt.Sprintf("Cluster: %s\n%s", e.ClusterName, runKubectlCommand(args))
}
if validNotifierCommands[args[0]] {
return runNotifierCommand(args)
return runNotifierCommand(args, e.ClusterName)
}
return unsupportedCmdMsg
}
Expand Down Expand Up @@ -129,14 +132,14 @@ func runKubectlCommand(args []string) string {
}

// TODO: Have a seperate cli which runs bot commands
func runNotifierCommand(args []string) string {
func runNotifierCommand(args []string, clustername string) string {
switch len(args) {
case 1:
if strings.ToLower(args[0]) == "help" {
return printHelp()
}
if strings.ToLower(args[0]) == "ping" {
return "pong"
return fmt.Sprintf("pong from cluster '%s'", clustername)
}
case 2:
if args[0] != "notifier" {
Expand All @@ -145,26 +148,26 @@ func runNotifierCommand(args []string) string {
if args[1] == "start" {
config.Notify = true
log.Logger.Info("Notifier enabled")
return notifierStartMsg
return fmt.Sprintf(notifierStartMsg, clustername)
}
if args[1] == "stop" {
config.Notify = false
log.Logger.Info("Notifier disabled")
return notifierStopMsg
return fmt.Sprintf(notifierStopMsg, clustername)
}
if args[1] == "status" {
if config.Notify == false {
return "stopped"
return fmt.Sprintf("Notifications are off for cluster '%s'", clustername)
}
return "running"
return fmt.Sprintf("Notifications are on for cluster '%s'", clustername)
}
if args[1] == "showconfig" {
out, err := showControllerConfig()
if err != nil {
log.Logger.Error("Error in executing showconfig command: ", err)
return "Error in getting configuration!"
}
return out
return fmt.Sprintf("Showing config for cluster '%s'\n\n%s", clustername, out)
}
}
return printDefaultMsg()
Expand Down
8 changes: 5 additions & 3 deletions pkg/slack/slack.go
Expand Up @@ -14,6 +14,7 @@ import (
type Bot struct {
Token string
AllowKubectl bool
ClusterName string
}

// slackMessage contains message details to execute command and send back the result
Expand All @@ -35,6 +36,7 @@ func NewSlackBot() *Bot {
return &Bot{
Token: c.Communications.Slack.Token,
AllowKubectl: c.Settings.AllowKubectl,
ClusterName: c.Settings.ClusterName,
}
}

Expand Down Expand Up @@ -95,7 +97,7 @@ func (b *Bot) Start() {
InMessage: inMessage,
RTM: rtm,
}
sm.HandleMessage(b.AllowKubectl)
sm.HandleMessage(b.AllowKubectl, b.ClusterName)

case *slack.RTMError:
logging.Logger.Errorf("Slack RMT error: %+v", ev.Error())
Expand All @@ -108,8 +110,8 @@ func (b *Bot) Start() {
}
}

func (sm *slackMessage) HandleMessage(allowkubectl bool) {
e := execute.NewDefaultExecutor(sm.InMessage, allowkubectl)
func (sm *slackMessage) HandleMessage(allowkubectl bool, clustername string) {
e := execute.NewDefaultExecutor(sm.InMessage, allowkubectl, clustername)
sm.OutMessage = e.Execute()
sm.OutMsgLength = len(sm.OutMessage)
sm.Send()
Expand Down

0 comments on commit 4b3696a

Please sign in to comment.