diff --git a/README.md b/README.md index 5c6d1f5..1a3c8bc 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/main.go b/cmd/main.go index 2448b34..ca617d4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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.") @@ -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{ @@ -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") diff --git a/internal/service/service.go b/internal/service/service.go index 38d5ea8..f4669b4 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -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 } diff --git a/internal/service/slack.go b/internal/service/slack.go index e47adf1..61a52de 100644 --- a/internal/service/slack.go +++ b/internal/service/slack.go @@ -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) } } diff --git a/internal/service/slack_test.go b/internal/service/slack_test.go new file mode 100644 index 0000000..16a68d2 --- /dev/null +++ b/internal/service/slack_test.go @@ -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) + }) + } +}