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

add persistent volume capacity metric #674

Merged
merged 3 commits into from
Feb 22, 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/persistentvolume-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

| Metric name| Metric type | Labels/tags | Status |
| ---------- | ----------- | ----------- | ----------- |
| kube_persistentvolume_capacity_bytes | Gauge | `persistentvolume`=<pv-name> | STABLE |
| kube_persistentvolume_status_phase | Gauge | `persistentvolume`=&lt;pv-name&gt; <br>`phase`=&lt;Bound\|Failed\|Pending\|Available\|Released&gt;| STABLE |
| kube_persistentvolume_labels | Gauge | `persistentvolume`=&lt;persistentvolume-name&gt; <br> `label_PERSISTENTVOLUME_LABEL`=&lt;PERSISTENTVOLUME_LABEL&gt; | STABLE |
| kube_persistentvolume_info | Gauge | `persistentvolume`=&lt;pv-name&gt; <br> `storageclass`=&lt;storageclass-name&gt; | STABLE |
Expand Down
15 changes: 15 additions & 0 deletions internal/collector/persistentvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ var (
}
}),
},
{
Name: "kube_persistentvolume_capacity_bytes",
Type: metric.MetricTypeGauge,
Help: "Persistentvolume capacity in bytes.",
GenerateFunc: wrapPersistentVolumeFunc(func(p *v1.PersistentVolume) *metric.Family {
storage := p.Spec.Capacity[v1.ResourceStorage]
Copy link
Member

Choose a reason for hiding this comment

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

could there ever be an additoinal capacity? if so we should make the v1.ResourceStorage a label instead, similar to what we do with resource_requests on pod/containers

Copy link
Contributor Author

@r0fls r0fls Feb 19, 2019

Choose a reason for hiding this comment

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

I do not believe there can ever be an additional capacity, because p.Spec.Capacity is a v1.ResourceList, which in turn is a map[ResourceName]resource.Quantity. So, there will only be one resource.Quantity for each ResourceName in the map (in this case the resource name is v1.ResourceStorage).

The name v1.ResourceList is a little misleading in that regard, but since it's a map to resource.Quantity (which is never a list) I think we're good.

return &metric.Family{
Metrics: []*metric.Metric{
{
Value: float64(storage.Value()),
},
},
}
}),
},
}
)

Expand Down
19 changes: 19 additions & 0 deletions internal/collector/persistentvolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kube-state-metrics/pkg/metric"
)
Expand All @@ -34,6 +35,8 @@ func TestPersistentVolumeCollector(t *testing.T) {
# TYPE kube_persistentvolume_labels gauge
# HELP kube_persistentvolume_info Information about persistentvolume.
# TYPE kube_persistentvolume_info gauge
# HELP kube_persistentvolume_capacity_bytes The size of the Persistentvolume in bytes.
# TYPE kube_persistentvolume_capacity_bytes gauge
`
cases := []generateMetricsTestCase{
// Verify phase enumerations.
Expand Down Expand Up @@ -201,6 +204,22 @@ func TestPersistentVolumeCollector(t *testing.T) {
`,
MetricNames: []string{"kube_persistentvolume_labels"},
},
{
Obj: &v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pv",
},
Spec: v1.PersistentVolumeSpec{
Capacity: v1.ResourceList{
v1.ResourceStorage: resource.MustParse("5Gi"),
},
},
},
Want: `
kube_persistentvolume_capacity_bytes{persistentvolume="test-pv"} 5.36870912e+09
`,
MetricNames: []string{"kube_persistentvolume_capacity_bytes"},
},
}
for i, c := range cases {
c.Func = metric.ComposeMetricGenFuncs(persistentVolumeMetricFamilies)
Expand Down