-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
276 lines (235 loc) · 8.32 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
// 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-2019 Datadog, Inc.
package main
import (
"flag"
"fmt"
"net/http"
"os"
goruntime "runtime"
"strings"
"github.com/blang/semver"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
kversion "k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
klog "k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
ctrlzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
datadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
"github.com/DataDog/extendeddaemonset/controllers"
"github.com/DataDog/extendeddaemonset/pkg/config"
"github.com/DataDog/extendeddaemonset/pkg/controller/debug"
"github.com/DataDog/extendeddaemonset/pkg/controller/metrics"
"github.com/DataDog/extendeddaemonset/pkg/version"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
// +kubebuilder:scaffold:imports
)
const (
maximumGoroutines = 200
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
klog.SetLogger(ctrl.Log.WithName("klog"))
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(datadoghqv1alpha1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var leaderElectionResourceLock string
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", "leases", "determines which resource lock to use for leader election. option:[leases]")
// Custom flags
var printVersion, pprofActive, ddProfilingEnabled bool
var logEncoder string
flag.StringVar(&logEncoder, "logEncoder", "json", "log encoding ('json' or 'console')")
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(&ddProfilingEnabled, "ddProfilingEnabled", false, "Enable the datadog profiler")
// Parsing flags
flag.Parse()
exitCode := 0
defer func() { os.Exit(exitCode) }()
// Logging setup
if err := customSetupLogging(*logLevel, logEncoder); err != nil {
setupLog.Error(err, "unable to setup the logger")
exitCode = 1
return
}
if ddProfilingEnabled {
setupLog.Info("Starting datadog profiler")
if err := profiler.Start(
profiler.WithVersion(version.Version),
profiler.WithProfileTypes(profiler.CPUProfile, profiler.HeapProfile),
); err != nil {
setupLog.Error(err, "unable to start datadog profiler")
}
defer profiler.Stop()
}
// Print version information
if printVersion {
version.PrintVersionWriter(os.Stdout)
exitCode = 0
return
}
version.PrintVersionLogs(setupLog)
restConfig := ctrl.GetConfigOrDie()
restConfig.UserAgent = "eds-controller"
extraMetricHandlers, err := getExtraMetricHandlers(scheme)
if err != nil {
setupLog.Error(err, "problem registering external extra metric handlers")
exitCode = 1
return
}
mgr, err := ctrl.NewManager(restConfig, config.ManagerOptionsWithNamespaces(setupLog, ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
ExtraHandlers: extraMetricHandlers,
},
HealthProbeBindAddress: ":8081",
WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}),
LeaderElection: enableLeaderElection,
LeaderElectionID: "extendeddaemonset-lock",
LeaderElectionResourceLock: leaderElectionResourceLock,
}))
if err != nil {
setupLog.Error(err, "unable to start manager")
exitCode = 1
return
}
// Custom setup
customSetupMetrics(mgr)
customSetupEnvironment(mgr)
customSetupHealthChecks(mgr)
// Read conf (env + CLI flags)
nodeAffinityMatchSupport := os.Getenv(config.NodeAffinityMatchSupportEnvVar) == "1"
validationModeEnvVar := os.Getenv(config.ValidationModeEnvVar)
var defaultValidationMode datadoghqv1alpha1.ExtendedDaemonSetSpecStrategyCanaryValidationMode
switch {
case validationModeEnvVar == "" || validationModeEnvVar == "auto":
defaultValidationMode = datadoghqv1alpha1.ExtendedDaemonSetSpecStrategyCanaryValidationModeAuto
case validationModeEnvVar == "manual":
defaultValidationMode = datadoghqv1alpha1.ExtendedDaemonSetSpecStrategyCanaryValidationModeManual
default:
setupLog.Error(fmt.Errorf("unable to parse %s env var: unknown validation mode: %s", config.ValidationModeEnvVar, validationModeEnvVar), "")
exitCode = 1
return
}
// Setup controllers and start manager
err = controllers.SetupControllers(mgr, nodeAffinityMatchSupport, defaultValidationMode)
if err != nil {
setupLog.Error(err, "unable to setup controllers")
exitCode = 1
return
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
exitCode = 1
return
}
}
func customSetupEnvironment(mgr manager.Manager) {
// auto discover if Node affinity match is supported in the current cluster
if os.Getenv(config.NodeAffinityMatchSupportEnvVar) == "" {
discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(mgr.GetConfig())
var err error
var serverVersion *kversion.Info
if serverVersion, err = discoveryClient.ServerVersion(); err != nil {
setupLog.Error(err, "")
os.Exit(1)
}
var minServerVersion semver.Version
if minServerVersion, err = semver.Make("1.12.0"); err != nil {
setupLog.Error(err, "")
os.Exit(1)
}
var currentServerVersion semver.Version
if currentServerVersion, err = semver.Make(strings.TrimPrefix(serverVersion.String(), "v")); err != nil {
setupLog.Error(err, "")
os.Exit(1)
}
if minServerVersion.Compare(currentServerVersion) < 0 {
// it means the kubernetes cluster support Node assignment with AffinityMatchField
if err = os.Setenv(config.NodeAffinityMatchSupportEnvVar, "1"); err != nil {
setupLog.Error(err, "")
os.Exit(1)
}
}
}
}
func customSetupMetrics(mgr manager.Manager) {
go func() {
// This channel is closed when this instance is elected leader
// Apparently there's no releasing the lease except if application dies
<-mgr.Elected()
setupLog.Info("Controller elected - metric changed")
metrics.SetLeader(true)
}()
}
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),
ctrlzap.WriteTo(os.Stdout),
))
return nil
}
func customSetupHealthChecks(mgr manager.Manager) {
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 start ")
}
}
func getExtraMetricHandlers(scheme *runtime.Scheme) (map[string]http.Handler, error) {
extraHandlers := map[string]http.Handler{}
debugHandler := debug.GetExtraMetricHandlers()
ksmHandler, err := metrics.GetExtraMetricHandlers(scheme)
if err != nil {
return nil, err
}
for k, v := range debugHandler {
extraHandlers[k] = v
}
for k, v := range ksmHandler {
extraHandlers[k] = v
}
return extraHandlers, nil
}