forked from nginx/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
315 lines (263 loc) · 10.8 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
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
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package main
import (
"context"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"time"
agent_config "github.com/nginx/agent/sdk/v2/agent/config"
"github.com/nginx/agent/sdk/v2/client"
sdkGRPC "github.com/nginx/agent/sdk/v2/grpc"
"github.com/nginx/agent/v2/src/core"
"github.com/nginx/agent/v2/src/core/config"
"github.com/nginx/agent/v2/src/core/logger"
"github.com/nginx/agent/v2/src/extensions"
"github.com/nginx/agent/v2/src/plugins"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)
var (
// set at buildtime
commit = ""
version = ""
)
func init() {
config.SetVersion(version, commit)
config.SetDefaults()
config.RegisterFlags()
dynamicConfigPath := config.DynamicConfigFileAbsPath
if runtime.GOOS == "freebsd" {
dynamicConfigPath = config.DynamicConfigFileAbsFreeBsdPath
}
configPath, err := config.RegisterConfigFile(dynamicConfigPath, config.ConfigFileName, config.ConfigFilePaths()...)
if err != nil {
log.Fatalf("Failed to load configuration file: %v", err)
}
log.Debugf("Configuration file loaded %v", configPath)
config.Viper.Set(config.ConfigPathKey, configPath)
}
func main() {
config.RegisterRunner(func(cmd *cobra.Command, _ []string) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
env := &core.EnvironmentType{}
loadedConfig, err := config.GetConfig(env.GetSystemUUID())
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
logger.SetLogLevel(loadedConfig.Log.Level)
logFile := logger.SetLogFile(loadedConfig.Log.Path)
if logFile != nil {
defer logFile.Close()
}
log.Tracef("Config loaded from disk, %v", loadedConfig)
if loadedConfig.DisplayName == "" {
loadedConfig.DisplayName = env.GetHostname()
log.Infof("setting displayName to %s", loadedConfig.DisplayName)
}
log.Infof("NGINX Agent %s at %s with pid %d, clientID=%s name=%s features=%v",
version, commit, os.Getpid(), loadedConfig.ClientID, loadedConfig.DisplayName, loadedConfig.Features)
sdkGRPC.InitMeta(loadedConfig.ClientID, loadedConfig.CloudAccountID)
controller, commander, reporter := createGrpcClients(ctx, loadedConfig)
if controller != nil {
if err := controller.Connect(); err != nil {
log.Warnf("Unable to connect to control plane: %v", err)
return
}
}
binary := core.NewNginxBinary(env, loadedConfig)
corePlugins, extensionPlugins := loadPlugins(commander, binary, env, reporter, loadedConfig)
pipe := initializeMessagePipe(ctx, corePlugins, extensionPlugins)
pipe.Process(core.NewMessage(core.AgentStarted,
plugins.NewAgentEventMeta(version, strconv.Itoa(os.Getpid()))),
)
handleSignals(ctx, commander, loadedConfig, env, pipe, cancel, controller)
pipe.Run()
})
if err := config.Execute(); err != nil {
log.Fatal(err)
}
}
// handleSignals handles signals to attempt graceful shutdown
// for now it also handles sending the agent stopped event because as of today we don't have a mechanism for synchronizing
// tasks between multiple plugins from outside a plugin
func handleSignals(
ctx context.Context,
cmder client.Commander,
loadedConfig *config.Config,
env core.Environment,
pipe core.MessagePipeInterface,
cancel context.CancelFunc,
controller client.Controller,
) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case <-sigChan:
stopCmd := plugins.GenerateAgentStopEventCommand(
plugins.NewAgentEventMeta(version, strconv.Itoa(os.Getpid())), loadedConfig, env,
)
log.Debugf("Sending agent stopped event: %v", stopCmd)
if cmder == nil {
log.Warn("Command channel not configured. Skipping sending AgentStopped event")
} else if err := cmder.Send(ctx, client.MessageFromCommand(stopCmd)); err != nil {
log.Errorf("Error sending AgentStopped event to command channel: %v", err)
}
if controller != nil {
if err := controller.Close(); err != nil {
log.Warnf("Unable to close controller: %v", err)
}
}
log.Warn("NGINX Agent exiting")
cancel()
timeout := time.Second * 5
time.Sleep(timeout)
log.Fatalf("Failed to gracefully shutdown within timeout of %v. Exiting", timeout)
case <-ctx.Done():
}
}()
}
func createGrpcClients(ctx context.Context, loadedConfig *config.Config) (client.Controller, client.Commander, client.MetricReporter) {
if !loadedConfig.IsGrpcServerConfigured() {
log.Info("GRPC clients not created due to missing server config")
return nil, nil, nil
}
grpcDialOptions := setDialOptions(loadedConfig)
secureMetricsDialOpts, err := sdkGRPC.SecureDialOptions(
loadedConfig.TLS.Enable,
loadedConfig.TLS.Cert,
loadedConfig.TLS.Key,
loadedConfig.TLS.Ca,
loadedConfig.Server.Metrics,
loadedConfig.TLS.SkipVerify)
if err != nil {
log.Fatalf("Failed to load secure metric gRPC dial options: %v", err)
}
secureCmdDialOpts, err := sdkGRPC.SecureDialOptions(
loadedConfig.TLS.Enable,
loadedConfig.TLS.Cert,
loadedConfig.TLS.Key,
loadedConfig.TLS.Ca,
loadedConfig.Server.Command,
loadedConfig.TLS.SkipVerify)
if err != nil {
log.Fatalf("Failed to load secure command gRPC dial options: %v", err)
}
controller := client.NewClientController()
controller.WithContext(ctx)
commander := client.NewCommanderClient()
commander.WithBackoffSettings(loadedConfig.GetServerBackoffSettings())
commander.WithServer(loadedConfig.Server.Target)
commander.WithDialOptions(append(grpcDialOptions, secureCmdDialOpts)...)
reporter := client.NewMetricReporterClient()
reporter.WithBackoffSettings(loadedConfig.GetServerBackoffSettings())
reporter.WithServer(loadedConfig.Server.Target)
reporter.WithDialOptions(append(grpcDialOptions, secureMetricsDialOpts)...)
controller.WithClient(commander)
controller.WithClient(reporter)
return controller, commander, reporter
}
func loadPlugins(commander client.Commander, binary *core.NginxBinaryType, env *core.EnvironmentType, reporter client.MetricReporter, loadedConfig *config.Config) ([]core.Plugin, []core.ExtensionPlugin) {
var corePlugins []core.Plugin
var extensionPlugins []core.ExtensionPlugin
if commander != nil {
corePlugins = append(corePlugins,
plugins.NewCommander(commander, loadedConfig),
)
if loadedConfig.IsFeatureEnabled(agent_config.FeatureFileWatcher) {
corePlugins = append(corePlugins,
plugins.NewFileWatcher(loadedConfig, env),
plugins.NewFileWatchThrottle(),
)
}
}
if reporter != nil {
corePlugins = append(corePlugins,
plugins.NewMetricsSender(reporter),
)
}
corePlugins = append(corePlugins,
plugins.NewConfigReader(loadedConfig),
plugins.NewNginx(commander, binary, env, loadedConfig),
plugins.NewExtensions(loadedConfig, env),
plugins.NewFeatures(commander, loadedConfig, env, binary, version),
)
if loadedConfig.IsFeatureEnabled(agent_config.FeatureRegistration) {
corePlugins = append(corePlugins, plugins.NewOneTimeRegistration(loadedConfig, binary, env, sdkGRPC.NewMessageMeta(uuid.NewString()), version))
}
if loadedConfig.IsFeatureEnabled(agent_config.FeatureMetrics) || (len(loadedConfig.Nginx.NginxCountingSocket) > 0 && loadedConfig.IsFeatureEnabled(agent_config.FeatureNginxCounting)) {
corePlugins = append(corePlugins, plugins.NewMetrics(loadedConfig, env, binary))
}
if loadedConfig.IsFeatureEnabled(agent_config.FeatureMetricsThrottle) {
corePlugins = append(corePlugins, plugins.NewMetricsThrottle(loadedConfig, env))
}
if loadedConfig.IsFeatureEnabled(agent_config.FeatureDataPlaneStatus) {
corePlugins = append(corePlugins, plugins.NewDataPlaneStatus(loadedConfig, sdkGRPC.NewMessageMeta(uuid.NewString()), binary, env, version))
}
if loadedConfig.IsFeatureEnabled(agent_config.FeatureProcessWatcher) {
corePlugins = append(corePlugins, plugins.NewProcessWatcher(env, binary))
}
if loadedConfig.IsFeatureEnabled(agent_config.FeatureActivityEvents) {
corePlugins = append(corePlugins, plugins.NewEvents(loadedConfig, env, sdkGRPC.NewMessageMeta(uuid.NewString()), binary))
}
if loadedConfig.AgentAPI.Port != 0 && loadedConfig.IsFeatureEnabled(agent_config.FeatureAgentAPI) {
corePlugins = append(corePlugins, plugins.NewAgentAPI(loadedConfig, env, binary))
} else {
log.Info("Agent API not configured")
}
if len(loadedConfig.Nginx.NginxCountingSocket) > 0 && loadedConfig.IsFeatureEnabled(agent_config.FeatureNginxCounting) {
corePlugins = append(corePlugins, plugins.NewNginxCounter(loadedConfig, binary, env))
}
if loadedConfig.Extensions != nil && len(loadedConfig.Extensions) > 0 {
for _, extension := range loadedConfig.Extensions {
switch {
case extension == agent_config.AdvancedMetricsExtensionPlugin:
advancedMetricsExtensionPlugin := extensions.NewAdvancedMetrics(env, loadedConfig, config.Viper.Get(agent_config.AdvancedMetricsExtensionPluginConfigKey))
extensionPlugins = append(extensionPlugins, advancedMetricsExtensionPlugin)
case extension == agent_config.NginxAppProtectExtensionPlugin:
nginxAppProtectExtensionPlugin, err := extensions.NewNginxAppProtect(loadedConfig, env, config.Viper.Get(agent_config.NginxAppProtectExtensionPluginConfigKey))
if err != nil {
log.Errorf("Unable to load the Nginx App Protect plugin due to the following error: %v", err)
} else {
extensionPlugins = append(extensionPlugins, nginxAppProtectExtensionPlugin)
}
case extension == agent_config.NginxAppProtectMonitoringExtensionPlugin:
nginxAppProtectMonitoringExtensionPlugin, err := extensions.NewNAPMonitoring(env, loadedConfig, config.Viper.Get(agent_config.NginxAppProtectMonitoringExtensionPluginConfigKey))
if err != nil {
log.Errorf("Unable to load the Nginx App Protect Monitoring plugin due to the following error: %v", err)
} else {
extensionPlugins = append(extensionPlugins, nginxAppProtectMonitoringExtensionPlugin)
}
default:
log.Warnf("unknown extension configured: %s", extension)
}
}
}
return corePlugins, extensionPlugins
}
func initializeMessagePipe(ctx context.Context, corePlugins []core.Plugin, extensionPlugins []core.ExtensionPlugin) core.MessagePipeInterface {
pipe := core.NewMessagePipe(ctx)
err := pipe.Register(agent_config.DefaultPluginSize, corePlugins, extensionPlugins)
if err != nil {
log.Warnf("Failed to start agent successfully, error loading plugins %v", err)
}
return pipe
}
func setDialOptions(loadedConfig *config.Config) []grpc.DialOption {
grpcDialOptions := []grpc.DialOption{grpc.WithUserAgent("nginx-agent/" + strings.TrimPrefix(version, "v"))}
grpcDialOptions = append(grpcDialOptions, sdkGRPC.DefaultClientDialOptions...)
grpcDialOptions = append(grpcDialOptions, sdkGRPC.DataplaneConnectionDialOptions(loadedConfig.Server.Token, sdkGRPC.NewMessageMeta(uuid.NewString()))...)
return grpcDialOptions
}