diff --git a/cmd/analyze/analyze.go b/cmd/analyze/analyze.go index 82f67a8..61ecd22 100644 --- a/cmd/analyze/analyze.go +++ b/cmd/analyze/analyze.go @@ -4,12 +4,8 @@ import ( "fmt" "io/ioutil" "log" - "math/rand" "strconv" - "strings" - "time" - notify "github.com/kedark3/cpa/cmd/notify" prometheus "github.com/kedark3/cpa/cmd/prometheus" exutil "github.com/openshift/openshift-tests/test/extended/util" @@ -103,23 +99,3 @@ func Queries(queryList queryList, oc *exutil.CLI, baseURL, bearerToken string, c } } } - -func Notify(c chan string) { - waitChars := []string{"/", "-", "\\", "|"} - for { - select { - case msg := <-c: - msgFmt := fmt.Sprintf(` -%s -Received following on the channel: %s -%[1]s - `, strings.Repeat("~", 80), msg) - fmt.Println(msgFmt) - notify.SlackNotify(msg) - default: - fmt.Printf("\r%s Please Wait. No new message received on the channel....", waitChars[rand.Intn(4)]) - time.Sleep(time.Millisecond * 500) - } - } - -} diff --git a/cmd/notify/notifications.go b/cmd/notify/notifications.go index 63aa37e..0c9edc6 100644 --- a/cmd/notify/notifications.go +++ b/cmd/notify/notifications.go @@ -2,19 +2,42 @@ package Notify import ( "fmt" + "io/ioutil" + "log" + "math/rand" + "strings" + "time" "github.com/slack-go/slack" + "gopkg.in/yaml.v2" ) -type SlackConfig struct { - UserID string `json:"userID"` - ChannelID string `json:"channelID"` - SlackToken string `json:"slackToken"` +const configPath = "./config/" + +type slackConfig struct { + UserID string `json:"userid"` + ChannelID string `json:"channelid"` + SlackToken string `json:"slacktoken"` } -// TODO Add function to Read Slack Config +func (c *slackConfig) Parse(data []byte) error { + return yaml.Unmarshal(data, c) +} -func (s SlackConfig) SlackNotify(message string) { +func ReadslackConfig() (config slackConfig, err error) { + data, err := ioutil.ReadFile(configPath + "slack.yaml") + msg := fmt.Sprintf("Cound't read %sslack.yaml", configPath) + if err != nil { + return config, fmt.Errorf(msg) + } + if err := config.Parse(data); err != nil { + log.Fatal(err) + return config, err + } + fmt.Println(config) + return config, nil +} +func (s slackConfig) SlackNotify(message string) { api := slack.New(s.SlackToken, slack.OptionDebug(true)) msgText := slack.NewTextBlockObject("mrkdwn", fmt.Sprintf("Hi <@%s>, following query failed:%s", s.UserID, message), false, false) msgSection := slack.NewSectionBlock(msgText, nil, nil) @@ -28,3 +51,23 @@ func (s SlackConfig) SlackNotify(message string) { } } + +func (s slackConfig) Notify(c chan string) { + waitChars := []string{"/", "-", "\\", "|"} + for { + select { + case msg := <-c: + msgFmt := fmt.Sprintf(` +%s +Received following on the channel: %s +%[1]s + `, strings.Repeat("~", 80), msg) + fmt.Println(msgFmt) + s.SlackNotify(msg) + default: + fmt.Printf("\r%s Please Wait. No new message received on the channel....", waitChars[rand.Intn(4)]) + time.Sleep(time.Millisecond * 500) + } + } + +} diff --git a/config/slack.yaml.template b/config/slack.yaml.template index 02c8e1b..4e02e97 100644 --- a/config/slack.yaml.template +++ b/config/slack.yaml.template @@ -1,3 +1,4 @@ -slackToken: xoxb- -channelID: C0XXXXXXXXX -userID: U0XXXXXXXXX \ No newline at end of file +--- +userid: U0XXXXXXXXX +channelid: C0XXXXXXXXX +slacktoken: xoxb- diff --git a/go.mod b/go.mod index 2d8af20..78a06fe 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,8 @@ require ( github.com/openshift/openshift-tests v0.0.0-20210916082130-4fca21c38ee6 github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/common v0.6.0 - github.com/slack-go/slack v0.9.5 // indirect + github.com/slack-go/slack v0.9.5 + gopkg.in/yaml.v2 v2.4.0 k8s.io/api v0.17.1 k8s.io/apimachinery v0.17.1 k8s.io/kubernetes v1.21.0 diff --git a/go.sum b/go.sum index 637e0bb..51d7bfe 100644 --- a/go.sum +++ b/go.sum @@ -218,6 +218,7 @@ github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2K github.com/go-openapi/validate v0.19.5/go.mod h1:8DJv2CVJQ6kGNpFW6eV9N3JviE1C85nY1c2z52x1Gk4= github.com/go-ozzo/ozzo-validation v3.5.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho= github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/go-toolsmith/astcast v1.0.0/go.mod h1:mt2OdQTeAQcY4DQgPSArJjHCcOwlX+Wl/kwN+LbLGQ4= github.com/go-toolsmith/astcopy v1.0.0/go.mod h1:vrgyG+5Bxrnz4MZWPF+pI4R8h3qKRjjyvV/DSez4WVQ= @@ -306,7 +307,6 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51 github.com/gorilla/mux v0.0.0-20191024121256-f395758b854c/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= -github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= @@ -495,7 +495,6 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9 github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/main.go b/main.go index 2016ad8..9467612 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/alexflint/go-arg" analyze "github.com/kedark3/cpa/cmd/analyze" + notify "github.com/kedark3/cpa/cmd/notify" prometheus "github.com/kedark3/cpa/cmd/prometheus" exutil "github.com/openshift/openshift-tests/test/extended/util" @@ -61,6 +62,13 @@ func main() { return } + slackConfig, err := notify.ReadslackConfig() + if err != nil { + log.Printf("Oops something went wrong while trying to fetch Prometheus url and bearerToken") + log.Println(err) + return + } + // fmt.Println("UserID, Channel ID, slackToken: ", slackConfig) // queries := []string{ // `sum(kube_pod_status_phase{}) by (phase) > 0`, // pod count by phase // `sum(kube_namespace_status_phase) by (phase)`, // namespace count by phase @@ -100,7 +108,7 @@ func main() { } } }(c) - go analyze.Notify(c) + go slackConfig.Notify(c) d, err := time.ParseDuration(args.Timeout.String()) if err != nil { log.Println(err)