-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.go
More file actions
69 lines (55 loc) · 1.32 KB
/
notifier.go
File metadata and controls
69 lines (55 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"os"
"strings"
"github.com/containrrr/shoutrrr"
"github.com/containrrr/shoutrrr/pkg/router"
"github.com/containrrr/shoutrrr/pkg/types"
)
type Notifier struct {
router *router.ServiceRouter
}
func NewNotifierFromEnv() *Notifier {
urlsRaw := os.Getenv("SHOUTRRR_URLS")
if urlsRaw == "" {
return nil
}
urls := strings.Split(urlsRaw, ",")
var validURLs []string
for _, u := range urls {
u = strings.TrimSpace(u)
if u != "" {
validURLs = append(validURLs, u)
}
}
if len(validURLs) == 0 {
return nil
}
r, err := shoutrrr.CreateSender(validURLs...)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create shoutrrr sender: %v\n", err)
return nil
}
return &Notifier{router: r}
}
func (n *Notifier) Send(message string) {
if n == nil || n.router == nil {
return
}
go func() {
_ = n.router.Send(message, &types.Params{})
}()
}
func (n *Notifier) Sendf(format string, args ...any) {
n.Send(fmt.Sprintf(format, args...))
}
func (n *Notifier) TaintAdded(nodeName string) {
n.Sendf("🚫 Node %s marked out-of-service (NotReady threshold exceeded)", nodeName)
}
func (n *Notifier) TaintRemoved(nodeName string) {
n.Sendf("✅ Node %s back in service (now Ready)", nodeName)
}
func (n *Notifier) Error(context string, err error) {
n.Sendf("❌ Error [%s]: %v", context, err)
}