Skip to content

Commit e80de49

Browse files
fix(controller/ui): fix pod with sidecar state (#19843)
* fix(controller): change pod status calculate with sidecar Signed-off-by: linghaoSu <linghao.su@daocloud.io> * fix(controller): add restartable sidecar count in total container display Signed-off-by: linghaoSu <linghao.su@daocloud.io> * fix(controller): update info test case conditions Co-authored-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> Signed-off-by: Linghao Su <slh001@live.cn> * fix(controller): add more test case to cover more conditions Signed-off-by: linghaoSu <linghao.su@daocloud.io> * fix(ui): check is condition exist before for of Signed-off-by: linghaoSu <linghao.su@daocloud.io> --------- Signed-off-by: linghaoSu <linghao.su@daocloud.io> Signed-off-by: Linghao Su <slh001@live.cn> Co-authored-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
1 parent d16df52 commit e80de49

File tree

3 files changed

+640
-9
lines changed

3 files changed

+640
-9
lines changed

controller/cache/info.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,32 @@ func populateIstioServiceEntryInfo(un *unstructured.Unstructured, res *ResourceI
296296
}
297297
}
298298

299+
func isPodInitializedConditionTrue(status *v1.PodStatus) bool {
300+
for _, condition := range status.Conditions {
301+
if condition.Type != v1.PodInitialized {
302+
continue
303+
}
304+
305+
return condition.Status == v1.ConditionTrue
306+
}
307+
return false
308+
}
309+
310+
func isRestartableInitContainer(initContainer *v1.Container) bool {
311+
if initContainer == nil {
312+
return false
313+
}
314+
if initContainer.RestartPolicy == nil {
315+
return false
316+
}
317+
318+
return *initContainer.RestartPolicy == v1.ContainerRestartPolicyAlways
319+
}
320+
321+
func isPodPhaseTerminal(phase v1.PodPhase) bool {
322+
return phase == v1.PodFailed || phase == v1.PodSucceeded
323+
}
324+
299325
func populatePodInfo(un *unstructured.Unstructured, res *ResourceInfo) {
300326
pod := v1.Pod{}
301327
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, &pod)
@@ -306,7 +332,8 @@ func populatePodInfo(un *unstructured.Unstructured, res *ResourceInfo) {
306332
totalContainers := len(pod.Spec.Containers)
307333
readyContainers := 0
308334

309-
reason := string(pod.Status.Phase)
335+
podPhase := pod.Status.Phase
336+
reason := string(podPhase)
310337
if pod.Status.Reason != "" {
311338
reason = pod.Status.Reason
312339
}
@@ -324,13 +351,34 @@ func populatePodInfo(un *unstructured.Unstructured, res *ResourceInfo) {
324351
res.Images = append(res.Images, image)
325352
}
326353

354+
// If the Pod carries {type:PodScheduled, reason:SchedulingGated}, set reason to 'SchedulingGated'.
355+
for _, condition := range pod.Status.Conditions {
356+
if condition.Type == v1.PodScheduled && condition.Reason == v1.PodReasonSchedulingGated {
357+
reason = v1.PodReasonSchedulingGated
358+
}
359+
}
360+
361+
initContainers := make(map[string]*v1.Container)
362+
for i := range pod.Spec.InitContainers {
363+
initContainers[pod.Spec.InitContainers[i].Name] = &pod.Spec.InitContainers[i]
364+
if isRestartableInitContainer(&pod.Spec.InitContainers[i]) {
365+
totalContainers++
366+
}
367+
}
368+
327369
initializing := false
328370
for i := range pod.Status.InitContainerStatuses {
329371
container := pod.Status.InitContainerStatuses[i]
330372
restarts += int(container.RestartCount)
331373
switch {
332374
case container.State.Terminated != nil && container.State.Terminated.ExitCode == 0:
333375
continue
376+
case isRestartableInitContainer(initContainers[container.Name]) &&
377+
container.Started != nil && *container.Started:
378+
if container.Ready {
379+
readyContainers++
380+
}
381+
continue
334382
case container.State.Terminated != nil:
335383
// initialization is failed
336384
if len(container.State.Terminated.Reason) == 0 {
@@ -352,8 +400,7 @@ func populatePodInfo(un *unstructured.Unstructured, res *ResourceInfo) {
352400
}
353401
break
354402
}
355-
if !initializing {
356-
restarts = 0
403+
if !initializing || isPodInitializedConditionTrue(&pod.Status) {
357404
hasRunning := false
358405
for i := len(pod.Status.ContainerStatuses) - 1; i >= 0; i-- {
359406
container := pod.Status.ContainerStatuses[i]
@@ -388,7 +435,9 @@ func populatePodInfo(un *unstructured.Unstructured, res *ResourceInfo) {
388435
// and https://github.com/kubernetes/kubernetes/issues/90358#issuecomment-617859364
389436
if pod.DeletionTimestamp != nil && pod.Status.Reason == "NodeLost" {
390437
reason = "Unknown"
391-
} else if pod.DeletionTimestamp != nil {
438+
// If the pod is being deleted and the pod phase is not succeeded or failed, set the reason to "Terminating".
439+
// See https://github.com/kubernetes/kubectl/issues/1595#issuecomment-2080001023
440+
} else if pod.DeletionTimestamp != nil && !isPodPhaseTerminal(podPhase) {
392441
reason = "Terminating"
393442
}
394443

0 commit comments

Comments
 (0)