-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
main.go
142 lines (112 loc) · 3.81 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------
package main
import (
"context"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/dapr/kit/logger"
"github.com/dapr/dapr/pkg/credentials"
"github.com/dapr/dapr/pkg/fswatcher"
"github.com/dapr/dapr/pkg/health"
"github.com/dapr/dapr/pkg/placement"
"github.com/dapr/dapr/pkg/placement/hashing"
"github.com/dapr/dapr/pkg/placement/monitoring"
"github.com/dapr/dapr/pkg/placement/raft"
"github.com/dapr/dapr/pkg/version"
)
var log = logger.NewLogger("dapr.placement")
const gracefulTimeout = 10 * time.Second
func main() {
logger.DaprVersion = version.Version()
log.Infof("starting Dapr Placement Service -- version %s -- commit %s", version.Version(), version.Commit())
cfg := newConfig()
// Apply options to all loggers.
if err := logger.ApplyOptionsToLoggers(&cfg.loggerOptions); err != nil {
log.Fatal(err)
}
log.Infof("log level set to: %s", cfg.loggerOptions.OutputLevel)
// Initialize dapr metrics for placement.
if err := cfg.metricsExporter.Init(); err != nil {
log.Fatal(err)
}
if err := monitoring.InitMetrics(); err != nil {
log.Fatal(err)
}
// Start Raft cluster.
raftServer := raft.New(cfg.raftID, cfg.raftInMemEnabled, cfg.raftPeers, cfg.raftLogStorePath)
if raftServer == nil {
log.Fatal("failed to create raft server.")
}
if err := raftServer.StartRaft(nil); err != nil {
log.Fatalf("failed to start Raft Server: %v", err)
}
// Start Placement gRPC server.
hashing.SetReplicationFactor(cfg.replicationFactor)
apiServer := placement.NewPlacementService(raftServer)
var certChain *credentials.CertChain
if cfg.tlsEnabled {
certChain = loadCertChains(cfg.certChainPath)
}
go apiServer.MonitorLeadership()
go apiServer.Run(strconv.Itoa(cfg.placementPort), certChain)
log.Infof("placement service started on port %d", cfg.placementPort)
// Start Healthz endpoint.
go startHealthzServer(cfg.healthzPort)
// Relay incoming process signal to exit placement gracefully
signalCh := make(chan os.Signal, 10)
gracefulExitCh := make(chan struct{})
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
defer signal.Stop(signalCh)
<-signalCh
// Shutdown servers
go func() {
apiServer.Shutdown()
raftServer.Shutdown()
close(gracefulExitCh)
}()
select {
case <-time.After(gracefulTimeout):
log.Info("Timeout on graceful leave. Exiting...")
os.Exit(1)
case <-gracefulExitCh:
log.Info("Gracefully exit.")
os.Exit(0)
}
}
func startHealthzServer(healthzPort int) {
healthzServer := health.NewServer(log)
healthzServer.Ready()
if err := healthzServer.Run(context.Background(), healthzPort); err != nil {
log.Fatalf("failed to start healthz server: %s", err)
}
}
func loadCertChains(certChainPath string) *credentials.CertChain {
tlsCreds := credentials.NewTLSCredentials(certChainPath)
log.Info("mTLS enabled, getting tls certificates")
// try to load certs from disk, if not yet there, start a watch on the local filesystem
chain, err := credentials.LoadFromDisk(tlsCreds.RootCertPath(), tlsCreds.CertPath(), tlsCreds.KeyPath())
if err != nil {
fsevent := make(chan struct{})
go func() {
log.Infof("starting watch for certs on filesystem: %s", certChainPath)
err = fswatcher.Watch(context.Background(), tlsCreds.Path(), fsevent)
if err != nil {
log.Fatal("error starting watch on filesystem: %s", err)
}
}()
<-fsevent
log.Info("certificates detected")
chain, err = credentials.LoadFromDisk(tlsCreds.RootCertPath(), tlsCreds.CertPath(), tlsCreds.KeyPath())
if err != nil {
log.Fatal("failed to load cert chain from disk: %s", err)
}
}
log.Info("tls certificates loaded successfully")
return chain
}