Skip to content

Commit 3f043dd

Browse files
tedyuyutedz
authored andcommitted
Sort init container statuses using non-nested loop
1 parent 9ddc9cc commit 3f043dd

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

pkg/kubelet/kubelet_pods.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,18 +1545,17 @@ func (kl *Kubelet) convertToAPIContainerStatuses(pod *v1.Pod, podStatus *kubecon
15451545
statuses[container.Name] = status
15461546
}
15471547

1548+
// Sort the container statuses since clients of this interface expect the list
1549+
// of containers in a pod has a deterministic order.
1550+
if isInitContainer {
1551+
return kubetypes.SortStatusesOfInitContainers(pod, statuses)
1552+
}
15481553
var containerStatuses []v1.ContainerStatus
15491554
for _, status := range statuses {
15501555
containerStatuses = append(containerStatuses, *status)
15511556
}
15521557

1553-
// Sort the container statuses since clients of this interface expect the list
1554-
// of containers in a pod has a deterministic order.
1555-
if isInitContainer {
1556-
kubetypes.SortInitContainerStatuses(pod, containerStatuses)
1557-
} else {
1558-
sort.Sort(kubetypes.SortedContainerStatuses(containerStatuses))
1559-
}
1558+
sort.Sort(kubetypes.SortedContainerStatuses(containerStatuses))
15601559
return containerStatuses
15611560
}
15621561

pkg/kubelet/types/types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ func SortInitContainerStatuses(p *v1.Pod, statuses []v1.ContainerStatus) {
8585
}
8686
}
8787

88+
func SortStatusesOfInitContainers(p *v1.Pod, statusMap map[string]*v1.ContainerStatus) []v1.ContainerStatus {
89+
containers := p.Spec.InitContainers
90+
statuses := []v1.ContainerStatus{}
91+
for _, container := range containers {
92+
if status, found := statusMap[container.Name]; found {
93+
statuses = append(statuses, *status)
94+
}
95+
}
96+
return statuses
97+
}
98+
8899
// Reservation represents reserved resources for non-pod components.
89100
type Reservation struct {
90101
// System represents resources reserved for non-kubernetes components.

0 commit comments

Comments
 (0)