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

Add support for multiple integrations #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,53 @@ package client

import (
"log"
"os"
"os/signal"
"syscall"

"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/handlers"
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
"github.com/bitnami-labs/kubewatch/pkg/controller"
"github.com/bitnami-labs/kubewatch/pkg/handlers"
"github.com/bitnami-labs/kubewatch/pkg/handlers/flock"
"github.com/bitnami-labs/kubewatch/pkg/handlers/hipchat"
"github.com/bitnami-labs/kubewatch/pkg/handlers/mattermost"
"github.com/bitnami-labs/kubewatch/pkg/handlers/flock"
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
"github.com/bitnami-labs/kubewatch/pkg/handlers/webhook"
)

// Run runs the event loop processing with given handler
func Run(conf *config.Config) {
var eventHandler handlers.Handler
switch {
case len(conf.Handler.Slack.Channel) > 0 || len(conf.Handler.Slack.Token) > 0:
eventHandler = new(slack.Slack)
case len(conf.Handler.Hipchat.Room) > 0 || len(conf.Handler.Hipchat.Token) > 0:
eventHandler = new(hipchat.Hipchat)
case len(conf.Handler.Mattermost.Channel) > 0 || len(conf.Handler.Mattermost.Url) > 0:
eventHandler = new(mattermost.Mattermost)
case len(conf.Handler.Flock.Url) > 0:
eventHandler = new(flock.Flock)
case len(conf.Handler.Webhook.Url) > 0:
eventHandler = new(webhook.Webhook)
default:
eventHandler = new(handlers.Default)
stopCh := make(chan struct{})
defer close(stopCh)

var eventHandler []handlers.Handler
if len(conf.Handler.Slack.Channel) > 0 || len(conf.Handler.Slack.Token) > 0 {
eventHandler = append(eventHandler, new(slack.Slack))
}
if len(conf.Handler.Hipchat.Room) > 0 || len(conf.Handler.Hipchat.Token) > 0 {
eventHandler = append(eventHandler, new(hipchat.Hipchat))
}
if len(conf.Handler.Mattermost.Channel) > 0 || len(conf.Handler.Mattermost.Url) > 0 {
eventHandler = append(eventHandler, new(mattermost.Mattermost))
}
if len(conf.Handler.Flock.Url) > 0 {
eventHandler = append(eventHandler, new(flock.Flock))
}
if len(conf.Handler.Webhook.Url) > 0 {
eventHandler = append(eventHandler, new(webhook.Webhook))
}

if err := eventHandler.Init(conf); err != nil {
log.Fatal(err)
if len(eventHandler) == 0 {
eventHandler = append(eventHandler, new(handlers.Default))
}
controller.Start(conf, eventHandler)

log.Printf("%v\n", eventHandler)
for _, handler := range eventHandler {
controller.Start(conf, handler, stopCh)
}

sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGTERM)
signal.Notify(sigterm, syscall.SIGINT)
<-sigterm
}
50 changes: 6 additions & 44 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package controller

import (
"fmt"
"os"
"os/signal"
"syscall"
"log"
"time"

"github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -65,7 +63,11 @@ type Controller struct {
eventHandler handlers.Handler
}

func Start(conf *config.Config, eventHandler handlers.Handler) {
func Start(conf *config.Config, eventHandler handlers.Handler, stopCh <-chan struct{}) {
if err := eventHandler.Init(conf); err != nil {
log.Fatal(err)
}

var kubeClient kubernetes.Interface
_, err := rest.InClusterConfig()
if err != nil {
Expand All @@ -89,9 +91,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "pod")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -111,9 +110,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "daemonset")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -133,9 +129,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "replicaset")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -155,9 +148,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "service")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -177,9 +167,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "deployment")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -199,9 +186,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "namespace")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -221,9 +205,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "replication controller")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -243,9 +224,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "job")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -265,9 +243,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "persistent volume")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -287,9 +262,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "secret")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -309,9 +281,6 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "configmap")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

Expand All @@ -331,16 +300,9 @@ func Start(conf *config.Config, eventHandler handlers.Handler) {
)

c := newResourceController(kubeClient, eventHandler, informer, "ingress")
stopCh := make(chan struct{})
defer close(stopCh)

go c.Run(stopCh)
}

sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGTERM)
signal.Notify(sigterm, syscall.SIGINT)
<-sigterm
}

func newResourceController(client kubernetes.Interface, eventHandler handlers.Handler, informer cache.SharedIndexInformer, resourceType string) *Controller {
Expand Down