@@ -191,9 +191,9 @@ func CreateBackup(request *msgs.CreateBackrestBackupRequest, ns, pgouser string)
191191
192192 }
193193
194- //get pod name from cluster
194+ // get pod name from cluster
195195 var podname string
196- podname , err = getPrimaryPodName (& cluster , ns )
196+ podname , err = getBackrestRepoPodName (& cluster , ns )
197197
198198 if err != nil {
199199 log .Error (err )
@@ -202,6 +202,14 @@ func CreateBackup(request *msgs.CreateBackrestBackupRequest, ns, pgouser string)
202202 return resp
203203 }
204204
205+ // check if primary is ready
206+ if err := isPrimaryReady (& cluster , ns ); err != nil {
207+ log .Error (err )
208+ resp .Status .Code = msgs .Error
209+ resp .Status .Msg = err .Error ()
210+ return resp
211+ }
212+
205213 jobName := "backrest-" + crv1 .PgtaskBackrestBackup + "-" + clusterName
206214 log .Debugf ("setting jobName to %s" , jobName )
207215
@@ -253,28 +261,9 @@ func getBackupParams(identifier, clusterName, taskName, action, podName, contain
253261 return newInstance
254262}
255263
256- func getDeployName (cluster * crv1.Pgcluster , ns string ) (string , error ) {
257- var depName string
258-
259- selector := config .LABEL_PG_CLUSTER + "=" + cluster .Spec .Name + "," + config .LABEL_SERVICE_NAME + "=" + cluster .Spec .Name
260-
261- deps , err := kubeapi .GetDeployments (apiserver .Clientset , selector , ns )
262- if err != nil {
263- return depName , err
264- }
265-
266- if len (deps .Items ) != 1 {
267- return depName , errors .New ("error: deployment count is wrong for backrest backup " + cluster .Spec .Name )
268- }
269- for _ , d := range deps .Items {
270- return d .Name , err
271- }
272-
273- return depName , errors .New ("unknown error in backrest backup" )
274- }
275-
276- func getPrimaryPodName (cluster * crv1.Pgcluster , ns string ) (string , error ) {
277-
264+ // getBackrestRepoPodName goes through the pod list to identify the
265+ // pgBackRest repo pod and then returns the pod name.
266+ func getBackrestRepoPodName (cluster * crv1.Pgcluster , ns string ) (string , error ) {
278267 //look up the backrest-repo pod name
279268 selector := "pg-cluster=" + cluster .Spec .Name + ",pgo-backrest-repo=true"
280269 repopods , err := kubeapi .GetPods (apiserver .Clientset , selector , ns )
@@ -289,25 +278,6 @@ func getPrimaryPodName(cluster *crv1.Pgcluster, ns string) (string, error) {
289278
290279 repopodName := repopods .Items [0 ].Name
291280
292- primaryReady := false
293-
294- //make sure the primary pod is in the ready state
295- selector = config .LABEL_SERVICE_NAME + "=" + cluster .Spec .Name
296-
297- pods , err := kubeapi .GetPods (apiserver .Clientset , selector , ns )
298- if err != nil {
299- return "" , err
300- }
301- for _ , p := range pods .Items {
302- if isPrimary (& p , cluster .Spec .Name ) && isReady (& p ) {
303- primaryReady = true
304- }
305- }
306-
307- if primaryReady == false {
308- return "" , errors .New ("primary pod is not in Ready state" )
309- }
310-
311281 return repopodName , err
312282}
313283
@@ -335,6 +305,32 @@ func isReady(pod *v1.Pod) bool {
335305
336306}
337307
308+ // isPrimaryReady goes through the pod list to first identify the
309+ // Primary pod and, once identified, determine if it is in a
310+ // ready state. If not, it returns an error, otherwise it returns
311+ // a nil value
312+ func isPrimaryReady (cluster * crv1.Pgcluster , ns string ) error {
313+ primaryReady := false
314+
315+ selector := fmt .Sprintf ("%s=%s,%s=%s" , config .LABEL_PG_CLUSTER , cluster .Name ,
316+ config .LABEL_PGHA_ROLE , config .LABEL_PGHA_ROLE_PRIMARY )
317+
318+ pods , err := apiserver .Clientset .CoreV1 ().Pods (ns ).List (meta_v1.ListOptions {LabelSelector : selector })
319+ if err != nil {
320+ return err
321+ }
322+ for _ , p := range pods .Items {
323+ if isPrimary (& p , cluster .Spec .Name ) && isReady (& p ) {
324+ primaryReady = true
325+ }
326+ }
327+
328+ if primaryReady == false {
329+ return errors .New ("primary pod is not in Ready state" )
330+ }
331+ return nil
332+ }
333+
338334// ShowBackrest ...
339335func ShowBackrest (name , selector , ns string ) msgs.ShowBackrestResponse {
340336 var err error
@@ -364,7 +360,7 @@ func ShowBackrest(name, selector, ns string) msgs.ShowBackrestResponse {
364360 log .Debugf ("clusters found len is %d\n " , len (clusterList .Items ))
365361
366362 for _ , c := range clusterList .Items {
367- podname , err := getPrimaryPodName (& c , ns )
363+ podname , err := getBackrestRepoPodName (& c , ns )
368364
369365 if err != nil {
370366 log .Error (err )
0 commit comments