Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K8SPSMDB-1156: simplify smartUpdate if rs isn't initialized #1708

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions pkg/controller/perconaservermongodb/smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api
return nil
}

waitLimit := int(replset.LivenessProbe.InitialDelaySeconds)

updatePod := func(pod *corev1.Pod) error {
updateRevision := sfs.Status.UpdateRevision
if pod.Labels[naming.LabelKubernetesComponent] == "arbiter" {
arbiterSfs, err := r.getArbiterStatefulset(ctx, cr, replset)
if err != nil {
return errors.Wrap(err, "failed to get arbiter statefulset")
}

updateRevision = arbiterSfs.Status.UpdateRevision
}

if err := r.applyNWait(ctx, cr, updateRevision, pod, waitLimit); err != nil {
return errors.Wrap(err, "failed to apply changes")
}
return nil
}

if rsStatus, ok := cr.Status.Replsets[replset.Name]; !ok || !rsStatus.Initialized {
log.Info("Replset wasn't initialized. Continuing smart update", "replset", replset.Name)
for _, pod := range list.Items {
log.Info("apply changes to pod", "pod", pod.Name)

if err := updatePod(&pod); err != nil {
return err
}
}
log.Info("smart update finished for statefulset", "statefulset", sfs.Name)
return nil
}

isBackupRunning, err := r.isBackupRunning(ctx, cr)
if err != nil {
return errors.Wrap(err, "failed to check active backups")
Expand All @@ -103,7 +135,11 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api

hasActiveJobs, err := backup.HasActiveJobs(ctx, r.newPBM, r.client, cr, backup.Job{}, backup.NotPITRLock)
if err != nil {
return errors.Wrap(err, "failed to check active jobs")
if cr.Status.State == api.AppStateError {
log.Info("Failed to check active jobs. Proceeding with Smart Update because the cluster is in an error state", "error", err.Error())
} else {
return errors.Wrap(err, "failed to check active jobs")
}
}

_, ok = sfs.Annotations[api.AnnotationRestoreInProgress]
Expand All @@ -119,8 +155,6 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api
}
}

waitLimit := int(replset.LivenessProbe.InitialDelaySeconds)

sort.Slice(list.Items, func(i, j int) bool {
return list.Items[i].Name > list.Items[j].Name
})
Expand All @@ -138,18 +172,8 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api

log.Info("apply changes to secondary pod", "pod", pod.Name)

updateRevision := sfs.Status.UpdateRevision
if pod.Labels[naming.LabelKubernetesComponent] == "arbiter" {
arbiterSfs, err := r.getArbiterStatefulset(ctx, cr, replset)
if err != nil {
return errors.Wrap(err, "failed to get arbiter statefulset")
}

updateRevision = arbiterSfs.Status.UpdateRevision
}

if err := r.applyNWait(ctx, cr, updateRevision, &pod, waitLimit); err != nil {
return errors.Wrap(err, "failed to apply changes")
if err := updatePod(&pod); err != nil {
return err
}
}

Expand Down Expand Up @@ -183,8 +207,8 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api
}

log.Info("apply changes to primary pod", "pod", primaryPod.Name)
if err := r.applyNWait(ctx, cr, sfs.Status.UpdateRevision, &primaryPod, waitLimit); err != nil {
return fmt.Errorf("failed to apply changes: %v", err)
if err := updatePod(&primaryPod); err != nil {
return err
}
}

Expand Down
Loading