From 20589ac310a2efec015e0f844449f16640aff5cd Mon Sep 17 00:00:00 2001 From: Richa Banker Date: Fri, 23 Aug 2024 15:14:55 -0700 Subject: [PATCH 1/2] Set metric even when there are no job.Status.Conditions present --- internal/store/job.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/store/job.go b/internal/store/job.go index 6e0e97b218..8dd53b0a19 100644 --- a/internal/store/job.go +++ b/internal/store/job.go @@ -219,10 +219,10 @@ func jobMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat } } + reasonKnown := false for _, c := range j.Status.Conditions { condition := c if condition.Type == v1batch.JobFailed { - reasonKnown := false for _, reason := range jobFailureReasons { reasonKnown = reasonKnown || failureReason(&condition, reason) @@ -233,16 +233,16 @@ func jobMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat Value: boolFloat64(failureReason(&condition, reason)), }) } - // for unknown reasons - if !reasonKnown { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{"reason"}, - LabelValues: []string{""}, - Value: float64(j.Status.Failed), - }) - } } } + // for unknown reasons + if !reasonKnown { + ms = append(ms, &metric.Metric{ + LabelKeys: []string{"reason"}, + LabelValues: []string{""}, + Value: float64(j.Status.Failed), + }) + } return &metric.Family{ Metrics: ms, From 643739d04a03d5263868194efc5d3f9664e158ee Mon Sep 17 00:00:00 2001 From: Richa Banker Date: Tue, 27 Aug 2024 20:11:39 -0700 Subject: [PATCH 2/2] Add unit test --- internal/store/job_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/internal/store/job_test.go b/internal/store/job_test.go index b45ac1ab15..b808c321b5 100644 --- a/internal/store/job_test.go +++ b/internal/store/job_test.go @@ -208,6 +208,28 @@ func TestJobStore(t *testing.T) { kube_job_status_failed{job_name="FailedJob1",namespace="ns1",reason="Evicted"} 0 kube_job_status_start_time{job_name="FailedJob1",namespace="ns1"} 1.495807207e+09 kube_job_status_succeeded{job_name="FailedJob1",namespace="ns1"} 0 +`, + }, + { + Obj: &v1batch.Job{ + ObjectMeta: metav1.ObjectMeta{ + Name: "FailedJobWithNoConditions", + Namespace: "ns1", + }, + Status: v1batch.JobStatus{ + Failed: 1, + }, + Spec: v1batch.JobSpec{ + ActiveDeadlineSeconds: &ActiveDeadlineSeconds900, + }, + }, + Want: metadata + ` + kube_job_owner{job_name="FailedJobWithNoConditions",namespace="ns1",owner_is_controller="",owner_kind="",owner_name=""} 1 + kube_job_info{job_name="FailedJobWithNoConditions",namespace="ns1"} 1 + kube_job_spec_active_deadline_seconds{job_name="FailedJobWithNoConditions",namespace="ns1"} 900 + kube_job_status_active{job_name="FailedJobWithNoConditions",namespace="ns1"} 0 + kube_job_status_failed{job_name="FailedJobWithNoConditions",namespace="ns1",reason=""} 1 + kube_job_status_succeeded{job_name="FailedJobWithNoConditions",namespace="ns1"} 0 `, }, {