Add patroni api client

This commit is contained in:
Murat Kabilov 2017-08-30 16:01:18 +02:00 committed by GitHub
parent 899c0bef45
commit 8aa11ecee2
2 changed files with 83 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import (
"github.com/zalando-incubator/postgres-operator/pkg/util/config" "github.com/zalando-incubator/postgres-operator/pkg/util/config"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants" "github.com/zalando-incubator/postgres-operator/pkg/util/constants"
"github.com/zalando-incubator/postgres-operator/pkg/util/k8sutil" "github.com/zalando-incubator/postgres-operator/pkg/util/k8sutil"
"github.com/zalando-incubator/postgres-operator/pkg/util/patroni"
"github.com/zalando-incubator/postgres-operator/pkg/util/teams" "github.com/zalando-incubator/postgres-operator/pkg/util/teams"
"github.com/zalando-incubator/postgres-operator/pkg/util/users" "github.com/zalando-incubator/postgres-operator/pkg/util/users"
"github.com/zalando-incubator/postgres-operator/pkg/util/volumes" "github.com/zalando-incubator/postgres-operator/pkg/util/volumes"
@ -55,6 +56,7 @@ type Cluster struct {
spec.Postgresql spec.Postgresql
Config Config
logger *logrus.Entry logger *logrus.Entry
patroni patroni.Interface
pgUsers map[string]spec.PgUser pgUsers map[string]spec.PgUser
systemUsers map[string]spec.PgUser systemUsers map[string]spec.PgUser
podSubscribers map[spec.NamespacedName]chan spec.PodEvent podSubscribers map[spec.NamespacedName]chan spec.PodEvent
@ -105,6 +107,7 @@ func New(cfg Config, kubeClient k8sutil.KubernetesClient, pgSpec spec.Postgresql
teamsAPIClient: teams.NewTeamsAPI(cfg.OpConfig.TeamsAPIUrl, logger), teamsAPIClient: teams.NewTeamsAPI(cfg.OpConfig.TeamsAPIUrl, logger),
} }
cluster.logger = logger.WithField("pkg", "cluster").WithField("cluster-name", cluster.clusterName()) cluster.logger = logger.WithField("pkg", "cluster").WithField("cluster-name", cluster.clusterName())
cluster.patroni = patroni.New(cluster.logger)
return cluster return cluster
} }

View File

@ -0,0 +1,80 @@
package patroni
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/Sirupsen/logrus"
"k8s.io/client-go/pkg/api/v1"
)
const (
failoverPath = "/failover"
apiPort = 8008
timeout = 30 * time.Second
)
// Interface describe patroni methods
type Interface interface {
Failover(master *v1.Pod, candidate string) error
}
// Patroni API client
type Patroni struct {
httpClient *http.Client
logger *logrus.Entry
}
// New create patroni
func New(logger *logrus.Entry) *Patroni {
cl := http.Client{
Timeout: timeout,
}
return &Patroni{
logger: logger,
httpClient: &cl,
}
}
func (p *Patroni) apiURL(masterPod *v1.Pod) string {
return fmt.Sprintf("http://%s:%d", masterPod.Status.PodIP, apiPort)
}
// Failover does manual failover via patroni api
func (p *Patroni) Failover(master *v1.Pod, candidate string) error {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(map[string]string{"leader": master.Name, "member": candidate})
if err != nil {
return fmt.Errorf("could not encode json: %v", err)
}
request, err := http.NewRequest(http.MethodPost, p.apiURL(master)+failoverPath, buf)
if err != nil {
return fmt.Errorf("could not create request: %v", err)
}
p.logger.Debugf("making http request: %s", request.URL.String())
resp, err := p.httpClient.Do(request)
if err != nil {
return fmt.Errorf("could not make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("could not read response: %v", err)
}
return fmt.Errorf("patroni returned '%s'", string(bodyBytes))
}
return nil
}