Skip to content

Commit 100207b

Browse files
author
Marko Mikulicic
committed
Implement SMTP handler
``` $ kubewatch config add smtp CLI setters not implemented yet, please edit ~/.kubewatch.yaml directly. Example: handler: smtp: to: "[email protected]" from: "[email protected]" smarthost: smtp.mycompany.com:2525 subject: Test notification auth: username: myusername password: mypassword requireTLS: true ```
1 parent 40bfb18 commit 100207b

File tree

18 files changed

+792
-3
lines changed

18 files changed

+792
-3
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ $ kubewatch -h
1313
1414
Kubewatch: A watcher for Kubernetes
1515
16-
kubewatch is a Kubernetes watcher that publishes notifications
17-
to Slack/hipchat/mattermost/flock channels. It watches the cluster
16+
kubewatch is a Kubernetes watcher that publishes notifications
17+
to Slack/hipchat/mattermost/flock channels. It watches the cluster
1818
for resource changes and notifies them through webhooks.
1919
2020
supported webhooks:
@@ -23,6 +23,7 @@ supported webhooks:
2323
- mattermost
2424
- flock
2525
- webhook
26+
- smtp
2627
2728
Usage:
2829
kubewatch [flags]

cmd/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
"os"
2323
"path/filepath"
2424

25-
"github.com/sirupsen/logrus"
2625
"github.com/bitnami-labs/kubewatch/config"
2726
"github.com/bitnami-labs/kubewatch/pkg/client"
27+
"github.com/sirupsen/logrus"
2828
"github.com/spf13/cobra"
2929
)
3030

@@ -98,5 +98,6 @@ func init() {
9898
flockConfigCmd,
9999
webhookConfigCmd,
100100
msteamsConfigCmd,
101+
smtpConfigCmd,
101102
)
102103
}

cmd/smtp.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2020 VMware
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/bitnami-labs/kubewatch/pkg/handlers/smtp"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
// smtpConfigCmd represents the smtp subcommand
28+
var smtpConfigCmd = &cobra.Command{
29+
Use: "smtp",
30+
Short: "specific smtp configuration",
31+
Long: `specific smtp configuration`,
32+
Run: func(cmd *cobra.Command, args []string) {
33+
fmt.Fprintf(os.Stderr, "CLI setters not implemented yet, please edit ~/.kubewatch.yaml directly. Example:\n\n%s", smtp.ConfigExample)
34+
},
35+
}
36+
37+
func init() {
38+
}

config/config.go

+20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Handler struct {
3636
Flock Flock `json:"flock"`
3737
Webhook Webhook `json:"webhook"`
3838
MSTeams MSTeams `json:"msteams"`
39+
SMTP SMTP `json:"smtp"`
3940
}
4041

4142
// Resource contains resource configuration
@@ -100,6 +101,25 @@ type MSTeams struct {
100101
WebhookURL string `json:"webhookurl"`
101102
}
102103

104+
// SMTP contains SMTP configuration.
105+
type SMTP struct {
106+
To string `json:"to" yaml:"to,omitempty"`
107+
From string `json:"from" yaml:"from,omitempty"`
108+
Hello string `json:"hello" yaml:"hello,omitempty"`
109+
Smarthost string `json:"smarthost" yaml:"smarthost,omitempty"`
110+
Subject string `json:"subject" yaml:"subject,omitempty"`
111+
Headers map[string]string `json:"headers" yaml:"headers,omitempty"`
112+
Auth SMTPAuth `json:"auth" yaml:"auth,omitempty"`
113+
RequireTLS bool `json:"requireTLS" yaml:"requireTLS"`
114+
}
115+
116+
type SMTPAuth struct {
117+
Username string `json:"username" yaml:"username,omitempty"`
118+
Password string `json:"password" yaml:"password,omitempty"`
119+
Secret string `json:"secret" yaml:"secret,omitempty"`
120+
Identity string `json:"identity" yaml:"identity,omitempty"`
121+
}
122+
103123
// New creates new config object
104124
func New() (*Config, error) {
105125
c := &Config{}

docs/design.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ With each event get from k8s and matched filtering from configuration, it is pas
2727
- `Hipchat`: which send notification to Hipchat room based on information from config
2828
- `Mattermost`: which send notification to Mattermost channel based on information from config
2929
- `Flock`: which send notification to Flock channel based on information from config
30+
- 'Smtp': which sends notifications to email recipients using a SMTP server obtained from config
3031

3132
More handlers will be added in future.
3233

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1313
github.com/magiconair/properties v1.7.4 // indirect
1414
github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc // indirect
15+
github.com/mkmik/multierror v0.3.0
1516
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
1617
github.com/pelletier/go-toml v1.0.1 // indirect
1718
github.com/sirupsen/logrus v1.6.0

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ github.com/magiconair/properties v1.7.4/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
9191
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
9292
github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc h1:5T6hzGUO5OrL6MdYXYoLQtRWJDDgjdlOVBn9mIqGY1g=
9393
github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
94+
github.com/mkmik/multierror v0.3.0/go.mod h1:wjBYXRpDhh+8mIp+iLBOq0kZ3Y4ICTncojwvP8LUYLQ=
9495
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
9596
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
9697
github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=

pkg/client/run.go

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/bitnami-labs/kubewatch/pkg/handlers/mattermost"
2828
"github.com/bitnami-labs/kubewatch/pkg/handlers/msteam"
2929
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
30+
"github.com/bitnami-labs/kubewatch/pkg/handlers/smtp"
3031
"github.com/bitnami-labs/kubewatch/pkg/handlers/webhook"
3132
)
3233

@@ -54,6 +55,8 @@ func ParseEventHandler(conf *config.Config) handlers.Handler {
5455
eventHandler = new(webhook.Webhook)
5556
case len(conf.Handler.MSTeams.WebhookURL) > 0:
5657
eventHandler = new(msteam.MSTeams)
58+
case len(conf.Handler.SMTP.Smarthost) > 0 || len(conf.Handler.SMTP.To) > 0:
59+
eventHandler = new(smtp.SMTP)
5760
default:
5861
eventHandler = new(handlers.Default)
5962
}

pkg/handlers/handler.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/bitnami-labs/kubewatch/pkg/handlers/mattermost"
2424
"github.com/bitnami-labs/kubewatch/pkg/handlers/msteam"
2525
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
26+
"github.com/bitnami-labs/kubewatch/pkg/handlers/smtp"
2627
"github.com/bitnami-labs/kubewatch/pkg/handlers/webhook"
2728
)
2829

@@ -45,6 +46,7 @@ var Map = map[string]interface{}{
4546
"flock": &flock.Flock{},
4647
"webhook": &webhook.Webhook{},
4748
"ms-teams": &msteam.MSTeams{},
49+
"smtp": &smtp.SMTP{},
4850
}
4951

5052
// Default handler implements Handler interface,

0 commit comments

Comments
 (0)