func setAllWorkAvailableCondition()

in pkg/controllers/workgenerator/controller.go [1154:1262]


func setAllWorkAvailableCondition(works map[string]*fleetv1beta1.Work, binding *fleetv1beta1.ClusterResourceBinding) workConditionSummarizedStatus {
	// If the Applied condition has been set to False, skip setting the Available condition.
	appliedCond := meta.FindStatusCondition(binding.Status.Conditions, string(fleetv1beta1.ResourceBindingApplied))
	if !condition.IsConditionStatusTrue(appliedCond, binding.GetGeneration()) {
		klog.V(2).InfoS("Some works are not yet applied or have failed to get applied; skip populating the Available condition", "binding", klog.KObj(binding))
		return workConditionSummarizedStatusFalse
	}

	// Fleet here makes a clear distinction between incomplete, failed and successful availability checks.
	// This is to ensure that stale information will not leak into the current reportings.
	areAllWorksAvailabilityCheckCompleted := true
	areAllWorksAvailabilityCheckSuccessful := true

	var firstWorkWithIncompleteAvailabilityCheck *fleetv1beta1.Work
	var firstWorkWithFailedAvailabilityCheck *fleetv1beta1.Work
	var firstWorkWithSuccessfulAvailabilityCheckDueToUntrackableRes *fleetv1beta1.Work
	for _, w := range works {
		availableCond := meta.FindStatusCondition(w.Status.Conditions, fleetv1beta1.WorkConditionTypeAvailable)
		switch {
		case condition.IsConditionStatusTrue(availableCond, w.GetGeneration()) && availableCond.Reason == workapplier.WorkNotAllManifestsTrackableReasonNew:
			// The Work object has completed the availability check successfully, due to the
			// resources being untrackable.
			//
			// This branch is currently never visited as the work applier would still populate
			// the Available condition using the old reason string for compatibility reasons.
			if firstWorkWithSuccessfulAvailabilityCheckDueToUntrackableRes == nil {
				firstWorkWithSuccessfulAvailabilityCheckDueToUntrackableRes = w
			}
		case condition.IsConditionStatusTrue(availableCond, w.GetGeneration()) && availableCond.Reason == workapplier.WorkNotAllManifestsTrackableReason:
			// The Work object has completed the availability check successfully, due to the
			// resources being untrackable. This is the same branch as the one above but checks
			// for the old reason string; it is kept for compatibility reasons.
			//
			// TO-DO (chenyu1): drop this branch after the rollout completes.
			if firstWorkWithSuccessfulAvailabilityCheckDueToUntrackableRes == nil {
				firstWorkWithSuccessfulAvailabilityCheckDueToUntrackableRes = w
			}
		case condition.IsConditionStatusTrue(availableCond, w.GetGeneration()):
			// The Work object has completed the availability check successfully.
		case condition.IsConditionStatusFalse(availableCond, w.GetGeneration()):
			// The Work object has failed the availability check.
			areAllWorksAvailabilityCheckSuccessful = false
			if firstWorkWithFailedAvailabilityCheck == nil {
				firstWorkWithFailedAvailabilityCheck = w
			}
		default:
			// The Work object has not yet completed the availability check.
			//
			// This in theory should never happen as the Fleet work applier always set the Applied
			// and Available conditions on a Work object together in one call and Fleet will not
			// check resource availability if the apply op itself has failed. However, Fleet can
			// still handle this case for completeness reasons.
			areAllWorksAvailabilityCheckCompleted = false
			if firstWorkWithIncompleteAvailabilityCheck == nil {
				firstWorkWithIncompleteAvailabilityCheck = w
			}
		}
	}

	switch {
	case !areAllWorksAvailabilityCheckCompleted:
		// Not all Work objects have completed the availability check.
		//
		// As previously explained, this should never happen in practice. Fleet here handles
		// this case for completeness reasons.
		klog.V(2).InfoS("Some works are not yet completed availability check", "binding", klog.KObj(binding), "firstWorkWithIncompleteAvailabilityCheck", klog.KObj(firstWorkWithIncompleteAvailabilityCheck))
		meta.SetStatusCondition(&binding.Status.Conditions, metav1.Condition{
			Status:             metav1.ConditionFalse,
			Type:               string(fleetv1beta1.ResourceBindingAvailable),
			Reason:             condition.WorkNotAvailableReason,
			Message:            fmt.Sprintf("Work object %s has not yet completed availability check", firstWorkWithIncompleteAvailabilityCheck.Name),
			ObservedGeneration: binding.GetGeneration(),
		})
		return workConditionSummarizedStatusIncomplete
	case !areAllWorksAvailabilityCheckSuccessful:
		// All Work objects have completed the availability check, but at least one of them has failed.
		klog.V(2).InfoS("Some works have failed to get available", "binding", klog.KObj(binding), "firstWorkWithFailedAvailabilityCheck", klog.KObj(firstWorkWithFailedAvailabilityCheck))
		meta.SetStatusCondition(&binding.Status.Conditions, metav1.Condition{
			Status:             metav1.ConditionFalse,
			Type:               string(fleetv1beta1.ResourceBindingAvailable),
			Reason:             condition.WorkNotAvailableReason,
			Message:            fmt.Sprintf("Work object %s is not yet available", firstWorkWithFailedAvailabilityCheck.Name),
			ObservedGeneration: binding.GetGeneration(),
		})
		return workConditionSummarizedStatusFalse
	case firstWorkWithSuccessfulAvailabilityCheckDueToUntrackableRes != nil:
		// All Work objects have completed the availability check successfully, and at least one of them has succeeded due to untrackable resources.
		klog.V(2).InfoS("All works associated with the binding are available; untrackable resources are present", "binding", klog.KObj(binding), "firstWorkWithSuccessfulAvailabilityCheckDueToUntrackableRes", klog.KObj(firstWorkWithSuccessfulAvailabilityCheckDueToUntrackableRes))
		meta.SetStatusCondition(&binding.Status.Conditions, metav1.Condition{
			Status:             metav1.ConditionTrue,
			Type:               string(fleetv1beta1.ResourceBindingAvailable),
			Reason:             condition.WorkNotAvailabilityTrackableReason,
			Message:            fmt.Sprintf("The availability of work object %s is not trackable", firstWorkWithSuccessfulAvailabilityCheckDueToUntrackableRes.Name),
			ObservedGeneration: binding.GetGeneration(),
		})
		return workConditionSummarizedStatusTrue
	default:
		// All Work objects have completed the availability check successfully.
		klog.V(2).InfoS("All works associated with the binding are available", "binding", klog.KObj(binding))
		meta.SetStatusCondition(&binding.Status.Conditions, metav1.Condition{
			Status:             metav1.ConditionTrue,
			Type:               string(fleetv1beta1.ResourceBindingAvailable),
			Reason:             condition.AllWorkAvailableReason,
			Message:            "All corresponding work objects are available",
			ObservedGeneration: binding.GetGeneration(),
		})
		return workConditionSummarizedStatusTrue
	}
}