Skip to content

Commit

Permalink
replace slack token with webhook url
Browse files Browse the repository at this point in the history
PDOK-16153
  • Loading branch information
roelarents committed Apr 29, 2024
1 parent 9c7c52f commit f72a59d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ OPTIONS:
Namespace(s) to watch for changes. Specify this flag multiple times for each namespace to watch. When not provided all namespaces will be watched.
-slack-channel string
The Slack Channel ID for posting updates when uptime checks are mutated.
-slack-token string
The token required to access the given Slack channel.
-slack-webhook-url string
The webhook URL required to post messages to the given Slack channel.
-uptime-provider string
Name of the (SaaS) uptime monitoring provider to use. (default "mock")
-zap-devel
Expand Down
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func main() {
var enableHTTP2 bool
var namespaces util.SliceFlag
var slackChannel string
var slackToken string
var slackWebhookURL string
var uptimeProvider string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
Expand All @@ -81,7 +81,7 @@ func main() {
flag.Var(&namespaces, "namespace", "Namespace(s) to watch for changes. "+
"Specify this flag multiple times for each namespace to watch. When not provided all namespaces will be watched.")
flag.StringVar(&slackChannel, "slack-channel", "", "The Slack Channel ID for posting updates when uptime checks are mutated.")
flag.StringVar(&slackToken, "slack-token", "", "The token required to access the given Slack channel.")
flag.StringVar(&slackWebhookURL, "slack-webhook-url", "", "The webhook URL required to post messages to the given Slack channel.")
flag.StringVar(&uptimeProvider, "uptime-provider", "mock", "Name of the (SaaS) uptime monitoring provider to use.")

opts := zap.Options{
Expand Down Expand Up @@ -159,7 +159,7 @@ func main() {
Scheme: mgr.GetScheme(),
UptimeCheckService: service.New(
service.WithProviderName(uptimeProvider),
service.WithSlack(slackToken, slackChannel),
service.WithSlack(slackWebhookURL, slackChannel),
),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "IngressRoute")
Expand Down
6 changes: 3 additions & 3 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func WithProviderName(provider string) UptimeCheckOption {
}
}

func WithSlack(slackToken string, slackChannel string) UptimeCheckOption {
func WithSlack(slackWebhookURL string, slackChannel string) UptimeCheckOption {
return func(service *UptimeCheckService) *UptimeCheckService {
if slackToken != "" && slackChannel != "" {
service.slack = NewSlack(slackToken, slackChannel)
if slackWebhookURL != "" && slackChannel != "" {
service.slack = NewSlack(slackWebhookURL, slackChannel)
}
return service
}
Expand Down
23 changes: 12 additions & 11 deletions internal/service/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ import (
)

type Slack struct {
client *slack.Client
channelID string
webhookURL string
channelID string
}

func NewSlack(token, channelID string) *Slack {
func NewSlack(webhookURL, channelID string) *Slack {
return &Slack{
client: slack.New(token),
channelID: channelID,
webhookURL: webhookURL,
channelID: channelID,
}
}

func (s *Slack) Send(ctx context.Context, message string) {
channelID, timestamp, err := s.client.PostMessageContext(ctx, s.channelID,
slack.MsgOptionText(message, false),
slack.MsgOptionUsername(model.OperatorName),
slack.MsgOptionIconEmoji(":up:"),
)
err := slack.PostWebhook(s.webhookURL, &slack.WebhookMessage{
Channel: s.channelID,
Text: message,
Username: model.OperatorName,
IconEmoji: ":up:",
})
if err != nil {
log.FromContext(ctx).Error(err, "failed to post Slack message",
"message", message, "channel", channelID, "timestamp", timestamp)
"message", message, "channel", s.channelID)
}
}
38 changes: 38 additions & 0 deletions internal/service/slack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package service

import (
"context"
"os"
"testing"
)

func TestSlack_Send(t *testing.T) {
t.Skip() // only for local testing
type slackConfig struct {
webhookURL string
channelID string
}
tests := []struct {
name string
fields slackConfig
message string
}{
{
name: "test send to some webhook url",
fields: slackConfig{
webhookURL: os.Getenv("SLACK_WEBHOOK_URL"), // secret!
channelID: os.Getenv("SLACK_CHANNEL_ID"),
},
message: ":warning:\ntest",
},
}
for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
s := &Slack{
webhookURL: tt.fields.webhookURL,
channelID: tt.fields.channelID,
}
s.Send(context.TODO(), tt.message)
})
}
}

0 comments on commit f72a59d

Please sign in to comment.