Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Merge #227
Browse files Browse the repository at this point in the history
227: Implement simple SMTP handler r=mkmik a=mkmik

Closes #215

This PR implements a SMTP client (supporting auth, TLS etc).

```
$ 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
```

TODO: template the subject based on the event metadata/actions.

Co-authored-by: Marko Mikulicic <[email protected]>
Co-authored-by: Marko Mikulicic <[email protected]>
  • Loading branch information
3 people authored Jul 3, 2020
2 parents d5375fa + 3fdd594 commit b9ee12c
Show file tree
Hide file tree
Showing 18 changed files with 792 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ $ kubewatch -h
Kubewatch: A watcher for Kubernetes
kubewatch is a Kubernetes watcher that publishes notifications
to Slack/hipchat/mattermost/flock channels. It watches the cluster
kubewatch is a Kubernetes watcher that publishes notifications
to Slack/hipchat/mattermost/flock channels. It watches the cluster
for resource changes and notifies them through webhooks.
supported webhooks:
Expand All @@ -23,6 +23,7 @@ supported webhooks:
- mattermost
- flock
- webhook
- smtp
Usage:
kubewatch [flags]
Expand Down
3 changes: 2 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"os"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/client"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -98,5 +98,6 @@ func init() {
flockConfigCmd,
webhookConfigCmd,
msteamsConfigCmd,
smtpConfigCmd,
)
}
38 changes: 38 additions & 0 deletions cmd/smtp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2020 VMware
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"os"

"github.com/bitnami-labs/kubewatch/pkg/handlers/smtp"
"github.com/spf13/cobra"
)

// smtpConfigCmd represents the smtp subcommand
var smtpConfigCmd = &cobra.Command{
Use: "smtp",
Short: "specific smtp configuration",
Long: `specific smtp configuration`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "CLI setters not implemented yet, please edit ~/.kubewatch.yaml directly. Example:\n\n%s", smtp.ConfigExample)
},
}

func init() {
}
20 changes: 20 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Handler struct {
Flock Flock `json:"flock"`
Webhook Webhook `json:"webhook"`
MSTeams MSTeams `json:"msteams"`
SMTP SMTP `json:"smtp"`
}

// Resource contains resource configuration
Expand Down Expand Up @@ -103,6 +104,25 @@ type MSTeams struct {
WebhookURL string `json:"webhookurl"`
}

// SMTP contains SMTP configuration.
type SMTP struct {
To string `json:"to" yaml:"to,omitempty"`
From string `json:"from" yaml:"from,omitempty"`
Hello string `json:"hello" yaml:"hello,omitempty"`
Smarthost string `json:"smarthost" yaml:"smarthost,omitempty"`
Subject string `json:"subject" yaml:"subject,omitempty"`
Headers map[string]string `json:"headers" yaml:"headers,omitempty"`
Auth SMTPAuth `json:"auth" yaml:"auth,omitempty"`
RequireTLS bool `json:"requireTLS" yaml:"requireTLS"`
}

type SMTPAuth struct {
Username string `json:"username" yaml:"username,omitempty"`
Password string `json:"password" yaml:"password,omitempty"`
Secret string `json:"secret" yaml:"secret,omitempty"`
Identity string `json:"identity" yaml:"identity,omitempty"`
}

// New creates new config object
func New() (*Config, error) {
c := &Config{}
Expand Down
1 change: 1 addition & 0 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ With each event get from k8s and matched filtering from configuration, it is pas
- `Mattermost`: which send notification to Mattermost channel based on information from config
- `MS Teams`: which send notification to MS Team incoming webhook based on information from config
- `Slack`: which send notification to Slack channel based on information from config
- `Smtp`: which sends notifications to email recipients using a SMTP server obtained from config

More handlers will be added in future.

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.7.4 // indirect
github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc // indirect
github.com/mkmik/multierror v0.3.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pelletier/go-toml v1.0.1 // indirect
github.com/sirupsen/logrus v1.6.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ github.com/magiconair/properties v1.7.4/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc h1:5T6hzGUO5OrL6MdYXYoLQtRWJDDgjdlOVBn9mIqGY1g=
github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mkmik/multierror v0.3.0/go.mod h1:wjBYXRpDhh+8mIp+iLBOq0kZ3Y4ICTncojwvP8LUYLQ=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/bitnami-labs/kubewatch/pkg/handlers/mattermost"
"github.com/bitnami-labs/kubewatch/pkg/handlers/msteam"
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
"github.com/bitnami-labs/kubewatch/pkg/handlers/smtp"
"github.com/bitnami-labs/kubewatch/pkg/handlers/webhook"
)

Expand Down Expand Up @@ -54,6 +55,8 @@ func ParseEventHandler(conf *config.Config) handlers.Handler {
eventHandler = new(webhook.Webhook)
case len(conf.Handler.MSTeams.WebhookURL) > 0:
eventHandler = new(msteam.MSTeams)
case len(conf.Handler.SMTP.Smarthost) > 0 || len(conf.Handler.SMTP.To) > 0:
eventHandler = new(smtp.SMTP)
default:
eventHandler = new(handlers.Default)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/bitnami-labs/kubewatch/pkg/handlers/mattermost"
"github.com/bitnami-labs/kubewatch/pkg/handlers/msteam"
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
"github.com/bitnami-labs/kubewatch/pkg/handlers/smtp"
"github.com/bitnami-labs/kubewatch/pkg/handlers/webhook"
)

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

// Default handler implements Handler interface,
Expand Down
Loading

0 comments on commit b9ee12c

Please sign in to comment.