@@ -719,11 +719,59 @@ func (r *PostgresReconciler) updatePatroniConfig(ctx context.Context, instance *
719
719
return err
720
720
}
721
721
if len (pods .Items ) == 0 {
722
- r .Log .Info ("no leader pod ready, requeuing" )
723
- // TODO return proper error
724
- return errors .New ("no leader pods found" )
722
+ r .Log .Info ("no leader pod found, selecting all spilo pods as a last resort (might be ok if it is still creating)" )
723
+
724
+ err = r .updatePatroniConfigOnAllPods (ctx , instance )
725
+ if err != nil {
726
+ r .Log .Info ("updating patroni config failed, got one or more errors" )
727
+ return err
728
+ }
729
+ return nil
725
730
}
726
731
podIP := pods .Items [0 ].Status .PodIP
732
+
733
+ return r .httpPatchPatroni (ctx , instance , podIP )
734
+ }
735
+
736
+ func (r * PostgresReconciler ) updatePatroniConfigOnAllPods (ctx context.Context , instance * pg.Postgres ) error {
737
+ pods := & corev1.PodList {}
738
+ opts := []client.ListOption {
739
+ client .InNamespace (instance .ToPeripheralResourceNamespace ()),
740
+ client.MatchingLabels {"application" : "spilo" },
741
+ }
742
+ if err := r .SvcClient .List (ctx , pods , opts ... ); err != nil {
743
+ r .Log .Info ("could not query pods, requeuing" )
744
+ return err
745
+ }
746
+
747
+ if len (pods .Items ) == 0 {
748
+ r .Log .Info ("no spilo pods found at all, requeueing" )
749
+ return errors .New ("no spilo pods found at all" )
750
+ }
751
+
752
+ // iterate all spilo pods
753
+ var lastErr error
754
+ for _ , pod := range pods .Items {
755
+ pod := pod // pin!
756
+ podIP := pod .Status .PodIP
757
+ if err := r .httpPatchPatroni (ctx , instance , podIP ); err != nil {
758
+ lastErr = err
759
+ r .Log .Info ("failed to update pod" )
760
+ }
761
+ }
762
+ if lastErr != nil {
763
+ r .Log .Info ("updating patroni config failed, got one or more errors" )
764
+ return lastErr
765
+ }
766
+ r .Log .Info ("updating patroni config succeeded" )
767
+ return nil
768
+ }
769
+
770
+ func (r * PostgresReconciler ) httpPatchPatroni (ctx context.Context , instance * pg.Postgres , podIP string ) error {
771
+ if podIP == "" {
772
+ return errors .New ("podIP must not be empty" )
773
+ }
774
+
727
775
podPort := "8008"
728
776
path := "config"
729
777
@@ -775,12 +823,14 @@ func (r *PostgresReconciler) updatePatroniConfig(ctx context.Context, instance *
775
823
776
824
req , err := http .NewRequestWithContext (ctx , http .MethodPatch , url , bytes .NewBuffer (jsonReq ))
777
825
if err != nil {
826
+ r .Log .Error (err , "could not create request" )
778
827
return err
779
828
}
780
829
req .Header .Set ("Content-Type" , "application/json" )
781
830
782
831
resp , err := httpClient .Do (req )
783
832
if err != nil {
833
+ r .Log .Error (err , "could not perform request" )
784
834
return err
785
835
}
786
836
defer resp .Body .Close ()
0 commit comments