Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

```shell
helm repo add kwatch https://kwatch.dev/charts
helm install [RELEASE_NAME] kwatch/kwatch --namespace kwatch --create-namespace --version 0.10.4
helm install [RELEASE_NAME] kwatch/kwatch --namespace kwatch --create-namespace --version 0.10.5
```

To get more details, please check [chart's configuration](https://github.com/abahmed/kwatch/blob/main/deploy/chart/README.md)
Expand All @@ -46,7 +46,7 @@ To get more details, please check [chart's configuration](https://github.com/aba
You need to get config template to add your configs

```shell
curl -L https://raw.githubusercontent.com/abahmed/kwatch/v0.10.4/deploy/config.yaml -o config.yaml
curl -L https://raw.githubusercontent.com/abahmed/kwatch/v0.10.5/deploy/config.yaml -o config.yaml
```

Then edit `config.yaml` file and apply your configuration
Expand All @@ -58,7 +58,7 @@ kubectl apply -f config.yaml
To deploy **kwatch**, execute following command:

```shell
kubectl apply -f https://raw.githubusercontent.com/abahmed/kwatch/v0.10.4/deploy/deploy.yaml
kubectl apply -f https://raw.githubusercontent.com/abahmed/kwatch/v0.10.5/deploy/deploy.yaml
```

## ⚙️ Configuration
Expand Down Expand Up @@ -86,7 +86,7 @@ kubectl apply -f https://raw.githubusercontent.com/abahmed/kwatch/v0.10.4/deploy
| `app.logFormatter` | used for setting custom formatter when app prints logs: text, json (default: text) |


### 💓 Health Check (Not Released)
### 💓 Health Check

| Parameter | Description |
|:------------------------------|:------------------------------------------- |
Expand Down Expand Up @@ -129,7 +129,9 @@ kubectl apply -f https://raw.githubusercontent.com/abahmed/kwatch/v0.10.4/deploy
<img src="./assets/slack.png" width="30%"/>
</p>

If you want to enable Slack, provide the webhook with optional text and title
If you want to enable Slack, provide either a webhook URL or a bot token with channel

**Webhook mode:**

| Parameter | Description |
|:---------------------------------|:------------------------------------------- |
Expand All @@ -138,6 +140,15 @@ If you want to enable Slack, provide the webhook with optional text and title
| `alert.slack.title` | Customized title in slack message |
| `alert.slack.text` | Customized text in slack message |

**Bot Token mode:**

| Parameter | Description |
|:---------------------------------|:------------------------------------------- |
| `alert.slack.token` | Slack bot token (xoxb-...) |
| `alert.slack.channel` | Channel to post to (e.g. #alerts) |
| `alert.slack.title` | Customized title in slack message |
| `alert.slack.text` | Customized text in slack message |

#### Discord

<p>
Expand Down Expand Up @@ -328,8 +339,8 @@ basic auth
### 🧹 Cleanup

```shell
kubectl delete -f https://raw.githubusercontent.com/abahmed/kwatch/v0.10.4/deploy/config.yaml
kubectl delete -f https://raw.githubusercontent.com/abahmed/kwatch/v0.10.4/deploy/deploy.yaml
kubectl delete -f https://raw.githubusercontent.com/abahmed/kwatch/v0.10.5/deploy/config.yaml
kubectl delete -f https://raw.githubusercontent.com/abahmed/kwatch/v0.10.5/deploy/deploy.yaml
```

## 👍 Contribute & Support
Expand Down
65 changes: 52 additions & 13 deletions alertmanager/slack/slack.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slack

import (
"context"
"fmt"
"strings"

Expand All @@ -17,41 +18,60 @@ const (
)

type Slack struct {
webhook string
title string
text string

// used by legacy webhook to send messages to specific channel,
// instead of default one
channel string
appCfg *config.App

// reference for general app configuration
appCfg *config.App
// webhook mode
webhook string
send func(url string, msg *slackClient.WebhookMessage) error

send func(url string, msg *slackClient.WebhookMessage) error
// token mode
token string
apiClient *slackClient.Client
}

// NewSlack returns new Slack instance
func NewSlack(config map[string]interface{}, appCfg *config.App) *Slack {
title, _ := config["title"].(string)
text, _ := config["text"].(string)

// token mode: requires token + channel
token, hasToken := config["token"].(string)
channel, hasChannel := config["channel"].(string)
if hasToken && len(token) > 0 {
if !hasChannel || len(channel) == 0 {
logrus.Warnf("initializing slack with token but missing channel")
return nil
}
logrus.Infof("initializing slack with token and channel: %s", channel)
return &Slack{
token: token,
channel: channel,
title: title,
text: text,
appCfg: appCfg,
apiClient: slackClient.New(token),
}
}

// webhook mode: requires webhook
webhook, ok := config["webhook"].(string)
if !ok || len(webhook) == 0 {
logrus.Warnf("initializing slack with empty webhook url")
logrus.Warnf("initializing slack with empty webhook url and no token")
return nil
}

logrus.Infof("initializing slack with webhook url: %s", webhook)

channel, _ := config["channel"].(string)
title, _ := config["title"].(string)
text, _ := config["text"].(string)

return &Slack{
webhook: webhook,
channel: channel,
title: title,
text: text,
send: slackClient.PostWebhook,
appCfg: appCfg,
send: slackClient.PostWebhook,
}
}

Expand Down Expand Up @@ -132,12 +152,31 @@ func (s *Slack) SendMessage(msg string) error {
}

func (s *Slack) sendAPI(msg *slackClient.WebhookMessage) error {
if s.apiClient != nil {
return s.sendAPIWithToken(msg)
}
if len(s.channel) > 0 {
msg.Channel = s.channel
}
return s.send(s.webhook, msg)
}

func (s *Slack) sendAPIWithToken(msg *slackClient.WebhookMessage) error {
opts := []slackClient.MsgOption{}
if len(msg.Text) > 0 {
opts = append(opts, slackClient.MsgOptionText(msg.Text, false))
}
if msg.Blocks != nil {
opts = append(opts, slackClient.MsgOptionBlocks(msg.Blocks.BlockSet...))
}
_, _, err := s.apiClient.PostMessageContext(
context.Background(),
s.channel,
opts...,
)
return err
}

func chunks(s string, chunkSize int) []string {
if chunkSize >= len(s) {
return []string{s}
Expand Down
Loading
Loading