@@ -23,7 +23,6 @@ import (
2323"k8s.io/apimachinery/pkg/api/meta"
2424metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26- "k8s.io/apimachinery/pkg/labels"
2726utilerrors "k8s.io/apimachinery/pkg/util/errors"
2827"sigs.k8s.io/controller-runtime/pkg/client"
2928"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -774,12 +773,7 @@ func (r *Reconciler) generateRepoVolumeIntent(postgresCluster *v1beta1.PostgresC
774773// generateBackupJobSpecIntent generates a JobSpec for a pgBackRest backup job
775774func generateBackupJobSpecIntent (ctx context.Context , postgresCluster * v1beta1.PostgresCluster ,
776775repo 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 {
783777
784778repoIndex := regexRepoIndex .FindString (repo .Name )
785779cmdOpts := []string {
@@ -794,21 +788,31 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
794788cmdOpts = append (cmdOpts , opts ... )
795789
796790container := 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- },
806791Image : config .PGBackRestContainerImage (postgresCluster ),
807792ImagePullPolicy : postgresCluster .Spec .ImagePullPolicy ,
808793Name : naming .PGBackRestRepoContainerName ,
809794SecurityContext : 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+
812816if postgresCluster .Spec .Backups .PGBackRest .Jobs != nil {
813817container .Resources = postgresCluster .Spec .Backups .PGBackRest .Jobs .Resources
814818}
@@ -862,13 +866,16 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
862866jobSpec .Template .Spec .ImagePullSecrets = postgresCluster .Spec .ImagePullSecrets
863867
864868// add pgBackRest configs to template
865- if containerName == naming . PGBackRestRepoContainerName {
869+ if repo . Volume != nil {
866870pgbackrest .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
871- return jobSpec , nil
878+ return jobSpec
872879}
873880
874881// +kubebuilder:rbac:groups="",resources="configmaps",verbs={delete,list}
@@ -2027,14 +2034,12 @@ func (r *Reconciler) copyConfigurationResources(ctx context.Context, cluster,
20272034return nil
20282035}
20292036
2030- // reconcilePGBackRestConfig is responsible for reconciling the pgBackRest ConfigMaps and Secrets .
2037+ // reconcilePGBackRestConfig is responsible for reconciling the pgBackRest ConfigMaps.
20312038func (r * Reconciler ) reconcilePGBackRestConfig (ctx context.Context ,
20322039postgresCluster * v1beta1.PostgresCluster ,
20332040repoHostName , configHash , serviceName , serviceNamespace string ,
20342041instanceNames []string ) error {
20352042
2036- log := logging .FromContext (ctx ).WithValues ("reconcileResource" , "repoConfig" )
2037-
20382043backrestConfig , err := pgbackrest .CreatePGBackRestConfigMapIntent (ctx , postgresCluster , repoHostName ,
20392044configHash , serviceName , serviceNamespace , instanceNames )
20402045if err != nil {
@@ -2048,12 +2053,6 @@ func (r *Reconciler) reconcilePGBackRestConfig(ctx context.Context,
20482053return 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-
20572056return nil
20582057}
20592058
@@ -2455,11 +2454,8 @@ func (r *Reconciler) reconcileManualBackup(ctx context.Context,
24552454backupJob .Labels = labels
24562455backupJob .Annotations = annotations
24572456
2458- spec , err := generateBackupJobSpecIntent (ctx , postgresCluster , repo ,
2457+ spec := generateBackupJobSpecIntent (ctx , postgresCluster , repo ,
24592458serviceAccount .GetName (), labels , annotations , backupOpts ... )
2460- if err != nil {
2461- return errors .WithStack (err )
2462- }
24632459
24642460backupJob .Spec = * spec
24652461
@@ -2547,11 +2543,15 @@ func (r *Reconciler) reconcileReplicaCreateBackup(ctx context.Context,
25472543replicaRepoReady = (condition .Status == metav1 .ConditionTrue )
25482544}
25492545
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
25552555}
25562556
25572557// 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,
26032603}
26042604}
26052605
2606- dedicatedEnabled := pgbackrest .RepoHostVolumeDefined (postgresCluster )
26072606// return if no job has been created and the replica repo or the dedicated
26082607// repo host is not ready
2609- if job == nil && ((dedicatedEnabled && ! dedicatedRepoReady ) || ! replicaRepoReady ) {
2608+ if job == nil && ((pgbackrest .RepoHostVolumeDefined (postgresCluster ) && ! dedicatedRepoReady ) ||
2609+ ! replicaRepoReady ) {
26102610return nil
26112611}
26122612
@@ -2631,11 +2631,8 @@ func (r *Reconciler) reconcileReplicaCreateBackup(ctx context.Context,
26312631backupJob .Labels = labels
26322632backupJob .Annotations = annotations
26332633
2634- spec , err := generateBackupJobSpecIntent (ctx , postgresCluster , replicaCreateRepo ,
2634+ spec := generateBackupJobSpecIntent (ctx , postgresCluster , replicaCreateRepo ,
26352635serviceAccount .GetName (), labels , annotations )
2636- if err != nil {
2637- return errors .WithStack (err )
2638- }
26392636
26402637backupJob .Spec = * spec
26412638
@@ -2817,27 +2814,6 @@ func (r *Reconciler) reconcileStanzaCreate(ctx context.Context,
28172814return false , nil
28182815}
28192816
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-
28412817// getRepoHostStatus is responsible for returning the pgBackRest status for the
28422818// provided pgBackRest repository host
28432819func getRepoHostStatus (repoHost * appsv1.StatefulSet ) * v1beta1.RepoHostStatus {
@@ -3082,11 +3058,8 @@ func (r *Reconciler) reconcilePGBackRestCronJob(
30823058// set backup type (i.e. "full", "diff", "incr")
30833059backupOpts := []string {"--type=" + backupType }
30843060
3085- jobSpec , err := generateBackupJobSpecIntent (ctx , cluster , repo ,
3061+ jobSpec := generateBackupJobSpecIntent (ctx , cluster , repo ,
30863062serviceAccount .GetName (), labels , annotations , backupOpts ... )
3087- if err != nil {
3088- return errors .WithStack (err )
3089- }
30903063
30913064// Suspend cronjobs when shutdown or read-only. Any jobs that have already
30923065// started will continue.
@@ -3119,7 +3092,7 @@ func (r *Reconciler) reconcilePGBackRestCronJob(
31193092
31203093// set metadata
31213094pgBackRestCronJob .SetGroupVersionKind (batchv1 .SchemeGroupVersion .WithKind ("CronJob" ))
3122- err = errors .WithStack (r .setControllerReference (cluster , pgBackRestCronJob ))
3095+ err : = errors .WithStack (r .setControllerReference (cluster , pgBackRestCronJob ))
31233096
31243097if err == nil {
31253098err = r .apply (ctx , pgBackRestCronJob )
0 commit comments