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

Commit

Permalink
Merge pull request #231 from bitnami-labs/api
Browse files Browse the repository at this point in the history
Simplify handler interface
  • Loading branch information
Marko Mikulicic authored Jul 6, 2020
2 parents a456787 + aae175b commit 1d51a8d
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 334 deletions.
12 changes: 11 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/client"
"github.com/bitnami-labs/kubewatch/pkg/event"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -63,7 +64,16 @@ Tests handler configs present in ~/.kubewatch.yaml by sending test messages`,
logrus.Fatal(err)
}
eventHandler := client.ParseEventHandler(conf)
eventHandler.TestHandler()
e := event.Event{
Namespace: "testNamespace",
Name: "testResource",
Kind: "testKind",
Component: "testComponent",
Host: "testHost",
Reason: "Tested",
Status: "Normal",
}
eventHandler.Handle(e)
},
}

Expand Down
33 changes: 16 additions & 17 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"strings"

"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/event"
Expand Down Expand Up @@ -147,8 +147,7 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {

go nodeRebootedController.Run(stopNodeRebootedCh)


// User Configured Events
// User Configured Events
if conf.Resource.Pod {
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
Expand Down Expand Up @@ -626,7 +625,7 @@ func (c *Controller) processItem(newEvent Event) error {
}
// get object's metedata
objectMeta := utils.GetObjectMetaData(obj)

// hold status type for default critical alerts
var status string

Expand Down Expand Up @@ -656,13 +655,13 @@ func (c *Controller) processItem(newEvent Event) error {
status = "Normal"
}
kbEvent := event.Event{
Name: objectMeta.Name,
Name: objectMeta.Name,
Namespace: newEvent.namespace,
Kind: newEvent.resourceType,
Status: status,
Reason: "Created",
Kind: newEvent.resourceType,
Status: status,
Reason: "Created",
}
c.eventHandler.ObjectCreated(kbEvent)
c.eventHandler.Handle(kbEvent)
return nil
}
case "update":
Expand All @@ -676,23 +675,23 @@ func (c *Controller) processItem(newEvent Event) error {
status = "Warning"
}
kbEvent := event.Event{
Name: newEvent.key,
Name: newEvent.key,
Namespace: newEvent.namespace,
Kind: newEvent.resourceType,
Status: status,
Reason: "Updated",
Kind: newEvent.resourceType,
Status: status,
Reason: "Updated",
}
c.eventHandler.ObjectUpdated(obj, kbEvent)
c.eventHandler.Handle(kbEvent)
return nil
case "delete":
kbEvent := event.Event{
Name: newEvent.key,
Namespace: newEvent.namespace,
Kind: newEvent.resourceType,
Status: "Danger",
Reason: "Deleted",
Status: "Danger",
Reason: "Deleted",
}
c.eventHandler.ObjectDeleted(kbEvent)
c.eventHandler.Handle(kbEvent)
return nil
}
return nil
Expand Down
47 changes: 4 additions & 43 deletions pkg/handlers/flock/flock.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"time"

"github.com/bitnami-labs/kubewatch/config"
kbEvent "github.com/bitnami-labs/kubewatch/pkg/event"
"github.com/bitnami-labs/kubewatch/pkg/event"
)

var flockColors = map[string]string{
Expand Down Expand Up @@ -86,46 +86,8 @@ func (f *Flock) Init(c *config.Config) error {
return checkMissingFlockVars(f)
}

// ObjectCreated calls notifyFlock on event creation
func (f *Flock) ObjectCreated(obj interface{}) {
notifyFlock(f, obj)
}

// ObjectDeleted calls notifyFlock on event creation
func (f *Flock) ObjectDeleted(obj interface{}) {
notifyFlock(f, obj)
}

// ObjectUpdated calls notifyFlock on event creation
func (f *Flock) ObjectUpdated(oldObj, newObj interface{}) {
notifyFlock(f, newObj)
}

// TestHandler tests the handler configurarion by sending test messages.
func (f *Flock) TestHandler() {

flockMessage := &FlockMessage{
Text: "Kubewatch Alert",
Notification: "Kubewatch Alert",
Attachements: []FlockMessageAttachement{
{
Title: "Testing Handler Configuration. This is a Test message.",
},
},
}

err := postMessage(f.Url, flockMessage)
if err != nil {
log.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to channel %s at %s", f.Url, time.Now())
}

func notifyFlock(f *Flock, obj interface{}) {
e,_ := obj.(kbEvent.Event)

// Handle handles an event.
func (f *Flock) Handle(e event.Event) {
flockMessage := prepareFlockMessage(e, f)

err := postMessage(f.Url, flockMessage)
Expand All @@ -145,8 +107,7 @@ func checkMissingFlockVars(s *Flock) error {
return nil
}

func prepareFlockMessage(e kbEvent.Event, f *Flock) *FlockMessage {

func prepareFlockMessage(e event.Event, f *Flock) *FlockMessage {
return &FlockMessage{
Text: "Kubewatch Alert",
Notification: "Kubewatch Alert",
Expand Down
26 changes: 4 additions & 22 deletions pkg/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package handlers

import (
"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/event"
"github.com/bitnami-labs/kubewatch/pkg/handlers/flock"
"github.com/bitnami-labs/kubewatch/pkg/handlers/hipchat"
"github.com/bitnami-labs/kubewatch/pkg/handlers/mattermost"
Expand All @@ -31,10 +32,7 @@ import (
// The Handle method is used to process event
type Handler interface {
Init(c *config.Config) error
ObjectCreated(obj interface{})
ObjectDeleted(obj interface{})
ObjectUpdated(oldObj, newObj interface{})
TestHandler()
Handle(e event.Event)
}

// Map maps each event handler function to a name for easily lookup
Expand All @@ -60,22 +58,6 @@ func (d *Default) Init(c *config.Config) error {
return nil
}

// ObjectCreated sends events on object creation
func (d *Default) ObjectCreated(obj interface{}) {

}

// ObjectDeleted sends events on object deletion
func (d *Default) ObjectDeleted(obj interface{}) {

}

// ObjectUpdated sends events on object updation
func (d *Default) ObjectUpdated(oldObj, newObj interface{}) {

}

// TestHandler tests the handler configurarion by sending test messages.
func (d *Default) TestHandler() {

// Handle handles an event.
func (d *Default) Handle(e event.Event) {
}
49 changes: 2 additions & 47 deletions pkg/handlers/hipchat/hipchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/event"
kbEvent "github.com/bitnami-labs/kubewatch/pkg/event"
)

var hipchatColors = map[string]hipchat.Color{
Expand Down Expand Up @@ -83,51 +82,8 @@ func (s *Hipchat) Init(c *config.Config) error {
return checkMissingHipchatVars(s)
}

// ObjectCreated calls notifyHipchat on event creation
func (s *Hipchat) ObjectCreated(obj interface{}) {
notifyHipchat(s, obj)
}

// ObjectDeleted calls notifyHipchat on event creation
func (s *Hipchat) ObjectDeleted(obj interface{}) {
notifyHipchat(s, obj)
}

// ObjectUpdated calls notifyHipchat on event creation
func (s *Hipchat) ObjectUpdated(oldObj, newObj interface{}) {
notifyHipchat(s, newObj)
}

// TestHandler tests the handler configurarion by sending test messages.
func (s *Hipchat) TestHandler() {

client := hipchat.NewClient(s.Token)
if s.Url != "" {
baseUrl, err := url.Parse(s.Url)
if err != nil {
panic(err)
}
client.BaseURL = baseUrl
}

notificationRequest := hipchat.NotificationRequest{
Message: "Testing Handler Configuration. This is a Test message.",
Notify: true,
From: "kubewatch",
}
_, err := client.Room.Notification(s.Room, &notificationRequest)

if err != nil {
log.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to room %s", s.Room)
}

func notifyHipchat(s *Hipchat, obj interface{}) {
e,_ := obj.(kbEvent.Event)

// Handle handles the notification.
func (s *Hipchat) Handle(e event.Event) {
client := hipchat.NewClient(s.Token)
if s.Url != "" {
baseUrl, err := url.Parse(s.Url)
Expand Down Expand Up @@ -157,7 +113,6 @@ func checkMissingHipchatVars(s *Hipchat) error {
}

func prepareHipchatNotification(e event.Event) hipchat.NotificationRequest {

notification := hipchat.NotificationRequest{
Message: e.Message(),
Notify: true,
Expand Down
47 changes: 4 additions & 43 deletions pkg/handlers/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"time"

"github.com/bitnami-labs/kubewatch/config"
kbEvent "github.com/bitnami-labs/kubewatch/pkg/event"
"github.com/bitnami-labs/kubewatch/pkg/event"
)

var mattermostColors = map[string]string{
Expand Down Expand Up @@ -98,46 +98,8 @@ func (m *Mattermost) Init(c *config.Config) error {
return checkMissingMattermostVars(m)
}

// ObjectCreated calls notifyMattermost on event creation
func (m *Mattermost) ObjectCreated(obj interface{}) {
notifyMattermost(m, obj)
}

// ObjectDeleted calls notifyMattermost on event creation
func (m *Mattermost) ObjectDeleted(obj interface{}) {
notifyMattermost(m, obj)
}

// ObjectUpdated calls notifyMattermost on event creation
func (m *Mattermost) ObjectUpdated(oldObj, newObj interface{}) {
notifyMattermost(m, newObj)
}

// TestHandler tests the handler configurarion by sending test messages.
func (m *Mattermost) TestHandler() {
mattermostMessage := &MattermostMessage{
Channel: m.Channel,
Username: m.Username,
IconUrl: "https://raw.githubusercontent.com/kubernetes/kubernetes/master/logo/logo_with_border.png",
Attachements: []MattermostMessageAttachement{
{
Title: "Testing Handler Configuration. This is a Test message.",
},
},
}

err := postMessage(m.Url, mattermostMessage)
if err != nil {
log.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to channel %s at %s", m.Channel, time.Now())
}

func notifyMattermost(m *Mattermost, obj interface{}) {
e,_ := obj.(kbEvent.Event)

// Handle handles an event.
func (m *Mattermost) Handle(e event.Event) {
mattermostMessage := prepareMattermostMessage(e, m)

err := postMessage(m.Url, mattermostMessage)
Expand All @@ -157,8 +119,7 @@ func checkMissingMattermostVars(s *Mattermost) error {
return nil
}

func prepareMattermostMessage(e kbEvent.Event, m *Mattermost) *MattermostMessage {

func prepareMattermostMessage(e event.Event, m *Mattermost) *MattermostMessage {
return &MattermostMessage{
Channel: m.Channel,
Username: m.Username,
Expand Down
Loading

0 comments on commit 1d51a8d

Please sign in to comment.