-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathpostgres.go
1102 lines (954 loc) · 40 KB
/
postgres.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 2021 - 2024 Crunchy Data Solutions, Inc.
//
// SPDX-License-Identifier: Apache-2.0
package postgrescluster
import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/url"
"regexp"
"sort"
"strings"
gover "github.com/hashicorp/go-version"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/percona/percona-postgresql-operator/internal/feature"
"github.com/percona/percona-postgresql-operator/internal/initialize"
"github.com/percona/percona-postgresql-operator/internal/logging"
"github.com/percona/percona-postgresql-operator/internal/naming"
"github.com/percona/percona-postgresql-operator/internal/pgaudit"
"github.com/percona/percona-postgresql-operator/internal/pgrepack"
"github.com/percona/percona-postgresql-operator/internal/pgstatmonitor"
"github.com/percona/percona-postgresql-operator/internal/pgstatstatements"
"github.com/percona/percona-postgresql-operator/internal/pgvector"
"github.com/percona/percona-postgresql-operator/internal/postgis"
"github.com/percona/percona-postgresql-operator/internal/postgres"
pgpassword "github.com/percona/percona-postgresql-operator/internal/postgres/password"
"github.com/percona/percona-postgresql-operator/internal/util"
"github.com/percona/percona-postgresql-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)
// generatePostgresUserSecret returns a Secret containing a password and
// connection details for the first database in spec. When existing is nil or
// lacks a password or verifier, a new password and verifier are generated.
func (r *Reconciler) generatePostgresUserSecret(
cluster *v1beta1.PostgresCluster, spec *v1beta1.PostgresUserSpec, existing *corev1.Secret,
) (*corev1.Secret, error) {
username := string(spec.Name)
// K8SPG-359
intent := &corev1.Secret{}
if spec.SecretName != "" {
intent = &corev1.Secret{ObjectMeta: naming.PostgresCustomUserSecretName(cluster, string(spec.SecretName))}
} else {
intent = &corev1.Secret{ObjectMeta: naming.PostgresUserSecret(cluster, username)}
}
intent.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret"))
initialize.Map(&intent.Data)
// Populate the Secret with libpq keywords for connecting through
// the primary Service.
// - https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
primary := naming.ClusterPrimaryService(cluster)
hostname := primary.Name + "." + primary.Namespace + ".svc"
port := fmt.Sprint(*cluster.Spec.Port)
intent.Data["host"] = []byte(hostname)
intent.Data["port"] = []byte(port)
intent.Data["user"] = []byte(username)
// Use the existing password and verifier.
if existing != nil {
intent.Data["password"] = existing.Data["password"]
intent.Data["verifier"] = existing.Data["verifier"]
}
// When password is unset, generate a new one according to the specified policy.
if len(intent.Data["password"]) == 0 {
// NOTE: The tests around ASCII passwords are lacking. When changing
// this, make sure that ASCII is the default.
generate := util.GenerateASCIIPassword
if spec.Password != nil {
switch spec.Password.Type {
case v1beta1.PostgresPasswordTypeAlphaNumeric:
generate = util.GenerateAlphaNumericPassword
}
}
password, err := generate(util.DefaultGeneratedPasswordLength)
if err != nil {
return nil, errors.WithStack(err)
}
intent.Data["password"] = []byte(password)
intent.Data["verifier"] = nil
}
// When a password has been generated or the verifier is empty,
// generate a verifier based on the current password.
// NOTE(cbandy): We don't have a function to compare a plaintext
// password to a SCRAM verifier.
if len(intent.Data["verifier"]) == 0 {
verifier, err := pgpassword.NewSCRAMPassword(string(intent.Data["password"])).Build()
if err != nil {
return nil, errors.WithStack(err)
}
intent.Data["verifier"] = []byte(verifier)
}
// When a database has been specified, include it and a connection URI.
// - https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
if len(spec.Databases) > 0 {
database := string(spec.Databases[0])
intent.Data["dbname"] = []byte(database)
intent.Data["uri"] = []byte((&url.URL{
Scheme: "postgresql",
User: url.UserPassword(username, string(intent.Data["password"])),
Host: net.JoinHostPort(hostname, port),
Path: database,
}).String())
// The JDBC driver requires a different URI scheme and query component.
// - https://jdbc.postgresql.org/documentation/use/#connection-parameters
query := url.Values{}
query.Set("user", username)
query.Set("password", string(intent.Data["password"]))
intent.Data["jdbc-uri"] = []byte((&url.URL{
Scheme: "jdbc:postgresql",
Host: net.JoinHostPort(hostname, port),
Path: database,
RawQuery: query.Encode(),
}).String())
}
// When PgBouncer is enabled, include values for connecting through it.
if cluster.Spec.Proxy != nil && cluster.Spec.Proxy.PGBouncer != nil {
pgBouncer := naming.ClusterPGBouncer(cluster)
hostname := pgBouncer.Name + "." + pgBouncer.Namespace + ".svc"
port := fmt.Sprint(*cluster.Spec.Proxy.PGBouncer.Port)
intent.Data["pgbouncer-host"] = []byte(hostname)
intent.Data["pgbouncer-port"] = []byte(port)
if len(spec.Databases) > 0 {
database := string(spec.Databases[0])
intent.Data["pgbouncer-uri"] = []byte((&url.URL{
Scheme: "postgresql",
User: url.UserPassword(username, string(intent.Data["password"])),
Host: net.JoinHostPort(hostname, port),
Path: database,
}).String())
// The JDBC driver requires a different URI scheme and query component.
// Disable prepared statements to be compatible with PgBouncer's
// transaction pooling.
// - https://jdbc.postgresql.org/documentation/use/#connection-parameters
// - https://www.pgbouncer.org/faq.html#how-to-use-prepared-statements-with-transaction-pooling
query := url.Values{}
query.Set("user", username)
query.Set("password", string(intent.Data["password"]))
query.Set("prepareThreshold", "0")
intent.Data["pgbouncer-jdbc-uri"] = []byte((&url.URL{
Scheme: "jdbc:postgresql",
Host: net.JoinHostPort(hostname, port),
Path: database,
RawQuery: query.Encode(),
}).String())
}
}
intent.Annotations = cluster.Spec.Metadata.GetAnnotationsOrNil()
intent.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
naming.WithPerconaLabels(map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePostgresUser,
naming.LabelPostgresUser: username,
}, cluster.Name, "", cluster.Labels[naming.LabelVersion]))
// K8SPG-328: Keep this commented in case of conflicts.
// We don't want to delete secrets if custom resource is deleted.
// err := errors.WithStack(r.setControllerReference(cluster, intent))
return intent, nil
}
// reconcilePostgresDatabases creates databases inside of PostgreSQL.
func (r *Reconciler) reconcilePostgresDatabases(
ctx context.Context, cluster *v1beta1.PostgresCluster, instances *observedInstances,
) error {
const container = naming.ContainerDatabase
var podExecutor postgres.Executor
log := logging.FromContext(ctx)
// K8SPG-377
for _, inst := range instances.forCluster {
if matches, known := inst.PodMatchesPodTemplate(); !matches || !known {
log.V(1).Info("Waiting for instance to be updated", "instance", inst.Name)
return nil
}
}
// Find the PostgreSQL instance that can execute SQL that writes system
// catalogs. When there is none, return early.
pod, _ := instances.writablePod(container)
if pod == nil {
return nil
}
ctx = logging.NewContext(ctx, logging.FromContext(ctx).WithValues("pod", pod.Name))
podExecutor = func(
ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
return r.PodExec(ctx, pod.Namespace, pod.Name, container, stdin, stdout, stderr, command...)
}
// Gather the list of database that should exist in PostgreSQL.
databases := sets.Set[string]{}
if cluster.Spec.Users == nil {
// Users are unspecified; create one database matching the cluster name
// if it is also a valid database name.
// TODO(cbandy): Move this to a defaulting (mutating admission) webhook
// to leverage regular validation.
path := field.NewPath("spec", "users").Index(0).Child("databases").Index(0)
// Database names cannot be too long. PostgresCluster.Name is a DNS
// subdomain, so use len() to count characters.
if n := len(cluster.Name); n > 63 {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "InvalidDatabase",
field.Invalid(path, cluster.Name,
fmt.Sprintf("should be at most %d chars long", 63)).Error())
} else {
databases.Insert(cluster.Name)
}
} else {
for _, user := range cluster.Spec.Users {
for _, database := range user.Databases {
databases.Insert(string(database))
}
}
}
// Calculate a hash of the SQL that should be executed in PostgreSQL.
// K8SPG-375, K8SPG-577, K8SPG-699
var pgAuditOK, pgStatMonitorOK, pgStatStatementsOK, pgvectorOK, pgRepackOK, postgisInstallOK bool
create := func(ctx context.Context, exec postgres.Executor) error {
// validate version string before running it in database
_, err := gover.NewVersion(cluster.Labels[naming.LabelVersion])
if err != nil {
return err
}
_, _, err = exec.ExecInAllDatabases(ctx,
fmt.Sprintf("SELECT '%s';", cluster.Labels[naming.LabelVersion]),
map[string]string{
"QUIET": "on", // Do not print successful commands to stdout.
})
if err != nil {
return err
}
if cluster.Spec.Extensions.PGStatMonitor {
if pgStatMonitorOK = pgstatmonitor.EnableInPostgreSQL(ctx, exec) == nil; !pgStatMonitorOK {
// pg_stat_monitor can only be enabled after its shared library is loaded,
// but early versions of PGO do not load it automatically. Assume
// that an error here is because the cluster started during one of
// those versions and has not been restarted.
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgStatMonitorDisabled",
"Unable to install pgStatMonitor")
}
} else {
if pgStatMonitorOK = pgstatmonitor.DisableInPostgreSQL(ctx, exec) == nil; !pgStatMonitorOK {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgStatMonitorEnabled",
"Unable to disable pgStatMonitor")
}
}
if cluster.Spec.Extensions.PGAudit {
if pgAuditOK = pgaudit.EnableInPostgreSQL(ctx, exec) == nil; !pgAuditOK {
// pgAudit can only be enabled after its shared library is loaded,
// but early versions of PGO do not load it automatically. Assume
// that an error here is because the cluster started during one of
// those versions and has not been restarted.
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgAuditDisabled",
"Unable to install pgAudit")
}
} else {
if pgAuditOK = pgaudit.DisableInPostgreSQL(ctx, exec) == nil; !pgAuditOK {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgAuditEnabled",
"Unable to disable pgAudit")
}
}
if cluster.Spec.Extensions.PGStatStatements {
if pgStatStatementsOK = pgstatstatements.EnableInPostgreSQL(ctx, exec) == nil; !pgStatStatementsOK {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgStatStatementsDisabled",
"Unable to install pgStatStatements")
}
} else {
if pgStatStatementsOK = pgstatstatements.DisableInPostgreSQL(ctx, exec) == nil; !pgStatStatementsOK {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgStatStatementsEnabled",
"Unable to disable pgStatStatements")
}
}
// K8SPG-699
if cluster.Spec.Extensions.PGVector {
if pgvectorOK = pgvector.EnableInPostgreSQL(ctx, exec) == nil; !pgvectorOK {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgvectorDisabled",
"Unable to install pgvector")
}
} else {
if pgvectorOK = pgvector.DisableInPostgreSQL(ctx, exec) == nil; !pgvectorOK {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgvectorEnabled",
"Unable to disable pgvector")
}
}
// K8SPG-574
if cluster.Spec.Extensions.PGRepack {
if pgRepackOK = pgrepack.EnableInPostgreSQL(ctx, exec) == nil; !pgRepackOK {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgRepackDisabled",
"Unable to install pg_repack")
}
} else {
if pgRepackOK = pgrepack.DisableInPostgreSQL(ctx, exec) == nil; !pgRepackOK {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgRepackEnabled",
"Unable to disable pg_repack")
}
}
// Enabling PostGIS extensions is a one-way operation
// e.g., you can take a PostgresCluster and turn it into a PostGISCluster,
// but you cannot reverse the process, as that would potentially remove an extension
// that is being used by some database/tables
if cluster.Spec.PostGISVersion == "" {
postgisInstallOK = true
} else if postgisInstallOK = postgis.EnableInPostgreSQL(ctx, exec) == nil; !postgisInstallOK {
// TODO(benjaminjb): Investigate under what conditions postgis would fail install
r.Recorder.Event(cluster, corev1.EventTypeWarning, "PostGISDisabled",
"Unable to install PostGIS")
}
return postgres.CreateDatabasesInPostgreSQL(ctx, exec, sets.List(databases))
}
// Calculate a hash of the SQL that should be executed in PostgreSQL.
revision, err := safeHash32(func(hasher io.Writer) error {
// Discard log messages about executing SQL.
return create(logging.NewContext(ctx, logging.Discard()), func(
_ context.Context, stdin io.Reader, _, _ io.Writer, command ...string,
) error {
_, err := fmt.Fprint(hasher, command)
if err == nil && stdin != nil {
_, err = io.Copy(hasher, stdin)
}
return err
})
})
if err == nil && revision == cluster.Status.DatabaseRevision {
// The necessary SQL has already been applied; there's nothing more to do.
// TODO(cbandy): Give the user a way to trigger execution regardless.
// The value of an annotation could influence the hash, for example.
return nil
}
// Apply the necessary SQL and record its hash in cluster.Status. Include
// the hash in any log messages.
if err == nil {
log := logging.FromContext(ctx).WithValues("revision", revision)
err = errors.WithStack(create(logging.NewContext(ctx, log), podExecutor))
}
// K8SPG-472
if err == nil && pgStatMonitorOK && pgAuditOK && pgvectorOK && postgisInstallOK && pgRepackOK {
cluster.Status.DatabaseRevision = revision
}
return err
}
// reconcilePostgresUsers writes the objects necessary to manage users and their
// passwords in PostgreSQL.
func (r *Reconciler) reconcilePostgresUsers(
ctx context.Context, cluster *v1beta1.PostgresCluster, instances *observedInstances,
) error {
r.validatePostgresUsers(cluster)
users, secrets, err := r.reconcilePostgresUserSecrets(ctx, cluster)
if err == nil {
err = r.reconcilePostgresUsersInPostgreSQL(ctx, cluster, instances, users, secrets)
}
if err == nil {
// Copy PostgreSQL users and passwords into pgAdmin. This is here because
// reconcilePostgresUserSecrets is building a (default) PostgresUserSpec
// that is not in the PostgresClusterSpec. The freshly generated Secrets
// are available here, too.
err = r.reconcilePGAdminUsers(ctx, cluster, users, secrets)
}
return err
}
// validatePostgresUsers emits warnings when cluster.Spec.Users contains values
// that are no longer valid. NOTE(ratcheting) NOTE(validation)
// - https://docs.k8s.io/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-ratcheting
func (r *Reconciler) validatePostgresUsers(cluster *v1beta1.PostgresCluster) {
if len(cluster.Spec.Users) == 0 {
return
}
path := field.NewPath("spec", "users")
reComments := regexp.MustCompile(`(?:--|/[*]|[*]/)`)
rePassword := regexp.MustCompile(`(?i:PASSWORD)`)
for i := range cluster.Spec.Users {
errs := field.ErrorList{}
spec := cluster.Spec.Users[i]
if reComments.MatchString(spec.Options) {
errs = append(errs,
field.Invalid(path.Index(i).Child("options"), spec.Options,
"cannot contain comments"))
}
if rePassword.MatchString(spec.Options) {
errs = append(errs,
field.Invalid(path.Index(i).Child("options"), spec.Options,
"cannot assign password"))
}
if len(errs) > 0 {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "InvalidUser",
errs.ToAggregate().Error())
}
}
}
// +kubebuilder:rbac:groups="",resources="secrets",verbs={list}
// +kubebuilder:rbac:groups="",resources="secrets",verbs={create,delete,patch}
// reconcilePostgresUserSecrets writes Secrets for the PostgreSQL users
// specified in cluster and deletes existing Secrets that are not specified.
// It returns the user specifications it acted on (because defaults) and the
// Secrets it wrote.
func (r *Reconciler) reconcilePostgresUserSecrets(
ctx context.Context, cluster *v1beta1.PostgresCluster,
) (
[]v1beta1.PostgresUserSpec, map[string]*corev1.Secret, error,
) {
// When users are unspecified, create one user matching the cluster name if
// it is also a valid user name.
// TODO(cbandy): Move this to a defaulting (mutating admission) webhook to
// leverage regular validation.
specUsers := cluster.Spec.Users
if specUsers == nil {
path := field.NewPath("spec", "users").Index(0).Child("name")
reUser := regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
allErrors := field.ErrorList{}
// User names cannot be too long. PostgresCluster.Name is a DNS
// subdomain, so use len() to count characters.
if n := len(cluster.Name); n > 63 {
allErrors = append(allErrors,
field.Invalid(path, cluster.Name,
fmt.Sprintf("should be at most %d chars long", 63)))
}
// See v1beta1.PostgresRoleSpec validation markers.
if !reUser.MatchString(cluster.Name) {
allErrors = append(allErrors,
field.Invalid(path, cluster.Name,
fmt.Sprintf("should match '%s'", reUser)))
}
if len(allErrors) > 0 {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "InvalidUser",
allErrors.ToAggregate().Error())
} else {
identifier := v1beta1.PostgresIdentifier(cluster.Name)
specUsers = []v1beta1.PostgresUserSpec{{
Name: identifier,
Databases: []v1beta1.PostgresIdentifier{identifier},
}}
}
}
// Index user specifications by PostgreSQL user name.
userSpecs := make(map[string]*v1beta1.PostgresUserSpec, len(specUsers))
for i := range specUsers {
userSpecs[string(specUsers[i].Name)] = &specUsers[i]
}
secrets := &corev1.SecretList{}
selector, err := naming.AsSelector(naming.ClusterPostgresUsers(cluster.Name))
if err == nil {
err = errors.WithStack(
r.Client.List(ctx, secrets,
client.InNamespace(cluster.Namespace),
client.MatchingLabelsSelector{Selector: selector},
))
}
// Sorts the slice of secrets.Items based on secrets with identical labels
// If one secret has "pguser" in its name and the other does not, the
// one without "pguser" is moved to the front.
// If both secrets have "pguser" in their names or neither has "pguser", they
// are sorted by creation timestamp.
// If two secrets have the same creation timestamp, they are further sorted by name.
// The secret to be used by PGO is put at the end of the sorted slice.
sort.Slice(secrets.Items, func(i, j int) bool {
// Check if either secrets have "pguser" in their names
isIPgUser := strings.Contains(secrets.Items[i].Name, "pguser")
isJPgUser := strings.Contains(secrets.Items[j].Name, "pguser")
// If one secret has "pguser" and the other does not,
// move the one without "pguser" to the front
if isIPgUser && !isJPgUser {
return false
} else if !isIPgUser && isJPgUser {
return true
}
if secrets.Items[i].CreationTimestamp.Time.Equal(secrets.Items[j].CreationTimestamp.Time) {
// If the creation timestamps are equal, sort by name
return secrets.Items[i].Name < secrets.Items[j].Name
}
// If both secrets have "pguser" or neither have "pguser",
// sort by creation timestamp
return secrets.Items[i].CreationTimestamp.Time.After(secrets.Items[j].CreationTimestamp.Time)
})
// Index secrets by PostgreSQL user name and delete any that are not in the
// cluster spec. Keep track of the deprecated default secret to migrate its
// contents when the current secret doesn't exist.
var (
defaultSecret *corev1.Secret
defaultSecretName = naming.DeprecatedPostgresUserSecret(cluster).Name
defaultUserName string
userSecrets = make(map[string]*corev1.Secret, len(secrets.Items))
)
if err == nil {
for i := range secrets.Items {
secret := &secrets.Items[i]
secretUserName := secret.Labels[naming.LabelPostgresUser]
if _, specified := userSpecs[secretUserName]; specified {
if secret.Name == defaultSecretName {
defaultSecret = secret
defaultUserName = secretUserName
} else {
userSecrets[secretUserName] = secret
}
} else if err == nil {
err = errors.WithStack(r.deleteControlled(ctx, cluster, secret))
}
}
}
// Reconcile each PostgreSQL user in the cluster spec.
for userName, user := range userSpecs {
secret := userSecrets[userName]
if secret == nil && userName == defaultUserName {
// The current secret doesn't exist, so read from the deprecated
// default secret, if any.
secret = defaultSecret
}
if err == nil {
userSecrets[userName], err = r.generatePostgresUserSecret(cluster, user, secret)
}
if err == nil {
err = errors.WithStack(r.apply(ctx, userSecrets[userName]))
}
}
return specUsers, userSecrets, err
}
// reconcilePostgresUsersInPostgreSQL creates users inside of PostgreSQL and
// sets their options and database access as specified.
func (r *Reconciler) reconcilePostgresUsersInPostgreSQL(
ctx context.Context, cluster *v1beta1.PostgresCluster, instances *observedInstances,
specUsers []v1beta1.PostgresUserSpec, userSecrets map[string]*corev1.Secret,
) error {
const container = naming.ContainerDatabase
var podExecutor postgres.Executor
// Find the PostgreSQL instance that can execute SQL that writes system
// catalogs. When there is none, return early.
for _, instance := range instances.forCluster {
if terminating, known := instance.IsTerminating(); terminating || !known {
continue
}
if writable, known := instance.IsWritable(); !writable || !known {
continue
}
running, known := instance.IsRunning(container)
if running && known && len(instance.Pods) > 0 {
pod := instance.Pods[0]
ctx = logging.NewContext(ctx, logging.FromContext(ctx).WithValues("pod", pod.Name))
podExecutor = func(
ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
return r.PodExec(ctx, pod.Namespace, pod.Name, container, stdin, stdout, stderr, command...)
}
break
}
}
if podExecutor == nil {
return nil
}
// Calculate a hash of the SQL that should be executed in PostgreSQL.
verifiers := make(map[string]string, len(userSecrets))
for userName := range userSecrets {
verifiers[userName] = string(userSecrets[userName].Data["verifier"])
}
write := func(ctx context.Context, exec postgres.Executor) error {
return postgres.WriteUsersInPostgreSQL(ctx, cluster, exec, specUsers, verifiers)
}
revision, err := safeHash32(func(hasher io.Writer) error {
// Discard log messages about executing SQL.
return write(logging.NewContext(ctx, logging.Discard()), func(
_ context.Context, stdin io.Reader, _, _ io.Writer, command ...string,
) error {
_, err := fmt.Fprint(hasher, command)
if err == nil && stdin != nil {
_, err = io.Copy(hasher, stdin)
}
return err
})
})
if err == nil && revision == cluster.Status.UsersRevision {
// The necessary SQL has already been applied; there's nothing more to do.
// TODO(cbandy): Give the user a way to trigger execution regardless.
// The value of an annotation could influence the hash, for example.
return nil
}
// Apply the necessary SQL and record its hash in cluster.Status. Include
// the hash in any log messages.
if err == nil {
log := logging.FromContext(ctx).WithValues("revision", revision)
err = errors.WithStack(write(logging.NewContext(ctx, log), podExecutor))
}
if err == nil {
cluster.Status.UsersRevision = revision
}
return err
}
// +kubebuilder:rbac:groups="",resources="persistentvolumeclaims",verbs={create,patch}
// reconcilePostgresDataVolume writes the PersistentVolumeClaim for instance's
// PostgreSQL data volume.
func (r *Reconciler) reconcilePostgresDataVolume(
ctx context.Context, cluster *v1beta1.PostgresCluster,
instanceSpec *v1beta1.PostgresInstanceSetSpec, instance *appsv1.StatefulSet,
clusterVolumes []corev1.PersistentVolumeClaim, sourceCluster *v1beta1.PostgresCluster,
) (*corev1.PersistentVolumeClaim, error) {
labelMap := map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelInstanceSet: instanceSpec.Name,
naming.LabelInstance: instance.Name,
naming.LabelRole: naming.RolePostgresData,
naming.LabelData: naming.DataPostgres,
}
var pvc *corev1.PersistentVolumeClaim
existingPVCName, err := getPGPVCName(labelMap, clusterVolumes)
if err != nil {
return nil, errors.WithStack(err)
}
if existingPVCName != "" {
pvc = &corev1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.GetNamespace(),
Name: existingPVCName,
}}
} else {
pvc = &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstancePostgresDataVolume(instance)}
}
pvc.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim"))
// K8SPG-328: Keep this commented in case of conflicts.
// We don't want to delete PVCs if custom resource is deleted.
// err = errors.WithStack(r.setControllerReference(cluster, pvc))
pvc.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
instanceSpec.Metadata.GetAnnotationsOrNil())
pvc.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
instanceSpec.Metadata.GetLabelsOrNil(),
naming.WithPerconaLabels(labelMap, cluster.Name, "", cluster.Labels[naming.LabelVersion]),
)
pvc.Spec = instanceSpec.DataVolumeClaimSpec
// If a source cluster was provided and VolumeSnapshots are turned on in the source cluster and
// there is a VolumeSnapshot available for the source cluster that is ReadyToUse, use it as the
// source for the PVC. If there is an error when retrieving VolumeSnapshots, or no ReadyToUse
// snapshots were found, create a warning event, but continue creating PVC in the usual fashion.
if sourceCluster != nil && sourceCluster.Spec.Backups.Snapshots != nil && feature.Enabled(ctx, feature.VolumeSnapshots) {
snapshots, err := r.getSnapshotsForCluster(ctx, sourceCluster)
if err == nil {
snapshot := getLatestReadySnapshot(snapshots)
if snapshot != nil {
r.Recorder.Eventf(cluster, corev1.EventTypeNormal, "BootstrappingWithSnapshot",
"Snapshot found for %v; bootstrapping cluster with snapshot.", sourceCluster.Name)
pvc.Spec.DataSource = &corev1.TypedLocalObjectReference{
APIGroup: initialize.String("snapshot.storage.k8s.io"),
Kind: snapshot.Kind,
Name: snapshot.Name,
}
} else {
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "SnapshotNotFound",
"No ReadyToUse snapshots were found for %v; proceeding with typical restore process.", sourceCluster.Name)
}
} else {
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "SnapshotNotFound",
"Could not get snapshots for %v, proceeding with typical restore process.", sourceCluster.Name)
}
}
r.setVolumeSize(ctx, cluster, pvc, instanceSpec.Name)
// Clear any set limit before applying PVC. This is needed to allow the limit
// value to change later.
pvc.Spec.Resources.Limits = nil
if err == nil {
err = r.handlePersistentVolumeClaimError(cluster,
errors.WithStack(r.apply(ctx, pvc)))
}
return pvc, err
}
// setVolumeSize compares the potential sizes from the instance spec, status
// and limit and sets the appropriate current value.
func (r *Reconciler) setVolumeSize(ctx context.Context, cluster *v1beta1.PostgresCluster,
pvc *corev1.PersistentVolumeClaim, instanceSpecName string,
) {
log := logging.FromContext(ctx)
// Store the limit for this instance set. This value will not change below.
volumeLimitFromSpec := pvc.Spec.Resources.Limits.Storage()
// Capture the largest pgData volume size currently defined for a given instance set.
// This value will capture our desired update.
volumeRequestSize := pvc.Spec.Resources.Requests.Storage()
// If the request value is greater than the set limit, use the limit and issue
// a warning event. A limit of 0 is ignorned.
if !volumeLimitFromSpec.IsZero() &&
volumeRequestSize.Value() > volumeLimitFromSpec.Value() {
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "VolumeRequestOverLimit",
"pgData volume request (%v) for %s/%s is greater than set limit (%v). Limit value will be used.",
volumeRequestSize, cluster.Name, instanceSpecName, volumeLimitFromSpec)
pvc.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: *resource.NewQuantity(volumeLimitFromSpec.Value(), resource.BinarySI),
}
// Otherwise, if the limit is not set or the feature gate is not enabled, do not autogrow.
} else if !volumeLimitFromSpec.IsZero() && feature.Enabled(ctx, feature.AutoGrowVolumes) {
for i := range cluster.Status.InstanceSets {
if instanceSpecName == cluster.Status.InstanceSets[i].Name {
for _, dpv := range cluster.Status.InstanceSets[i].DesiredPGDataVolume {
if dpv != "" {
desiredRequest, err := resource.ParseQuantity(dpv)
if err == nil {
if desiredRequest.Value() > volumeRequestSize.Value() {
volumeRequestSize = &desiredRequest
}
} else {
log.Error(err, "Unable to parse volume request: "+dpv)
}
}
}
}
}
// If the volume request size is greater than or equal to the limit and the
// limit is not zero, update the request size to the limit value.
// If the user manually requests a lower limit that is smaller than the current
// or requested volume size, it will be ignored in favor of the limit value.
if volumeRequestSize.Value() >= volumeLimitFromSpec.Value() {
r.Recorder.Eventf(cluster, corev1.EventTypeNormal, "VolumeLimitReached",
"pgData volume(s) for %s/%s are at size limit (%v).", cluster.Name,
instanceSpecName, volumeLimitFromSpec)
// If the volume size request is greater than the limit, issue an
// additional event warning.
if volumeRequestSize.Value() > volumeLimitFromSpec.Value() {
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "DesiredVolumeAboveLimit",
"The desired size (%v) for the %s/%s pgData volume(s) is greater than the size limit (%v).",
volumeRequestSize, cluster.Name, instanceSpecName, volumeLimitFromSpec)
}
volumeRequestSize = volumeLimitFromSpec
}
pvc.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: *resource.NewQuantity(volumeRequestSize.Value(), resource.BinarySI),
}
}
}
// +kubebuilder:rbac:groups="",resources="persistentvolumeclaims",verbs={create,patch}
// reconcileTablespaceVolumes writes the PersistentVolumeClaims for instance's
// tablespace data volumes.
func (r *Reconciler) reconcileTablespaceVolumes(
ctx context.Context, cluster *v1beta1.PostgresCluster,
instanceSpec *v1beta1.PostgresInstanceSetSpec, instance *appsv1.StatefulSet,
clusterVolumes []corev1.PersistentVolumeClaim,
) (tablespaceVolumes []*corev1.PersistentVolumeClaim, err error) {
if !feature.Enabled(ctx, feature.TablespaceVolumes) {
return
}
if instanceSpec.TablespaceVolumes == nil {
return
}
for _, vol := range instanceSpec.TablespaceVolumes {
labelMap := map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelInstanceSet: instanceSpec.Name,
naming.LabelInstance: instance.Name,
naming.LabelRole: "tablespace",
naming.LabelData: vol.Name,
}
var pvc *corev1.PersistentVolumeClaim
existingPVCName, err := getPGPVCName(labelMap, clusterVolumes)
if err != nil {
return nil, errors.WithStack(err)
}
if existingPVCName != "" {
pvc = &corev1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.GetNamespace(),
Name: existingPVCName,
}}
} else {
pvc = &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstanceTablespaceDataVolume(instance, vol.Name)}
}
pvc.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim"))
// K8SPG-328: Keep this commented in case of conflicts.
// We don't want to delete PVCs if custom resource is deleted.
// err = errors.WithStack(r.setControllerReference(cluster, pvc))
pvc.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
instanceSpec.Metadata.GetAnnotationsOrNil())
pvc.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
instanceSpec.Metadata.GetLabelsOrNil(),
naming.WithPerconaLabels(labelMap, cluster.Name, "", cluster.Labels[naming.LabelVersion]),
)
pvc.Spec = vol.DataVolumeClaimSpec
if err == nil {
err = r.handlePersistentVolumeClaimError(cluster,
errors.WithStack(r.apply(ctx, pvc)))
}
if err != nil {
return nil, err
}
tablespaceVolumes = append(tablespaceVolumes, pvc)
}
return
}
// +kubebuilder:rbac:groups="",resources="persistentvolumeclaims",verbs={get}
// +kubebuilder:rbac:groups="",resources="persistentvolumeclaims",verbs={create,delete,patch}
// reconcilePostgresWALVolume writes the PersistentVolumeClaim for instance's
// PostgreSQL WAL volume.
func (r *Reconciler) reconcilePostgresWALVolume(
ctx context.Context, cluster *v1beta1.PostgresCluster,
instanceSpec *v1beta1.PostgresInstanceSetSpec, instance *appsv1.StatefulSet,
observed *Instance, clusterVolumes []corev1.PersistentVolumeClaim,
) (*corev1.PersistentVolumeClaim, error) {
labelMap := map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelInstanceSet: instanceSpec.Name,
naming.LabelInstance: instance.Name,
naming.LabelRole: naming.RolePostgresWAL,
naming.LabelData: naming.DataPostgres,
}
var pvc *corev1.PersistentVolumeClaim
existingPVCName, err := getPGPVCName(labelMap, clusterVolumes)
if err != nil {
return nil, errors.WithStack(err)
}
if existingPVCName != "" {
pvc = &corev1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.GetNamespace(),
Name: existingPVCName,
}}
} else {
pvc = &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstancePostgresWALVolume(instance)}
}
pvc.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim"))
if instanceSpec.WALVolumeClaimSpec == nil {
// No WAL volume is specified; delete the PVC safely if it exists. Check
// the client cache first using Get.
key := client.ObjectKeyFromObject(pvc)
err := errors.WithStack(r.Client.Get(ctx, key, pvc))
if err != nil {
return nil, client.IgnoreNotFound(err)
}
// The "StorageObjectInUseProtection" admission controller adds a
// finalizer to every PVC so that the "pvc-protection" controller can
// remove it safely. Return early when it is already scheduled for deletion.
// - https://docs.k8s.io/reference/access-authn-authz/admission-controllers/
if pvc.DeletionTimestamp != nil {
return nil, nil
}
// The WAL PVC exists and should be removed. Delete it only when WAL
// files are safely on their intended volume. The PVC will continue to
// exist until all Pods using it are also deleted.
// - https://docs.k8s.io/concepts/storage/persistent-volumes/#storage-object-in-use-protection
var walDirectory string
if observed != nil && len(observed.Pods) == 1 {
if running, known := observed.IsRunning(naming.ContainerDatabase); running && known {
// NOTE(cbandy): Despite the guard above, calling PodExec may still fail
// due to a missing or stopped container.
// This assumes that $PGDATA matches the configured PostgreSQL "data_directory".
var stdout bytes.Buffer
err = errors.WithStack(r.PodExec(
ctx, observed.Pods[0].Namespace, observed.Pods[0].Name, naming.ContainerDatabase,
nil, &stdout, nil, "bash", "-ceu", "--", `exec realpath "${PGDATA}/pg_wal"`))
walDirectory = strings.TrimRight(stdout.String(), "\n")
}
}
if err == nil && walDirectory == postgres.WALDirectory(cluster, instanceSpec) {
return nil, errors.WithStack(
client.IgnoreNotFound(r.deleteControlled(ctx, cluster, pvc)))
}
// The WAL PVC exists and might contain WAL files. There is no spec to
// reconcile toward so return early.
return pvc, err
}
// K8SPG-328: Keep this commented in case of conflicts.
// We don't want to delete PVCs if custom resource is deleted.
// err = errors.WithStack(r.setControllerReference(cluster, pvc))
pvc.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
instanceSpec.Metadata.GetAnnotationsOrNil())
pvc.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
instanceSpec.Metadata.GetLabelsOrNil(),
naming.WithPerconaLabels(labelMap, cluster.Name, "", cluster.Labels[naming.LabelVersion]),
)
pvc.Spec = *instanceSpec.WALVolumeClaimSpec
if err == nil {
err = r.handlePersistentVolumeClaimError(cluster,
errors.WithStack(r.apply(ctx, pvc)))
}
return pvc, err