in pkg/networkaware/networkoverhead/networkoverhead.go [175:298]
func (no *NetworkOverhead) PreFilter(ctx context.Context, state *framework.CycleState, pod *corev1.Pod) (*framework.PreFilterResult, *framework.Status) {
// Init PreFilter State
preFilterState := &PreFilterState{
scoreEqually: true,
}
// Write initial status
state.Write(preFilterStateKey, preFilterState)
// Check if Pod belongs to an AppGroup
agName := networkawareutil.GetPodAppGroupLabel(pod)
if len(agName) == 0 { // Return
return nil, framework.NewStatus(framework.Success, "Pod does not belong to an AppGroup, return")
}
// Get AppGroup CR
appGroup := no.findAppGroupNetworkOverhead(agName)
// Get NetworkTopology CR
networkTopology := no.findNetworkTopologyNetworkOverhead()
// Sort Costs if manual weights were selected
no.sortNetworkTopologyCosts(networkTopology)
// Get Dependencies of the given pod
dependencyList := networkawareutil.GetDependencyList(pod, appGroup)
// If the pod has no dependencies, return
if dependencyList == nil {
return nil, framework.NewStatus(framework.Success, "Pod has no dependencies, return")
}
// Get pods from lister
selector := labels.Set(map[string]string{agv1alpha1.AppGroupLabel: agName}).AsSelector()
pods, err := no.podLister.List(selector)
if err != nil {
return nil, framework.NewStatus(framework.Success, "Error while returning pods from appGroup, return")
}
// Return if pods are not yet allocated for the AppGroup...
if len(pods) == 0 {
return nil, framework.NewStatus(framework.Success, "No pods yet allocated, return")
}
// Pods already scheduled: Get Scheduled List (Deployment name, replicaID, hostname)
scheduledList := networkawareutil.GetScheduledList(pods)
// Check if scheduledList is empty...
if len(scheduledList) == 0 {
klog.ErrorS(nil, "Scheduled list is empty, return")
return nil, framework.NewStatus(framework.Success, "Scheduled list is empty, return")
}
// Get all nodes
nodeList, err := no.handle.SnapshotSharedLister().NodeInfos().List()
if err != nil {
return nil, framework.NewStatus(framework.Error, fmt.Sprintf("Error getting the nodelist: %v", err))
}
// Create variables to fill PreFilterState
nodeCostMap := make(map[string]map[networkawareutil.CostKey]int64)
satisfiedMap := make(map[string]int64)
violatedMap := make(map[string]int64)
finalCostMap := make(map[string]int64)
// For each node:
// 1 - Get region and zone labels
// 2 - Calculate satisfied and violated number of dependencies
// 3 - Calculate the final cost of the node to be used by the scoring plugin
for _, nodeInfo := range nodeList {
// retrieve region and zone labels
region := networkawareutil.GetNodeRegion(nodeInfo.Node())
zone := networkawareutil.GetNodeZone(nodeInfo.Node())
klog.V(6).InfoS("Node info",
"name", nodeInfo.Node().Name,
"region", region,
"zone", zone)
// Create map for cost / destinations. Search for requirements faster...
costMap := make(map[networkawareutil.CostKey]int64)
// Populate cost map for the given node
no.populateCostMap(costMap, networkTopology, region, zone)
klog.V(6).InfoS("Map", "costMap", costMap)
// Update nodeCostMap
nodeCostMap[nodeInfo.Node().Name] = costMap
// Get Satisfied and Violated number of dependencies
satisfied, violated, ok := checkMaxNetworkCostRequirements(scheduledList, dependencyList, nodeInfo, region, zone, costMap, no)
if ok != nil {
return nil, framework.NewStatus(framework.Error, fmt.Sprintf("pod hostname not found: %v", ok))
}
// Update Satisfied and Violated maps
satisfiedMap[nodeInfo.Node().Name] = satisfied
violatedMap[nodeInfo.Node().Name] = violated
klog.V(6).InfoS("Number of dependencies", "satisfied", satisfied, "violated", violated)
// Get accumulated cost based on pod dependencies
cost, ok := no.getAccumulatedCost(scheduledList, dependencyList, nodeInfo.Node().Name, region, zone, costMap)
if ok != nil {
return nil, framework.NewStatus(framework.Error, fmt.Sprintf("getting pod hostname from Snapshot: %v", ok))
}
klog.V(6).InfoS("Node final cost", "cost", cost)
finalCostMap[nodeInfo.Node().Name] = cost
}
// Update PreFilter State
preFilterState = &PreFilterState{
scoreEqually: false,
agName: agName,
appGroup: appGroup,
networkTopology: networkTopology,
dependencyList: dependencyList,
scheduledList: scheduledList,
nodeCostMap: nodeCostMap,
satisfiedMap: satisfiedMap,
violatedMap: violatedMap,
finalCostMap: finalCostMap,
}
state.Write(preFilterStateKey, preFilterState)
return nil, framework.NewStatus(framework.Success, "PreFilter State updated")
}