Skip to content

Commit

Permalink
Allow notifications prefixes
Browse files Browse the repository at this point in the history
Could be useful when you have, for instance, several
clusters, and want to convey the current cluster with
the alerts notifications.
  • Loading branch information
bpineau committed Oct 8, 2017
1 parent 8d29aec commit e6f608b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ provide this server's address with "-s" flag, or a kube config with "-k").
You can pass configuration values either by command line arguments, or
environment variables, a yaml configuration file, or a combination or those.

The command line flags are:
The command line flags are (all optionals):
```
Usage:
kube-alert [flags]
Expand All @@ -45,13 +45,15 @@ Flags:
-v, --log-level string log level (default "debug")
-l, --log-output string log output (default "stderr")
-r, --log-server string log server (if using syslog)
-m, --messages-prefix string prefix appended to notifications
```

Using an (optional) configuration file:
```yaml
dry-run: false
healthcheck-port: 8080
api-server: http://example.com:8080
messages-prefix: kube-prod-cluster-42

log:
output: "stdout"
Expand Down
7 changes: 7 additions & 0 deletions cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
ddApiKey string
ddAppKey string
healthP int
msgPrefix string

// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Expand All @@ -42,6 +43,7 @@ var (
DdAppKey: viper.GetString("datadog.app-key"),
DdApiKey: viper.GetString("datadog.api-key"),
HealthPort: viper.GetInt("healthcheck-port"),
MsgPrefix: viper.GetString("messages-prefix"),
}
config.Init(viper.GetString("api-server"), viper.GetString("kube-config"))
run.Run(config)
Expand Down Expand Up @@ -109,6 +111,11 @@ func init() {
if err := viper.BindPFlag("healthcheck-port", rootCmd.PersistentFlags().Lookup("healthcheck-port")); err != nil {
log.Fatal("Failed to bind cli argument:", err)
}

rootCmd.PersistentFlags().StringVarP(&msgPrefix, "messages-prefix", "m", "", "prefix appended to notifications")
if err := viper.BindPFlag("messages-prefix", rootCmd.PersistentFlags().Lookup("messages-prefix")); err != nil {
log.Fatal("Failed to bind cli argument:", err)
}
}

func initConfig() {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type AlertConfig struct {
DdAppKey string
DdApiKey string
HealthPort int
MsgPrefix string
}

func (c *AlertConfig) Init(apiserver string, kubeconfig string) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (c *CommonController) Stop() {
c.Conf.Logger.Infof("Stopping %s controller", c.Name)
close(c.StopCh)

// give everything 2 seconds to stop gracefully
// give everything 2s max to stop gracefully
time.Sleep(2 * time.Second)
c.wg.Done()
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func New(logLevel string, logServer string, logOutput string) *logrus.Logger {
level = logrus.InfoLevel
}

formater := &logrus.TextFormatter{
formatter := &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
}

log := &logrus.Logger{
Out: output,
Formatter: formater,
Formatter: formatter,
Hooks: make(logrus.LevelHooks),
Level: level,
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/notifiers/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ var Notifiers = []Notifier{
}

func Notify(c *config.AlertConfig, title string, msg string) {
ptitle := title
if c.MsgPrefix != "" {
ptitle = fmt.Sprintf("%s %s", c.MsgPrefix, title)
}

if c.DryRun {
fmt.Printf("%s: %s\n", title, msg)
fmt.Printf("%s: %s\n", ptitle, msg)
return
}

for _, notifier := range Notifiers {
err := notifier.Notify(c, title, msg)
err := notifier.Notify(c, ptitle, msg)
if err != nil {
c.Logger.Warningf("Failed to notify: %s", err)
}
Expand Down

0 comments on commit e6f608b

Please sign in to comment.