Skip to content

Commit

Permalink
chore(postgres): Filter Zalando pods using label filter
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Feb 17, 2024
1 parent 9357a48 commit 1eebf22
Showing 1 changed file with 4 additions and 28 deletions.
32 changes: 4 additions & 28 deletions internal/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"strconv"
Expand Down Expand Up @@ -162,34 +161,11 @@ func (Postgres) FilterPods(ctx context.Context, client kubernetes.KubeClient, po

// Zalando Postgres Operator
if matched := zalandoQuery.FindPods(pods); len(matched) != 0 {
log.Debug("querying Patroni for primary instance")
cmd := command.NewBuilder("patronictl", "list", "--format=json")
log.Debug("filtering Zalando Pods for Leader")

var buf bytes.Buffer
var errBuf strings.Builder
if err := client.Exec(ctx, kubernetes.ExecOptions{
Pod: matched[0],
Cmd: cmd.String(),
Stdout: &buf,
Stderr: &errBuf,
}); err != nil {
return pods, fmt.Errorf("%w: %s", err, errBuf.String())
}

var data []map[string]any
if err := json.NewDecoder(&buf).Decode(&data); err != nil {
return pods, err
}

for _, member := range data {
if role, ok := member["Role"]; ok && role == "Leader" {
if leaderName, ok := member["Member"].(string); ok {
for _, pod := range matched {
if pod.Name == leaderName {
preferred = append(preferred, pod)
}
}
}
for _, pod := range matched {
if role, ok := pod.Labels["spilo-role"]; ok && role == "master" {
preferred = append(preferred, pod)
}
}
}
Expand Down

0 comments on commit 1eebf22

Please sign in to comment.