diff --git a/cluster-autoscaler/simulator/basic_cluster_snapshot.go b/cluster-autoscaler/simulator/basic_cluster_snapshot.go index 780be803c7..0f3e3e1194 100644 --- a/cluster-autoscaler/simulator/basic_cluster_snapshot.go +++ b/cluster-autoscaler/simulator/basic_cluster_snapshot.go @@ -53,6 +53,17 @@ func (data *internalBasicSnapshotData) listNodeInfosThatHavePodsWithAffinityList return havePodsWithAffinityList, nil } +func (data *internalBasicSnapshotData) listNodeInfosThatHavePodsWithRequiredAntiAffinityList() ([]*schedulerframework.NodeInfo, error) { + havePodsWithRequiredAntiAffinityList := make([]*schedulerframework.NodeInfo, 0, len(data.nodeInfoMap)) + for _, v := range data.nodeInfoMap { + if len(v.PodsWithRequiredAntiAffinity) > 0 { + havePodsWithRequiredAntiAffinityList = append(havePodsWithRequiredAntiAffinityList, v) + } + } + + return havePodsWithRequiredAntiAffinityList, nil +} + func (data *internalBasicSnapshotData) getNodeInfo(nodeName string) (*schedulerframework.NodeInfo, error) { if v, ok := data.nodeInfoMap[nodeName]; ok { return v, nil @@ -235,6 +246,11 @@ func (snapshot *basicClusterSnapshotNodeLister) HavePodsWithAffinityList() ([]*s return (*BasicClusterSnapshot)(snapshot).getInternalData().listNodeInfosThatHavePodsWithAffinityList() } +// HavePodsWithRequiredAntiAffinityList returns the list of NodeInfos of nodes with pods with required anti-affinity terms. +func (snapshot *basicClusterSnapshotNodeLister) HavePodsWithRequiredAntiAffinityList() ([]*schedulerframework.NodeInfo, error) { + return (*BasicClusterSnapshot)(snapshot).getInternalData().listNodeInfosThatHavePodsWithRequiredAntiAffinityList() +} + // Returns the NodeInfo of the given node name. func (snapshot *basicClusterSnapshotNodeLister) Get(nodeName string) (*schedulerframework.NodeInfo, error) { return (*BasicClusterSnapshot)(snapshot).getInternalData().getNodeInfo(nodeName) diff --git a/cluster-autoscaler/simulator/delegating_shared_lister.go b/cluster-autoscaler/simulator/delegating_shared_lister.go index 5254970cf4..ea21bc4e50 100644 --- a/cluster-autoscaler/simulator/delegating_shared_lister.go +++ b/cluster-autoscaler/simulator/delegating_shared_lister.go @@ -62,6 +62,11 @@ func (lister *unsetNodeInfoLister) HavePodsWithAffinityList() ([]*schedulerframe return nil, fmt.Errorf("lister not set in delegate") } +// HavePodsWithRequiredAntiAffinityList always returns an error. +func (lister *unsetNodeInfoLister) HavePodsWithRequiredAntiAffinityList() ([]*schedulerframework.NodeInfo, error) { + return nil, fmt.Errorf("lister not set in delegate") +} + // Get always returns an error func (lister *unsetNodeInfoLister) Get(nodeName string) (*schedulerframework.NodeInfo, error) { return nil, fmt.Errorf("lister not set in delegate") diff --git a/cluster-autoscaler/simulator/delta_cluster_snapshot.go b/cluster-autoscaler/simulator/delta_cluster_snapshot.go index 1aba8ff990..e06d987bbd 100644 --- a/cluster-autoscaler/simulator/delta_cluster_snapshot.go +++ b/cluster-autoscaler/simulator/delta_cluster_snapshot.go @@ -52,8 +52,9 @@ type internalDeltaSnapshotData struct { modifiedNodeInfoMap map[string]*schedulerframework.NodeInfo deletedNodeInfos map[string]bool - nodeInfoList []*schedulerframework.NodeInfo - havePodsWithAffinity []*schedulerframework.NodeInfo + nodeInfoList []*schedulerframework.NodeInfo + havePodsWithAffinity []*schedulerframework.NodeInfo + havePodsWithRequiredAntiAffinity []*schedulerframework.NodeInfo } func newInternalDeltaSnapshotData() *internalDeltaSnapshotData { @@ -178,6 +179,7 @@ func (data *internalDeltaSnapshotData) clearCaches() { func (data *internalDeltaSnapshotData) clearPodCaches() { data.havePodsWithAffinity = nil + data.havePodsWithRequiredAntiAffinity = nil } func (data *internalDeltaSnapshotData) removeNode(nodeName string) error { @@ -326,6 +328,24 @@ func (snapshot *deltaSnapshotNodeLister) HavePodsWithAffinityList() ([]*schedule return data.havePodsWithAffinity, nil } +// HavePodsWithRequiredAntiAffinityList returns the list of NodeInfos of nodes with pods with required anti-affinity terms. +func (snapshot *deltaSnapshotNodeLister) HavePodsWithRequiredAntiAffinityList() ([]*schedulerframework.NodeInfo, error) { + data := snapshot.data + if data.havePodsWithRequiredAntiAffinity != nil { + return data.havePodsWithRequiredAntiAffinity, nil + } + + nodeInfoList := snapshot.data.getNodeInfoList() + havePodsWithRequiredAntiAffinityList := make([]*schedulerframework.NodeInfo, 0, len(nodeInfoList)) + for _, node := range nodeInfoList { + if len(node.PodsWithRequiredAntiAffinity) > 0 { + havePodsWithRequiredAntiAffinityList = append(havePodsWithRequiredAntiAffinityList, node) + } + } + data.havePodsWithRequiredAntiAffinity = havePodsWithRequiredAntiAffinityList + return data.havePodsWithRequiredAntiAffinity, nil +} + // Get returns node info by node name. func (snapshot *deltaSnapshotNodeLister) Get(nodeName string) (*schedulerframework.NodeInfo, error) { return (*DeltaClusterSnapshot)(snapshot).getNodeInfo(nodeName)