func()

in oracle/controllers/pitrcontroller/pitr_controller.go [426:493]


func (r *PITRReconciler) updateStatus(ctx context.Context, p *v1alpha1.PITR, i *v1alpha1.Instance, log logr.Logger) error {
	backups, err := r.BackupCtrl.List(ctx, client.InNamespace(i.GetNamespace()), client.MatchingLabels{controllers.PITRLabel: p.GetName(), controllers.IncarnationLabel: i.Status.CurrentDatabaseIncarnation})
	if err != nil {
		log.Error(err, "failed to get a list of Backups applicable to pitr")
		return errors.New("failed to update available recovery window")
	}

	windows, err := r.PITRCtrl.AvailableRecoveryWindows(ctx, p)
	if err != nil {
		log.Error(err, "failed to get status from data plane")
		return errors.New("failed to update available recovery window")
	}

	timeToBackups := make(map[int64]v1alpha1.Backup)
	for _, b := range backups {
		if b.Status.Phase != commonv1alpha1.BackupSucceeded {
			continue
		}
		bTimestamp, err := time.Parse(time.RFC3339, b.Annotations[controllers.TimestampAnnotation])
		if err != nil {
			log.Error(err, "failed to parse backup timestamp", "backup", b)
			continue
		}
		timeToBackups[bTimestamp.Unix()] = b
	}

	keys := make([]int64, len(timeToBackups))
	idx := 0
	for k := range timeToBackups {
		keys[idx] = k
		idx += 1
	}
	sort.Slice(keys, func(i, j int) bool {
		return keys[i] < keys[j]
	})

	var aWindowTime []v1alpha1.TimeWindow
	var aWindowSCN []v1alpha1.SCNWindow

	for _, w := range windows {
		firstIdx := sort.Search(len(keys), func(i int) bool {
			return w.GetStart().GetTime().AsTime().Unix() <= keys[i]
		})

		if firstIdx >= len(keys) || keys[firstIdx] > w.GetEnd().GetTime().AsTime().Unix() {
			continue
		}

		aWindowTime = append(aWindowTime, v1alpha1.TimeWindow{
			Begin: metav1.NewTime(time.Unix(keys[firstIdx], 0)),
			End:   metav1.NewTime(w.GetEnd().Time.AsTime()),
		})

		aWindowSCN = append(aWindowSCN, v1alpha1.SCNWindow{
			Begin: timeToBackups[keys[firstIdx]].Annotations[controllers.SCNAnnotation],
			End:   w.GetEnd().GetScn(),
		})
	}

	p.Status.AvailableRecoveryWindowTime = aWindowTime
	p.Status.AvailableRecoveryWindowSCN = aWindowSCN
	p.Status.CurrentDatabaseIncarnation = i.Status.CurrentDatabaseIncarnation

	if len(aWindowTime) > 0 && len(aWindowSCN) > 0 {
		p.Status.Conditions = k8s.Upsert(p.Status.Conditions, k8s.Ready, metav1.ConditionTrue, k8s.CreateComplete, "")
	}
	return r.PITRCtrl.UpdateStatus(ctx, p)
}