@@ -221,10 +221,10 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
221
221
}
222
222
}
223
223
224
- // Make sure the standby secrets exist, if neccessary
225
- if err := r .ensureStandbySecrets (ctx , instance ); err != nil {
226
- r .recorder .Eventf (instance , "Warning" , "Error" , "failed to create standby secrets: %v" , err )
227
- return ctrl.Result {}, fmt .Errorf ("error while creating standby secrets: %w" , err )
224
+ // Make sure the postgres secrets exist, if neccessary
225
+ if err := r .ensurePostgresSecrets (ctx , instance ); err != nil {
226
+ r .recorder .Eventf (instance , "Warning" , "Error" , "failed to create postgres secrets: %v" , err )
227
+ return ctrl.Result {}, fmt .Errorf ("error while creating postgres secrets: %w" , err )
228
228
}
229
229
230
230
if instance .IsReplicationPrimary () {
@@ -735,6 +735,20 @@ func (r *PostgresReconciler) getZPostgresqlByLabels(ctx context.Context, matchin
735
735
return zpl .Items , nil
736
736
}
737
737
738
+ func (r * PostgresReconciler ) ensurePostgresSecrets (ctx context.Context , instance * pg.Postgres ) error {
739
+
740
+ if err := r .ensureStandbySecrets (ctx , instance ); err != nil {
741
+ return err
742
+ }
743
+
744
+ if err := r .ensureCloneSecrets (ctx , instance ); err != nil {
745
+ return err
746
+ }
747
+
748
+ return nil
749
+
750
+ }
751
+
738
752
func (r * PostgresReconciler ) ensureStandbySecrets (ctx context.Context , instance * pg.Postgres ) error {
739
753
if instance .IsReplicationPrimary () {
740
754
// nothing is configured, or we are the leader. nothing to do.
@@ -747,7 +761,7 @@ func (r *PostgresReconciler) ensureStandbySecrets(ctx context.Context, instance
747
761
}
748
762
749
763
// Check if secrets exist local in SERVICE Cluster
750
- localStandbySecretName := "standby ." + instance .ToPeripheralResourceName () + ".credentials"
764
+ localStandbySecretName := operatormanager . PostgresConfigReplicationUsername + " ." + instance .ToPeripheralResourceName () + ".credentials"
751
765
localSecretNamespace := instance .ToPeripheralResourceNamespace ()
752
766
localStandbySecret := & corev1.Secret {}
753
767
r .Log .Info ("checking for local standby secret" , "namespace" , localSecretNamespace , "name" , localStandbySecretName )
@@ -765,26 +779,77 @@ func (r *PostgresReconciler) ensureStandbySecrets(ctx context.Context, instance
765
779
766
780
r .Log .Info ("no local standby secret found, continuing to create one" )
767
781
782
+ remoteSecretNamespacedName := types.NamespacedName {
783
+ Namespace : instance .ObjectMeta .Namespace ,
784
+ Name : instance .Spec .PostgresConnection .ConnectionSecretName ,
785
+ }
786
+ return r .copySecrets (ctx , remoteSecretNamespacedName , instance , false )
787
+
788
+ }
789
+
790
+ func (r * PostgresReconciler ) ensureCloneSecrets (ctx context.Context , instance * pg.Postgres ) error {
791
+ if instance .Spec .PostgresRestore == nil {
792
+ // not a clone. nothing to do.
793
+ return nil
794
+ }
795
+
796
+ // Check if instance.Spec.PostgresConnectionInfo.ConnectionSecretName is defined
797
+ if instance .Spec .PostgresRestore .SourcePostgresID == "" {
798
+ return errors .New ("SourcePostgresID not configured" )
799
+ }
800
+
801
+ // Check if secrets exist local in SERVICE Cluster
802
+ localStandbySecretName := operatormanager .PostresConfigSuperUsername + "." + instance .ToPeripheralResourceName () + ".credentials"
803
+ localSecretNamespace := instance .ToPeripheralResourceNamespace ()
804
+ localStandbySecret := & corev1.Secret {}
805
+ r .Log .Info ("checking for local postgres secret" , "namespace" , localSecretNamespace , "name" , localStandbySecretName )
806
+ err := r .SvcClient .Get (ctx , types.NamespacedName {Namespace : localSecretNamespace , Name : localStandbySecretName }, localStandbySecret )
807
+
808
+ if err == nil {
809
+ r .Log .Info ("local postgres secret found, no action needed" )
810
+ return nil
811
+ }
812
+
813
+ // we got an error other than not found, so we cannot continue!
814
+ if ! apierrors .IsNotFound (err ) {
815
+ return fmt .Errorf ("error while fetching local stadnby secret from service cluster: %w" , err )
816
+ }
817
+
818
+ r .Log .Info ("no local postgres secret found, continuing to create one" )
819
+
820
+ remoteSecretName := strings .Replace (instance .ToUserPasswordsSecretName (), instance .Name , instance .Spec .PostgresRestore .SourcePostgresID , 1 ) // TODO this is hacky-wacky...
821
+ remoteSecretNamespacedName := types.NamespacedName {
822
+ Namespace : instance .ObjectMeta .Namespace ,
823
+ Name : remoteSecretName ,
824
+ }
825
+ return r .copySecrets (ctx , remoteSecretNamespacedName , instance , true )
826
+
827
+ }
828
+
829
+ func (r * PostgresReconciler ) copySecrets (ctx context.Context , sourceSecret types.NamespacedName , targetInstance * pg.Postgres , ignoreStandbyUser bool ) error {
768
830
// Check if secrets exist in remote CONTROL Cluster
769
- remoteSecretName := instance .Spec .PostgresConnection .ConnectionSecretName
770
- remoteSecretNamespace := instance .ObjectMeta .Namespace
771
831
remoteSecret := & corev1.Secret {}
772
- r .Log .Info ("fetching remote standby secret" , "namespace" , remoteSecretNamespace , "name" , remoteSecretName )
773
- if err := r .CtrlClient .Get (ctx , types. NamespacedName { Namespace : remoteSecretNamespace , Name : remoteSecretName } , remoteSecret ); err != nil {
832
+ r .Log .Info ("fetching remote postgres secret" , "namespace" , sourceSecret . Namespace , "name" , sourceSecret . Name )
833
+ if err := r .CtrlClient .Get (ctx , sourceSecret , remoteSecret ); err != nil {
774
834
// we cannot read the secret given in the configuration, so we cannot continue!
775
- return fmt .Errorf ("error while fetching remote standby secret from control plane: %w" , err )
835
+ return fmt .Errorf ("error while fetching remote postgres secret from control plane: %w" , err )
776
836
}
777
837
778
- // copy ALL secrets...
838
+ // copy all but the standby secrets...
779
839
for username := range remoteSecret .Data {
840
+ // check if we skip the standby user (e.g. to prevent old standby intances from connecting once a clone took over its sources ip/port)
841
+ if ignoreStandbyUser && username == operatormanager .PostgresConfigReplicationUsername {
842
+ continue
843
+ }
844
+
780
845
r .Log .Info ("creating local secret" , "username" , username )
781
846
782
- currentSecretName := strings .ReplaceAll (username , "_" , "-" ) + "." + instance .ToPeripheralResourceName () + ".credentials"
847
+ currentSecretName := strings .ReplaceAll (username , "_" , "-" ) + "." + targetInstance .ToPeripheralResourceName () + ".credentials"
783
848
postgresSecret := & corev1.Secret {
784
849
ObjectMeta : metav1.ObjectMeta {
785
850
Name : currentSecretName ,
786
- Namespace : localSecretNamespace ,
787
- Labels : map [string ]string (instance .ToZalandoPostgresqlMatchingLabels ()),
851
+ Namespace : targetInstance . ToPeripheralResourceNamespace () ,
852
+ Labels : map [string ]string (targetInstance .ToZalandoPostgresqlMatchingLabels ()),
788
853
},
789
854
Data : map [string ][]byte {
790
855
"username" : []byte (username ),
@@ -795,11 +860,9 @@ func (r *PostgresReconciler) ensureStandbySecrets(ctx context.Context, instance
795
860
if err := r .SvcClient .Create (ctx , postgresSecret ); err != nil {
796
861
return fmt .Errorf ("error while creating local secrets in service cluster: %w" , err )
797
862
}
798
- r .Log .Info ("created local secret" , "secret" , postgresSecret )
799
863
}
800
864
801
865
return nil
802
-
803
866
}
804
867
805
868
func (r * PostgresReconciler ) updatePatroniConfig (ctx context.Context , instance * pg.Postgres ) error {
0 commit comments