Sort the daemon pods by creation time

pull/6/head
hzliangmingqiang 2015-09-29 17:07:43 +08:00 committed by mqliang
parent 3902f76512
commit b1412997d6
1 changed files with 14 additions and 1 deletions

View File

@ -366,7 +366,8 @@ func (dsc *DaemonSetsController) manage(ds *experimental.DaemonSet) {
nodesNeedingDaemonPods = append(nodesNeedingDaemonPods, nodeName) nodesNeedingDaemonPods = append(nodesNeedingDaemonPods, nodeName)
} else if shouldRun && len(daemonPods) > 1 { } else if shouldRun && len(daemonPods) > 1 {
// If daemon pod is supposed to be running on node, but more than 1 daemon pod is running, delete the excess daemon pods. // If daemon pod is supposed to be running on node, but more than 1 daemon pod is running, delete the excess daemon pods.
// TODO: sort the daemon pods by creation time, so the the oldest is preserved. // Sort the daemon pods by creation time, so the the oldest is preserved.
sort.Sort(podByCreationTimestamp(daemonPods))
for i := 1; i < len(daemonPods); i++ { for i := 1; i < len(daemonPods); i++ {
podsToDelete = append(podsToDelete, daemonPods[i].Name) podsToDelete = append(podsToDelete, daemonPods[i].Name)
} }
@ -519,3 +520,15 @@ func (o byCreationTimestamp) Less(i, j int) bool {
} }
return o[i].CreationTimestamp.Before(o[j].CreationTimestamp) return o[i].CreationTimestamp.Before(o[j].CreationTimestamp)
} }
type podByCreationTimestamp []*api.Pod
func (o podByCreationTimestamp) Len() int { return len(o) }
func (o podByCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o podByCreationTimestamp) Less(i, j int) bool {
if o[i].CreationTimestamp.Equal(o[j].CreationTimestamp) {
return o[i].Name < o[j].Name
}
return o[i].CreationTimestamp.Before(o[j].CreationTimestamp)
}