-
Notifications
You must be signed in to change notification settings - Fork 606
/
Copy pathpostgres.go
851 lines (727 loc) · 29.1 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
/*
Copyright 2021 - 2024 Crunchy Data Solutions, Inc.
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 postgrescluster
import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/url"
"regexp"
"strings"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
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/crunchydata/postgres-operator/internal/initialize"
"github.com/crunchydata/postgres-operator/internal/logging"
"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/internal/pgaudit"
"github.com/crunchydata/postgres-operator/internal/postgis"
"github.com/crunchydata/postgres-operator/internal/postgres"
pgpassword "github.com/crunchydata/postgres-operator/internal/postgres/password"
"github.com/crunchydata/postgres-operator/internal/util"
"github.com/crunchydata/postgres-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)
intent := &corev1.Secret{ObjectMeta: naming.PostgresUserSecret(cluster, username)}
intent.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret"))
initialize.ByteMap(&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())
// The R2DBC driver requires a different URI scheme than the JDBC driver
// - https://r2dbc.io/spec/1.0.0.RELEASE/spec/html/#overview.connection.url
intent.Data["r2dbc-uri"] = []byte((&url.URL{
Scheme: "r2dbc: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.Data["pgbouncer-r2dbc-uri"] = []byte((&url.URL{
Scheme: "r2dbc: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(),
map[string]string{
naming.LabelCluster: cluster.Name,
naming.LabelRole: naming.RolePostgresUser,
naming.LabelPostgresUser: username,
})
err := errors.WithStack(r.setControllerReference(cluster, intent))
return intent, err
}
// 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
// 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(
_ context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
return r.PodExec(pod.Namespace, pod.Name, container, stdin, stdout, stderr, command...)
}
// Gather the list of database that should exist in PostgreSQL.
databases := sets.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.
var pgAuditOK, postgisInstallOK bool
create := func(ctx context.Context, exec postgres.Executor) error {
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")
}
// 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, databases.List())
}
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))
}
if err == nil && pgAuditOK && postgisInstallOK {
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 {
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
}
// +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},
))
}
// 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(
_ context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
return r.PodExec(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, 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,
) (*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"))
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(),
labelMap,
)
pvc.Spec = instanceSpec.DataVolumeClaimSpec
if err == nil {
err = r.handlePersistentVolumeClaimError(cluster,
errors.WithStack(r.apply(ctx, pvc)))
}
return pvc, err
}
// +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 !util.DefaultMutableFeatureGate.Enabled(util.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"))
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(),
labelMap,
)
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(
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
}
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(),
labelMap,
)
pvc.Spec = *instanceSpec.WALVolumeClaimSpec
if err == nil {
err = r.handlePersistentVolumeClaimError(cluster,
errors.WithStack(r.apply(ctx, pvc)))
}
return pvc, err
}
// reconcileDatabaseInitSQL runs custom SQL files in the database. When
// DatabaseInitSQL is defined, the function will find the primary pod and run
// SQL from the defined ConfigMap
func (r *Reconciler) reconcileDatabaseInitSQL(ctx context.Context,
cluster *v1beta1.PostgresCluster, instances *observedInstances) error {
log := logging.FromContext(ctx)
// Spec is not defined, unset status and return
if cluster.Spec.DatabaseInitSQL == nil {
// If database init sql is not requested, we will always expect the
// status to be nil
cluster.Status.DatabaseInitSQL = nil
return nil
}
// Spec is defined but status is already set, return
if cluster.Status.DatabaseInitSQL != nil {
return nil
}
// Based on the previous checks, the user wants to run sql in the database.
// Check the provided ConfigMap name and key to ensure the a string
// exists in the ConfigMap data
var (
err error
data string
)
getDataFromConfigMap := func() (string, error) {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: cluster.Spec.DatabaseInitSQL.Name,
Namespace: cluster.Namespace,
},
}
err := r.Client.Get(ctx, client.ObjectKeyFromObject(cm), cm)
if err != nil {
return "", err
}
key := cluster.Spec.DatabaseInitSQL.Key
if _, ok := cm.Data[key]; !ok {
err := errors.Errorf("ConfigMap did not contain expected key: %s", key)
return "", err
}
return cm.Data[key], nil
}
if data, err = getDataFromConfigMap(); err != nil {
log.Error(err, "Could not get data from ConfigMap",
"ConfigMap", cluster.Spec.DatabaseInitSQL.Name,
"Key", cluster.Spec.DatabaseInitSQL.Key)
return err
}
// Now that we have the data provided by the user. We can check for a
// writable pod and get the podExecutor for the pod's database container
var podExecutor postgres.Executor
pod, _ := instances.writablePod(naming.ContainerDatabase)
if pod == nil {
log.V(1).Info("Could not find a pod with a writable database container.")
return nil
}
podExecutor = func(
_ context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
return r.PodExec(pod.Namespace, pod.Name, naming.ContainerDatabase, stdin, stdout, stderr, command...)
}
// A writable pod executor has been found and we have the sql provided by
// the user. Setup a write function to execute the sql using the podExecutor
write := func(ctx context.Context, exec postgres.Executor) error {
stdout, stderr, err := exec.Exec(ctx, strings.NewReader(data), map[string]string{})
log.V(1).Info("applied init SQL", "stdout", stdout, "stderr", stderr)
return err
}
// Update the logger to include fields from the user provided ResourceRef
log = log.WithValues(
"name", cluster.Spec.DatabaseInitSQL.Name,
"key", cluster.Spec.DatabaseInitSQL.Key,
)
// Write SQL to database using the podExecutor
err = errors.WithStack(write(logging.NewContext(ctx, log), podExecutor))
// If the podExec returns with exit code 0 the write is considered a
// success, keep track of the ConfigMap using a status. This helps to
// ensure SQL doesn't get run again. SQL can be run again if the
// status is lost and the DatabaseInitSQL field exists in the spec.
if err == nil {
status := cluster.Spec.DatabaseInitSQL.Name
cluster.Status.DatabaseInitSQL = &status
}
return err
}