Skip to content

Commit 1e15d78

Browse files
authored
As a last restor, select *all* spilo pods when no leader found (#392)
* As a last restor, select *all* spilo pods when no leader found * improve logging * fix error handling * only log error when error occurs * reduce logging a bit * Incorporate review changes * Improve readability * Improve readability & logging
1 parent 6b81db4 commit 1e15d78

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

controllers/postgres_controller.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,11 +719,59 @@ func (r *PostgresReconciler) updatePatroniConfig(ctx context.Context, instance *
719719
return err
720720
}
721721
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
725730
}
726731
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+
727775
podPort := "8008"
728776
path := "config"
729777

@@ -775,12 +823,14 @@ func (r *PostgresReconciler) updatePatroniConfig(ctx context.Context, instance *
775823

776824
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, bytes.NewBuffer(jsonReq))
777825
if err != nil {
826+
r.Log.Error(err, "could not create request")
778827
return err
779828
}
780829
req.Header.Set("Content-Type", "application/json")
781830

782831
resp, err := httpClient.Do(req)
783832
if err != nil {
833+
r.Log.Error(err, "could not perform request")
784834
return err
785835
}
786836
defer resp.Body.Close()

0 commit comments

Comments
 (0)