forked from cloudnative-pg/cloudnative-pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
486 lines (413 loc) · 15.5 KB
/
controller.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
Copyright The CloudNativePG Contributors
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 controller implement the command used to start the operator
package controller
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/pprof"
"time"
corev1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
// +kubebuilder:scaffold:imports
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/controllers"
"github.com/cloudnative-pg/cloudnative-pg/internal/configuration"
schemeBuilder "github.com/cloudnative-pg/cloudnative-pg/internal/scheme"
"github.com/cloudnative-pg/cloudnative-pg/pkg/certs"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/log"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver"
"github.com/cloudnative-pg/cloudnative-pg/pkg/multicache"
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
"github.com/cloudnative-pg/cloudnative-pg/pkg/versions"
)
var (
scheme = schemeBuilder.BuildWithAllKnownScheme()
setupLog = log.WithName("setup")
)
const (
// WebhookSecretName is the name of the secret where the certificates
// for the webhook server are stored
WebhookSecretName = "cnpg-webhook-cert" // #nosec
// WebhookServiceName is the name of the service where the webhook server
// is reachable
WebhookServiceName = "cnpg-webhook-service" // #nosec
// MutatingWebhookConfigurationName is the name of the mutating webhook configuration
MutatingWebhookConfigurationName = "cnpg-mutating-webhook-configuration"
// ValidatingWebhookConfigurationName is the name of the validating webhook configuration
ValidatingWebhookConfigurationName = "cnpg-validating-webhook-configuration"
// The name of the directory containing the TLS certificates
defaultWebhookCertDir = "/run/secrets/cnpg.io/webhook"
// LeaderElectionID The operator Leader Election ID
LeaderElectionID = "db9c8771.cnpg.io"
// CaSecretName is the name of the secret which is hosting the Operator CA
CaSecretName = "cnpg-ca-secret" // #nosec
)
// leaderElectionConfiguration contains the leader parameters that will be passed to controllerruntime.Options.
type leaderElectionConfiguration struct {
enable bool
leaseDuration time.Duration
renewDeadline time.Duration
}
// RunController is the main procedure of the operator, and is used as the
// controller-manager of the operator and as the controller of a certain
// PostgreSQL instance.
//
// This code really belongs to app/controller_manager.go but we can't put
// it here to respect the project layout created by kubebuilder.
func RunController(
metricsAddr,
configMapName,
secretName string,
leaderConfig leaderElectionConfiguration,
pprofDebug bool,
port int,
) error {
ctx := context.Background()
setupLog.Info("Starting CloudNativePG Operator",
"version", versions.Version,
"build", versions.Info)
if pprofDebug {
startPprofDebugServer(ctx)
}
managerOptions := ctrl.Options{
Scheme: scheme,
Metrics: server.Options{
BindAddress: metricsAddr,
},
LeaderElection: leaderConfig.enable,
LeaseDuration: &leaderConfig.leaseDuration,
RenewDeadline: &leaderConfig.renewDeadline,
LeaderElectionID: LeaderElectionID,
WebhookServer: webhook.NewServer(webhook.Options{
Port: port,
CertDir: defaultWebhookCertDir,
}),
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
// speeds up voluntary leader transitions as the new leader don't have to wait
// LeaseDuration time first.
//
// In the default scaffold provided, the program ends immediately after
// the manager stops, so would be fine to enable this option. However,
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
LeaderElectionReleaseOnCancel: true,
}
if configuration.Current.WatchNamespace != "" {
namespaces := configuration.Current.WatchedNamespaces()
managerOptions.NewCache = multicache.DelegatingMultiNamespacedCacheBuilder(
namespaces,
configuration.Current.OperatorNamespace)
setupLog.Info("Listening for changes", "watchNamespaces", namespaces)
} else {
setupLog.Info("Listening for changes on all namespaces")
}
if configuration.Current.WebhookCertDir != "" {
// If OLM will generate certificates for us, let's just
// use those
managerOptions.WebhookServer.(*webhook.DefaultServer).Options.CertDir = configuration.Current.WebhookCertDir
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), managerOptions)
if err != nil {
setupLog.Error(err, "unable to start manager")
return err
}
webhookServer := mgr.GetWebhookServer().(*webhook.DefaultServer)
if configuration.Current.WebhookCertDir != "" {
// Use certificate names compatible with OLM
webhookServer.Options.CertName = "apiserver.crt"
webhookServer.Options.KeyName = "apiserver.key"
} else {
webhookServer.Options.CertName = "tls.crt"
webhookServer.Options.KeyName = "tls.key"
}
// kubeClient is the kubernetes client set with
// support for the apiextensions that is used
// during the initialization of the operator
// kubeClient client.Client
kubeClient, err := client.New(mgr.GetConfig(), client.Options{Scheme: scheme})
if err != nil {
setupLog.Error(err, "unable to create Kubernetes client")
return err
}
err = loadConfiguration(ctx, kubeClient, configMapName, secretName)
if err != nil {
return err
}
setupLog.Info("Operator configuration loaded", "configuration", configuration.Current)
discoveryClient, err := utils.GetDiscoveryClient()
if err != nil {
return err
}
// Detect if we are running under a system that implements OpenShift Security Context Constraints
if err = utils.DetectSecurityContextConstraints(discoveryClient); err != nil {
setupLog.Error(err, "unable to detect OpenShift Security Context Constraints presence")
return err
}
// Detect if we are running under a system that provides Volume Snapshots
if err = utils.DetectVolumeSnapshotExist(discoveryClient); err != nil {
setupLog.Error(err, "unable to detect the if the cluster have the VolumeSnapshot CRD installed")
return err
}
// Detect if we support SeccompProfile
if err = utils.DetectSeccompSupport(discoveryClient); err != nil {
setupLog.Error(err, "unable to detect SeccompProfile support")
return err
}
// Retrieve the Kubernetes cluster system UID
if err = utils.DetectKubeSystemUID(ctx, kubeClient); err != nil {
setupLog.Error(err, "unable to retrieve the Kubernetes cluster system UID")
return err
}
setupLog.Info("Kubernetes system metadata",
"systemUID", utils.GetKubeSystemUID(),
"haveSCC", utils.HaveSecurityContextConstraints(),
"haveSeccompProfile", utils.HaveSeccompSupport(),
"haveVolumeSnapshot", utils.HaveVolumeSnapshot())
if err := ensurePKI(ctx, kubeClient, webhookServer.Options.CertDir); err != nil {
return err
}
if err = controllers.NewClusterReconciler(mgr, discoveryClient).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Cluster")
return err
}
if err = controllers.NewBackupReconciler(mgr, discoveryClient).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Backup")
return err
}
if err = (&controllers.ScheduledBackupReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("cloudnative-pg-scheduledbackup"),
}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ScheduledBackup")
return err
}
if err = (&controllers.PoolerReconciler{
Client: mgr.GetClient(),
DiscoveryClient: discoveryClient,
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("cloudnative-pg-pooler"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Pooler")
return err
}
if err = (&apiv1.Cluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Cluster", "version", "v1")
return err
}
if err = (&apiv1.Backup{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Backup", "version", "v1")
return err
}
if err = (&apiv1.ScheduledBackup{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ScheduledBackup", "version", "v1")
return err
}
if err = (&apiv1.Pooler{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Pooler", "version", "v1")
return err
}
// Setup the handler used by the readiness and liveliness probe.
//
// Unfortunately the readiness of the probe is not sufficient for the operator to be
// working correctly. The probe may be positive even when:
//
// 1. the CA is not yet updated inside the CRD and/or in the validating/mutating
// webhook configuration. In that case we have a timeout error after trying
// to send a POST message and getting no response message.
//
// 2. the webhook service and/or the CNI are being updated, e.g. when a POD is
// deleted. In that case we could get a "Connection refused" error message.
webhookServer.WebhookMux().HandleFunc("/readyz", readinessProbeHandler)
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
return err
}
return nil
}
// loadConfiguration reads the configuration from the provided configmap and secret
func loadConfiguration(
ctx context.Context,
kubeClient client.Client,
configMapName string,
secretName string,
) error {
configData := make(map[string]string)
// First read the configmap if provided and store it in configData
if configMapName != "" {
configMapData, err := readConfigMap(ctx, kubeClient, configuration.Current.OperatorNamespace, configMapName)
if err != nil {
setupLog.Error(err, "unable to read ConfigMap",
"namespace", configuration.Current.OperatorNamespace,
"name", configMapName)
return err
}
for k, v := range configMapData {
configData[k] = v
}
}
// Then read the secret if provided and store it in configData, overwriting configmap's values
if secretName != "" {
secretData, err := readSecret(ctx, kubeClient, configuration.Current.OperatorNamespace, secretName)
if err != nil {
setupLog.Error(err, "unable to read Secret",
"namespace", configuration.Current.OperatorNamespace,
"name", secretName)
return err
}
for k, v := range secretData {
configData[k] = v
}
}
// Finally, read the config if it was provided
if len(configData) > 0 {
configuration.Current.ReadConfigMap(configData)
}
return nil
}
// readinessProbeHandler is used to implement the readiness probe handler
func readinessProbeHandler(w http.ResponseWriter, _ *http.Request) {
_, _ = fmt.Fprint(w, "OK")
}
// ensurePKI ensures that we have the required PKI infrastructure to make
// the operator and the clusters working
func ensurePKI(
ctx context.Context,
kubeClient client.Client,
mgrCertDir string,
) error {
if configuration.Current.WebhookCertDir != "" {
// OLM is generating certificates for us, so we can avoid injecting/creating certificates.
return nil
}
// We need to self-manage required PKI infrastructure and install the certificates into
// the webhooks configuration
pkiConfig := certs.PublicKeyInfrastructure{
CaSecretName: CaSecretName,
CertDir: mgrCertDir,
SecretName: WebhookSecretName,
ServiceName: WebhookServiceName,
OperatorNamespace: configuration.Current.OperatorNamespace,
MutatingWebhookConfigurationName: MutatingWebhookConfigurationName,
ValidatingWebhookConfigurationName: ValidatingWebhookConfigurationName,
CustomResourceDefinitionsName: []string{
"backups.postgresql.cnpg.io",
"clusters.postgresql.cnpg.io",
"scheduledbackups.postgresql.cnpg.io",
},
OperatorDeploymentLabelSelector: "app.kubernetes.io/name=cloudnative-pg",
}
err := pkiConfig.Setup(ctx, kubeClient)
if err != nil {
setupLog.Error(err, "unable to setup PKI infrastructure")
}
return err
}
// readConfigMap reads the configMap and returns its content as map
func readConfigMap(
ctx context.Context,
kubeClient client.Client,
namespace string,
name string,
) (map[string]string, error) {
if name == "" {
return nil, nil
}
if namespace == "" {
return nil, nil
}
setupLog.Info("Loading configuration from ConfigMap",
"namespace", namespace,
"name", name)
configMap := &corev1.ConfigMap{}
err := kubeClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, configMap)
if apierrs.IsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, err
}
return configMap.Data, nil
}
// readSecret reads the secret and returns its content as map
func readSecret(
ctx context.Context,
kubeClient client.Client,
namespace,
name string,
) (map[string]string, error) {
if name == "" {
return nil, nil
}
if namespace == "" {
return nil, nil
}
setupLog.Info("Loading configuration from Secret",
"namespace", namespace,
"name", name)
secret := &corev1.Secret{}
err := kubeClient.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, secret)
if apierrs.IsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, err
}
data := make(map[string]string)
for k, v := range secret.Data {
data[k] = string(v)
}
return data, nil
}
// startPprofDebugServer exposes pprof debug server if POD_DEBUG env variable is set to 1
func startPprofDebugServer(ctx context.Context) {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
pprofServer := http.Server{
Addr: "0.0.0.0:6060",
Handler: mux,
ReadTimeout: webserver.DefaultReadTimeout,
ReadHeaderTimeout: webserver.DefaultReadHeaderTimeout,
}
setupLog.Info("Starting pprof HTTP server", "addr", pprofServer.Addr)
go func() {
go func() {
<-ctx.Done()
setupLog.Info("shutting down pprof HTTP server")
ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFunc()
if err := pprofServer.Shutdown(ctx); err != nil {
setupLog.Error(err, "Failed to shutdown pprof HTTP server")
}
}()
if err := pprofServer.ListenAndServe(); !errors.Is(http.ErrServerClosed, err) {
setupLog.Error(err, "Failed to start pprof HTTP server")
}
}()
}