-
Notifications
You must be signed in to change notification settings - Fork 99
/
main.go
235 lines (199 loc) · 8.54 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
// 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"
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"
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"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
// +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
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var leaderElectionResourceLock string
var leaderElectionLeaseDuration time.Duration
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", true,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&leaderElectionResourceLock, "leader-election-resource", "configmaps", "determines which resource lock to use for leader election. option:[configmapsleases|endpointsleases|configmaps]")
flag.DurationVar(&leaderElectionLeaseDuration, "leader-election-lease-duration", 60*time.Second, "Define LeaseDuration as well as RenewDeadline (leaseDuration / 2) and RetryPeriod (leaseDuration / 4)")
// Custom flags
var printVersion, pprofActive, supportExtendedDaemonset, supportCilium, datadogAgentEnabled, datadogMonitorEnabled, operatorMetricsEnabled, webhookEnabled, v2APIEnabled bool
var logEncoder, secretBackendCommand string
var secretBackendArgs stringSlice
flag.StringVar(&logEncoder, "logEncoder", "json", "log encoding ('json' or 'console')")
flag.StringVar(&secretBackendCommand, "secretBackendCommand", "", "Secret backend command")
flag.Var(&secretBackendArgs, "secretBackendArgs", "Space separated arguments of the secret backend command")
logLevel := zap.LevelFlag("loglevel", zapcore.InfoLevel, "Set log level")
flag.BoolVar(&printVersion, "version", false, "Print version and exit")
flag.BoolVar(&pprofActive, "pprof", false, "Enable pprof endpoint")
flag.BoolVar(&supportExtendedDaemonset, "supportExtendedDaemonset", false, "Support usage of Datadog ExtendedDaemonset CRD.")
flag.BoolVar(&supportCilium, "supportCilium", false, "Support usage of Cilium network policies.")
flag.BoolVar(&datadogAgentEnabled, "datadogAgentEnabled", true, "Enable the DatadogAgent controller")
flag.BoolVar(&datadogMonitorEnabled, "datadogMonitorEnabled", false, "Enable the DatadogMonitor controller")
flag.BoolVar(&operatorMetricsEnabled, "operatorMetricsEnabled", true, "Enable sending operator metrics to Datadog")
flag.BoolVar(&v2APIEnabled, "v2APIEnabled", true, "Enable the v2 api")
flag.BoolVar(&webhookEnabled, "webhookEnabled", true, "Enable CRD conversion webhook.")
maximumGoroutines := flag.Int("maximumGoroutines", defaultMaximumGoroutines, "Override health check threshold for maximum number of goroutines.")
// Parsing flags
flag.Parse()
// Logging setup
if err := customSetupLogging(*logLevel, logEncoder); err != nil {
setupLog.Error(err, "Unable to setup the logger")
os.Exit(1)
}
// Print version information
if printVersion {
version.PrintVersionWriter(os.Stdout, "text")
os.Exit(0)
}
version.PrintVersionLogs(setupLog)
// Dispatch CLI flags to each package
secrets.SetSecretBackendCommand(secretBackendCommand)
secrets.SetSecretBackendArgs(secretBackendArgs)
renewDeadline := leaderElectionLeaseDuration / 2
retryPeriod := leaderElectionLeaseDuration / 4
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), config.ManagerOptionsWithNamespaces(setupLog, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
HealthProbeBindAddress: ":8081",
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "datadog-operator-lock",
LeaderElectionResourceLock: leaderElectionResourceLock,
LeaseDuration: &leaderElectionLeaseDuration,
RenewDeadline: &renewDeadline,
RetryPeriod: &retryPeriod,
}))
if err != nil {
setupLog.Error(err, "Unable to start manager")
os.Exit(1)
}
// Custom setup
customSetupHealthChecks(setupLog, mgr, maximumGoroutines)
customSetupEndpoints(pprofActive, mgr)
creds, err := config.NewCredentialManager().GetCredentials()
if err != nil && datadogMonitorEnabled {
setupLog.Error(err, "Unable to get credentials for DatadogMonitor")
os.Exit(1)
}
options := controllers.SetupOptions{
SupportExtendedDaemonset: supportExtendedDaemonset,
SupportCilium: supportCilium,
Creds: creds,
DatadogAgentEnabled: datadogAgentEnabled,
DatadogMonitorEnabled: datadogMonitorEnabled,
OperatorMetricsEnabled: operatorMetricsEnabled,
V2APIEnabled: v2APIEnabled,
}
if err = controllers.SetupControllers(setupLog, mgr, options); err != nil {
setupLog.Error(err, "Unable to start controllers")
os.Exit(1)
}
if webhookEnabled && datadogAgentEnabled {
if err = (&datadoghqv2alpha1.DatadogAgent{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "DatadogAgent")
os.Exit(1)
}
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "Problem running manager")
os.Exit(1)
}
}
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 much goroutines: %d > limit: %d", goruntime.NumGoroutine(), maximumGoroutines)
}
return nil
})
if err != nil {
setupLog.Error(err, "Unable to add healthchecks")
}
}
func customSetupEndpoints(pprofActive bool, mgr manager.Manager) {
if pprofActive {
if err := debug.RegisterEndpoint(mgr.AddMetricsExtraHandler, nil); err != nil {
setupLog.Error(err, "Unable to register pprof endpoint")
}
}
if err := metrics.RegisterEndpoint(mgr, mgr.AddMetricsExtraHandler); err != nil {
setupLog.Error(err, "Unable to register custom metrics endpoints")
}
}