Skip to content

Commit 08e2b05

Browse files
Jonathan S. Katzjkatz
authored andcommitted
Ensure proper PostgreSQL instance name appears after failover
After a failover occurs, the `--query` method found on `pgo scaledown` and `pgo failover` could potentially show the same name for every single replica, due to it utilizing just the prefix. This of course, is not good when one may need to failover or scale down in the future. This fix moves the check over to a regular expression that follows the Kubernetes pod name. Issue: [ch6931]
1 parent 4d4a7f5 commit 08e2b05

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

util/failover.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"encoding/json"
2020
"errors"
2121
"fmt"
22-
"strings"
22+
"regexp"
2323

2424
"github.com/crunchydata/postgres-operator/config"
2525
"github.com/crunchydata/postgres-operator/kubeapi"
@@ -67,6 +67,9 @@ const (
6767
// instanceReplicationInfoTypePrimary is the label used by Patroni to indicate that an instance
6868
// is indeed a primary PostgreSQL instance
6969
instanceReplicationInfoTypePrimary = "Leader"
70+
// pgPodNamePattern pattern is a pattern used by regexp to look up the
71+
// name of the pod
72+
pgPodNamePattern = "%s-[0-9a-z]{10}-[0-9a-z]{5}"
7073
)
7174

7275
var (
@@ -195,7 +198,18 @@ func ReplicationStatus(request ReplicationStatusRequest) (ReplicationStatusRespo
195198
//
196199
// This is not the cleanest way of doing it, but it works
197200
for name, node := range instanceNodeMap {
198-
if strings.HasPrefix(rawInstance.PodName, name) {
201+
r, err := regexp.Compile(fmt.Sprintf(pgPodNamePattern, name))
202+
203+
// if there is an error compiling the regular expression, add an error to
204+
// log log and keep iterating
205+
if err != nil {
206+
log.Error(err)
207+
continue
208+
}
209+
210+
// see if there is a match in the names. If it , add the name and node for
211+
// this particular instance
212+
if r.Match([]byte(rawInstance.PodName)) {
199213
instance.Name = name
200214
instance.Node = node
201215
break

0 commit comments

Comments
 (0)