Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export Job's owner #681

Merged
merged 2 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/job-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
| ---------- | ----------- | ----------- | ----------- |
| kube_job_info | Gauge | `job_name`=&lt;job-name&gt; <br> `namespace`=&lt;job-namespace&gt; | STABLE |
| kube_job_labels | Gauge | `job_name`=&lt;job-name&gt; <br> `namespace`=&lt;job-namespace&gt; <br> `label_JOB_LABEL`=&lt;JOB_LABEL&gt; | STABLE |
| kube_job_owner | Gauge | `job_name`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `owner_kind`=&lt;owner kind&gt; <br> `owner_name`=&lt;owner name&gt; <br> `owner_is_controller`=&lt;whether owner is controller&gt; | STABLE |
| kube_job_spec_parallelism | Gauge | `job_name`=&lt;job-name&gt; <br> `namespace`=&lt;job-namespace&gt; | STABLE |
| kube_job_spec_completions | Gauge | `job_name`=&lt;job-name&gt; <br> `namespace`=&lt;job-namespace&gt; | STABLE |
| kube_job_spec_active_deadline_seconds | Gauge | `job_name`=&lt;job-name&gt; <br> `namespace`=&lt;job-namespace&gt; | STABLE |
Expand Down
34 changes: 34 additions & 0 deletions internal/collector/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package collector

import (
"strconv"

"k8s.io/kube-state-metrics/pkg/metric"

v1batch "k8s.io/api/batch/v1"
Expand Down Expand Up @@ -255,6 +257,38 @@ var (
})
}

return &metric.Family{
Metrics: ms,
}
}),
},
{
Name: "kube_job_owner",
Type: metric.MetricTypeGauge,
Help: "Information about the Job's owner.",
GenerateFunc: wrapJobFunc(func(j *v1batch.Job) *metric.Family {
labelKeys := []string{"owner_kind", "owner_name", "owner_is_controller"}
ms := []*metric.Metric{}

owners := j.GetOwnerReferences()
if len(owners) > 0 {
for _, owner := range owners {
if owner.Controller != nil {
ms = append(ms, &metric.Metric{
LabelKeys: labelKeys,
LabelValues: []string{owner.Kind, owner.Name, strconv.FormatBool(*owner.Controller)},
Value: 1,
})
} else {
ms = append(ms, &metric.Metric{
LabelKeys: labelKeys,
LabelValues: []string{owner.Kind, owner.Name, "false"},
Value: 1,
})
}
}
}

return &metric.Family{
Metrics: ms,
}
Expand Down
12 changes: 12 additions & 0 deletions internal/collector/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ var (
)

func TestJobCollector(t *testing.T) {
var trueValue = true

// 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.
const metadata = `
# HELP kube_job_created Unix creation timestamp
# TYPE kube_job_created gauge
# HELP kube_job_owner Information about the Job's owner.
# TYPE kube_job_owner gauge
# HELP kube_job_complete The job has completed its execution.
# TYPE kube_job_complete gauge
# HELP kube_job_failed The job has failed its execution.
Expand Down Expand Up @@ -83,6 +87,13 @@ func TestJobCollector(t *testing.T) {
Labels: map[string]string{
"app": "example-running-1",
},
OwnerReferences: []metav1.OwnerReference{
{
Kind: "CronJob",
Name: "cronjob-name",
Controller: &trueValue,
},
},
},
Status: v1batch.JobStatus{
Active: 1,
Expand All @@ -98,6 +109,7 @@ func TestJobCollector(t *testing.T) {
},
},
Want: `
kube_job_owner{job_name="RunningJob1",namespace="ns1",owner_is_controller="true",owner_kind="CronJob",owner_name="cronjob-name"} 1
kube_job_created{job_name="RunningJob1",namespace="ns1"} 1.5e+09
kube_job_info{job_name="RunningJob1",namespace="ns1"} 1
kube_job_labels{job_name="RunningJob1",label_app="example-running-1",namespace="ns1"} 1
Expand Down