Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor GetProviders #34

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package provider

import "github.com/abahmed/kwatch/event"
import (
"github.com/abahmed/kwatch/event"
"github.com/spf13/viper"
)

const (
footer = "<https://github.com/abahmed/kwatch|kwatch>"
Expand All @@ -14,3 +17,21 @@ type Provider interface {
SendEvent(*event.Event) error
SendMessage(string) error
}

// New returns a new Provider object
func New(name string) Provider {
switch name {
case "slack":
return NewSlack(viper.GetString("alert.slack.webhook"))
case "discord":
return NewDiscord(viper.GetString("alert.discord.webhook"))
case "pagerduty":
return NewPagerDuty(viper.GetString("alert.pagerduty.integrationKey"))
case "telegram":
return NewTelegram(viper.GetString("alert.telegram.token"), viper.GetString("alert.telegram.chatId"))
case "teams":
return NewTeams(viper.GetString("alert.teams.webhook"))
default:
return nil
}
}
18 changes: 18 additions & 0 deletions provider/providers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"slack": {
"webhook": ""
},
"pagerduty": {
"integrationkey": ""
},
"discord": {
"webhook": ""
},
"telegram": {
"token": "",
"chatid": ""
},
"teams": {
"webhook": ""
}
}
63 changes: 34 additions & 29 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"strings"

"github.com/abahmed/kwatch/event"
Expand Down Expand Up @@ -96,33 +97,26 @@ func getPodEvents(c kubernetes.Interface, name, namespace string) (*v1.EventList

// GetProviders returns slice of provider objects after parsing config
func GetProviders() []provider.Provider {
var providers []provider.Provider
const isPresent = false
telegram := []bool{isPresent, isPresent}
pAttributes := populateProvidersAttributes()

var providers []provider.Provider
for key, value := range viper.Get("alert").(map[string]interface{}) {
// checks if the provider name exists
att, found := pAttributes[key]
if !found {
continue
}

// checks if the corresponding attributes exists
for c, v := range value.(map[string]interface{}) {
if key == "slack" && c == "webhook" && len(strings.TrimSpace(v.(string))) > 0 {
providers = append(providers, provider.NewSlack(viper.GetString("alert.slack.webhook")))
}
if key == "pagerduty" && c == "integrationkey" && len(strings.TrimSpace(v.(string))) > 0 {
providers = append(providers, provider.NewPagerDuty(viper.GetString("alert.pagerduty.integrationKey")))
}
if key == "discord" && c == "webhook" && len(strings.TrimSpace(v.(string))) > 0 {
providers = append(providers, provider.NewDiscord(viper.GetString("alert.discord.webhook")))
}
if key == "telegram" && c == "token" && len(strings.TrimSpace(v.(string))) > 0 {
telegram[0] = true
}
if key == "telegram" && c == "chatid" && len(strings.TrimSpace(v.(string))) > 0 {
telegram[1] = true
}
if key == "teams" && c == "webhook" && len(strings.TrimSpace(v.(string))) > 0 {
providers = append(providers, provider.NewTeams(viper.GetString("alert.teams.webhook")))
if !IsStrInSlice(c, att) || len(strings.TrimSpace(v.(string))) <= 0 {
found = false
}
}
if key == "telegram" && isListAllBool(true, telegram) {
providers = append(providers, provider.NewTelegram(viper.GetString("alert.telegram.token"), viper.GetString("alert.telegram.chatId")))

// add a new object from corresponding provider
if found {
providers = append(providers, provider.New(key))
}
}

Expand Down Expand Up @@ -173,13 +167,24 @@ func IsStrInSlice(str string, strList []string) bool {
return false
}

// checks if all elements in a boolean list have the same value
func isListAllBool(v bool, l []bool) bool {

for _, x := range l {
if x != v {
return false
// populateProvidersAttributes populates and maps providers names and required attributes from json file
func populateProvidersAttributes() map[string][]string {
js, err := ioutil.ReadFile("provider/providers.json")
if err != nil {
logrus.Error(err)
}
activeProviders := make(map[string]interface{})
parseErr := json.Unmarshal(js, &activeProviders)
if parseErr != nil {
logrus.Error(parseErr)
}
pAttributes := make(map[string][]string)
for key, value := range activeProviders {
var attList []string
for att := range value.(map[string]interface{}) {
attList = append(attList, att)
}
pAttributes[key] = attList
}
return true
return pAttributes
}