-
Notifications
You must be signed in to change notification settings - Fork 104
/
main.go
348 lines (295 loc) · 14 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
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package main
import (
"flag"
"fmt"
"net/http"
"os"
goruntime "runtime"
"strings"
"time"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/leaderelection/resourcelock"
klog "k8s.io/klog/v2"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
ctrl "sigs.k8s.io/controller-runtime"
ctrlzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
edsdatadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
"github.com/DataDog/extendeddaemonset/pkg/controller/metrics"
"github.com/go-logr/logr"
datadoghqv1alpha1 "github.com/DataDog/datadog-operator/apis/datadoghq/v1alpha1"
datadoghqv2alpha1 "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
"github.com/DataDog/datadog-operator/controllers"
"github.com/DataDog/datadog-operator/pkg/config"
"github.com/DataDog/datadog-operator/pkg/controller/debug"
"github.com/DataDog/datadog-operator/pkg/secrets"
"github.com/DataDog/datadog-operator/pkg/version"
// +kubebuilder:scaffold:imports
)
const (
defaultMaximumGoroutines = 400
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
klog.SetLogger(ctrl.Log.WithName("klog"))
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(apiregistrationv1.AddToScheme(scheme))
utilruntime.Must(datadoghqv1alpha1.AddToScheme(scheme))
utilruntime.Must(edsdatadoghqv1alpha1.AddToScheme(scheme))
utilruntime.Must(datadoghqv2alpha1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
// stringSlice implements flag.Value
type stringSlice []string
func (ss *stringSlice) String() string {
return fmt.Sprintf("%s", *ss)
}
func (ss *stringSlice) Set(value string) error {
*ss = strings.Split(value, " ")
return nil
}
const (
// ExtendedDaemonset default configuration values from https://github.com/DataDog/extendeddaemonset/blob/main/api/v1alpha1/extendeddaemonset_default.go
defaultCanaryAutoPauseEnabled = true
defaultCanaryAutoFailEnabled = true
// default to 0, to use default value from EDS.
defaultCanaryAutoPauseMaxRestarts = 0
defaultCanaryAutoFailMaxRestarts = 0
defaultCanaryAutoPauseMaxSlowStartDuration = 0
)
type options struct {
// Observability options
metricsAddr string
profilingEnabled bool
logLevel *zapcore.Level
logEncoder string
printVersion bool
pprofActive bool
// Leader Election options
enableLeaderElection bool
leaderElectionResourceLock string
leaderElectionLeaseDuration time.Duration
// Controllers options
supportExtendedDaemonset bool
edsMaxPodUnavailable string
edsMaxPodSchedulerFailure string
edsCanaryDuration time.Duration
edsCanaryReplicas string
edsCanaryAutoPauseEnabled bool
edsCanaryAutoPauseMaxRestarts int
edsCanaryAutoFailEnabled bool
edsCanaryAutoFailMaxRestarts int
edsCanaryAutoPauseMaxSlowStartDuration time.Duration
supportCilium bool
datadogAgentEnabled bool
datadogMonitorEnabled bool
datadogSLOEnabled bool
operatorMetricsEnabled bool
webhookEnabled bool
v2APIEnabled bool
maximumGoroutines int
introspectionEnabled bool
// Secret Backend options
secretBackendCommand string
secretBackendArgs stringSlice
}
func (opts *options) Parse() {
// Observability flags
flag.StringVar(&opts.metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&opts.profilingEnabled, "profiling-enabled", false, "Enable Datadog profile in the Datadog Operator process.")
opts.logLevel = zap.LevelFlag("loglevel", zapcore.InfoLevel, "Set log level")
flag.StringVar(&opts.logEncoder, "logEncoder", "json", "log encoding ('json' or 'console')")
flag.BoolVar(&opts.printVersion, "version", false, "Print version and exit")
flag.BoolVar(&opts.pprofActive, "pprof", false, "Enable pprof endpoint")
// Leader Election options flags
flag.BoolVar(&opts.enableLeaderElection, "enable-leader-election", true,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&opts.leaderElectionResourceLock, "leader-election-resource", resourcelock.ConfigMapsLeasesResourceLock, "determines which resource lock to use for leader election. option:[configmapsleases|endpointsleases|leases]")
flag.DurationVar(&opts.leaderElectionLeaseDuration, "leader-election-lease-duration", 60*time.Second, "Define LeaseDuration as well as RenewDeadline (leaseDuration / 2) and RetryPeriod (leaseDuration / 4)")
// Custom flags
flag.StringVar(&opts.secretBackendCommand, "secretBackendCommand", "", "Secret backend command")
flag.Var(&opts.secretBackendArgs, "secretBackendArgs", "Space separated arguments of the secret backend command")
flag.BoolVar(&opts.supportCilium, "supportCilium", false, "Support usage of Cilium network policies.")
flag.BoolVar(&opts.datadogAgentEnabled, "datadogAgentEnabled", true, "Enable the DatadogAgent controller")
flag.BoolVar(&opts.datadogMonitorEnabled, "datadogMonitorEnabled", false, "Enable the DatadogMonitor controller")
flag.BoolVar(&opts.datadogSLOEnabled, "datadogSLOEnabled", false, "Enable the DatadogSLO controller")
flag.BoolVar(&opts.operatorMetricsEnabled, "operatorMetricsEnabled", true, "Enable sending operator metrics to Datadog")
flag.BoolVar(&opts.v2APIEnabled, "v2APIEnabled", true, "Enable the v2 api")
flag.BoolVar(&opts.webhookEnabled, "webhookEnabled", false, "Enable CRD conversion webhook.")
flag.IntVar(&opts.maximumGoroutines, "maximumGoroutines", defaultMaximumGoroutines, "Override health check threshold for maximum number of goroutines.")
flag.BoolVar(&opts.introspectionEnabled, "introspectionEnabled", false, "Enable introspection (beta)")
// ExtendedDaemonset configuration
flag.BoolVar(&opts.supportExtendedDaemonset, "supportExtendedDaemonset", false, "Support usage of Datadog ExtendedDaemonset CRD.")
flag.StringVar(&opts.edsMaxPodUnavailable, "edsMaxPodUnavailable", "", "ExtendedDaemonset number of max unavailable pods during the rolling update")
flag.StringVar(&opts.edsMaxPodSchedulerFailure, "edsMaxPodSchedulerFailure", "", "ExtendedDaemonset number of max pod scheduler failures")
flag.DurationVar(&opts.edsCanaryDuration, "edsCanaryDuration", 10*time.Minute, "ExtendedDaemonset canary duration")
flag.StringVar(&opts.edsCanaryReplicas, "edsCanaryReplicas", "", "ExtendedDaemonset number of canary pods")
flag.BoolVar(&opts.edsCanaryAutoPauseEnabled, "edsCanaryAutoPauseEnabled", defaultCanaryAutoPauseEnabled, "ExtendedDaemonset canary auto pause enabled")
flag.IntVar(&opts.edsCanaryAutoPauseMaxRestarts, "edsCanaryAutoPauseMaxRestarts", defaultCanaryAutoPauseMaxRestarts, "ExtendedDaemonset canary auto pause max restart count")
flag.BoolVar(&opts.edsCanaryAutoFailEnabled, "edsCanaryAutoFailEnabled", defaultCanaryAutoFailEnabled, "ExtendedDaemonset canary auto fail enabled")
flag.IntVar(&opts.edsCanaryAutoFailMaxRestarts, "edsCanaryAutoFailMaxRestarts", defaultCanaryAutoFailMaxRestarts, "ExtendedDaemonset canary auto fail max restart count")
flag.DurationVar(&opts.edsCanaryAutoPauseMaxSlowStartDuration, "edsCanaryAutoPauseMaxSlowStartDuration", defaultCanaryAutoPauseMaxSlowStartDuration*time.Minute, "ExtendedDaemonset canary max slow start duration")
// Parsing flags
flag.Parse()
}
func main() {
var opts options
opts.Parse()
if err := run(&opts); err != nil {
os.Exit(1)
}
os.Exit(0)
}
// run allow to use defer func() paradigm properly.
// do not use `os.Exit()` in this function
func run(opts *options) error {
// Logging setup
if err := customSetupLogging(*opts.logLevel, opts.logEncoder); err != nil {
return setupErrorf(setupLog, err, "Unable to setup the logger")
}
// Print version information
if opts.printVersion {
version.PrintVersionWriter(os.Stdout, "text")
return nil
}
version.PrintVersionLogs(setupLog)
if !opts.v2APIEnabled {
setupLog.Info("The 'v2APIEnabled' flag is deprecated in v1.2.0+ and will be removed in a future release. " +
"Once removed, the Datadog Operator cannot be configured to reconcile the v1alpha1 DatadogAgent CRD. " +
"However, you will still be able to apply a v1alpha1 manifest with the conversion webhook enabled (using the flag 'webhookEnabled').")
}
if opts.profilingEnabled {
setupLog.Info("Starting datadog profiler")
if err := profiler.Start(
profiler.WithVersion(version.Version),
profiler.WithProfileTypes(profiler.CPUProfile, profiler.HeapProfile),
); err != nil {
return setupErrorf(setupLog, err, "unable to start datadog profiler")
}
defer profiler.Stop()
}
// Dispatch CLI flags to each package
secrets.SetSecretBackendCommand(opts.secretBackendCommand)
secrets.SetSecretBackendArgs(opts.secretBackendArgs)
renewDeadline := opts.leaderElectionLeaseDuration / 2
retryPeriod := opts.leaderElectionLeaseDuration / 4
restConfig := ctrl.GetConfigOrDie()
restConfig.UserAgent = "datadog-operator"
mgr, err := ctrl.NewManager(restConfig, config.ManagerOptionsWithNamespaces(setupLog, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: opts.metricsAddr,
HealthProbeBindAddress: ":8081",
Port: 9443,
LeaderElection: opts.enableLeaderElection,
LeaderElectionID: "datadog-operator-lock",
LeaderElectionResourceLock: opts.leaderElectionResourceLock,
LeaseDuration: &opts.leaderElectionLeaseDuration,
RenewDeadline: &renewDeadline,
RetryPeriod: &retryPeriod,
}))
if err != nil {
return setupErrorf(setupLog, err, "Unable to start manager")
}
// Custom setup
customSetupHealthChecks(setupLog, mgr, &opts.maximumGoroutines)
customSetupEndpoints(opts.pprofActive, mgr)
creds, err := config.NewCredentialManager().GetCredentials()
if err != nil && opts.datadogMonitorEnabled {
return setupErrorf(setupLog, err, "Unable to get credentials for DatadogMonitor")
}
options := controllers.SetupOptions{
SupportExtendedDaemonset: controllers.ExtendedDaemonsetOptions{
Enabled: opts.supportExtendedDaemonset,
MaxPodUnavailable: opts.edsMaxPodUnavailable,
CanaryDuration: opts.edsCanaryDuration,
CanaryReplicas: opts.edsCanaryReplicas,
CanaryAutoPauseEnabled: opts.edsCanaryAutoPauseEnabled,
CanaryAutoPauseMaxRestarts: opts.edsCanaryAutoPauseMaxRestarts,
CanaryAutoFailEnabled: opts.edsCanaryAutoFailEnabled,
CanaryAutoFailMaxRestarts: opts.edsCanaryAutoFailMaxRestarts,
CanaryAutoPauseMaxSlowStartDuration: opts.edsCanaryAutoPauseMaxSlowStartDuration,
MaxPodSchedulerFailure: opts.edsMaxPodSchedulerFailure,
},
SupportCilium: opts.supportCilium,
Creds: creds,
DatadogAgentEnabled: opts.datadogAgentEnabled,
DatadogMonitorEnabled: opts.datadogMonitorEnabled,
DatadogSLOEnabled: opts.datadogSLOEnabled,
OperatorMetricsEnabled: opts.operatorMetricsEnabled,
V2APIEnabled: opts.v2APIEnabled,
IntrospectionEnabled: opts.introspectionEnabled,
}
if err = controllers.SetupControllers(setupLog, mgr, options); err != nil {
return setupErrorf(setupLog, err, "Unable to start controllers")
}
if opts.webhookEnabled && opts.datadogAgentEnabled {
if err = (&datadoghqv2alpha1.DatadogAgent{}).SetupWebhookWithManager(mgr); err != nil {
return setupErrorf(setupLog, err, "unable to create webhook", "webhook", "DatadogAgent")
}
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
return setupErrorf(setupLog, err, "Problem running manager")
}
return nil
}
func customSetupLogging(logLevel zapcore.Level, logEncoder string) error {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
var encoder zapcore.Encoder
switch logEncoder {
case "console":
encoder = zapcore.NewConsoleEncoder(encoderConfig)
case "json":
encoder = zapcore.NewJSONEncoder(encoderConfig)
default:
return fmt.Errorf("unknow log encoder: %s", logEncoder)
}
ctrl.SetLogger(ctrlzap.New(
ctrlzap.Encoder(encoder),
ctrlzap.Level(logLevel),
ctrlzap.StacktraceLevel(zapcore.PanicLevel)),
)
return nil
}
func customSetupHealthChecks(logger logr.Logger, mgr manager.Manager, maximumGoroutines *int) {
setupLog.Info("configuring manager health check", "maximumGoroutines", *maximumGoroutines)
err := mgr.AddHealthzCheck("goroutines-number", func(req *http.Request) error {
if goruntime.NumGoroutine() > *maximumGoroutines {
return fmt.Errorf("too many goroutines: %d > limit: %d", goruntime.NumGoroutine(), *maximumGoroutines)
}
return nil
})
if err != nil {
setupErrorf(setupLog, err, "Unable to add healthchecks")
}
}
func customSetupEndpoints(pprofActive bool, mgr manager.Manager) {
if pprofActive {
if err := debug.RegisterEndpoint(mgr.AddMetricsExtraHandler, nil); err != nil {
setupErrorf(setupLog, err, "Unable to register pprof endpoint")
}
}
if err := metrics.RegisterEndpoint(mgr, mgr.AddMetricsExtraHandler); err != nil {
setupErrorf(setupLog, err, "Unable to register custom metrics endpoints")
}
}
func setupErrorf(logger logr.Logger, err error, msg string, keysAndValues ...any) error {
setupLog.Error(err, msg, keysAndValues...)
return fmt.Errorf("%s, err:%w", msg, err)
}