@@ -14,8 +14,8 @@ import (
1414 "github.com/cybertec-postgresql/cybertec-pg-operator/pkg/util/constants"
1515 httpclient "github.com/cybertec-postgresql/cybertec-pg-operator/pkg/util/httpclient"
1616
17- "github.com/sirupsen/logrus"
1817 cpov1 "github.com/cybertec-postgresql/cybertec-pg-operator/pkg/apis/cpo.opensource.cybertec.at/v1"
18+ "github.com/sirupsen/logrus"
1919 v1 "k8s.io/api/core/v1"
2020)
2121
@@ -25,6 +25,7 @@ const (
2525 clusterPath = "/cluster"
2626 statusPath = "/patroni"
2727 restartPath = "/restart"
28+ leaderPath = "/leader"
2829 ApiPort = 8008
2930 timeout = 30 * time .Second
3031)
@@ -38,6 +39,7 @@ type Interface interface {
3839 Restart (server * v1.Pod ) error
3940 GetConfig (server * v1.Pod ) (cpov1.Patroni , map [string ]string , error )
4041 SetConfig (server * v1.Pod , config map [string ]interface {}) error
42+ IsLeader (server * v1.Pod ) (bool , error )
4143}
4244
4345// Patroni API client
@@ -150,7 +152,7 @@ func (p *Patroni) Switchover(master *v1.Pod, candidate string) error {
150152
151153//TODO: add an option call /patroni to check if it is necessary to restart the server
152154
153- //SetPostgresParameters sets Postgres options via Patroni patch API call.
155+ // SetPostgresParameters sets Postgres options via Patroni patch API call.
154156func (p * Patroni ) SetPostgresParameters (server * v1.Pod , parameters map [string ]string ) error {
155157 buf := & bytes.Buffer {}
156158 err := json .NewEncoder (buf ).Encode (map [string ]map [string ]interface {}{"postgresql" : {"parameters" : parameters }})
@@ -164,7 +166,7 @@ func (p *Patroni) SetPostgresParameters(server *v1.Pod, parameters map[string]st
164166 return p .httpPostOrPatch (http .MethodPatch , apiURLString + configPath , buf )
165167}
166168
167- //SetConfig sets Patroni options via Patroni patch API call.
169+ // SetConfig sets Patroni options via Patroni patch API call.
168170func (p * Patroni ) SetConfig (server * v1.Pod , config map [string ]interface {}) error {
169171 buf := & bytes.Buffer {}
170172 err := json .NewEncoder (buf ).Encode (config )
@@ -320,3 +322,24 @@ func (p *Patroni) GetMemberData(server *v1.Pod) (MemberData, error) {
320322
321323 return data , nil
322324}
325+
326+ // Call leader-Endpoint (expecting statuscode 200 or 503)
327+ func (p * Patroni ) IsLeader (server * v1.Pod ) (bool , error ) {
328+ apiURLString , err := apiURL (server )
329+ if err != nil {
330+ return false , err
331+ }
332+
333+ resp , err := p .httpClient .Get (apiURLString + leaderPath )
334+ if err != nil {
335+ return false , fmt .Errorf ("request failed: %v" , err )
336+ }
337+ defer resp .Body .Close ()
338+
339+ // 200 = Leader, 503 = Replica
340+ if resp .StatusCode == http .StatusOK {
341+ return true , nil
342+ }
343+
344+ return false , nil
345+ }
0 commit comments