Skip to content

Commit 54c8437

Browse files
jkatzJonathan S. Katz
authored andcommitted
Modify primary check before taking backup
This was using a legacy method and is now modified to use the method used by other Operator operations to check for both the primary cluster and its availability.
1 parent bb771cf commit 54c8437

File tree

1 file changed

+14
-42
lines changed

1 file changed

+14
-42
lines changed

internal/apiserver/backrestservice/backrestimpl.go

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,9 @@ func CreateBackup(request *msgs.CreateBackrestBackupRequest, ns, pgouser string)
215215
}
216216

217217
// check if primary is ready
218-
if err := isPrimaryReady(&cluster, ns); err != nil {
219-
log.Error(err)
218+
if !isPrimaryReady(cluster) {
220219
resp.Status.Code = msgs.Error
221-
resp.Status.Msg = err.Error()
220+
resp.Status.Msg = "primary pod is not in Ready state"
222221
return resp
223222
}
224223

@@ -300,54 +299,27 @@ func getBackrestRepoPodName(cluster *crv1.Pgcluster, ns string) (string, error)
300299
return repopodName, err
301300
}
302301

303-
func isPrimary(pod *v1.Pod, clusterName string) bool {
304-
if pod.ObjectMeta.Labels[config.LABEL_SERVICE_NAME] == clusterName {
305-
return true
306-
}
307-
return false
308-
309-
}
310-
311-
func isReady(pod *v1.Pod) bool {
312-
readyCount := 0
313-
containerCount := 0
314-
for _, stat := range pod.Status.ContainerStatuses {
315-
containerCount++
316-
if stat.Ready {
317-
readyCount++
318-
}
319-
}
320-
if readyCount != containerCount {
321-
return false
322-
}
323-
return true
324-
325-
}
326-
327302
// isPrimaryReady goes through the pod list to first identify the
328303
// Primary pod and, once identified, determine if it is in a
329304
// ready state. If not, it returns an error, otherwise it returns
330305
// a nil value
331-
func isPrimaryReady(cluster *crv1.Pgcluster, ns string) error {
332-
primaryReady := false
306+
func isPrimaryReady(cluster *crv1.Pgcluster) bool {
307+
options := metav1.ListOptions{
308+
FieldSelector: fields.OneTermEqualSelector("status.phase", string(v1.PodRunning)).String(),
309+
LabelSelector: fields.AndSelectors(
310+
fields.OneTermEqualSelector(config.LABEL_PG_CLUSTER, cluster.Name),
311+
fields.OneTermEqualSelector(config.LABEL_PGHA_ROLE, config.LABEL_PGHA_ROLE_PRIMARY),
312+
).String(),
313+
}
333314

334-
selector := fmt.Sprintf("%s=%s,%s=%s", config.LABEL_PG_CLUSTER, cluster.Name,
335-
config.LABEL_PGHA_ROLE, config.LABEL_PGHA_ROLE_PRIMARY)
315+
pods, err := apiserver.Clientset.CoreV1().Pods(cluster.Namespace).List(options)
336316

337-
pods, err := apiserver.Clientset.CoreV1().Pods(ns).List(metav1.ListOptions{LabelSelector: selector})
338317
if err != nil {
339-
return err
340-
}
341-
for _, p := range pods.Items {
342-
if isPrimary(&p, cluster.Spec.Name) && isReady(&p) {
343-
primaryReady = true
344-
}
318+
log.Error(err)
319+
return false
345320
}
346321

347-
if primaryReady == false {
348-
return errors.New("primary pod is not in Ready state")
349-
}
350-
return nil
322+
return len(pods.Items) > 0
351323
}
352324

353325
// ShowBackrest ...

0 commit comments

Comments
 (0)