Skip to content

Commit 5afc42d

Browse files
authored
Merge pull request kubernetes#78373 from tedyu/sort-init-container
Sort init container statuses using non-nested loop
2 parents 7e75a5e + 3f043dd commit 5afc42d

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
@@ -1616,18 +1616,17 @@ func (kl *Kubelet) convertToAPIContainerStatuses(pod *v1.Pod, podStatus *kubecon
16161616
statuses[container.Name] = status
16171617
}
16181618

1619+
// Sort the container statuses since clients of this interface expect the list
1620+
// of containers in a pod has a deterministic order.
1621+
if isInitContainer {
1622+
return kubetypes.SortStatusesOfInitContainers(pod, statuses)
1623+
}
16191624
var containerStatuses []v1.ContainerStatus
16201625
for _, status := range statuses {
16211626
containerStatuses = append(containerStatuses, *status)
16221627
}
16231628

1624-
// Sort the container statuses since clients of this interface expect the list
1625-
// of containers in a pod has a deterministic order.
1626-
if isInitContainer {
1627-
kubetypes.SortInitContainerStatuses(pod, containerStatuses)
1628-
} else {
1629-
sort.Sort(kubetypes.SortedContainerStatuses(containerStatuses))
1630-
}
1629+
sort.Sort(kubetypes.SortedContainerStatuses(containerStatuses))
16311630
return containerStatuses
16321631
}
16331632

pkg/kubelet/types/types.go

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

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

0 commit comments

Comments
 (0)