Skip to content

Commit

Permalink
Merge pull request #270 from alauda/perf/recycle-pod-early
Browse files Browse the repository at this point in the history
perf: recycle ip and lsp for pod that in failed or succeeded phase
  • Loading branch information
oilbeater committed Mar 12, 2020
2 parents a5d4321 + 5a1cf37 commit 97c00bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ func (c *Controller) InitIPAM() error {
return err
}
for _, pod := range pods {
if pod.Annotations[util.AllocatedAnnotation] == "true" &&
if isPodAlive(pod) &&
pod.Annotations[util.AllocatedAnnotation] == "true" &&
pod.Annotations[util.LogicalSwitchAnnotation] != "" {
_, _, err := c.ipam.GetStaticAddress(
fmt.Sprintf("%s/%s", pod.Namespace, pod.Name),
Expand Down
21 changes: 19 additions & 2 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ import (
"k8s.io/klog"
)

func isPodAlive(p *v1.Pod) bool {
if p.Status.Phase == v1.PodSucceeded && p.Spec.RestartPolicy != v1.RestartPolicyAlways {
return false
}

if p.Status.Phase == v1.PodFailed && p.Spec.RestartPolicy == v1.RestartPolicyNever {
return false
}

if p.Status.Phase == v1.PodFailed && p.Status.Reason == "Evicted" {
return false
}
return true
}

func (c *Controller) enqueueAddPod(obj interface{}) {
if !c.isLeader() {
return
Expand All @@ -46,7 +61,8 @@ func (c *Controller) enqueueAddPod(obj interface{}) {
if p.Spec.HostNetwork {
return
}
if p.Status.Phase == v1.PodFailed && p.Status.Reason == "Evicted" {

if !isPodAlive(p) {
c.deletePodQueue.Add(key)
return
}
Expand Down Expand Up @@ -142,10 +158,11 @@ func (c *Controller) enqueueUpdatePod(oldObj, newObj interface{}) {
return
}

if newPod.Status.Phase == v1.PodFailed && newPod.Status.Reason == "Evicted" {
if !isPodAlive(newPod) {
c.deletePodQueue.Add(key)
return
}

// pod assigned an ip
if oldPod.Status.PodIP != newPod.Status.PodIP {
klog.V(3).Infof("enqueue update pod %s", key)
Expand Down

0 comments on commit 97c00bd

Please sign in to comment.