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 1 commit
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
40 changes: 40 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,44 @@ 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 {
ms = append(ms, &metric.Metric{
LabelKeys: labelKeys,
LabelValues: []string{"<none>", "<none>", "<none>"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no metric should be appended if none exists, in prometheus that's queryable through the absent function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aye, I've had the same concern, but I've taken the Pod collector as a reference. There is no such machinery there.
https://github.com/kubernetes/kube-state-metrics/blob/master/internal/collector/pod.go#L121

Copy link
Contributor Author

@zuzzas zuzzas Feb 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to leave it as it is to preserve uniformity in how we present metrics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. @brancz as changing this in pod.go would be a breaking change, should we do this with the v2.0 release?

Copy link
Member

@brancz brancz Feb 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it right in this case, and add the pod case to the 2.0 release tracking issue: #569.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zuzzas IIUC, you need to delete the logic for appending <none> value metrics when the owners are empty as what @brancz proposed in comment https://github.com/kubernetes/kube-state-metrics/pull/681/files#r259773399

Copy link
Contributor Author

@zuzzas zuzzas Feb 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That I understood, @andyxning. My question was about the case in Pod collector, which works differently. Right now my curiosity is satisfied and I'm adding a new feature.

Copy link
Contributor Author

@zuzzas zuzzas Feb 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done that in a separate commit: 15b93c4

Value: 1,
})
} else {
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
15 changes: 15 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 Expand Up @@ -137,6 +149,7 @@ func TestJobCollector(t *testing.T) {
},
},
Want: `
kube_job_owner{job_name="SuccessfulJob1",namespace="ns1",owner_is_controller="<none>",owner_kind="<none>",owner_name="<none>"} 1
kube_job_complete{condition="false",job_name="SuccessfulJob1",namespace="ns1"} 0
kube_job_complete{condition="true",job_name="SuccessfulJob1",namespace="ns1"} 1
kube_job_complete{condition="unknown",job_name="SuccessfulJob1",namespace="ns1"} 0
Expand Down Expand Up @@ -179,6 +192,7 @@ func TestJobCollector(t *testing.T) {
},
},
Want: `
kube_job_owner{job_name="FailedJob1",namespace="ns1",owner_is_controller="<none>",owner_kind="<none>",owner_name="<none>"} 1
kube_job_failed{condition="false",job_name="FailedJob1",namespace="ns1"} 0
kube_job_failed{condition="true",job_name="FailedJob1",namespace="ns1"} 1
kube_job_failed{condition="unknown",job_name="FailedJob1",namespace="ns1"} 0
Expand Down Expand Up @@ -221,6 +235,7 @@ func TestJobCollector(t *testing.T) {
},
},
Want: `
kube_job_owner{job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1",owner_is_controller="<none>",owner_kind="<none>",owner_name="<none>"} 1
kube_job_complete{condition="false",job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 0
kube_job_complete{condition="true",job_name="SuccessfulJob2NoActiveDeadlineSeconds",namespace="ns1"} 1

Expand Down