Skip to content

Commit

Permalink
Merge pull request #2542 from Indresh2410/add-kube-job-status-suspend…
Browse files Browse the repository at this point in the history
…ed-metric

feat: Add new metric kube_job_status_suspended
  • Loading branch information
k8s-ci-robot authored Nov 7, 2024
2 parents c87b507 + 7a54a8e commit 5e7d0f4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/metrics/workload/job-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
| kube_job_complete | Gauge | | `job_name`=&lt;job-name&gt; <br> `namespace`=&lt;job-namespace&gt; <br> `condition`=&lt;true\|false\|unknown&gt; | STABLE |
| kube_job_failed | Gauge | | `job_name`=&lt;job-name&gt; <br> `namespace`=&lt;job-namespace&gt; <br> `condition`=&lt;true\|false\|unknown&gt; | STABLE |
| kube_job_created | Gauge | | `job_name`=&lt;job-name&gt; <br> `namespace`=&lt;job-namespace&gt; | STABLE |
| kube_job_status_suspended | Gauge | | `job_name`=&lt;job-name&gt; <br> `namespace`=&lt;job-namespace&gt; | EXPERIMENTAL |
22 changes: 22 additions & 0 deletions internal/store/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"

v1batch "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
Expand Down Expand Up @@ -355,6 +356,27 @@ func jobMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
}
}),
),
*generator.NewFamilyGeneratorWithStability(
"kube_job_status_suspended",
"The number of pods which reached Phase Suspended.",
metric.Gauge,
basemetrics.ALPHA,
"",
wrapJobFunc(func(j *v1batch.Job) *metric.Family {
ms := []*metric.Metric{}
for _, c := range j.Status.Conditions {
if c.Type == v1batch.JobSuspended {
ms = append(ms, &metric.Metric{
Value: boolFloat64(c.Status == v1.ConditionTrue),
})
}
}

return &metric.Family{
Metrics: ms,
}
}),
),
*generator.NewFamilyGeneratorWithStability(
"kube_job_owner",
"Information about the Job's owner.",
Expand Down
74 changes: 73 additions & 1 deletion internal/store/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (

func TestJobStore(t *testing.T) {
var trueValue = true
var falseValue = false

// Fixed metadata on type and help text. We prepend this to every expected
// output so we only have to modify a single place when doing adjustments.
Expand Down Expand Up @@ -77,7 +78,10 @@ func TestJobStore(t *testing.T) {
# HELP kube_job_status_start_time [STABLE] StartTime represents time when the job was acknowledged by the Job Manager.
# TYPE kube_job_status_start_time gauge
# HELP kube_job_status_succeeded [STABLE] The number of pods which reached Phase Succeeded.
# TYPE kube_job_status_succeeded gauge`
# TYPE kube_job_status_succeeded gauge
# HELP kube_job_status_suspended The number of pods which reached Phase Suspended.
# TYPE kube_job_status_suspended gauge
`

cases := []generateMetricsTestCase{
{
Expand Down Expand Up @@ -272,6 +276,74 @@ func TestJobStore(t *testing.T) {
kube_job_status_failed{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_status_start_time{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1.495800607e+09
kube_job_status_succeeded{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1
`,
},
{
Obj: &v1batch.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "SuspendedNoActiveDeadlineSeconds",
Namespace: "ns1",
Generation: 1,
},
Status: v1batch.JobStatus{
Active: 0,
Failed: 0,
Succeeded: 0,
StartTime: &metav1.Time{Time: SuccessfulJob2StartTime},
Conditions: []v1batch.JobCondition{
{Type: v1batch.JobSuspended, Status: v1.ConditionTrue},
},
},
Spec: v1batch.JobSpec{
Suspend: &trueValue,
Parallelism: &Parallelism1,
Completions: &Completions1,
},
},
Want: metadata + `
kube_job_owner{job_name="SuspendedNoActiveDeadlineSeconds",namespace="ns1",owner_is_controller="",owner_kind="",owner_name=""} 1
kube_job_info{job_name="SuspendedNoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_spec_completions{job_name="SuspendedNoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_spec_parallelism{job_name="SuspendedNoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_status_active{job_name="SuspendedNoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_status_failed{job_name="SuspendedNoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_status_start_time{job_name="SuspendedNoActiveDeadlineSeconds",namespace="ns1"} 1.495800607e+09
kube_job_status_succeeded{job_name="SuspendedNoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_status_suspended{job_name="SuspendedNoActiveDeadlineSeconds",namespace="ns1"} 1
`,
},
{
Obj: &v1batch.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "UnsuspendedNoActiveDeadlineSeconds",
Namespace: "ns1",
Generation: 1,
},
Status: v1batch.JobStatus{
Active: 0,
Failed: 0,
Succeeded: 0,
StartTime: &metav1.Time{Time: SuccessfulJob2StartTime},
Conditions: []v1batch.JobCondition{
{Type: v1batch.JobSuspended, Status: v1.ConditionFalse},
},
},
Spec: v1batch.JobSpec{
Suspend: &falseValue,
Parallelism: &Parallelism1,
Completions: &Completions1,
},
},
Want: metadata + `
kube_job_owner{job_name="UnsuspendedNoActiveDeadlineSeconds",namespace="ns1",owner_is_controller="",owner_kind="",owner_name=""} 1
kube_job_info{job_name="UnsuspendedNoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_spec_completions{job_name="UnsuspendedNoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_spec_parallelism{job_name="UnsuspendedNoActiveDeadlineSeconds",namespace="ns1"} 1
kube_job_status_active{job_name="UnsuspendedNoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_status_failed{job_name="UnsuspendedNoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_status_start_time{job_name="UnsuspendedNoActiveDeadlineSeconds",namespace="ns1"} 1.495800607e+09
kube_job_status_succeeded{job_name="UnsuspendedNoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_status_suspended{job_name="UnsuspendedNoActiveDeadlineSeconds",namespace="ns1"} 0
`,
},
}
Expand Down

0 comments on commit 5e7d0f4

Please sign in to comment.