forked from projectcalico/typha
/
daemon.go
385 lines (348 loc) · 13.8 KB
/
daemon.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright (c) 2017 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package daemon
import (
"context"
"fmt"
"math/rand"
"net/http"
"os"
"os/signal"
"runtime"
"runtime/debug"
"runtime/pprof"
"strings"
"syscall"
"time"
docopt "github.com/docopt/docopt-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/libcalico-go/lib/api"
"github.com/projectcalico/libcalico-go/lib/backend"
bapi "github.com/projectcalico/libcalico-go/lib/backend/api"
"github.com/projectcalico/libcalico-go/lib/health"
"github.com/projectcalico/typha/pkg/buildinfo"
"github.com/projectcalico/typha/pkg/calc"
"github.com/projectcalico/typha/pkg/config"
"github.com/projectcalico/typha/pkg/jitter"
"github.com/projectcalico/typha/pkg/k8s"
"github.com/projectcalico/typha/pkg/logutils"
"github.com/projectcalico/typha/pkg/snapcache"
"github.com/projectcalico/typha/pkg/syncserver"
)
const usage = `Typha, Calico's fan-out proxy.
Usage:
calico-typha [options]
Options:
-c --config-file=<filename> Config file to load [default: /etc/calico/typha.cfg].
--version Print the version and exit.
`
// TyphaDaemon handles the lifecycle of the Typha process. The main() function of the Typha executable
// should simply call InitializeAndServeForever() to start the Typha server. The lifecycle is broken out into
// several individual methods for ease of testing.
type TyphaDaemon struct {
BuildInfoLogCxt *log.Entry
ConfigFilePath string
DatastoreClient BackendClient
ConfigParams *config.Config
// The components of the server, created in CreateServer() below.
Syncer bapi.Syncer
SyncerToValidator *calc.SyncerCallbacksDecoupler
Validator *calc.ValidationFilter
ValidatorToCache *calc.SyncerCallbacksDecoupler
Cache *snapcache.Cache
Server *syncserver.Server
// The functions below default to real library functions but they can be overridden for testing.
NewBackendClient func(config api.CalicoAPIConfig) (BackendClient, error)
ConfigureEarlyLogging func()
ConfigureLogging func(configParams *config.Config)
// Health monitoring.
healthAggregator *health.HealthAggregator
}
func New() *TyphaDaemon {
return &TyphaDaemon{
NewBackendClient: func(config api.CalicoAPIConfig) (BackendClient, error) {
return backend.NewClient(config)
},
ConfigureEarlyLogging: logutils.ConfigureEarlyLogging,
ConfigureLogging: logutils.ConfigureLogging,
}
}
func (t *TyphaDaemon) InitializeAndServeForever(cxt context.Context) {
t.DoEarlyRuntimeSetup()
t.ParseCommandLineArgs(nil)
t.LoadConfiguration()
t.CreateServer()
t.Start(cxt)
t.WaitAndShutDown(cxt)
}
// DoEarlyRuntimeSetup does early runtime/logging configuration that needs to happen before we do any work.
func (t *TyphaDaemon) DoEarlyRuntimeSetup() {
// Go's RNG is not seeded by default. Do that now.
rand.Seed(time.Now().UTC().UnixNano())
// Special-case handling for environment variable-configured logging:
// Initialise early so we can trace out config parsing.
t.ConfigureEarlyLogging()
}
// ParseCommandLineArgs parses the command line args and either exits with a usage warning or stores the parsed
// arguments on fields of the struct.
func (t *TyphaDaemon) ParseCommandLineArgs(argv []string) {
// Parse command-line args.
version := "Version: " + buildinfo.GitVersion + "\n" +
"Full git commit ID: " + buildinfo.GitRevision + "\n" +
"Build date: " + buildinfo.BuildDate + "\n"
arguments, err := docopt.Parse(usage, argv, true, version, false)
if err != nil {
println(usage)
log.Fatalf("Failed to parse usage, exiting: %v", err)
}
t.ConfigFilePath = arguments["--config-file"].(string)
t.BuildInfoLogCxt = log.WithFields(log.Fields{
"version": buildinfo.GitVersion,
"buildDate": buildinfo.BuildDate,
"gitCommit": buildinfo.GitRevision,
"GOMAXPROCS": runtime.GOMAXPROCS(0),
})
t.BuildInfoLogCxt.Info("Typha starting up")
log.Infof("Command line arguments: %v", arguments)
}
// LoadConfiguration uses the command-line configuration and environment variables to load our configuration.
// It initializes the datastore connection.
func (t *TyphaDaemon) LoadConfiguration() {
// Load the configuration from all the different sources including the
// datastore and merge. Keep retrying on failure. We'll sit in this
// loop until the datastore is ready.
log.Infof("Loading configuration...")
var configParams *config.Config
configRetry:
for {
// Load locally-defined config, including the datastore connection
// parameters. First the environment variables.
configParams = config.New()
envConfig := config.LoadConfigFromEnvironment(os.Environ())
// Then, the config file.
fileConfig, err := config.LoadConfigFile(t.ConfigFilePath)
if err != nil {
log.WithError(err).WithField("configFile", t.ConfigFilePath).Error(
"Failed to load configuration file")
time.Sleep(1 * time.Second)
continue configRetry
}
// Parse and merge the local config.
_, err = configParams.UpdateFrom(envConfig, config.EnvironmentVariable)
if err != nil {
log.WithError(err).WithField("configFile", t.ConfigFilePath).Error(
"Failed to parse configuration environment variable")
time.Sleep(1 * time.Second)
continue configRetry
}
_, err = configParams.UpdateFrom(fileConfig, config.ConfigFile)
if err != nil {
log.WithError(err).WithField("configFile", t.ConfigFilePath).Error(
"Failed to parse configuration file")
time.Sleep(1 * time.Second)
continue configRetry
}
// We should now have enough config to connect to the datastore
// so we can load the remainder of the config.
datastoreConfig := configParams.DatastoreConfig()
t.DatastoreClient, err = t.NewBackendClient(datastoreConfig)
if err != nil {
log.WithError(err).Error("Failed to connect to datastore")
time.Sleep(1 * time.Second)
continue configRetry
}
err = configParams.Validate()
if err != nil {
log.WithError(err).Error(
"Failed to parse/validate configuration from datastore.")
time.Sleep(1 * time.Second)
continue configRetry
}
break configRetry
}
// If we get here, we've loaded the configuration successfully.
// Update log levels before we do anything else.
t.ConfigureLogging(configParams)
// Since we may have enabled more logging, log with the build context
// again.
t.BuildInfoLogCxt.WithField("config", configParams).Info(
"Successfully loaded configuration.")
// Make sure the datastore is initialized, otherwise the Syncer may spin, looking for
// non-existent resources.
for {
err := t.DatastoreClient.EnsureInitialized()
if err != nil {
log.WithError(err).Error("Failed to ensure datastore was initialized")
time.Sleep(1 * time.Second)
continue
}
break
}
t.ConfigParams = configParams
}
// CreateServer creates and configures (but does not start) the server components.
func (t *TyphaDaemon) CreateServer() {
// Now create the Syncer; our caching layer and the TCP server.
// Health monitoring, for liveness and readiness endpoints.
t.healthAggregator = health.NewHealthAggregator()
// Get a Syncer from the datastore, which will feed the validator layer with updates.
t.SyncerToValidator = calc.NewSyncerCallbacksDecoupler()
t.Syncer = t.DatastoreClient.Syncer(t.SyncerToValidator)
log.Debugf("Created Syncer: %#v", t.Syncer)
// Create the validator, which sits between the syncer and the cache.
t.ValidatorToCache = calc.NewSyncerCallbacksDecoupler()
t.Validator = calc.NewValidationFilter(t.ValidatorToCache)
// Create our snapshot cache, which stores point-in-time copies of the datastore contents.
t.Cache = snapcache.New(snapcache.Config{
MaxBatchSize: t.ConfigParams.SnapshotCacheMaxBatchSize,
HealthAggregator: t.healthAggregator,
})
// Create the server, which listens for connections from Felix.
t.Server = syncserver.New(
t.Cache,
syncserver.Config{
MaxMessageSize: t.ConfigParams.ServerMaxMessageSize,
MinBatchingAgeThreshold: t.ConfigParams.ServerMinBatchingAgeThresholdSecs,
MaxFallBehind: t.ConfigParams.ServerMaxFallBehindSecs,
PingInterval: t.ConfigParams.ServerPingIntervalSecs,
PongTimeout: t.ConfigParams.ServerPongTimeoutSecs,
DropInterval: t.ConfigParams.ConnectionDropIntervalSecs,
MaxConns: t.ConfigParams.MaxConnectionsUpperLimit,
Port: t.ConfigParams.ServerPort,
HealthAggregator: t.healthAggregator,
},
)
}
// Start starts all the server components in background goroutines.
func (t *TyphaDaemon) Start(cxt context.Context) {
// Now we've connected everything up, start the background processing threads.
log.Info("Starting the datastore Syncer/cache layer")
t.Syncer.Start()
go t.SyncerToValidator.SendToContext(cxt, t.Validator)
go t.ValidatorToCache.SendToContext(cxt, t.Cache)
t.Cache.Start(cxt)
t.Server.Start(cxt)
if t.ConfigParams.ConnectionRebalancingMode == "kubernetes" {
log.Info("Kubernetes connection rebalancing is enabled, starting k8s poll goroutine.")
k8sAPI := k8s.NewK8sAPI()
ticker := jitter.NewTicker(
t.ConfigParams.K8sServicePollIntervalSecs,
t.ConfigParams.K8sServicePollIntervalSecs/10)
go k8s.PollK8sForConnectionLimit(cxt, t.ConfigParams, ticker.C, k8sAPI, t.Server)
}
log.Info("Started the datastore Syncer/cache layer/server.")
if t.ConfigParams.PrometheusMetricsEnabled {
log.Info("Prometheus metrics enabled. Starting server.")
go servePrometheusMetrics(t.ConfigParams)
}
if t.ConfigParams.HealthEnabled {
log.WithField("port", t.ConfigParams.HealthPort).Info("Health enabled. Starting server.")
go t.healthAggregator.ServeHTTP(t.ConfigParams.HealthPort)
}
}
// WaitAndShutDown waits for OS signals or context.Done() and exits as appropriate.
func (t *TyphaDaemon) WaitAndShutDown(cxt context.Context) {
// Hook and process the signals we care about
usr1SignalChan := make(chan os.Signal, 1)
signal.Notify(usr1SignalChan, syscall.SIGUSR1)
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGTERM)
for {
select {
case <-termChan:
log.Fatal("Received SIGTERM, shutting down")
case <-usr1SignalChan:
log.Info("Received SIGUSR1, emitting heap profile")
dumpHeapMemoryProfile(t.ConfigParams)
case <-cxt.Done():
log.Info("Context asked us to stop.")
return
}
}
}
// BackendClient captures the sub-interface of the backend client that we actually use.
type BackendClient interface {
// Syncer creates an object that generates a series of KVPair updates,
// which paint an eventually-consistent picture of the full state of
// the datastore and then generates subsequent KVPair updates for
// changes to the datastore.
Syncer(callbacks bapi.SyncerCallbacks) bapi.Syncer
// EnsureInitialized ensures that the backend is initialized
// any ready to be used.
EnsureInitialized() error
}
// TODO Typha: Share with Felix.
func dumpHeapMemoryProfile(configParams *config.Config) {
// If a memory profile file name is configured, dump a heap memory profile. If the
// configured filename includes "<timestamp>", that will be replaced with a stamp indicating
// the current time.
memProfFileName := configParams.DebugMemoryProfilePath
if memProfFileName != "" {
logCxt := log.WithField("file", memProfFileName)
logCxt.Info("Asked to create a memory profile.")
// If the configured file name includes "<timestamp>", replace that with the current
// time.
if strings.Contains(memProfFileName, "<timestamp>") {
timestamp := time.Now().Format("2006-01-02-15:04:05")
memProfFileName = strings.Replace(memProfFileName, "<timestamp>", timestamp, 1)
logCxt = log.WithField("file", memProfFileName)
}
// Open a file with that name.
memProfFile, err := os.Create(memProfFileName)
if err != nil {
logCxt.WithError(err).Fatal("Could not create memory profile file")
memProfFile = nil
} else {
defer func() {
err := memProfFile.Close()
if err != nil {
log.WithError(err).Error("Error while closing memory profile file.")
}
}()
logCxt.Info("Writing memory profile...")
// The initial resync uses a lot of scratch space so now is
// a good time to force a GC and return any RAM that we can.
debug.FreeOSMemory()
if err := pprof.WriteHeapProfile(memProfFile); err != nil {
logCxt.WithError(err).Error("Could not write memory profile")
}
logCxt.Info("Finished writing memory profile")
}
}
}
// TODO Typha: Share with Felix.
func servePrometheusMetrics(configParams *config.Config) {
for {
log.WithField("port", configParams.PrometheusMetricsPort).Info("Starting prometheus metrics endpoint")
if configParams.PrometheusGoMetricsEnabled && configParams.PrometheusProcessMetricsEnabled {
log.Info("Including Golang & Process metrics")
} else {
if !configParams.PrometheusGoMetricsEnabled {
log.Info("Discarding Golang metrics")
prometheus.Unregister(prometheus.NewGoCollector())
}
if !configParams.PrometheusProcessMetricsEnabled {
log.Info("Discarding process metrics")
prometheus.Unregister(prometheus.NewProcessCollector(os.Getpid(), ""))
}
}
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(fmt.Sprintf(":%v", configParams.PrometheusMetricsPort), nil)
log.WithError(err).Error(
"Prometheus metrics endpoint failed, trying to restart it...")
time.Sleep(1 * time.Second)
}
}