Skip to content

Commit

Permalink
Working slack config struct
Browse files Browse the repository at this point in the history
Signed-off-by: Kedar Vijay Kulkarni <[email protected]>
  • Loading branch information
Kedar Vijay Kulkarni committed Nov 10, 2021
1 parent 8883a86 commit fc96a4d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 37 deletions.
24 changes: 0 additions & 24 deletions cmd/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
}

}
55 changes: 49 additions & 6 deletions cmd/notify/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}

}
7 changes: 4 additions & 3 deletions config/slack.yaml.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
slackToken: xoxb-<token>
channelID: C0XXXXXXXXX
userID: U0XXXXXXXXX
---
userid: U0XXXXXXXXX
channelid: C0XXXXXXXXX
slacktoken: xoxb-<token>
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fc96a4d

Please sign in to comment.