Skip to content

Commit

Permalink
add support for isPVC check in basic_cluster_snapshot.go and delta_cl…
Browse files Browse the repository at this point in the history
…uster_snapshot.go

- added a comment in delegating_shared_lister.go
  • Loading branch information
jayantjain93 committed Jul 7, 2022
1 parent f508ee1 commit aab1c73
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
54 changes: 50 additions & 4 deletions cluster-autoscaler/simulator/basic_cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type BasicClusterSnapshot struct {
}

type internalBasicSnapshotData struct {
nodeInfoMap map[string]*schedulerframework.NodeInfo
nodeInfoMap map[string]*schedulerframework.NodeInfo
pvcNamespaceNodeMap map[string]map[*schedulerframework.NodeInfo]bool
}

func (data *internalBasicSnapshotData) listNodeInfos() ([]*schedulerframework.NodeInfo, error) {
Expand Down Expand Up @@ -71,9 +72,43 @@ func (data *internalBasicSnapshotData) getNodeInfo(nodeName string) (*schedulerf
return nil, errNodeNotFound
}

func (data *internalBasicSnapshotData) isPVCUsedByPods(key string) bool {
if v, found := data.pvcNamespaceNodeMap[key]; found && v != nil && len(v) > 0 {
return true
}
return false
}

func (data *internalBasicSnapshotData) updatePvcUsedByNodes(nodeInfo *schedulerframework.NodeInfo) {
if nodeInfo == nil {
return
}
data.removePvcUsedByNodes(nodeInfo)
for k := range nodeInfo.PVCRefCounts {
_, found := data.pvcNamespaceNodeMap[k]
if !found {
data.pvcNamespaceNodeMap[k] = make(map[*schedulerframework.NodeInfo]bool)
}
data.pvcNamespaceNodeMap[k][nodeInfo] = true
}
}

func (data *internalBasicSnapshotData) removePvcUsedByNodes(nodeInfo *schedulerframework.NodeInfo) {
if nodeInfo == nil {
return
}

for _, nodesMap := range data.pvcNamespaceNodeMap {
if _, found := nodesMap[nodeInfo]; found {
delete(nodesMap, nodeInfo)
}
}
}

func newInternalBasicSnapshotData() *internalBasicSnapshotData {
return &internalBasicSnapshotData{
nodeInfoMap: make(map[string]*schedulerframework.NodeInfo),
nodeInfoMap: make(map[string]*schedulerframework.NodeInfo),
pvcNamespaceNodeMap: make(map[string]map[*schedulerframework.NodeInfo]bool),
}
}

Expand All @@ -82,8 +117,16 @@ func (data *internalBasicSnapshotData) clone() *internalBasicSnapshotData {
for k, v := range data.nodeInfoMap {
clonedNodeInfoMap[k] = v.Clone()
}
clonedPvcNamespaceNodeMap := make(map[string]map[*schedulerframework.NodeInfo]bool)
for k, v := range data.pvcNamespaceNodeMap {
clonedPvcNamespaceNodeMap[k] = make(map[*schedulerframework.NodeInfo]bool)
for k1, v1 := range v {
clonedPvcNamespaceNodeMap[k][k1] = v1
}
}
return &internalBasicSnapshotData{
nodeInfoMap: clonedNodeInfoMap,
nodeInfoMap: clonedNodeInfoMap,
pvcNamespaceNodeMap: clonedPvcNamespaceNodeMap,
}
}

Expand All @@ -110,6 +153,7 @@ func (data *internalBasicSnapshotData) removeNode(nodeName string) error {
if _, found := data.nodeInfoMap[nodeName]; !found {
return errNodeNotFound
}
data.removePvcUsedByNodes(data.nodeInfoMap[nodeName])
delete(data.nodeInfoMap, nodeName)
return nil
}
Expand All @@ -119,6 +163,7 @@ func (data *internalBasicSnapshotData) addPod(pod *apiv1.Pod, nodeName string) e
return errNodeNotFound
}
data.nodeInfoMap[nodeName].AddPod(pod)
data.updatePvcUsedByNodes(data.nodeInfoMap[nodeName])
return nil
}

Expand All @@ -133,6 +178,7 @@ func (data *internalBasicSnapshotData) removePod(namespace, podName, nodeName st
if err != nil {
return fmt.Errorf("cannot remove pod; %v", err)
}
data.updatePvcUsedByNodes(nodeInfo)
return nil
}
}
Expand Down Expand Up @@ -261,5 +307,5 @@ func (snapshot *basicClusterSnapshotNodeLister) Get(nodeName string) (*scheduler

// Returns the IsPVCUsedByPods in a given key.
func (snapshot *basicClusterSnapshotStorageLister) IsPVCUsedByPods(key string) bool {
return false
return (*BasicClusterSnapshot)(snapshot).getInternalData().isPVCUsedByPods(key)
}
1 change: 1 addition & 0 deletions cluster-autoscaler/simulator/delegating_shared_lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (lister *DelegatingSchedulerSharedLister) NodeInfos() schedulerframework.No
return lister.delegate.NodeInfos()
}

// StorageInfos returns a StorageInfoLister
func (lister *DelegatingSchedulerSharedLister) StorageInfos() schedulerframework.StorageInfoLister {
return lister.delegate.StorageInfos()
}
Expand Down
6 changes: 6 additions & 0 deletions cluster-autoscaler/simulator/delta_cluster_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ func (snapshot *deltaSnapshotNodeLister) Get(nodeName string) (*schedulerframewo

// IsPVCUsedByPods returns if PVC is used by pods
func (snapshot *deltaSnapshotStorageLister) IsPVCUsedByPods(key string) bool {
nodeInfos := snapshot.data.getNodeInfoList()
for _, v := range nodeInfos {
if v.PVCRefCounts[key] > 0 {
return true
}
}
return false
}

Expand Down

0 comments on commit aab1c73

Please sign in to comment.