in pkg/scheduler/framework/frameworkutils.go [608:727]
func crossReferenceValidTargetsWithBindings(
crpName string,
policy *placementv1beta1.ClusterSchedulingPolicySnapshot,
valid []*clusterv1beta1.MemberCluster,
bound, scheduled, unscheduled, obsolete []*placementv1beta1.ClusterResourceBinding,
) (
toCreate []*placementv1beta1.ClusterResourceBinding,
toDelete []*placementv1beta1.ClusterResourceBinding,
toPatch []*bindingWithPatch,
err error,
) {
// Pre-allocate with a reasonable capacity.
toCreate = make([]*placementv1beta1.ClusterResourceBinding, 0, len(valid))
toPatch = make([]*bindingWithPatch, 0, 20)
toDelete = make([]*placementv1beta1.ClusterResourceBinding, 0, 20)
// Build maps for quick lookup.
scheduledOrBoundClusterMap := make(map[string]bool)
for _, binding := range scheduled {
scheduledOrBoundClusterMap[binding.Spec.TargetCluster] = true
}
for _, binding := range bound {
scheduledOrBoundClusterMap[binding.Spec.TargetCluster] = true
}
unscheduledClusterMap := make(map[string]*placementv1beta1.ClusterResourceBinding)
for _, binding := range unscheduled {
unscheduledClusterMap[binding.Spec.TargetCluster] = binding
}
obsoleteClusterMap := make(map[string]*placementv1beta1.ClusterResourceBinding)
for _, binding := range obsolete {
obsoleteClusterMap[binding.Spec.TargetCluster] = binding
}
validTargetMap := make(map[string]bool)
for _, cluster := range valid {
validTargetMap[cluster.Name] = true
}
// Perform the cross-reference to find out bindings that should be created or patched.
for _, cluster := range valid {
_, foundInScheduledOrBound := scheduledOrBoundClusterMap[cluster.Name]
obsoleteBinding, foundInObsolete := obsoleteClusterMap[cluster.Name]
unscheduledBinding, foundInUnscheduled := unscheduledClusterMap[cluster.Name]
switch {
case foundInScheduledOrBound:
// The cluster already has a binding of the scheduled or bound state associated;
// do nothing.
case foundInObsolete:
// The cluster already has a binding associated, but it is selected in a previous
// scheduling run; update the binding to refer to the latest scheduling policy
// snapshot.
toPatch = append(toPatch, patchBindingFromFixedCluster(obsoleteBinding, obsoleteBinding.Spec.State, cluster.Name, policy))
case foundInUnscheduled:
// The binding's target cluster is picked again in the current run; yet the binding
// is originally de-selected by the previous scheduling round.
// Add the binding to the toPatch list so that we won't create more and more bindings.
// We need to recover the previous state before the binding is marked as unscheduled.
var desiredState placementv1beta1.BindingState
// we recorded the previous state in the binding's annotation
currentAnnotation := unscheduledBinding.GetAnnotations()
if previousState, exist := currentAnnotation[placementv1beta1.PreviousBindingStateAnnotation]; exist {
desiredState = placementv1beta1.BindingState(previousState)
// remove the annotation just to avoid confusion.
delete(currentAnnotation, placementv1beta1.PreviousBindingStateAnnotation)
unscheduledBinding.SetAnnotations(currentAnnotation)
} else {
return nil, nil, nil, controller.NewUnexpectedBehaviorError(fmt.Errorf("failed to find the previous state of an unscheduled binding: %+v", unscheduledBinding))
}
toPatch = append(toPatch, patchBindingFromFixedCluster(unscheduledBinding, desiredState, cluster.Name, policy))
default:
// The cluster does not have an associated binding yet; create one.
// Generate a unique name.
name, err := uniquename.NewClusterResourceBindingName(crpName, cluster.Name)
if err != nil {
// Cannot get a unique name for the binding; normally this should never happen.
return nil, nil, nil, controller.NewUnexpectedBehaviorError(fmt.Errorf("failed to cross reference picked clusters and existing bindings: %w", err))
}
newBinding := &placementv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
placementv1beta1.CRPTrackingLabel: crpName,
},
Finalizers: []string{placementv1beta1.SchedulerCRBCleanupFinalizer},
},
Spec: placementv1beta1.ResourceBindingSpec{
State: placementv1beta1.BindingStateScheduled,
// Leave the associated resource snapshot name empty; it is up to another controller
// to fulfill this field.
SchedulingPolicySnapshotName: policy.Name,
TargetCluster: cluster.Name,
ClusterDecision: placementv1beta1.ClusterDecision{
ClusterName: cluster.Name,
Selected: true,
// Scoring does not apply in this placement type.
Reason: fmt.Sprintf(resourceScheduleSucceededMessageFormat, cluster.Name),
},
},
}
toCreate = append(toCreate, newBinding)
}
}
// Perform the cross-reference to find out bindings that should be deleted.
for _, binding := range obsolete {
if _, ok := validTargetMap[binding.Spec.TargetCluster]; !ok {
// The cluster is no longer a valid target; mark the binding as unscheduled.
toDelete = append(toDelete, binding)
}
}
return toCreate, toDelete, toPatch, nil
}