mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 10:19:50 +00:00
Remove duplicate codes in framework RemovePod
Signed-off-by: kerthcet <kerthcet@gmail.com>
This commit is contained in:
parent
fa88c0b779
commit
9ee94b0204
@ -705,21 +705,27 @@ func podWithRequiredAntiAffinity(p *v1.Pod) bool {
|
||||
len(affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) != 0
|
||||
}
|
||||
|
||||
func removeFromSlice(s []*PodInfo, k string) []*PodInfo {
|
||||
func removeFromSlice(s []*PodInfo, k string) ([]*PodInfo, bool) {
|
||||
var removed bool
|
||||
for i := range s {
|
||||
k2, err := GetPodKey(s[i].Pod)
|
||||
tmpKey, err := GetPodKey(s[i].Pod)
|
||||
if err != nil {
|
||||
klog.ErrorS(err, "Cannot get pod key", "pod", klog.KObj(s[i].Pod))
|
||||
continue
|
||||
}
|
||||
if k == k2 {
|
||||
if k == tmpKey {
|
||||
// delete the element
|
||||
s[i] = s[len(s)-1]
|
||||
s = s[:len(s)-1]
|
||||
removed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
return s
|
||||
// resets the slices to nil so that we can do DeepEqual in unit tests.
|
||||
if len(s) == 0 {
|
||||
return nil, removed
|
||||
}
|
||||
return s, removed
|
||||
}
|
||||
|
||||
// RemovePod subtracts pod information from this NodeInfo.
|
||||
@ -729,27 +735,16 @@ func (n *NodeInfo) RemovePod(pod *v1.Pod) error {
|
||||
return err
|
||||
}
|
||||
if podWithAffinity(pod) {
|
||||
n.PodsWithAffinity = removeFromSlice(n.PodsWithAffinity, k)
|
||||
n.PodsWithAffinity, _ = removeFromSlice(n.PodsWithAffinity, k)
|
||||
}
|
||||
if podWithRequiredAntiAffinity(pod) {
|
||||
n.PodsWithRequiredAntiAffinity = removeFromSlice(n.PodsWithRequiredAntiAffinity, k)
|
||||
n.PodsWithRequiredAntiAffinity, _ = removeFromSlice(n.PodsWithRequiredAntiAffinity, k)
|
||||
}
|
||||
|
||||
for i := range n.Pods {
|
||||
k2, err := GetPodKey(n.Pods[i].Pod)
|
||||
if err != nil {
|
||||
klog.ErrorS(err, "Cannot get pod key", "pod", klog.KObj(n.Pods[i].Pod))
|
||||
continue
|
||||
}
|
||||
if k == k2 {
|
||||
// delete the element
|
||||
n.Pods[i] = n.Pods[len(n.Pods)-1]
|
||||
n.Pods = n.Pods[:len(n.Pods)-1]
|
||||
|
||||
n.update(pod, -1)
|
||||
n.resetSlicesIfEmpty()
|
||||
return nil
|
||||
}
|
||||
var removed bool
|
||||
if n.Pods, removed = removeFromSlice(n.Pods, k); removed {
|
||||
n.update(pod, -1)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("no corresponding pod %s in pods of node %s", pod.Name, n.node.Name)
|
||||
}
|
||||
@ -777,19 +772,6 @@ func (n *NodeInfo) update(pod *v1.Pod, sign int64) {
|
||||
n.Generation = nextGeneration()
|
||||
}
|
||||
|
||||
// resets the slices to nil so that we can do DeepEqual in unit tests.
|
||||
func (n *NodeInfo) resetSlicesIfEmpty() {
|
||||
if len(n.PodsWithAffinity) == 0 {
|
||||
n.PodsWithAffinity = nil
|
||||
}
|
||||
if len(n.PodsWithRequiredAntiAffinity) == 0 {
|
||||
n.PodsWithRequiredAntiAffinity = nil
|
||||
}
|
||||
if len(n.Pods) == 0 {
|
||||
n.Pods = nil
|
||||
}
|
||||
}
|
||||
|
||||
func max(a, b int64) int64 {
|
||||
if a >= b {
|
||||
return a
|
||||
|
Loading…
Reference in New Issue
Block a user