func()

in oracle/controllers/instancecontroller/utils.go [504:597]


func (r *InstanceReconciler) statusProgress(ctx context.Context, ns, name string, log logr.Logger) (int, error) {
	var sts appsv1.StatefulSetList
	if err := r.List(ctx, &sts, client.InNamespace(ns)); err != nil {
		log.Error(err, "failed to get a list of StatefulSets to check status")
		return 0, err
	}

	if len(sts.Items) < 1 {
		return 0, fmt.Errorf("failed to find a StatefulSet, found: %d", len(sts.Items))
	}

	// In theory a user should not be running any StatefulSet in a
	// namespace, but to be on a safe side, iterate over all until we find ours.
	var foundSts *appsv1.StatefulSet
	for index, s := range sts.Items {
		if s.Name == name {
			foundSts = &sts.Items[index]
		}
	}

	if foundSts == nil {
		return 0, fmt.Errorf("failed to find the right StatefulSet %s (out of %d)", name, len(sts.Items))
	}
	log.Info("found the right StatefulSet", "foundSts", &foundSts.Name,
		"sts.Status.CurrentReplicas", &foundSts.Status.CurrentReplicas, "sts.Status.ReadyReplicas", foundSts.Status.ReadyReplicas)

	if foundSts.Status.CurrentReplicas != 1 {
		return 10, fmt.Errorf("StatefulSet is not ready yet? (failed to find the expected number of current replicas): %d", foundSts.Status.CurrentReplicas)
	}

	if foundSts.Status.ReadyReplicas != 1 {
		return 50, fmt.Errorf("StatefulSet is not ready yet? (failed to find the expected number of ready replicas): %d", foundSts.Status.ReadyReplicas)
	}

	var pods corev1.PodList
	if err := r.List(ctx, &pods, client.InNamespace(ns), client.MatchingLabels{"statefulset": name}); err != nil {
		log.Error(err, "failed to get a list of Pods to check status")
		return 60, err
	}

	if len(pods.Items) < 1 {
		return 65, fmt.Errorf("failed to find enough pods, found: %d pods", len(pods.Items))
	}

	var foundPod *corev1.Pod
	for index, p := range pods.Items {
		if p.Name == name+"-0" {
			foundPod = &pods.Items[index]
		}
	}

	if foundPod == nil {
		return 75, fmt.Errorf("failed to find the right Pod %s (out of %d)", name+"-0", len(pods.Items))
	}
	log.Info("found the right Pod", "pod.Name", &foundPod.Name, "pod.Status", foundPod.Status.Phase, "#containers", len(foundPod.Status.ContainerStatuses))

	if foundPod.Status.Phase != "Running" {
		return 85, fmt.Errorf("failed to find the right Pod %s in status Running: %s", name+"-0", foundPod.Status.Phase)
	}

	for _, podCondition := range foundPod.Status.Conditions {
		if podCondition.Type == "Ready" && podCondition.Status == "False" {
			log.Info("statusProgress: podCondition.Type ready is False")
			return 85, fmt.Errorf("failed to find the right Pod %s in status Running: %s", name+"-0", foundPod.Status.Phase)
		}
		if podCondition.Type == "ContainersReady" && podCondition.Status == "False" {
			msg := "statusProgress: podCondition.Type ContainersReady is False"
			log.Info(msg)
			return 85, fmt.Errorf(msg)
		}
	}

	for _, c := range foundPod.Status.ContainerStatuses {
		if !c.Ready {
			msg := fmt.Sprintf("container %s is not ready", c.Name)
			log.Info(msg)
			return 85, fmt.Errorf(msg)
		}
		msg := fmt.Sprintf("container %s is ready", c.Name)
		log.Info(msg)
	}
	for _, c := range foundPod.Status.InitContainerStatuses {
		if !c.Ready {
			msg := fmt.Sprintf("init container %s is not ready", c.Name)
			log.Info(msg)
			return 85, fmt.Errorf(msg)
		}
		msg := fmt.Sprintf("container %s is ready", c.Name)
		log.Info(msg)
	}

	log.Info("Stateful set creation is complete")
	return 100, nil
}