-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (87 loc) · 2.61 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"context"
"errors"
"flag"
"fmt"
"io/fs"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"github.com/sapslaj/zonepop/config"
"github.com/sapslaj/zonepop/config/configtypes"
"github.com/sapslaj/zonepop/controller"
"github.com/sapslaj/zonepop/pkg/log"
)
var (
configFileName = flag.String("config-file", "config.lua", "Path to configuration file (default: config.lua)")
interval = flag.Duration("interval", 1*time.Minute, "The interval between two consecutive synchronizations in duration format (default: 1m)")
once = flag.Bool("once", false, "When enabled, exits the synchronization loop after the first iteration (default: disabled)")
dryRun = flag.Bool("dry-run", false, "When enabled, prints DNS record changes rather than actually performing them (default: disabled)")
metricsPort = flag.Int("metrics-port", 9412, "Prometheus metrics port")
)
func main() {
logger := log.MustNewLogger().Named("main")
defer func() {
err := logger.Sync()
var perr *fs.PathError
if !errors.As(err, &perr) {
panic(err)
}
}()
logger.Info("Starting ZonePop v" + VERSION)
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
go handleSigterm(cancel, logger)
if *dryRun {
ctx = context.WithValue(ctx, configtypes.DryRunContextKey, true)
}
c, err := config.NewLuaConfig(*configFileName)
if err != nil {
logger.Sugar().Panicf("could not create new configuration: %v", err)
}
err = c.Parse()
if err != nil {
logger.Sugar().Panicf("could not parse configuration: %v", err)
}
sources, err := c.Sources()
if err != nil {
logger.Sugar().Panicf("could not get sources from configuration: %v", err)
}
providers, err := c.Providers()
if err != nil {
logger.Sugar().Panicf("could not get providers from configuration: %v", err)
}
ctrl := controller.Controller{
Sources: sources,
Providers: providers,
Interval: *interval,
Logger: logger.Named("controller"),
}
if *once {
err := ctrl.RunOnce(ctx)
if err != nil {
logger.Sugar().Panicf("could not execute controller loop: %v", err)
}
return
}
go func() {
addr := fmt.Sprintf(":%d", *metricsPort)
logger.Sugar().Infof("Metrics available at http://0.0.0.0%s/metrics", addr)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(addr, nil)
}()
ctrl.ScheduleRunOnce(time.Now())
ctrl.Run(ctx)
}
func handleSigterm(cancel func(), logger *zap.Logger) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM)
<-signals
logger.Info("Received SIGTERM. Terminating...")
cancel()
}