-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
63 lines (50 loc) · 1.5 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
// Copyright Contributors to the Open Cluster Management project
package main
import (
"context"
"flag"
"os"
"os/signal"
"syscall"
"time"
"github.com/stolostron/search-indexer/pkg/clustersync"
"github.com/stolostron/search-indexer/pkg/config"
"github.com/stolostron/search-indexer/pkg/database"
"github.com/stolostron/search-indexer/pkg/server"
"k8s.io/klog/v2"
)
func main() {
// Initialize the logger.
klog.InitFlags(nil)
flag.Parse()
defer klog.Flush()
klog.Info("Starting search-indexer.")
// Read the config from the environment.
config.Cfg.PrintConfig()
// Validate required configuration to proceed.
configError := config.Cfg.Validate()
if configError != nil {
klog.Fatal(configError)
}
ctx, exitRoutines := context.WithCancel(context.Background())
// Initialize the database
dao := database.NewDAO(nil)
dao.InitializeTables(ctx)
// Start cluster sync.
go clustersync.ElectLeaderAndStart(ctx)
// Start the server.
srv := &server.ServerConfig{
Dao: &dao,
}
go srv.StartAndListen(ctx)
// Listen and wait for termination signal.
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs // Waits for termination signal.
klog.Warningf("Received termination signal %s. Exiting server and clustersync routines. ", sig)
exitRoutines()
// We could use a waitgroup to wait for leader election and server to shutdown
// but it add more complexity so keeping simple for now.
time.Sleep(5 * time.Second)
klog.Warning("Exiting search-indexer.")
}