Skip to content

Commit

Permalink
Support multiple receivers for each notification (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalsaha committed Aug 1, 2017
1 parent e981ff7 commit e37ffb1
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 120 deletions.
4 changes: 2 additions & 2 deletions docs/examples/event-forwarder/config.yaml
Expand Up @@ -9,8 +9,8 @@ eventForwarder:
handle: true
namespaces:
- kube-system
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
notifierSecretName: notifier-config
4 changes: 2 additions & 2 deletions docs/examples/recycle-bin/config.yaml
Expand Up @@ -2,8 +2,8 @@ notifierSecretName: notifier-config
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
ttl: 168h
8 changes: 4 additions & 4 deletions docs/install.md
Expand Up @@ -18,8 +18,8 @@ eventForwarder:
handle: true
nodeAdded:
handle: true
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
storageAdded:
Expand All @@ -42,8 +42,8 @@ notifierSecretName: notifier-config
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
ttl: 168h0m0s
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorials/event-forwarder.md
Expand Up @@ -25,8 +25,8 @@ eventForwarder:
handle: true
namespaces:
- kube-system
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
notifierSecretName: notifier-config
Expand Down Expand Up @@ -181,8 +181,8 @@ eventForwarder:
handle: true
namespaces:
- kube-system
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
notifierSecretName: notifier-config
Expand Down
39 changes: 22 additions & 17 deletions docs/tutorials/notifiers.md
Expand Up @@ -39,8 +39,8 @@ Now, to receiver notifications via Hipchat, configure receiver as below:
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: hipchat
receivers:
- notifier: hipchat
to:
- ops-alerts
ttl: 168h
Expand Down Expand Up @@ -96,8 +96,8 @@ Now, to receiver notifications via Mailgun, configure receiver as below:
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops-alerts@example.com
ttl: 168h
Expand Down Expand Up @@ -152,8 +152,8 @@ Now, to receiver notifications via SMTP, configure receiver as below:
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: smtp
receivers:
- notifier: smtp
to:
- ops-alerts@example.com
ttl: 168h
Expand Down Expand Up @@ -205,8 +205,8 @@ Now, to receiver notifications via SMTP, configure receiver as below:
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: twilio
receivers:
- notifier: twilio
to:
- +1-999-888-1234
ttl: 168h
Expand Down Expand Up @@ -250,8 +250,8 @@ Now, to receiver notifications via Hipchat, configure receiver as below:
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: slack
receivers:
- notifier: slack
to:
- #ops-alerts
ttl: 168h
Expand Down Expand Up @@ -303,8 +303,8 @@ Now, to receiver notifications via SMTP, configure receiver as below:
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: plivo
receivers:
- notifier: plivo
to:
- +1-999-888-1234
ttl: 168h
Expand Down Expand Up @@ -338,22 +338,27 @@ eventForwarder:
handle: true
namespaces:
- kube-system
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
- notifier: slack
to:
- #ops-alerts
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: slack
receivers:
- notifier: mailgun
to:
- ops@example.com
- notifier: slack
to:
- #ops-alerts
ttl: 168h
notifierSecretName: notifier-config
```


## Next Steps
- Learn how to use Kubed to take periodic snapshots of a Kubernetes cluster [here](/docs/tutorials/cluster-snapshot.md).
- To setup a recycle bin for deleted and/or updated Kubernetes objects, please visit [here](/docs/tutorials/recycle-bin.md).
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/recycle-bin.md
Expand Up @@ -19,8 +19,8 @@ recycleBin:
path: /tmp/kubed/trash
ttl: 168h
handleUpdates: false
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
```
Expand Down
9 changes: 6 additions & 3 deletions hack/config/main.go
Expand Up @@ -67,10 +67,11 @@ func CreateClusterConfig() config.ClusterConfig {
Path: "/tmp/kubed/trash",
TTL: metav1.Duration{Duration: 7 * 24 * time.Hour},
HandleUpdates: false,
Receiver: &config.Receiver{
Receivers: []config.Receiver{{
To: []string{"ops@example.com"},
Notifier: mailgun.UID,
},
},
},
EnableConfigSyncer: true,
EventForwarder: &config.EventForwarderSpec{
Expand All @@ -87,11 +88,13 @@ func CreateClusterConfig() config.ClusterConfig {
Handle: true,
Namespaces: []string{
"kube-system",
}},
Receiver: config.Receiver{
},
},
Receivers: []config.Receiver{{
To: []string{"ops@example.com"},
Notifier: mailgun.UID,
},
},
},
Janitors: []config.JanitorSpec{
{
Expand Down
8 changes: 4 additions & 4 deletions hack/deploy/config.yaml
Expand Up @@ -8,8 +8,8 @@ eventForwarder:
handle: true
nodeAdded:
handle: true
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
storageAdded:
Expand All @@ -32,8 +32,8 @@ notifierSecretName: notifier-config
recycleBin:
handleUpdates: false
path: /tmp/kubed/trash
receiver:
notifier: mailgun
receivers:
- notifier: mailgun
to:
- ops@example.com
ttl: 168h0m0s
Expand Down
2 changes: 1 addition & 1 deletion hack/deploy/kubed-config.yaml
@@ -1,6 +1,6 @@
apiVersion: v1
data:
config.yaml: YXBpU2VydmVyOgogIGFkZHJlc3M6IDo4MDgwCiAgZW5hYmxlUmV2ZXJzZUluZGV4OiB0cnVlCiAgZW5hYmxlU2VhcmNoSW5kZXg6IHRydWUKZW5hYmxlQ29uZmlnU3luY2VyOiB0cnVlCmV2ZW50Rm9yd2FyZGVyOgogIGluZ3Jlc3NBZ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eDogbWluaWt1YmUKICBzYW5pdGl6ZTogdHJ1ZQogIHNjaGVkdWxlOiAnQGV2ZXJ5IDZoJwogIHN0b3JhZ2VTZWNyZXROYW1lOiBzbmFwLXNlY3JldAo=
kind: ConfigMap
metadata:
creationTimestamp: null
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config.go
Expand Up @@ -51,15 +51,15 @@ type RecycleBinSpec struct {
Path string `json:"path,omitempty"`
TTL metav1.Duration `json:"ttl,omitempty"`
HandleUpdates bool `json:"handleUpdates"`
Receiver *Receiver `json:"receiver,omitempty"`
Receivers []Receiver `json:"receivers,omitempty"`
}

type EventForwarderSpec struct {
NodeAdded ForwarderSpec `json:"nodeAdded,omitempty"`
StorageAdded ForwarderSpec `json:"storageAdded,omitempty"`
IngressAdded ForwarderSpec `json:"ingressAdded,omitempty"`
WarningEvents ForwarderSpec `json:"warningEvents,omitempty"`
Receiver Receiver `json:"receiver,omitempty"`
Receivers []Receiver `json:"receivers,omitempty"`
}

type NoNamespacedForwarderSpec struct {
Expand Down
86 changes: 45 additions & 41 deletions pkg/eventer/forwarder.go
Expand Up @@ -14,34 +14,36 @@ import (
)

type EventForwarder struct {
Receiver config.Receiver
Loader envconfig.LoaderFunc
Receivers []config.Receiver
Loader envconfig.LoaderFunc
}

func (f *EventForwarder) ForwardEvent(e *apiv1.Event) error {
if e.Type == apiv1.EventTypeWarning {
if len(f.Receiver.To) > 0 {
sub := fmt.Sprintf("%s %s/%s: %s", e.InvolvedObject.Kind, e.InvolvedObject.Namespace, e.InvolvedObject.Name, e.Reason)
if notifier, err := unified.LoadVia(strings.ToLower(f.Receiver.Notifier), f.Loader); err == nil {
switch n := notifier.(type) {
case notify.ByEmail:
bytes, err := yaml.Marshal(e)
if err != nil {
return err
for _, receiver := range f.Receivers {
if len(receiver.To) > 0 {
sub := fmt.Sprintf("%s %s/%s: %s", e.InvolvedObject.Kind, e.InvolvedObject.Namespace, e.InvolvedObject.Name, e.Reason)
if notifier, err := unified.LoadVia(strings.ToLower(receiver.Notifier), f.Loader); err == nil {
switch n := notifier.(type) {
case notify.ByEmail:
bytes, err := yaml.Marshal(e)
if err != nil {
return err
}
n.To(receiver.To[0], receiver.To[1:]...).
WithSubject(sub).
WithBody(string(bytes)).
WithNoTracking().
Send()
case notify.BySMS:
n.To(receiver.To[0], receiver.To[1:]...).
WithBody(sub).
Send()
case notify.ByChat:
n.To(receiver.To[0], receiver.To[1:]...).
WithBody(sub).
Send()
}
n.To(f.Receiver.To[0], f.Receiver.To[1:]...).
WithSubject(sub).
WithBody(string(bytes)).
WithNoTracking().
Send()
case notify.BySMS:
n.To(f.Receiver.To[0], f.Receiver.To[1:]...).
WithBody(sub).
Send()
case notify.ByChat:
n.To(f.Receiver.To[0], f.Receiver.To[1:]...).
WithBody(sub).
Send()
}
}
}
Expand All @@ -54,24 +56,26 @@ func (f *EventForwarder) Forward(t metav1.TypeMeta, meta metav1.ObjectMeta, v in
if err != nil {
return err
}
if len(f.Receiver.To) > 0 {
sub := fmt.Sprintf("%s %s %s/%s added", t.APIVersion, t.Kind, meta.Namespace, meta.Name)
if notifier, err := unified.LoadVia(strings.ToLower(f.Receiver.Notifier), f.Loader); err == nil {
switch n := notifier.(type) {
case notify.ByEmail:
n.To(f.Receiver.To[0], f.Receiver.To[1:]...).
WithSubject(sub).
WithBody(string(bytes)).
WithNoTracking().
Send()
case notify.BySMS:
n.To(f.Receiver.To[0], f.Receiver.To[1:]...).
WithBody(sub).
Send()
case notify.ByChat:
n.To(f.Receiver.To[0], f.Receiver.To[1:]...).
WithBody(sub).
Send()
for _, receiver := range f.Receivers {
if len(receiver.To) > 0 {
sub := fmt.Sprintf("%s %s %s/%s added", t.APIVersion, t.Kind, meta.Namespace, meta.Name)
if notifier, err := unified.LoadVia(strings.ToLower(receiver.Notifier), f.Loader); err == nil {
switch n := notifier.(type) {
case notify.ByEmail:
n.To(receiver.To[0], receiver.To[1:]...).
WithSubject(sub).
WithBody(string(bytes)).
WithNoTracking().
Send()
case notify.BySMS:
n.To(receiver.To[0], receiver.To[1:]...).
WithBody(sub).
Send()
case notify.ByChat:
n.To(receiver.To[0], receiver.To[1:]...).
WithBody(sub).
Send()
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/operator.go
Expand Up @@ -106,8 +106,8 @@ func (op *Operator) Setup() error {

if op.Config.EventForwarder != nil {
op.Eventer = &eventer.EventForwarder{
Receiver: op.Config.EventForwarder.Receiver,
Loader: op.NotifierLoader,
Receivers: op.Config.EventForwarder.Receivers,
Loader: op.NotifierLoader,
}
}

Expand Down

0 comments on commit e37ffb1

Please sign in to comment.