func setBindingStatus()

in pkg/controllers/workgenerator/controller.go [848:1003]


func setBindingStatus(works map[string]*fleetv1beta1.Work, resourceBinding *fleetv1beta1.ClusterResourceBinding) {
	bindingRef := klog.KObj(resourceBinding)

	// Note (chenyu1): the work generator will refresh the status of a ClusterResourceBinding using
	// the following logic:
	//
	// a) If the currently active apply strategy (as dictated by the ClusterResourceBinding spec)
	//    is ClientSideApply or ServerSideApply, the work generator will update the Applied and
	//    Available conditions (plus the details about failed, diffed, and/or drifted placements)
	//    in the status, as appropriate; the DiffReported condition will not be updated.
	// b) If the currently active apply strategy is ReportDiff, the work generator will update
	//    the DiffReported condition in the status, plus the details about diffed placements;
	//    the Applied and Available conditions (plus the details about failed and/or drifted placements)
	//    will not be updated.

	// try to gather the resource binding applied status if we didn't update any associated work spec this time

	var isReportDiffModeOn = resourceBinding.Spec.ApplyStrategy != nil && resourceBinding.Spec.ApplyStrategy.Type == fleetv1beta1.ApplyStrategyTypeReportDiff
	var appliedSummarizedStatus, availabilitySummarizedStatus, diffReportedSummarizedStatus workConditionSummarizedStatus
	if isReportDiffModeOn {
		// Set the DiffReported condition if (and only if) a ReportDiff apply strategy is currently
		// being used.
		diffReportedSummarizedStatus = setAllWorkDiffReportedCondition(works, resourceBinding)
	} else {
		// Set the Applied and Available condition if (and only if) a ClientSideApply or ServerSideApply
		// apply strategy is currently being used.
		appliedSummarizedStatus = setAllWorkAppliedCondition(works, resourceBinding)
		// Note that Fleet will only set the Available condition if the apply op itself is successful, i.e.,
		// the Applied condition is True.
		availabilitySummarizedStatus = setAllWorkAvailableCondition(works, resourceBinding)
	}

	resourceBinding.Status.FailedPlacements = nil
	resourceBinding.Status.DiffedPlacements = nil
	resourceBinding.Status.DriftedPlacements = nil
	// collect and set the failed resource placements to the binding if not all the works are available
	driftedResourcePlacements := make([]fleetv1beta1.DriftedResourcePlacement, 0, maxDriftedResourcePlacementLimit) // preallocate the memory
	failedResourcePlacements := make([]fleetv1beta1.FailedResourcePlacement, 0, maxFailedResourcePlacementLimit)    // preallocate the memory
	diffedResourcePlacements := make([]fleetv1beta1.DiffedResourcePlacement, 0, maxDiffedResourcePlacementLimit)    // preallocate the memory
	for _, w := range works {
		if w.DeletionTimestamp != nil {
			klog.V(2).InfoS("Ignoring the deleting work", "clusterResourceBinding", bindingRef, "work", klog.KObj(w))
			continue // ignore the deleting work
		}

		// Populate the failed, diffed, and drifted placements based on the summarized status of the Applied,
		// Available, and DiffReported conditions on all Work objects.
		//
		// Note (chenyu1): Fleet will only report apply/availability check failures, diffs, and drifts (as applicable)
		// when all the Work objects have completed their apply ops, availability checks, and diff reporting, as dictated
		// by the currently specified apply strategy (successful or not). This is to make sure that previously
		// populated failures, diffs, and/or drifts will not leak into the current reportings.
		switch {
		case isReportDiffModeOn && diffReportedSummarizedStatus == workConditionSummarizedStatusTrue:
			// The ReportDiff apply straregy is in use and all works have reported configuration
			// differences.
			//
			// In this case, set diffed placements only; failed and drifted placements will not
			// be set (apply/availability check failure and drifts cannot occur in report diff mode).
			diffedManifests := extractDiffedResourcePlacementsFromWork(w)
			diffedResourcePlacements = append(diffedResourcePlacements, diffedManifests...)
		case isReportDiffModeOn:
			// The ReportDiff apply strategy is in use but not all works have reported configuration
			// differences.
			//
			// In this case, no diffed, failed, or drifted placements will be set (diff information present
			// might be incomplete or stale; apply/availability check failure and drifts cannot occur in
			// report diff mode).
		case appliedSummarizedStatus == workConditionSummarizedStatusIncomplete:
			// The ClientSideApply or ServerSideApply apply strategy is in use but some of the works have
			// not been applied yet.
			//
			// In this case, no diffed, failed, or drifted placements will be set (as information present
			// might be incomplete or stale).
		case appliedSummarizedStatus == workConditionSummarizedStatusFalse:
			// The ClientSideApply or ServerSideApply apply strategy is in use but some of the works have
			// apply op failures.
			//
			// In this case, set failed, diffed, and drifted placements.
			failedManifests := extractFailedResourcePlacementsFromWork(w)
			failedResourcePlacements = append(failedResourcePlacements, failedManifests...)

			diffedManifests := extractDiffedResourcePlacementsFromWork(w)
			diffedResourcePlacements = append(diffedResourcePlacements, diffedManifests...)

			driftedManifests := extractDriftedResourcePlacementsFromWork(w)
			driftedResourcePlacements = append(driftedResourcePlacements, driftedManifests...)
		case availabilitySummarizedStatus == workConditionSummarizedStatusIncomplete:
			// The ClientSideApply or ServerSideApply apply strategy is in use; all works have been applied but
			// some of the works have not completed the availability check yet.
			//
			// In theory this would not happen as the Fleet work applier will always set the Applied and
			// Available conditions together. However, Fleet can still handle this case for completeness reasons.
			//
			// In this case, set drifted placements; no failed or diffed placements will be set (availability
			// check failure information might be incomplete or stale; diffs will only occur when there
			// is an apply failure or the report diff mode is on).
			driftedManifests := extractDriftedResourcePlacementsFromWork(w)
			driftedResourcePlacements = append(driftedResourcePlacements, driftedManifests...)
		case availabilitySummarizedStatus == workConditionSummarizedStatusFalse:
			// The ClientSideApply or ServerSideApply apply strategy is in use; all works have been applied but
			// some of them have failed the availability check.
			//
			// In this case, set failed and drifted placements; no diffed placements will be set (diffs
			// will only occur when there is an apply failure or the report diff mode is on).
			failedManifests := extractFailedResourcePlacementsFromWork(w)
			failedResourcePlacements = append(failedResourcePlacements, failedManifests...)

			driftedManifests := extractDriftedResourcePlacementsFromWork(w)
			driftedResourcePlacements = append(driftedResourcePlacements, driftedManifests...)
		default:
			// The ClientSideApply or ServerSideApply apply strategy is in use; all works have been applied
			// and are available.
			//
			// In this case, set only drifted placements (drifts might occur even if the apply op itself
			// completes); no failed or diffed placements will be set (apply/availability
			// check failure and diffs will not occur when all works are applied and available).
			driftedManifests := extractDriftedResourcePlacementsFromWork(w)
			driftedResourcePlacements = append(driftedResourcePlacements, driftedManifests...)
		}
	}
	// cut the list to keep only the max limit
	if len(failedResourcePlacements) > maxFailedResourcePlacementLimit {
		failedResourcePlacements = failedResourcePlacements[0:maxFailedResourcePlacementLimit]
	}
	if len(failedResourcePlacements) > 0 {
		resourceBinding.Status.FailedPlacements = failedResourcePlacements
		klog.V(2).InfoS("Populated failed manifests", "clusterResourceBinding", bindingRef, "numberOfFailedPlacements", len(failedResourcePlacements))
	}

	// cut the list to keep only the max limit
	if len(diffedResourcePlacements) > maxDiffedResourcePlacementLimit {
		// Sort the slice
		sort.Slice(diffedResourcePlacements, func(i, j int) bool {
			return utils.LessFuncDiffedResourcePlacements(diffedResourcePlacements[i], diffedResourcePlacements[j])
		})
		diffedResourcePlacements = diffedResourcePlacements[0:maxDiffedResourcePlacementLimit]
	}
	if len(diffedResourcePlacements) > 0 {
		resourceBinding.Status.DiffedPlacements = diffedResourcePlacements
		klog.V(2).InfoS("Populated diffed manifests", "clusterResourceBinding", bindingRef, "numberOfDiffedPlacements", len(diffedResourcePlacements))
	}

	// cut the list to keep only the max limit
	if len(driftedResourcePlacements) > maxDriftedResourcePlacementLimit {
		// Sort the slice
		sort.Slice(driftedResourcePlacements, func(i, j int) bool {
			return utils.LessFuncDriftedResourcePlacements(driftedResourcePlacements[i], driftedResourcePlacements[j])
		})
		driftedResourcePlacements = driftedResourcePlacements[0:maxDriftedResourcePlacementLimit]
	}
	if len(driftedResourcePlacements) > 0 {
		resourceBinding.Status.DriftedPlacements = driftedResourcePlacements
		klog.V(2).InfoS("Populated drifted manifests", "clusterResourceBinding", bindingRef, "numberOfDriftedPlacements", len(driftedResourcePlacements))
	}
}