Skip to content

Commit 28bf0e8

Browse files
authored
Merge pull request #2644 from carlosmorenokm1/fix-pod-status-reason
fix: report correct reason in kube_pod_status_reason metric
2 parents 1018969 + 05187b4 commit 28bf0e8

File tree

2 files changed

+104
-8
lines changed

2 files changed

+104
-8
lines changed

internal/store/pod.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,15 +1541,12 @@ func createPodStatusReasonFamilyGenerator() generator.FamilyGenerator {
15411541
ms := []*metric.Metric{}
15421542

15431543
for _, reason := range podStatusReasons {
1544-
metric := &metric.Metric{}
1545-
metric.LabelKeys = []string{"reason"}
1546-
metric.LabelValues = []string{reason}
1547-
if p.Status.Reason == reason {
1548-
metric.Value = boolFloat64(true)
1549-
} else {
1550-
metric.Value = boolFloat64(false)
1544+
m := &metric.Metric{
1545+
LabelKeys: []string{"reason"},
1546+
LabelValues: []string{reason},
1547+
Value: getPodStatusReasonValue(p, reason),
15511548
}
1552-
ms = append(ms, metric)
1549+
ms = append(ms, m)
15531550
}
15541551

15551552
return &metric.Family{
@@ -1559,6 +1556,23 @@ func createPodStatusReasonFamilyGenerator() generator.FamilyGenerator {
15591556
)
15601557
}
15611558

1559+
func getPodStatusReasonValue(p *v1.Pod, reason string) float64 {
1560+
if p.Status.Reason == reason {
1561+
return 1
1562+
}
1563+
for _, cond := range p.Status.Conditions {
1564+
if cond.Reason == reason {
1565+
return 1
1566+
}
1567+
}
1568+
for _, cs := range p.Status.ContainerStatuses {
1569+
if cs.State.Terminated != nil && cs.State.Terminated.Reason == reason {
1570+
return 1
1571+
}
1572+
}
1573+
return 0
1574+
}
1575+
15621576
func createPodStatusScheduledFamilyGenerator() generator.FamilyGenerator {
15631577
return *generator.NewFamilyGeneratorWithStability(
15641578
"kube_pod_status_scheduled",

internal/store/pod_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,3 +2290,85 @@ func BenchmarkPodStore(b *testing.B) {
22902290
}
22912291
}
22922292
}
2293+
2294+
func TestGetPodStatusReasonValue(t *testing.T) {
2295+
reason := "TestReason"
2296+
2297+
tests := []struct {
2298+
name string
2299+
pod *v1.Pod
2300+
want float64
2301+
}{
2302+
{
2303+
name: "matches Status.Reason",
2304+
pod: &v1.Pod{
2305+
Status: v1.PodStatus{
2306+
Reason: "TestReason",
2307+
},
2308+
},
2309+
want: 1,
2310+
},
2311+
{
2312+
name: "matches condition Reason",
2313+
pod: &v1.Pod{
2314+
Status: v1.PodStatus{
2315+
Conditions: []v1.PodCondition{
2316+
{
2317+
Reason: "TestReason",
2318+
},
2319+
},
2320+
},
2321+
},
2322+
want: 1,
2323+
},
2324+
{
2325+
name: "matches container terminated Reason",
2326+
pod: &v1.Pod{
2327+
Status: v1.PodStatus{
2328+
ContainerStatuses: []v1.ContainerStatus{
2329+
{
2330+
State: v1.ContainerState{
2331+
Terminated: &v1.ContainerStateTerminated{
2332+
Reason: "TestReason",
2333+
},
2334+
},
2335+
},
2336+
},
2337+
},
2338+
},
2339+
want: 1,
2340+
},
2341+
{
2342+
name: "no match returns 0",
2343+
pod: &v1.Pod{
2344+
Status: v1.PodStatus{
2345+
Reason: "OtherReason",
2346+
Conditions: []v1.PodCondition{
2347+
{
2348+
Reason: "NotTestReason",
2349+
},
2350+
},
2351+
ContainerStatuses: []v1.ContainerStatus{
2352+
{
2353+
State: v1.ContainerState{
2354+
Terminated: &v1.ContainerStateTerminated{
2355+
Reason: "AnotherReason",
2356+
},
2357+
},
2358+
},
2359+
},
2360+
},
2361+
},
2362+
want: 0,
2363+
},
2364+
}
2365+
2366+
for _, tt := range tests {
2367+
t.Run(tt.name, func(t *testing.T) {
2368+
got := getPodStatusReasonValue(tt.pod, reason)
2369+
if got != tt.want {
2370+
t.Errorf("getPodStatusReasonValue() = %v, want %v", got, tt.want)
2371+
}
2372+
})
2373+
}
2374+
}

0 commit comments

Comments
 (0)