@@ -23,7 +23,6 @@ import (
23
23
"k8s.io/apimachinery/pkg/api/meta"
24
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26
- "k8s.io/apimachinery/pkg/labels"
27
26
utilerrors "k8s.io/apimachinery/pkg/util/errors"
28
27
"sigs.k8s.io/controller-runtime/pkg/client"
29
28
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -774,12 +773,7 @@ func (r *Reconciler) generateRepoVolumeIntent(postgresCluster *v1beta1.PostgresC
774
773
// generateBackupJobSpecIntent generates a JobSpec for a pgBackRest backup job
775
774
func generateBackupJobSpecIntent (ctx context.Context , postgresCluster * v1beta1.PostgresCluster ,
776
775
repo v1beta1.PGBackRestRepo , serviceAccountName string ,
777
- labels , annotations map [string ]string , opts ... string ) (* batchv1.JobSpec , error ) {
778
-
779
- selector , containerName , err := getPGBackRestExecSelector (postgresCluster , repo )
780
- if err != nil {
781
- return nil , errors .WithStack (err )
782
- }
776
+ labels , annotations map [string ]string , opts ... string ) * batchv1.JobSpec {
783
777
784
778
repoIndex := regexRepoIndex .FindString (repo .Name )
785
779
cmdOpts := []string {
@@ -794,21 +788,31 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
794
788
cmdOpts = append (cmdOpts , opts ... )
795
789
796
790
container := corev1.Container {
797
- Command : []string {"/opt/crunchy/bin/pgbackrest" },
798
- Env : []corev1.EnvVar {
799
- {Name : "COMMAND" , Value : "backup" },
800
- {Name : "COMMAND_OPTS" , Value : strings .Join (cmdOpts , " " )},
801
- {Name : "COMPARE_HASH" , Value : "true" },
802
- {Name : "CONTAINER" , Value : containerName },
803
- {Name : "NAMESPACE" , Value : postgresCluster .GetNamespace ()},
804
- {Name : "SELECTOR" , Value : selector .String ()},
805
- },
806
791
Image : config .PGBackRestContainerImage (postgresCluster ),
807
792
ImagePullPolicy : postgresCluster .Spec .ImagePullPolicy ,
808
793
Name : naming .PGBackRestRepoContainerName ,
809
794
SecurityContext : initialize .RestrictedSecurityContext (),
810
795
}
811
796
797
+ // If the repo that we are backing up to is a local volume, we will configure
798
+ // the job to use the pgbackrest go binary to exec into the repo host and run
799
+ // the backup. If the repo is a cloud-based repo, we will run the pgbackrest
800
+ // backup command directly in the job pod.
801
+ if repo .Volume != nil {
802
+ container .Command = []string {"/opt/crunchy/bin/pgbackrest" }
803
+ container .Env = []corev1.EnvVar {
804
+ {Name : "COMMAND" , Value : "backup" },
805
+ {Name : "COMMAND_OPTS" , Value : strings .Join (cmdOpts , " " )},
806
+ {Name : "COMPARE_HASH" , Value : "true" },
807
+ {Name : "CONTAINER" , Value : naming .PGBackRestRepoContainerName },
808
+ {Name : "NAMESPACE" , Value : postgresCluster .GetNamespace ()},
809
+ {Name : "SELECTOR" , Value : naming .PGBackRestDedicatedSelector (postgresCluster .GetName ()).String ()},
810
+ }
811
+ } else {
812
+ container .Command = []string {"/bin/pgbackrest" , "backup" }
813
+ container .Command = append (container .Command , cmdOpts ... )
814
+ }
815
+
812
816
if postgresCluster .Spec .Backups .PGBackRest .Jobs != nil {
813
817
container .Resources = postgresCluster .Spec .Backups .PGBackRest .Jobs .Resources
814
818
}
@@ -862,13 +866,16 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
862
866
jobSpec .Template .Spec .ImagePullSecrets = postgresCluster .Spec .ImagePullSecrets
863
867
864
868
// add pgBackRest configs to template
865
- if containerName == naming . PGBackRestRepoContainerName {
869
+ if repo . Volume != nil {
866
870
pgbackrest .AddConfigToRepoPod (postgresCluster , & jobSpec .Template .Spec )
867
871
} else {
868
- pgbackrest .AddConfigToInstancePod (postgresCluster , & jobSpec .Template .Spec )
872
+ // If we are doing a cloud repo backup, we need to give pgbackrest proper permissions
873
+ // to read certificate files
874
+ jobSpec .Template .Spec .SecurityContext = postgres .PodSecurityContext (postgresCluster )
875
+ pgbackrest .AddConfigToCloudBackupJob (postgresCluster , & jobSpec .Template )
869
876
}
870
877
871
- return jobSpec , nil
878
+ return jobSpec
872
879
}
873
880
874
881
// +kubebuilder:rbac:groups="",resources="configmaps",verbs={delete,list}
@@ -2027,14 +2034,12 @@ func (r *Reconciler) copyConfigurationResources(ctx context.Context, cluster,
2027
2034
return nil
2028
2035
}
2029
2036
2030
- // reconcilePGBackRestConfig is responsible for reconciling the pgBackRest ConfigMaps and Secrets .
2037
+ // reconcilePGBackRestConfig is responsible for reconciling the pgBackRest ConfigMaps.
2031
2038
func (r * Reconciler ) reconcilePGBackRestConfig (ctx context.Context ,
2032
2039
postgresCluster * v1beta1.PostgresCluster ,
2033
2040
repoHostName , configHash , serviceName , serviceNamespace string ,
2034
2041
instanceNames []string ) error {
2035
2042
2036
- log := logging .FromContext (ctx ).WithValues ("reconcileResource" , "repoConfig" )
2037
-
2038
2043
backrestConfig , err := pgbackrest .CreatePGBackRestConfigMapIntent (ctx , postgresCluster , repoHostName ,
2039
2044
configHash , serviceName , serviceNamespace , instanceNames )
2040
2045
if err != nil {
@@ -2048,12 +2053,6 @@ func (r *Reconciler) reconcilePGBackRestConfig(ctx context.Context,
2048
2053
return errors .WithStack (err )
2049
2054
}
2050
2055
2051
- repoHostConfigured := pgbackrest .RepoHostVolumeDefined (postgresCluster )
2052
- if ! repoHostConfigured {
2053
- log .V (1 ).Info ("skipping SSH reconciliation, no repo hosts configured" )
2054
- return nil
2055
- }
2056
-
2057
2056
return nil
2058
2057
}
2059
2058
@@ -2455,11 +2454,8 @@ func (r *Reconciler) reconcileManualBackup(ctx context.Context,
2455
2454
backupJob .Labels = labels
2456
2455
backupJob .Annotations = annotations
2457
2456
2458
- spec , err := generateBackupJobSpecIntent (ctx , postgresCluster , repo ,
2457
+ spec := generateBackupJobSpecIntent (ctx , postgresCluster , repo ,
2459
2458
serviceAccount .GetName (), labels , annotations , backupOpts ... )
2460
- if err != nil {
2461
- return errors .WithStack (err )
2462
- }
2463
2459
2464
2460
backupJob .Spec = * spec
2465
2461
@@ -2547,11 +2543,15 @@ func (r *Reconciler) reconcileReplicaCreateBackup(ctx context.Context,
2547
2543
replicaRepoReady = (condition .Status == metav1 .ConditionTrue )
2548
2544
}
2549
2545
2550
- // get pod name and container name as needed to exec into the proper pod and create
2551
- // the pgBackRest backup
2552
- _ , containerName , err := getPGBackRestExecSelector (postgresCluster , replicaCreateRepo )
2553
- if err != nil {
2554
- return errors .WithStack (err )
2546
+ // TODO: Since we now only exec into the repo host when backing up to a local volume and
2547
+ // run the backup in the job pod when backing up to a cloud-based repo, we should consider
2548
+ // using a different value than the container name for the "pgbackrest-config" annotation
2549
+ // that we attach to these backups
2550
+ var containerName string
2551
+ if replicaCreateRepo .Volume != nil {
2552
+ containerName = naming .PGBackRestRepoContainerName
2553
+ } else {
2554
+ containerName = naming .ContainerDatabase
2555
2555
}
2556
2556
2557
2557
// determine if the dedicated repository host is ready using the repo host ready status
@@ -2603,10 +2603,10 @@ func (r *Reconciler) reconcileReplicaCreateBackup(ctx context.Context,
2603
2603
}
2604
2604
}
2605
2605
2606
- dedicatedEnabled := pgbackrest .RepoHostVolumeDefined (postgresCluster )
2607
2606
// return if no job has been created and the replica repo or the dedicated
2608
2607
// repo host is not ready
2609
- if job == nil && ((dedicatedEnabled && ! dedicatedRepoReady ) || ! replicaRepoReady ) {
2608
+ if job == nil && ((pgbackrest .RepoHostVolumeDefined (postgresCluster ) && ! dedicatedRepoReady ) ||
2609
+ ! replicaRepoReady ) {
2610
2610
return nil
2611
2611
}
2612
2612
@@ -2631,11 +2631,8 @@ func (r *Reconciler) reconcileReplicaCreateBackup(ctx context.Context,
2631
2631
backupJob .Labels = labels
2632
2632
backupJob .Annotations = annotations
2633
2633
2634
- spec , err := generateBackupJobSpecIntent (ctx , postgresCluster , replicaCreateRepo ,
2634
+ spec := generateBackupJobSpecIntent (ctx , postgresCluster , replicaCreateRepo ,
2635
2635
serviceAccount .GetName (), labels , annotations )
2636
- if err != nil {
2637
- return errors .WithStack (err )
2638
- }
2639
2636
2640
2637
backupJob .Spec = * spec
2641
2638
@@ -2817,27 +2814,6 @@ func (r *Reconciler) reconcileStanzaCreate(ctx context.Context,
2817
2814
return false , nil
2818
2815
}
2819
2816
2820
- // getPGBackRestExecSelector returns a selector and container name that allows the proper
2821
- // Pod (along with a specific container within it) to be found within the Kubernetes
2822
- // cluster as needed to exec into the container and run a pgBackRest command.
2823
- func getPGBackRestExecSelector (postgresCluster * v1beta1.PostgresCluster ,
2824
- repo v1beta1.PGBackRestRepo ) (labels.Selector , string , error ) {
2825
-
2826
- var err error
2827
- var podSelector labels.Selector
2828
- var containerName string
2829
-
2830
- if repo .Volume != nil {
2831
- podSelector = naming .PGBackRestDedicatedSelector (postgresCluster .GetName ())
2832
- containerName = naming .PGBackRestRepoContainerName
2833
- } else {
2834
- podSelector , err = naming .AsSelector (naming .ClusterPrimary (postgresCluster .GetName ()))
2835
- containerName = naming .ContainerDatabase
2836
- }
2837
-
2838
- return podSelector , containerName , err
2839
- }
2840
-
2841
2817
// getRepoHostStatus is responsible for returning the pgBackRest status for the
2842
2818
// provided pgBackRest repository host
2843
2819
func getRepoHostStatus (repoHost * appsv1.StatefulSet ) * v1beta1.RepoHostStatus {
@@ -3082,11 +3058,8 @@ func (r *Reconciler) reconcilePGBackRestCronJob(
3082
3058
// set backup type (i.e. "full", "diff", "incr")
3083
3059
backupOpts := []string {"--type=" + backupType }
3084
3060
3085
- jobSpec , err := generateBackupJobSpecIntent (ctx , cluster , repo ,
3061
+ jobSpec := generateBackupJobSpecIntent (ctx , cluster , repo ,
3086
3062
serviceAccount .GetName (), labels , annotations , backupOpts ... )
3087
- if err != nil {
3088
- return errors .WithStack (err )
3089
- }
3090
3063
3091
3064
// Suspend cronjobs when shutdown or read-only. Any jobs that have already
3092
3065
// started will continue.
@@ -3119,7 +3092,7 @@ func (r *Reconciler) reconcilePGBackRestCronJob(
3119
3092
3120
3093
// set metadata
3121
3094
pgBackRestCronJob .SetGroupVersionKind (batchv1 .SchemeGroupVersion .WithKind ("CronJob" ))
3122
- err = errors .WithStack (r .setControllerReference (cluster , pgBackRestCronJob ))
3095
+ err : = errors .WithStack (r .setControllerReference (cluster , pgBackRestCronJob ))
3123
3096
3124
3097
if err == nil {
3125
3098
err = r .apply (ctx , pgBackRestCronJob )
0 commit comments