Skip to content

Commit 94ae1d4

Browse files
authored
Merge pull request #182 from Icinga/feature/workload-plugin-output
Adjust pod icinga state reason
2 parents 6c41afe + a43dfd7 commit 94ae1d4

File tree

1 file changed

+25
-57
lines changed

1 file changed

+25
-57
lines changed

pkg/schema/v1/pod.go

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func (p *Pod) getIcingaState(pod *kcorev1.Pod) (IcingaState, string) {
347347
}
348348

349349
if pod.Status.Phase == kcorev1.PodFailed {
350-
state, reasons := collectContainerStates(p)
350+
state, reasons, _ := collectContainerStates(p)
351351

352352
return state, fmt.Sprintf(
353353
"Pod %s/%s has failed as all its containers have been terminated and will not be restarted.\n%s",
@@ -365,60 +365,22 @@ func (p *Pod) getIcingaState(pod *kcorev1.Pod) (IcingaState, string) {
365365
podConditions[kcorev1.ContainersReady].Status == kcorev1.ConditionFalse ||
366366
podConditions[kcorev1.PodReady].Status == kcorev1.ConditionFalse {
367367

368-
state, reasons := collectContainerStates(p)
368+
state, reasons, _ := collectContainerStates(p)
369369

370370
return state, fmt.Sprintf("%s/%s is %s.\n%s", pod.Namespace, pod.Name, state, reasons)
371371
}
372372

373-
var notRunning int
374-
state := Ok
375-
reasons := make([]string, 0, len(p.InitContainers)+len(p.SidecarContainers)+len(p.Containers))
376-
377-
for _, c := range p.InitContainers {
378-
if c.IcingaState != Ok {
379-
state = max(state, c.IcingaState)
380-
notRunning++
381-
}
382-
383-
reasons = append(reasons, fmt.Sprintf(
384-
"[%s] %s", strings.ToUpper(c.IcingaState.String()), c.IcingaStateReason))
385-
}
386-
387-
for _, c := range p.SidecarContainers {
388-
if c.IcingaState != Ok {
389-
state = max(state, c.IcingaState)
390-
notRunning++
391-
}
392-
393-
reasons = append(reasons, fmt.Sprintf(
394-
"[%s] %s", strings.ToUpper(c.IcingaState.String()), c.IcingaStateReason))
395-
}
396-
397-
for _, c := range p.Containers {
398-
if c.IcingaState != Ok {
399-
state = max(state, c.IcingaState)
400-
notRunning++
401-
}
402-
403-
reasons = append(reasons, fmt.Sprintf(
404-
"[%s] %s", strings.ToUpper(c.IcingaState.String()), c.IcingaStateReason))
405-
}
406-
407-
if len(reasons) == 1 {
408-
// Remove square brackets state information.
409-
_, reason, _ := strings.Cut(reasons[0], " ")
410-
411-
return state, reason
412-
}
373+
state, reasons, notRunning := collectContainerStates(p)
413374

414375
return state, fmt.Sprintf(
415376
"Pod %s/%s is %s with %d out of %d containers running.\n%s",
416377
pod.Namespace,
417378
pod.Name,
418379
state,
419-
len(p.Containers)-notRunning,
420-
len(p.Containers),
421-
strings.Join(reasons, "\n"))
380+
len(p.Containers)+len(p.SidecarContainers)-notRunning,
381+
len(p.Containers)+len(p.SidecarContainers),
382+
reasons,
383+
)
422384
}
423385

424386
func NewContainers[T any](
@@ -506,34 +468,40 @@ func removeTrailingWhitespaceAndFullStop(input string) string {
506468
return trimmed
507469
}
508470

509-
func collectContainerStates(pod *Pod) (IcingaState, string) {
471+
func collectContainerStates(pod *Pod) (IcingaState, string, int) {
510472
state := Ok
511473
reasons := make([]string, 0, len(pod.Containers)+len(pod.SidecarContainers)+len(pod.InitContainers))
474+
notRunning := 0
512475

513476
for _, c := range pod.InitContainers {
514-
state = max(state, c.IcingaState)
477+
if c.IcingaState != Ok {
478+
state = max(state, c.IcingaState)
479+
notRunning++
480+
}
481+
515482
reasons = append(reasons, fmt.Sprintf(
516483
"[%s] %s", strings.ToUpper(c.IcingaState.String()), c.IcingaStateReason))
517484
}
518485

519486
for _, c := range pod.SidecarContainers {
520-
state = max(state, c.IcingaState)
487+
if c.IcingaState != Ok {
488+
state = max(state, c.IcingaState)
489+
notRunning++
490+
}
491+
521492
reasons = append(reasons, fmt.Sprintf(
522493
"[%s] %s", strings.ToUpper(c.IcingaState.String()), c.IcingaStateReason))
523494
}
524495

525496
for _, c := range pod.Containers {
526-
state = max(state, c.IcingaState)
497+
if c.IcingaState != Ok {
498+
state = max(state, c.IcingaState)
499+
notRunning++
500+
}
501+
527502
reasons = append(reasons, fmt.Sprintf(
528503
"[%s] %s", strings.ToUpper(c.IcingaState.String()), c.IcingaStateReason))
529504
}
530505

531-
if len(reasons) == 1 {
532-
// Remove square brackets state information.
533-
_, reason, _ := strings.Cut(reasons[0], " ")
534-
535-
return state, reason
536-
}
537-
538-
return state, strings.Join(reasons, "\n")
506+
return state, strings.Join(reasons, "\n"), notRunning
539507
}

0 commit comments

Comments
 (0)