-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (64 loc) · 1.82 KB
/
main.go
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
70
71
72
73
74
75
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/e-flux-platform/fluxcd-suspend-notifier/internal/config"
"github.com/e-flux-platform/fluxcd-suspend-notifier/internal/datastore"
"github.com/e-flux-platform/fluxcd-suspend-notifier/internal/k8s"
"github.com/e-flux-platform/fluxcd-suspend-notifier/internal/notification"
"github.com/e-flux-platform/fluxcd-suspend-notifier/internal/watch"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
if err := run(ctx); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
configPath := os.Getenv("CONFIG_PATH")
if configPath == "" {
return errors.New("config path environment variable not set")
}
conf, err := config.ParseFile(configPath)
if err != nil {
return fmt.Errorf("failed to parse config: %w", err)
}
k8sClient, err := k8s.NewClient(conf.KubernetesConfigPath)
if err != nil {
return err
}
store, err := datastore.NewBadgerStore(conf.BadgerPath)
if err != nil {
return err
}
defer store.Close()
notifiers := make([]notification.Notifier, 0, len(conf.Notification.Slack))
for _, slack := range conf.Notification.Slack {
var notifier notification.Notifier
notifier, err = notification.NewSlackNotifier(slack.WebhookURL)
if err != nil {
return fmt.Errorf("failed to create slack notifier: %w", err)
}
if slack.Filter != "" {
notifier, err = notification.NewFilteringNotifier(slack.Filter, notifier)
if err != nil {
return fmt.Errorf("failed to create filtering notifier: %w", err)
}
}
notifiers = append(notifiers, notifier)
}
watcher := watch.NewWatcher(
conf.GoogleCloudProjectID,
conf.GKEClusterName,
k8sClient,
store,
notification.NewMultiNotifier(notifiers),
)
return watcher.Watch(ctx)
}