@@ -23,7 +23,6 @@ import (
2323 "k8s.io/apimachinery/pkg/api/meta"
2424 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26- "k8s.io/apimachinery/pkg/labels"
2726 utilerrors "k8s.io/apimachinery/pkg/util/errors"
2827 "sigs.k8s.io/controller-runtime/pkg/client"
2928 "sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -776,11 +775,6 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
776775 repo v1beta1.PGBackRestRepo , serviceAccountName string ,
777776 labels , annotations map [string ]string , opts ... string ) (* batchv1.JobSpec , error ) {
778777
779- selector , containerName , err := getPGBackRestExecSelector (postgresCluster , repo )
780- if err != nil {
781- return nil , errors .WithStack (err )
782- }
783-
784778 repoIndex := regexRepoIndex .FindString (repo .Name )
785779 cmdOpts := []string {
786780 "--stanza=" + pgbackrest .DefaultStanzaName ,
@@ -794,21 +788,31 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
794788 cmdOpts = append (cmdOpts , opts ... )
795789
796790 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- },
806791 Image : config .PGBackRestContainerImage (postgresCluster ),
807792 ImagePullPolicy : postgresCluster .Spec .ImagePullPolicy ,
808793 Name : naming .PGBackRestRepoContainerName ,
809794 SecurityContext : initialize .RestrictedSecurityContext (),
810795 }
811796
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+
812816 if postgresCluster .Spec .Backups .PGBackRest .Jobs != nil {
813817 container .Resources = postgresCluster .Spec .Backups .PGBackRest .Jobs .Resources
814818 }
@@ -862,10 +866,13 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
862866 jobSpec .Template .Spec .ImagePullSecrets = postgresCluster .Spec .ImagePullSecrets
863867
864868 // add pgBackRest configs to template
865- if containerName == naming . PGBackRestRepoContainerName {
869+ if repo . Volume != nil {
866870 pgbackrest .AddConfigToRepoPod (postgresCluster , & jobSpec .Template .Spec )
867871 } 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 )
869876 }
870877
871878 return jobSpec , nil
@@ -2033,8 +2040,6 @@ func (r *Reconciler) reconcilePGBackRestConfig(ctx context.Context,
20332040 repoHostName , configHash , serviceName , serviceNamespace string ,
20342041 instanceNames []string ) error {
20352042
2036- log := logging .FromContext (ctx ).WithValues ("reconcileResource" , "repoConfig" )
2037-
20382043 backrestConfig , err := pgbackrest .CreatePGBackRestConfigMapIntent (ctx , postgresCluster , repoHostName ,
20392044 configHash , serviceName , serviceNamespace , instanceNames )
20402045 if err != nil {
@@ -2048,12 +2053,6 @@ func (r *Reconciler) reconcilePGBackRestConfig(ctx context.Context,
20482053 return errors .WithStack (err )
20492054 }
20502055
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-
20572056 return nil
20582057}
20592058
@@ -2547,11 +2546,15 @@ func (r *Reconciler) reconcileReplicaCreateBackup(ctx context.Context,
25472546 replicaRepoReady = (condition .Status == metav1 .ConditionTrue )
25482547 }
25492548
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 )
2549+ // TODO: Since we now only exec into the repo host when backing up to a local volume and
2550+ // run the backup in the job pod when backing up to a cloud-based repo, we should consider
2551+ // using a different value than the container name for the "pgbackrest-config" annotation
2552+ // that we attach to these backups
2553+ var containerName string
2554+ if replicaCreateRepo .Volume != nil {
2555+ containerName = naming .PGBackRestRepoContainerName
2556+ } else {
2557+ containerName = naming .ContainerDatabase
25552558 }
25562559
25572560 // determine if the dedicated repository host is ready using the repo host ready status
@@ -2603,10 +2606,10 @@ func (r *Reconciler) reconcileReplicaCreateBackup(ctx context.Context,
26032606 }
26042607 }
26052608
2606- dedicatedEnabled := pgbackrest .RepoHostVolumeDefined (postgresCluster )
26072609 // return if no job has been created and the replica repo or the dedicated
26082610 // repo host is not ready
2609- if job == nil && ((dedicatedEnabled && ! dedicatedRepoReady ) || ! replicaRepoReady ) {
2611+ if job == nil && ((pgbackrest .RepoHostVolumeDefined (postgresCluster ) && ! dedicatedRepoReady ) ||
2612+ ! replicaRepoReady ) {
26102613 return nil
26112614 }
26122615
@@ -2817,27 +2820,6 @@ func (r *Reconciler) reconcileStanzaCreate(ctx context.Context,
28172820 return false , nil
28182821}
28192822
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-
28412823// getRepoHostStatus is responsible for returning the pgBackRest status for the
28422824// provided pgBackRest repository host
28432825func getRepoHostStatus (repoHost * appsv1.StatefulSet ) * v1beta1.RepoHostStatus {
0 commit comments