-
Notifications
You must be signed in to change notification settings - Fork 252
/
instance_controller.go
1256 lines (1089 loc) · 39 KB
/
instance_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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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
import (
"context"
"database/sql"
"errors"
"fmt"
"math"
"path"
"strconv"
"time"
"github.com/lib/pq"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/controllers"
"github.com/cloudnative-pg/cloudnative-pg/internal/management/utils"
"github.com/cloudnative-pg/cloudnative-pg/pkg/certs"
"github.com/cloudnative-pg/cloudnative-pg/pkg/configfile"
"github.com/cloudnative-pg/cloudnative-pg/pkg/fileutils"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/log"
postgresManagement "github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/constants"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/metrics"
postgresutils "github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/utils"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver/metricserver"
"github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
pkgUtils "github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
)
const (
userSearchFunctionName = "user_search"
userSearchFunction = "SELECT usename, passwd FROM pg_shadow WHERE usename=$1;"
)
// RetryUntilWalReceiverDown is the default retry configuration that is used
// to wait for the WAL receiver process to be down
var RetryUntilWalReceiverDown = wait.Backoff{
Duration: 1 * time.Second,
// Steps is declared as an "int", so we are capping
// to int32 to support ARM-based 32 bit architectures
Steps: math.MaxInt32,
}
// shouldRequeue specifies whether a new reconciliation loop should be triggered
type shoudRequeue bool
// Reconcile is the main reconciliation loop for the instance
// TODO this function needs to be refactor
//nolint:gocognit
func (r *InstanceReconciler) Reconcile(
ctx context.Context,
request reconcile.Request,
) (reconcile.Result, error) {
// set up a convenient contextLog object so we don't have to type request over and over again
contextLogger, ctx := log.SetupLogger(ctx)
// if the context has already been cancelled,
// trying to reconcile would just lead to misleading errors being reported
if err := ctx.Err(); err != nil {
contextLogger.Warning("Context cancelled, will not reconcile", "err", err)
return ctrl.Result{}, nil
}
// Fetch the Cluster from the cache
cluster, err := r.GetCluster(ctx)
if err != nil {
if apierrors.IsNotFound(err) {
// The cluster has been deleted.
// We just need to wait for this instance manager to be terminated
contextLogger.Debug("Could not find Cluster")
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("could not fetch Cluster: %w", err)
}
// Print the Cluster
contextLogger.Debug("Reconciling Cluster", "cluster", cluster)
// Reconcile PostgreSQL instance parameters
r.reconcileInstance(cluster)
// Refresh the cache
requeue := r.updateCacheFromCluster(ctx, cluster)
// Reconcile monitoring section
r.reconcileMetrics(cluster)
r.reconcileMonitoringQueries(ctx, cluster)
// Reconcile secrets and cryptographic material
// This doesn't need the PG connection, but it needs to reload it in case of changes
reloadNeeded := r.RefreshSecrets(ctx, cluster)
reloadConfigNeeded, err := r.refreshConfigurationFiles(ctx, cluster)
if err != nil {
return reconcile.Result{}, err
}
reloadNeeded = reloadNeeded || reloadConfigNeeded
// here we execute initialization tasks that need to be executed only on the first reconciliation loop
if !r.firstReconcileDone.Load() {
if err = r.initialize(ctx, cluster); err != nil {
return handleErrNextLoop(err)
}
r.firstReconcileDone.Store(true)
}
// Reconcile cluster role without DB
reloadClusterRoleConfig, err := r.reconcileClusterRoleWithoutDB(ctx, cluster)
if err != nil {
return reconcile.Result{}, err
}
reloadNeeded = reloadNeeded || reloadClusterRoleConfig
r.systemInitialization.Broadcast()
if result := r.reconcileFencing(cluster); result != nil {
contextLogger.Info("Fencing status changed, will not proceed with the reconciliation loop")
return *result, nil
}
if r.instance.IsFenced() || r.instance.MightBeUnavailable() {
contextLogger.Info("Instance could be down, will not proceed with the reconciliation loop")
return reconcile.Result{}, nil
}
if r.instance.IsServerHealthy() != nil {
contextLogger.Info("Instance is still down, will retry in 1 second")
return reconcile.Result{RequeueAfter: time.Second}, nil
}
restarted, err := r.reconcilePrimary(ctx, cluster)
if err != nil {
return reconcile.Result{}, err
}
restartedFromOldPrimary, err := r.reconcileOldPrimary(ctx, cluster)
if err != nil {
return reconcile.Result{}, err
}
restarted = restarted || restartedFromOldPrimary
if r.IsDBUp(ctx) != nil {
return reconcile.Result{RequeueAfter: time.Second}, nil
}
restartedInplace, err := r.restartPrimaryInplaceIfRequested(ctx, cluster)
if err != nil {
return reconcile.Result{}, err
}
restarted = restarted || restartedInplace
// from now on the database can be assumed as running
if reloadNeeded && !restarted {
contextLogger.Info("reloading the instance")
if err = r.instance.Reload(); err != nil {
return reconcile.Result{}, fmt.Errorf("while reloading the instance: %w", err)
}
if err = r.waitForConfigurationReload(ctx, cluster); err != nil {
return reconcile.Result{}, fmt.Errorf("cannot apply new PostgreSQL configuration: %w", err)
}
}
if err = r.refreshCredentialsFromSecret(ctx, cluster); err != nil {
return reconcile.Result{}, fmt.Errorf("while updating database owner password: %w", err)
}
if err := r.reconcileDatabases(ctx, cluster); err != nil {
return reconcile.Result{}, fmt.Errorf("cannot reconcile database configurations: %w", err)
}
// Extremely important.
// It could happen that current primary is reconciled before all the topology is extracted by the operator.
// We should detect that and schedule the instance manager for another run otherwise we will end up having
// an incoherent state of electable synchronous_names inside the configuration.
// This is only relevant if syncReplicaElectionConstraint is enabled
if !requeue {
requeue = r.shouldRequeueForMissingTopology(cluster)
}
if requeue {
return reconcile.Result{RequeueAfter: 30 * time.Second}, nil
}
return reconcile.Result{}, nil
}
func (r *InstanceReconciler) restartPrimaryInplaceIfRequested(
ctx context.Context,
cluster *apiv1.Cluster,
) (bool, error) {
isPrimary, err := r.instance.IsPrimary()
if err != nil {
return false, err
}
if isPrimary && cluster.Status.Phase == apiv1.PhaseInplacePrimaryRestart {
if err := r.instance.RequestAndWaitRestartSmartFast(); err != nil {
return true, err
}
oldCluster := cluster.DeepCopy()
cluster.Status.Phase = apiv1.PhaseHealthy
cluster.Status.PhaseReason = "Primary instance restarted in-place"
return true, r.client.Status().Patch(ctx, cluster, client.MergeFrom(oldCluster))
}
return false, nil
}
func (r *InstanceReconciler) refreshConfigurationFiles(
ctx context.Context,
cluster *apiv1.Cluster,
) (reloadNeeded bool, err error) {
reloadNeeded, err = r.refreshPGHBA(ctx, cluster)
if err != nil {
return false, err
}
// Reconcile PostgreSQL configuration
// This doesn't need the PG connection, but it needs to reload it in case of changes
reloadConfig, err := r.instance.RefreshConfigurationFilesFromCluster(cluster)
if err != nil {
return false, err
}
reloadNeeded = reloadNeeded || reloadConfig
reloadReplicaConfig, err := r.refreshReplicaConfiguration(ctx, cluster)
if err != nil {
return false, err
}
reloadNeeded = reloadNeeded || reloadReplicaConfig
return reloadNeeded, nil
}
func (r *InstanceReconciler) reconcileFencing(cluster *apiv1.Cluster) *reconcile.Result {
fencingRequired := cluster.IsInstanceFenced(r.instance.PodName)
isFenced := r.instance.IsFenced()
switch {
case !isFenced && fencingRequired:
// fencing required and not enabled yet, request fencing and stop
r.instance.RequestFencingOn()
return &reconcile.Result{}
case isFenced && !fencingRequired:
// fencing enabled and not required anymore, request to disable fencing and continue
err := r.instance.RequestAndWaitFencingOff()
if err != nil {
log.Error(err, "while waiting for the instance to be restarted after lifting the fence")
}
return &reconcile.Result{}
}
return nil
}
func handleErrNextLoop(err error) (reconcile.Result, error) {
if errors.Is(err, controllers.ErrNextLoop) {
return reconcile.Result{RequeueAfter: time.Second}, nil
}
return reconcile.Result{}, err
}
// initialize will handle initialization tasks
func (r *InstanceReconciler) initialize(ctx context.Context, cluster *apiv1.Cluster) error {
// we check there are no parameters that would prevent a follower to start
if err := r.verifyParametersForFollower(cluster); err != nil {
return err
}
if err := r.verifyPgDataCoherenceForPrimary(ctx, cluster); err != nil {
return err
}
r.instance.SetFencing(cluster.IsInstanceFenced(r.instance.PodName))
return nil
}
// verifyParametersForFollower enforces that the follower's settings for the enforced
// parameters are higher than those of the primary.
// This could not be the case if the cluster spec value for one of those parameters
// is decreased shortly after having been increased. The follower would be restarting
// towards a high level, then write the lower value to the local config
func (r *InstanceReconciler) verifyParametersForFollower(cluster *apiv1.Cluster) error {
if isPrimary, _ := r.instance.IsPrimary(); isPrimary {
return nil
}
// we use a file as a flag to ensure the pod has been restarted already. I.e. on
// newly created pod we don't need to check the enforced parameters
filename := path.Join(r.instance.PgData, fmt.Sprintf("%s-%s", constants.Startup, r.instance.PodName))
exists, err := fileutils.FileExists(filename)
if err != nil {
return err
}
// if the file did not exist, the pod was newly created and we can skip out
if !exists {
_, err := fileutils.WriteFileAtomic(filename, []byte(nil), 0o600)
return err
}
log.Info("Found previous run flag", "filename", filename)
enforcedParams, err := postgresManagement.GetEnforcedParametersThroughPgControldata(r.instance.PgData)
if err != nil {
return err
}
clusterParams := cluster.Spec.PostgresConfiguration.Parameters
options := make(map[string]string)
for key, enforcedparam := range enforcedParams {
clusterparam, found := clusterParams[key]
if !found {
continue
}
enforcedparamInt, err := strconv.Atoi(enforcedparam)
if err != nil {
return err
}
clusterparamInt, err := strconv.Atoi(clusterparam)
if err != nil {
return err
}
// if the values from `pg_controldata` are higher than the cluster spec,
// they are the safer choice, so set them in config
if enforcedparamInt > clusterparamInt {
options[key] = enforcedparam
}
}
if len(options) == 0 {
return nil
}
log.Info("Updating some enforced parameters that would prevent the instance to start",
"parameters", options, "clusterParams", clusterParams)
// we write the safer enforced parameter values to pod config as safety
// in the face of cluster specs going up and down from nervous users
if _, err := configfile.UpdatePostgresConfigurationFile(
path.Join(r.instance.PgData, constants.PostgresqlCustomConfigurationFile),
options); err != nil {
return err
}
return nil
}
// reconcileOldPrimary shuts down the instance in case it is an old primary
func (r *InstanceReconciler) reconcileOldPrimary(
ctx context.Context,
cluster *apiv1.Cluster,
) (restarted bool, err error) {
contextLogger := log.FromContext(ctx)
if cluster.Status.TargetPrimary == r.instance.PodName {
return false, nil
}
isPrimary, err := r.instance.IsPrimary()
if err != nil || !isPrimary {
return false, err
}
contextLogger.Info("This is an old primary node. Requesting a checkpoint before demotion")
db, err := r.instance.GetSuperUserDB()
if err != nil {
contextLogger.Error(err, "Cannot connect to primary server")
} else {
_, err = db.Exec("CHECKPOINT")
if err != nil {
contextLogger.Error(err, "Error while requesting a checkpoint")
}
}
contextLogger.Info("This is an old primary node. Shutting it down to get it demoted to a replica")
// Here we need to invoke a fast shutdown on the instance, and wait the instance
// manager to be stopped.
// When the Pod will restart, we will demote as a replica of the new primary
r.Instance().RequestFastImmediateShutdown()
// We wait for the lifecycle manager to have received the immediate shutdown request
// and, having processed it, to request the termination of the instance manager.
// When the termination has been requested, this context will be cancelled.
<-ctx.Done()
cluster.LogTimestampsWithMessage(ctx, "Old primary shutdown complete")
return true, nil
}
// IsDBUp checks whether the superuserdb is reachable and returns an error if that's not the case
func (r *InstanceReconciler) IsDBUp(ctx context.Context) error {
contextLogger := log.FromContext(ctx)
db, err := r.instance.GetSuperUserDB()
if err != nil {
contextLogger.Warning(fmt.Sprintf("while getting a connection to the instance: %s", err))
return err
}
if err := db.Ping(); err != nil {
contextLogger.Info("DB not available, will retry", "err", err)
return err
}
return nil
}
// reconcileDatabases reconciles all the existing databases
func (r *InstanceReconciler) reconcileDatabases(ctx context.Context, cluster *apiv1.Cluster) error {
ok, err := r.instance.IsPrimary()
if err != nil {
return fmt.Errorf("unable to check if instance is primary: %w", err)
}
if !ok {
return nil
}
db, err := r.instance.GetSuperUserDB()
if err != nil {
return fmt.Errorf("getting the superuserdb: %w", err)
}
extensionStatusChanged := false
for _, extension := range postgres.ManagedExtensions {
extensionIsUsed := extension.IsUsed(cluster.Spec.PostgresConfiguration.Parameters)
if lastStatus, ok := r.extensionStatus[extension.Name]; !ok || lastStatus != extensionIsUsed {
extensionStatusChanged = true
break
}
}
databases, errors := r.getAllAccessibleDatabases(ctx, db)
for _, databaseName := range databases {
db, err := r.instance.ConnectionPool().Connection(databaseName)
if err != nil {
errors = append(errors,
fmt.Errorf("could not connect to database %s: %w", databaseName, err))
continue
}
if extensionStatusChanged {
if err = r.reconcileExtensions(ctx, db, cluster.Spec.PostgresConfiguration.Parameters); err != nil {
errors = append(errors,
fmt.Errorf("could not reconcile extensions for database %s: %w", databaseName, err))
}
}
if err = r.reconcilePoolers(ctx, db, databaseName, cluster.Status.PoolerIntegrations); err != nil {
errors = append(errors,
fmt.Errorf("could not reconcile extensions for database %s: %w", databaseName, err))
}
}
if errors != nil {
return fmt.Errorf("got errors while reconciling databases: %v", errors)
}
for _, extension := range postgres.ManagedExtensions {
extensionIsUsed := extension.IsUsed(cluster.Spec.PostgresConfiguration.Parameters)
r.extensionStatus[extension.Name] = extensionIsUsed
}
return nil
}
// getAllAccessibleDatabases returns the list of all the accessible databases using the superuser
func (r *InstanceReconciler) getAllAccessibleDatabases(
ctx context.Context,
db *sql.DB,
) (databases []string, errors []error) {
tx, err := db.BeginTx(ctx, &sql.TxOptions{
ReadOnly: true,
})
if err != nil {
errors = append(errors, err)
return nil, errors
}
defer func() {
if err := tx.Commit(); err != nil {
errors = append(errors, err)
}
}()
databases, errors = postgresutils.GetAllAccessibleDatabases(tx, "datallowconn")
return databases, errors
}
// ReconcileExtensions reconciles the expected extensions for this
// PostgreSQL instance
func (r *InstanceReconciler) reconcileExtensions(
ctx context.Context, db *sql.DB, userSettings map[string]string,
) (err error) {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() {
// This is a no-op when the transaction is committed
_ = tx.Rollback()
}()
_, err = tx.Exec("SET LOCAL synchronous_commit TO local")
if err != nil {
return err
}
for _, extension := range postgres.ManagedExtensions {
extensionIsUsed := extension.IsUsed(userSettings)
row := tx.QueryRow("SELECT COUNT(*) > 0 FROM pg_extension WHERE extname = $1", extension.Name)
err = row.Err()
if err != nil {
break
}
var extensionIsInstalled bool
if err = row.Scan(&extensionIsInstalled); err != nil {
break
}
// We don't just use the "IF EXIST" to avoid stressing PostgreSQL with
// a DDL when it is not really needed.
if !extension.SkipCreateExtension && extensionIsUsed && !extensionIsInstalled {
_, err = tx.Exec(fmt.Sprintf("CREATE EXTENSION %s", extension.Name))
} else if !extensionIsUsed && extensionIsInstalled {
_, err = tx.Exec(fmt.Sprintf("DROP EXTENSION %s", extension.Name))
}
if err != nil {
break
}
}
if err != nil {
return err
}
return tx.Commit()
}
// ReconcileExtensions reconciles the expected extensions for this
// PostgreSQL instance
func (r *InstanceReconciler) reconcilePoolers(
ctx context.Context, db *sql.DB, dbName string, integrations *apiv1.PoolerIntegrations,
) (err error) {
if integrations == nil || len(integrations.PgBouncerIntegration.Secrets) == 0 {
return
}
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() {
// This is a no-op when the transaction is committed
_ = tx.Rollback()
}()
_, err = tx.Exec("SET LOCAL synchronous_commit TO local")
if err != nil {
return err
}
var existsRole bool
row := tx.QueryRow(fmt.Sprintf("SELECT COUNT(*) > 0 FROM pg_catalog.pg_roles WHERE rolname = '%s'",
apiv1.PGBouncerPoolerUserName))
err = row.Scan(&existsRole)
if err != nil {
return err
}
if !existsRole {
_, err := tx.Exec(fmt.Sprintf("CREATE ROLE %s WITH LOGIN", apiv1.PGBouncerPoolerUserName))
if err != nil {
return err
}
_, err = tx.Exec(fmt.Sprintf("GRANT CONNECT ON DATABASE %s TO %s", dbName, apiv1.PGBouncerPoolerUserName))
if err != nil {
return err
}
}
var existsFunction bool
row = tx.QueryRow(fmt.Sprintf("SELECT COUNT(*) > 0 FROM pg_proc WHERE proname='%s' and prosrc='%s'",
userSearchFunctionName,
userSearchFunction))
err = row.Scan(&existsFunction)
if err != nil {
return err
}
if !existsFunction {
_, err = tx.Exec(fmt.Sprintf("CREATE OR REPLACE FUNCTION %s(uname TEXT) "+
"RETURNS TABLE (usename name, passwd text) "+
"as '%s' "+
"LANGUAGE sql SECURITY DEFINER",
userSearchFunctionName,
userSearchFunction))
if err != nil {
return err
}
_, err = tx.Exec(fmt.Sprintf("REVOKE ALL ON FUNCTION %s(text) FROM public;", userSearchFunctionName))
if err != nil {
return err
}
_, err = tx.Exec(fmt.Sprintf("GRANT EXECUTE ON FUNCTION %s(text) TO %s",
userSearchFunctionName,
apiv1.PGBouncerPoolerUserName))
if err != nil {
return err
}
}
return tx.Commit()
}
// reconcileClusterRoleWithoutDB updates this instance's configuration files
// according to the role written in the cluster status
func (r *InstanceReconciler) reconcileClusterRoleWithoutDB(
ctx context.Context,
cluster *apiv1.Cluster,
) (changed bool, err error) {
isPrimary, err := r.instance.IsPrimary()
if err != nil {
return false, err
}
// Reconcile replica role
if cluster.Status.TargetPrimary != r.instance.PodName {
if !isPrimary {
// We need to ensure that this instance is replicating from the correct server
return r.refreshReplicaConfiguration(ctx, cluster)
}
return false, nil
}
// Reconcile designated primary role
if cluster.IsReplica() {
return r.reconcileDesignatedPrimary(ctx, cluster)
}
// This is a primary server
return false, nil
}
// reconcileMetrics updates any required metrics
func (r *InstanceReconciler) reconcileMetrics(
cluster *apiv1.Cluster,
) {
exporter := r.metricsServerExporter
// We should never reset the SwitchoverRequired metrics as it needs the primary instance restarts,
// however, if the cluster is healthy we make sure it is set to 0.
if cluster.Status.CurrentPrimary == r.instance.PodName {
if cluster.Status.Phase == apiv1.PhaseWaitingForUser {
exporter.Metrics.SwitchoverRequired.Set(1)
} else {
exporter.Metrics.SwitchoverRequired.Set(0)
}
}
exporter.Metrics.SyncReplicas.WithLabelValues("min").Set(float64(cluster.Spec.MinSyncReplicas))
exporter.Metrics.SyncReplicas.WithLabelValues("max").Set(float64(cluster.Spec.MaxSyncReplicas))
syncReplicas, _ := cluster.GetSyncReplicasData()
exporter.Metrics.SyncReplicas.WithLabelValues("expected").Set(float64(syncReplicas))
if cluster.IsReplica() {
exporter.Metrics.ReplicaCluster.Set(1)
} else {
exporter.Metrics.ReplicaCluster.Set(0)
}
}
// reconcileMonitoringQueries applies the custom monitoring queries to the
// web server
func (r *InstanceReconciler) reconcileMonitoringQueries(
ctx context.Context,
cluster *apiv1.Cluster,
) {
contextLogger := log.FromContext(ctx)
contextLogger.Debug("Reconciling custom monitoring queries")
dbname := "postgres"
if len(cluster.GetApplicationDatabaseName()) != 0 {
dbname = cluster.GetApplicationDatabaseName()
}
queriesCollector := metrics.NewQueriesCollector("cnpg", r.instance, dbname)
queriesCollector.InjectUserQueries(metricserver.DefaultQueries)
if cluster.Spec.Monitoring == nil {
r.metricsServerExporter.SetCustomQueries(queriesCollector)
return
}
for _, reference := range cluster.Spec.Monitoring.CustomQueriesConfigMap {
var configMap corev1.ConfigMap
err := r.GetClient().Get(
ctx,
client.ObjectKey{Namespace: r.instance.Namespace, Name: reference.Name},
&configMap)
if err != nil {
contextLogger.Warning("Unable to get configMap containing custom monitoring queries",
"reference", reference,
"error", err.Error())
continue
}
data, ok := configMap.Data[reference.Key]
if !ok {
contextLogger.Warning("Missing key in configMap",
"reference", reference)
continue
}
err = queriesCollector.ParseQueries([]byte(data))
if err != nil {
contextLogger.Warning("Error while parsing custom queries in ConfigMap",
"reference", reference,
"error", err.Error())
continue
}
}
for _, reference := range cluster.Spec.Monitoring.CustomQueriesSecret {
var secret corev1.Secret
err := r.GetClient().Get(ctx, client.ObjectKey{Namespace: r.instance.Namespace, Name: reference.Name}, &secret)
if err != nil {
contextLogger.Warning("Unable to get secret containing custom monitoring queries",
"reference", reference,
"error", err.Error())
continue
}
data, ok := secret.Data[reference.Key]
if !ok {
contextLogger.Warning("Missing key in secret",
"reference", reference)
continue
}
err = queriesCollector.ParseQueries(data)
if err != nil {
contextLogger.Warning("Error while parsing custom queries in Secret",
"reference", reference,
"error", err.Error())
continue
}
}
r.metricsServerExporter.SetCustomQueries(queriesCollector)
}
// RefreshSecrets is called when the PostgreSQL secrets are changed
// and will refresh the contents of the file inside the Pod, without
// reloading the actual PostgreSQL instance.
//
// It returns a boolean flag telling if something changed. Usually
// the invoker will check that flag and reload the PostgreSQL
// instance it is up.
//
// This function manages its own errors by logging them, so the
// user cannot easily tell if the operation has been done completely.
// The rationale behind this is:
//
// 1. when invoked at the startup of the instance manager, PostgreSQL
// is not up. If this raise an error, then PostgreSQL won't
// be able to start correctly (TLS certs are missing, i.e.),
// making no difference between returning an error or not
//
// 2. when invoked inside the reconciliation loop, if the operation
// raise an error, it's pointless to retry. The only way to recover
// from such an error is wait for the CNPG operator to refresh the
// resource version of the secrets to be used, and in that case a
// reconciliation loop will be started again.
func (r *InstanceReconciler) RefreshSecrets(
ctx context.Context,
cluster *apiv1.Cluster,
) bool {
contextLogger := log.FromContext(ctx)
changed := false
serverSecretChanged, err := r.refreshServerCertificateFiles(ctx, cluster)
if err == nil {
changed = changed || serverSecretChanged
} else if !apierrors.IsNotFound(err) {
contextLogger.Error(err, "Error while getting server secret")
}
replicationSecretChanged, err := r.refreshReplicationUserCertificate(ctx, cluster)
if err == nil {
changed = changed || replicationSecretChanged
} else if !apierrors.IsNotFound(err) {
contextLogger.Error(err, "Error while getting streaming replication secret")
}
clientCaSecretChanged, err := r.refreshClientCA(ctx, cluster)
if err == nil {
changed = changed || clientCaSecretChanged
} else if !apierrors.IsNotFound(err) {
contextLogger.Error(err, "Error while getting cluster CA Client secret")
}
serverCaSecretChanged, err := r.refreshServerCA(ctx, cluster)
if err == nil {
changed = changed || serverCaSecretChanged
} else if !apierrors.IsNotFound(err) {
contextLogger.Error(err, "Error while getting cluster CA Server secret")
}
barmanEndpointCaSecretChanged, err := r.refreshBarmanEndpointCA(ctx, cluster)
if err == nil {
changed = changed || barmanEndpointCaSecretChanged
} else if !apierrors.IsNotFound(err) {
contextLogger.Error(err, "Error while getting barman endpoint CA secret")
}
return changed
}
// reconcileInstance sets PostgreSQL instance parameters to current values
func (r *InstanceReconciler) reconcileInstance(cluster *apiv1.Cluster) {
r.instance.PgCtlTimeoutForPromotion = cluster.GetPgCtlTimeoutForPromotion()
r.instance.MaxSwitchoverDelay = cluster.GetMaxSwitchoverDelay()
r.instance.MaxStopDelay = cluster.GetMaxStopDelay()
}
// waitForConfigurationReload waits for the db to be up and
// the new configuration to be reloaded
func (r *InstanceReconciler) waitForConfigurationReload(ctx context.Context, cluster *apiv1.Cluster) error {
contextLogger := log.FromContext(ctx)
// This function could also be called while the server is being
// started up, so we are not sure that the server is really active.
// Let's wait for that.
if r.instance.ConfigSha256 == "" {
return nil
}
err := r.instance.WaitForSuperuserConnectionAvailable()
if err != nil {
return fmt.Errorf("while applying new configuration: %w", err)
}
err = r.instance.WaitForConfigReloaded()
if err != nil {
return fmt.Errorf("while waiting for new configuration to be reloaded: %w", err)
}
status, err := r.instance.GetStatus()
if err != nil {
return fmt.Errorf("while applying new configuration: %w", err)
}
if status.MightBeUnavailableMaskedError != "" {
return fmt.Errorf(
"while applying new configuration encountered an error masked by mightBeUnavailable: %s",
status.MightBeUnavailableMaskedError,
)
}
if !status.PendingRestart {
// Everything fine
return nil
}
// if there is a pending restart, the instance is a primary and
// the restart is due to a decrease of sensible parameters,
// we will need to restart the primary instance in place
phase := apiv1.PhaseApplyingConfiguration
phaseReason := "PostgreSQL configuration changed"
if status.IsPrimary && status.PendingRestartForDecrease {
if cluster.GetPrimaryUpdateStrategy() == apiv1.PrimaryUpdateStrategyUnsupervised {
contextLogger.Info("Restarting primary in-place due to hot standby sensible parameters decrease")
return r.Instance().RequestAndWaitRestartSmartFast()
}
reason := "decrease of hot standby sensitive parameters"
contextLogger.Info("Waiting for the user to request a restart of the primary instance or a switchover "+
"to complete the rolling update",
"cluster", cluster.Name, "primaryPod", status.Pod.Name, "reason", reason)
phase = apiv1.PhaseWaitingForUser
phaseReason = "User must issue a supervised switchover"
}
if phase == apiv1.PhaseApplyingConfiguration &&
(cluster.Status.Phase == apiv1.PhaseApplyingConfiguration ||
(status.IsPrimary && cluster.Spec.Instances > 1)) {
// I'm not the first instance spotting the configuration
// change, everything is fine and there is no need to signal
// the operator again.
// We also don't want to trigger the reconciliation loop from the primary
// for parameters which are not to be considered sensible for hot standbys,
// so, we will wait for replicas to trigger it first.
return nil
}
oldCluster := cluster.DeepCopy()
cluster.Status.Phase = phase
cluster.Status.PhaseReason = phaseReason
return r.client.Status().Patch(ctx, cluster, client.MergeFrom(oldCluster))
}
// refreshCertificateFilesFromSecret receive a secret and rewrite the file
// corresponding to the server certificate
func (r *InstanceReconciler) refreshCertificateFilesFromSecret(
ctx context.Context,
secret *corev1.Secret,
certificateLocation string,
privateKeyLocation string,
) (bool, error) {
contextLogger := log.FromContext(ctx)
certificate, ok := secret.Data[corev1.TLSCertKey]
if !ok {
return false, fmt.Errorf("missing %s field in Secret", corev1.TLSCertKey)
}
privateKey, ok := secret.Data[corev1.TLSPrivateKeyKey]
if !ok {
return false, fmt.Errorf("missing %s field in Secret", corev1.TLSPrivateKeyKey)
}
certificateIsChanged, err := fileutils.WriteFileAtomic(certificateLocation, certificate, 0o600)
if err != nil {
return false, fmt.Errorf("while writing server certificate: %w", err)
}
if certificateIsChanged {
contextLogger.Info("Refreshed configuration file",
"filename", certificateLocation,
"secret", secret.Name)
}
privateKeyIsChanged, err := fileutils.WriteFileAtomic(privateKeyLocation, privateKey, 0o600)
if err != nil {
return false, fmt.Errorf("while writing server private key: %w", err)
}
if certificateIsChanged {
contextLogger.Info("Refreshed configuration file",
"filename", privateKeyLocation,
"secret", secret.Name)
}
return certificateIsChanged || privateKeyIsChanged, nil
}
// refreshCAFromSecret receive a secret and rewrite the ca.crt file to the provided location
func (r *InstanceReconciler) refreshCAFromSecret(
ctx context.Context,
secret *corev1.Secret,
destLocation string,
) (bool, error) {
caCertificate, ok := secret.Data[certs.CACertKey]
if !ok {
return false, fmt.Errorf("missing %s entry in Secret", certs.CACertKey)
}
changed, err := fileutils.WriteFileAtomic(destLocation, caCertificate, 0o600)
if err != nil {
return false, fmt.Errorf("while writing server certificate: %w", err)
}
if changed {
log.FromContext(ctx).Info("Refreshed configuration file",
"filename", destLocation,
"secret", secret.Name)
}
return changed, nil
}
// refreshFileFromSecret receive a secret and rewrite the file corresponding to the key to the provided location
func (r *InstanceReconciler) refreshFileFromSecret(
ctx context.Context,
secret *corev1.Secret,
key, destLocation string,
) (bool, error) {
contextLogger := log.FromContext(ctx)
data, ok := secret.Data[key]
if !ok {
return false, fmt.Errorf("missing %s entry in Secret", key)
}
changed, err := fileutils.WriteFileAtomic(destLocation, data, 0o600)
if err != nil {
return false, fmt.Errorf("while writing file: %w", err)
}
if changed {