Skip to content

Commit

Permalink
Added support for Mattermost
Browse files Browse the repository at this point in the history
  • Loading branch information
mugdha-adhav committed Mar 29, 2019
1 parent 70a7613 commit 71cd575
Show file tree
Hide file tree
Showing 203 changed files with 35,719 additions and 81 deletions.
65 changes: 63 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Expand Up @@ -25,3 +25,7 @@
[[constraint]]
name = "github.com/olivere/elastic"
version = "6.0.0"

[[constraint]]
name = "github.com/mattermost/mattermost-server"
version = "4.9.4"
39 changes: 38 additions & 1 deletion README.md
Expand Up @@ -31,7 +31,13 @@ $ git clone https://github.com/infracloudio/botkube.git
If you are not interested in events about particular resource, just remove it's entry from the config file.
- Deploy BotKube controller using **helm install** in your cluster.
```bash
$ helm install --name botkube --namespace botkube --set config.communications.slack.channel={SLACK_CHANNEL_NAME},config.communications.slack.token={SLACK_API_TOKEN_FOR_THE_BOT},config.settings.clustername={CLUSTER_NAME},config.settings.allowkubectl={ALLOW_KUBECTL} helm/botkube/
$ helm install --name botkube --namespace botkube \
--set config.communications.slack.enabled=true \
--set config.communications.slack.channel={SLACK_CHANNEL_NAME} \
--set config.communications.slack.token={SLACK_API_TOKEN_FOR_THE_BOT} \
--set config.settings.clustername={CLUSTER_NAME} \
--set config.settings.allowkubectl={ALLOW_KUBECTL} \
helm/botkube
```

where,<br>
Expand Down Expand Up @@ -72,6 +78,37 @@ $ kubectl create ns botkube && kubectl create -f deploy-all-in-one.yaml -n botku

- Check pod status in botkube namespace. Once running, send **@BotKube ping** in the Slack channel to confirm if BotKube is responding correctly.

## Add BotKube to your Mattermost team
Before adding `BotKube` to your Mattermost team, please make sure you have all the pre-requisites working.

### Pre-requisites
**Step 1**: Login with System Admin account, and in the Menu proceed to System console -> Integrations -> Custom Integrations and enable `Personal Access Token`.<br><br>
**Step 2**: To create a Botkube user, if not already created, proceed to menu and Get team invite link. Logout from admin account and paste the link in the address bar and create a user with the username `BotKube`.<br><br>
**Step 3**: Login as System Admin and in the Menu proceed to System console -> Users. For `BotKube` user, Manage Roles and allow tokens and post_all access.<br><br>
**Step 4**: Login as BotKube user, in the Menu proceed to Account Settings -> Security -> Personal Access Token -> Create and save the token.

### Configurations
In the helm chart, config.yaml or deploy-all-in-one.yaml update the following fields for enabling Mattermost support and providing Mattermost config parameters.

**MATTERMOST_SERVER_URL** is the URL where Mattermost is running<br>
**MATTERMOST_TOKEN** is the Token you received after installing BotKube user and creating Personal Access Token<br>
**MATTERMOST_TEAM** is the team name where BotKube will be added<br>
**MATTERMOST_CHANNEL** is the channel name where BotKube will be added<br>

#### Using helm

```bash
$ helm install --name botkube --namespace botkube \
--set config.communications.mattermost.enabled=true \
--set config.communications.mattermost.url={MATTERMOST_SERVER_URL} \
--set config.communications.mattermost.token={MATTERMOST_TOKEN} \
--set config.communications.mattermost.team={MATTERMOST_TEAM} \
--set config.communications.mattermost.channel={MATTERMOST_CHANNEL} \
--set config.settings.clustername={CLUSTER_NAME} \
--set config.settings.allowkubectl={ALLOW_KUBECTL} \
helm/botkube
```

## Architecture
![](/botkube_arch.jpg)
- **Informer Controller:** Registers informers to kube-apiserver to watch events on the configured k8s resources. It forwards the incoming k8s event to the Event Manager
Expand Down
9 changes: 8 additions & 1 deletion cmd/botkube/main.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/infracloudio/botkube/pkg/config"
"github.com/infracloudio/botkube/pkg/controller"
log "github.com/infracloudio/botkube/pkg/logging"
"github.com/infracloudio/botkube/pkg/mattermost"
"github.com/infracloudio/botkube/pkg/slack"
)

Expand All @@ -16,10 +17,16 @@ func main() {
log.Logger.Fatal(fmt.Sprintf("Error in loading configuration. Error:%s", err.Error()))
}

if Config.Communications.Slack.Enable {
if Config.Communications.Slack.Enabled {
log.Logger.Info("Starting slack bot")
sb := slack.NewSlackBot()
go sb.Start()
}

if Config.Communications.Mattermost.Enabled {
log.Logger.Info("Starting mattermost bot")
mb := mattermost.NewMattermostBot()
mb.Start()
}
controller.RegisterInformers(Config)
}
12 changes: 10 additions & 2 deletions config.yaml
Expand Up @@ -117,13 +117,21 @@ recommendations: true
communications:
# Settings for Slack
slack:
enable: false
enabled: false
channel: 'SLACK_CHANNEL'
token: 'SLACK_API_TOKEN'

# Settings for Mattermost
mattermost:
enabled: true
url: 'MATTERMOST_SERVER_URL' # e.g https://example.com:9243
token: 'MATTERMOST_TOKEN'
team: 'MATTERMOST_TEAM'
channel: 'MATTERMOST_CHANNEL'

# Settings for ELS
elasticsearch:
enable: false
enabled: false
server: 'ELASTICSEARCH_ADDRESS' # e.g https://example.com:9243
username: 'ELASTICSEARCH_USERNAME'
password: 'ELASTICSEARCH_PASSWORD'
Expand Down
10 changes: 9 additions & 1 deletion deploy-all-in-one.yaml
Expand Up @@ -127,10 +127,18 @@ data:
communications:
# Settings for Slack
slack:
enable: false
enabled: false
channel: 'SLACK_CHANNEL'
token: 'SLACK_API_TOKEN'
# Settings for Mattermost
mattermost:
enabled: false
url: 'MATTERMOST_SERVER_URL' # e.g https://example.com:9243
token: 'MATTERMOST_TOKEN'
team: 'MATTERMOST_TEAM'
channel: 'MATTERMOST_CHANNEL'
# Settings for ELS
elasticsearch:
enable: false
Expand Down
48 changes: 48 additions & 0 deletions design/mattermost-support.md
@@ -0,0 +1,48 @@
# Mattermost Support

#### Assumptions
- Mattermost is already installed.
- Mattermost server IP is reachable from the cluster.

### Summary
`Mattermost` is an open source, self-hosted Slack-alternative and we want to add support for the same in Botkube.

### Motivation
Currently Botkube is supporting Slack, and as a feature addition we want to include `Mattermost` support. Botkube will run on the clusters and send notifications and alerts to the configured Mattermost team. It will also be able to execute `@botkube` commands from `Mattermost`.

### Design
Steps for adding Mattermost support:
- Add package for Mattermost referring to `mattermost-server` repository.
- Add Mattermost configurations in helm chart or config.yaml.
- While starting controller, check in config if Mattermost support is enabled.
- If support enabled, initialize Mattermost with values from config.
- Start a go routinue for Mattermost.
- In controller, add notifier for `SendEvent` and `SendMessage` for Mattermost.

#### Adding package
The [mattermost-server](https://github.com/mattermost/mattermost-server) repository contains a client package to perform operations on `Mattermost` server. We will perform below operations using the package.
- Connecting to Mattermost server.
- Create `Botkube` user in Mattermost.
- Finding team in Mattermost and adding `Botkube` user to the Team.
- Finding channel or create channel if needed and adding `Botkube` user to the Channel.
- Send and Receive messages from Mattermost server.

#### Adding Configurations
We need to add below values for Mattermost in Helm Chart or config.yaml.
- Mattermost Support enabled/disabled flag
- Mattermost Server IP
- Team Name
- User-Email
- User-Name
- Password
- Channel Name(Same as Slack)

#### Controller Modifications
- In `main.go`, check in config if Mattermost support is enabled.
- If yes, then get config and add a go routine for Mattermost.
- In `controller.go`, for event notifications and sending start/stop messages, add notifier for Mattermost.

### References
- https://github.com/mattermost/mattermost-bot-sample-golang
- https://github.com/mattermost/mattermost-server/blob/master/model/client4.go
- https://docs.mattermost.com/install/install-ubuntu-1804.html
14 changes: 11 additions & 3 deletions helm/botkube/values.yaml
Expand Up @@ -130,18 +130,26 @@ config:
# Check true if you want to receive recommendations
# about the best practices for the created resource
recommendations: true

# Channels configuration
communications:
# Settings for Slack
slack:
enable: false
enabled: false
channel: 'SLACK_CHANNEL'
token: 'SLACK_API_TOKEN'

# Settings for Mattermost
mattermost:
enabled: false
url: 'MATTERMOST_SERVER_URL' # e.g https://example.com:9243
token: 'MATTERMOST_TOKEN'
team: 'MATTERMOST_TEAM'
channel: 'MATTERMOST_CHANNEL'

# Settings for ELS
elasticsearch:
enable: false
enabled: false
server: 'ELASTICSEARCH_ADDRESS' # e.g https://example.com:9243
username: 'ELASTICSEARCH_USERNAME'
password: 'ELASTICSEARCH_PASSWORD'
Expand Down
17 changes: 13 additions & 4 deletions pkg/config/config.go
Expand Up @@ -36,21 +36,21 @@ type Resource struct {

// Communications channels to send events to
type Communications struct {
Slack Slack

Slack Slack
ElasticSearch ElasticSearch
Mattermost Mattermost
}

// Slack configuration to authentication and send notifications
type Slack struct {
Enable bool
Enabled bool
Channel string
Token string `yaml:",omitempty"`
}

// ElasticSearch config auth settings
type ElasticSearch struct {
Enable bool
Enabled bool
Username string
Password string `yaml:",omitempty"`
Server string
Expand All @@ -65,6 +65,15 @@ type Index struct {
Replicas int
}

// Mattermost configuration to authentication and send notifications
type Mattermost struct {
Enabled bool
URL string
Token string
Team string
Channel string
}

// Settings for multicluster support
type Settings struct {
ClusterName string
Expand Down

0 comments on commit 71cd575

Please sign in to comment.