Skip to content

Commit 7b197d4

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 19de963 commit 7b197d4

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
@@ -203,10 +203,9 @@ func CreateBackup(request *msgs.CreateBackrestBackupRequest, ns, pgouser string)
203203
}
204204

205205
// check if primary is ready
206-
if err := isPrimaryReady(cluster, ns); err != nil {
207-
log.Error(err)
206+
if !isPrimaryReady(cluster) {
208207
resp.Status.Code = msgs.Error
209-
resp.Status.Msg = err.Error()
208+
resp.Status.Msg = "primary pod is not in Ready state"
210209
return resp
211210
}
212211

@@ -288,54 +287,27 @@ func getBackrestRepoPodName(cluster *crv1.Pgcluster, ns string) (string, error)
288287
return repopodName, err
289288
}
290289

291-
func isPrimary(pod *v1.Pod, clusterName string) bool {
292-
if pod.ObjectMeta.Labels[config.LABEL_SERVICE_NAME] == clusterName {
293-
return true
294-
}
295-
return false
296-
297-
}
298-
299-
func isReady(pod *v1.Pod) bool {
300-
readyCount := 0
301-
containerCount := 0
302-
for _, stat := range pod.Status.ContainerStatuses {
303-
containerCount++
304-
if stat.Ready {
305-
readyCount++
306-
}
307-
}
308-
if readyCount != containerCount {
309-
return false
310-
}
311-
return true
312-
313-
}
314-
315290
// isPrimaryReady goes through the pod list to first identify the
316291
// Primary pod and, once identified, determine if it is in a
317292
// ready state. If not, it returns an error, otherwise it returns
318293
// a nil value
319-
func isPrimaryReady(cluster *crv1.Pgcluster, ns string) error {
320-
primaryReady := false
294+
func isPrimaryReady(cluster *crv1.Pgcluster) bool {
295+
options := metav1.ListOptions{
296+
FieldSelector: fields.OneTermEqualSelector("status.phase", string(v1.PodRunning)).String(),
297+
LabelSelector: fields.AndSelectors(
298+
fields.OneTermEqualSelector(config.LABEL_PG_CLUSTER, cluster.Name),
299+
fields.OneTermEqualSelector(config.LABEL_PGHA_ROLE, config.LABEL_PGHA_ROLE_PRIMARY),
300+
).String(),
301+
}
321302

322-
selector := fmt.Sprintf("%s=%s,%s=%s", config.LABEL_PG_CLUSTER, cluster.Name,
323-
config.LABEL_PGHA_ROLE, config.LABEL_PGHA_ROLE_PRIMARY)
303+
pods, err := apiserver.Clientset.CoreV1().Pods(cluster.Namespace).List(options)
324304

325-
pods, err := apiserver.Clientset.CoreV1().Pods(ns).List(metav1.ListOptions{LabelSelector: selector})
326305
if err != nil {
327-
return err
328-
}
329-
for _, p := range pods.Items {
330-
if isPrimary(&p, cluster.Spec.Name) && isReady(&p) {
331-
primaryReady = true
332-
}
306+
log.Error(err)
307+
return false
333308
}
334309

335-
if primaryReady == false {
336-
return errors.New("primary pod is not in Ready state")
337-
}
338-
return nil
310+
return len(pods.Items) > 0
339311
}
340312

341313
// ShowBackrest ...

0 commit comments

Comments
 (0)