Skip to content

Commit 4b99c58

Browse files
committed
K8SPSMDB-1527: Cluster can't be ready before PBM is ready
1 parent 00affaf commit 4b99c58

File tree

6 files changed

+515
-173
lines changed

6 files changed

+515
-173
lines changed

pkg/apis/psmdb/v1/psmdb_types.go

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,13 @@ const (
339339
ConditionUnknown ConditionStatus = "Unknown"
340340
)
341341

342-
// ConditionTypePendingSmartUpdate is a condition type set on PSMDBCluster when a smart update is required
343-
// but has not yet started. For e.g., if a backup/restore is running at the same time as a smart update is triggered.
344-
const ConditionTypePendingSmartUpdate AppState = "pendingSmartUpdate"
342+
const (
343+
// ConditionTypePendingSmartUpdate is a condition type set on PSMDBCluster when a smart update is required
344+
// but has not yet started. For e.g., if a backup/restore is running at the same time as a smart update is triggered.
345+
ConditionTypePendingSmartUpdate AppState = "pendingSmartUpdate"
346+
347+
ConditionTypePBMReady AppState = "PBMReady"
348+
)
345349

346350
type ClusterCondition struct {
347351
Status ConditionStatus `json:"status"`
@@ -369,6 +373,33 @@ func (s *PerconaServerMongoDBStatus) IsStatusConditionTrue(conditionType AppStat
369373
return cond.Status == ConditionTrue
370374
}
371375

376+
func (s *PerconaServerMongoDBStatus) AddCondition(c ClusterCondition) {
377+
existingCondition := s.FindCondition(c.Type)
378+
if existingCondition == nil {
379+
if c.LastTransitionTime.IsZero() {
380+
c.LastTransitionTime = metav1.NewTime(time.Now())
381+
}
382+
s.Conditions = append(s.Conditions, c)
383+
return
384+
}
385+
386+
if existingCondition.Status != c.Status {
387+
existingCondition.Status = c.Status
388+
if !c.LastTransitionTime.IsZero() {
389+
existingCondition.LastTransitionTime = c.LastTransitionTime
390+
} else {
391+
existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
392+
}
393+
}
394+
395+
if existingCondition.Reason != c.Reason {
396+
existingCondition.Reason = c.Reason
397+
}
398+
if existingCondition.Message != c.Message {
399+
existingCondition.Message = c.Message
400+
}
401+
}
402+
372403
type PMMSpec struct {
373404
Enabled bool `json:"enabled,omitempty"`
374405
ServerHost string `json:"serverHost,omitempty"`
@@ -1439,33 +1470,6 @@ func (s *PerconaServerMongoDBStatus) RemoveCondition(conditionType AppState) {
14391470
}
14401471
}
14411472

1442-
func (s *PerconaServerMongoDBStatus) AddCondition(c ClusterCondition) {
1443-
existingCondition := s.FindCondition(c.Type)
1444-
if existingCondition == nil {
1445-
if c.LastTransitionTime.IsZero() {
1446-
c.LastTransitionTime = metav1.NewTime(time.Now())
1447-
}
1448-
s.Conditions = append(s.Conditions, c)
1449-
return
1450-
}
1451-
1452-
if existingCondition.Status != c.Status {
1453-
existingCondition.Status = c.Status
1454-
if !c.LastTransitionTime.IsZero() {
1455-
existingCondition.LastTransitionTime = c.LastTransitionTime
1456-
} else {
1457-
existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
1458-
}
1459-
}
1460-
1461-
if existingCondition.Reason != c.Reason {
1462-
existingCondition.Reason = c.Reason
1463-
}
1464-
if existingCondition.Message != c.Message {
1465-
existingCondition.Message = c.Message
1466-
}
1467-
}
1468-
14691473
// GetExternalNodes returns all external nodes for all replsets
14701474
func (cr *PerconaServerMongoDB) GetExternalNodes() []*ExternalNode {
14711475
extNodes := make([]*ExternalNode, 0)

pkg/controller/perconaservermongodb/backup.go

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package perconaservermongodb
22

33
import (
4-
"bytes"
54
"container/heap"
65
"context"
7-
"strings"
86
"time"
97

108
"github.com/pkg/errors"
@@ -19,12 +17,9 @@ import (
1917
logf "sigs.k8s.io/controller-runtime/pkg/log"
2018

2119
"github.com/percona/percona-backup-mongodb/pbm/defs"
22-
pbmVersion "github.com/percona/percona-backup-mongodb/pbm/version"
2320

2421
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
25-
"github.com/percona/percona-server-mongodb-operator/pkg/k8s"
2622
"github.com/percona/percona-server-mongodb-operator/pkg/naming"
27-
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb"
2823
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/backup"
2924
)
3025

@@ -403,117 +398,3 @@ func isPodUpToDate(pod *corev1.Pod, stsRevision, image string) bool {
403398

404399
return true
405400
}
406-
407-
func (r *ReconcilePerconaServerMongoDB) reconcileBackupVersion(ctx context.Context, cr *api.PerconaServerMongoDB) error {
408-
log := logf.FromContext(ctx)
409-
410-
if !cr.Spec.Backup.Enabled {
411-
return nil
412-
}
413-
414-
if cr.Status.State != api.AppStateReady {
415-
return nil
416-
}
417-
418-
if cr.Status.BackupVersion != "" && cr.Status.BackupImage == cr.Spec.Backup.Image {
419-
return nil
420-
}
421-
422-
if len(cr.Spec.Replsets) < 1 {
423-
return errors.New("no replsets found")
424-
}
425-
426-
var rs *api.ReplsetSpec
427-
for _, r := range cr.Spec.Replsets {
428-
rs = r
429-
break
430-
}
431-
432-
stsName := naming.MongodStatefulSetName(cr, rs)
433-
sts := psmdb.NewStatefulSet(stsName, cr.Namespace)
434-
err := r.client.Get(ctx, client.ObjectKeyFromObject(sts), sts)
435-
if err != nil {
436-
return errors.Wrapf(err, "get statefulset/%s", stsName)
437-
}
438-
439-
matchLabels := naming.RSLabels(cr, rs)
440-
label, ok := sts.Labels[naming.LabelKubernetesComponent]
441-
if ok {
442-
matchLabels[naming.LabelKubernetesComponent] = label
443-
}
444-
445-
podList := corev1.PodList{}
446-
if err := r.client.List(ctx,
447-
&podList,
448-
&client.ListOptions{
449-
Namespace: cr.Namespace,
450-
LabelSelector: labels.SelectorFromSet(matchLabels),
451-
},
452-
); err != nil {
453-
return errors.Wrap(err, "get pod list")
454-
}
455-
456-
var pod *corev1.Pod
457-
for _, p := range podList.Items {
458-
if !k8s.IsPodReady(p) {
459-
continue
460-
}
461-
462-
if !isPodUpToDate(&p, sts.Status.UpdateRevision, cr.Spec.Backup.Image) {
463-
continue
464-
}
465-
466-
pod = &p
467-
break
468-
}
469-
if pod == nil {
470-
log.V(1).Error(nil, "no ready pods to get pbm-agent version")
471-
return nil
472-
}
473-
474-
stdout := &bytes.Buffer{}
475-
stderr := &bytes.Buffer{}
476-
cmd := []string{"pbm-agent", "version", "--short"}
477-
478-
err = r.clientcmd.Exec(ctx, pod, naming.ContainerBackupAgent, cmd, nil, stdout, stderr, false)
479-
if err != nil {
480-
return errors.Wrap(err, "get pbm-agent version")
481-
}
482-
483-
// PBM v2.9.0 and above prints version to stderr, below prints it to stdout
484-
stdoutStr := strings.TrimSpace(stdout.String())
485-
stderrStr := strings.TrimSpace(stderr.String())
486-
if stdoutStr != "" && stderrStr != "" {
487-
log.V(1).Info("pbm-agent version found in both stdout and stderr; using stdout",
488-
"stdout", stdoutStr, "stderr", stderrStr)
489-
cr.Status.BackupVersion = stdoutStr
490-
} else if stdoutStr != "" {
491-
cr.Status.BackupVersion = stdoutStr
492-
} else if stderrStr != "" {
493-
cr.Status.BackupVersion = stderrStr
494-
} else {
495-
return errors.New("pbm-agent version not found in stdout or stderr")
496-
}
497-
498-
cr.Status.BackupImage = cr.Spec.Backup.Image
499-
500-
log.Info("pbm-agent version",
501-
"pod", pod.Name,
502-
"image", cr.Status.BackupImage,
503-
"version", cr.Status.BackupVersion)
504-
505-
pbmInfo := pbmVersion.Current()
506-
507-
compare, err := cr.ComparePBMAgentVersion(pbmInfo.Version)
508-
if err != nil {
509-
return errors.Wrap(err, "compare pbm-agent version with go module")
510-
}
511-
512-
if compare != 0 {
513-
log.Info("pbm-agent version is different than the go module, this might create problems",
514-
"pbmAgentVersion", cr.Status.BackupVersion,
515-
"goModuleVersion", pbmInfo.Version)
516-
}
517-
518-
return nil
519-
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package perconaservermongodb
2+
3+
import (
4+
"context"
5+
6+
psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
7+
)
8+
9+
func (r *ReconcilePerconaServerMongoDB) updateCondition(ctx context.Context, cr *psmdbv1.PerconaServerMongoDB, c psmdbv1.ClusterCondition) error {
10+
cr.Status.AddCondition(c)
11+
return r.writeStatus(ctx, cr)
12+
}

0 commit comments

Comments
 (0)