Skip to content

Commit 7189f39

Browse files
authored
Merge pull request #2075 from maxime1907/featpvdeletiontimestamp
feat(pv): support kube_persistentvolume_deletion_timestamp
2 parents 217acfc + 2be0d9e commit 7189f39

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

docs/persistentvolume-metrics.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,28 @@
88
| kube_persistentvolume_claim_ref | Gauge | | | `persistentvolume`=&lt;pv-name&gt; <br>`claim_namespace`=&lt;<namespace>&gt; <br>`name`=&lt;<name>&gt; | STABLE |
99
| kube_persistentvolume_labels | Gauge | | | `persistentvolume`=&lt;persistentvolume-name&gt; <br> `label_PERSISTENTVOLUME_LABEL`=&lt;PERSISTENTVOLUME_LABEL&gt; | STABLE |
1010
| kube_persistentvolume_info | Gauge | | | `persistentvolume`=&lt;pv-name&gt; <br> `storageclass`=&lt;storageclass-name&gt; <br> `gce_persistent_disk_name`=&lt;pd-name&gt; <br> `host_path`=&lt;path-of-a-host-volume&gt; <br> `host_path_type`=&lt;host-mount-type&gt; <br> `ebs_volume_id`=&lt;ebs-volume-id&gt; <br> `azure_disk_name`=&lt;azure-disk-name&gt; <br> `fc_wwids`=&lt;fc-wwids-comma-separated&gt; <br> `fc_lun`=&lt;fc-lun&gt; <br> `fc_target_wwns`=&lt;fc-target-wwns-comma-separated&gt; <br> `iscsi_target_portal`=&lt;iscsi-target-portal&gt; <br> `iscsi_iqn`=&lt;iscsi-iqn&gt; <br> `iscsi_lun`=&lt;iscsi-lun&gt; <br> `iscsi_initiator_name`=&lt;iscsi-initiator-name&gt; <br> `local_path`=&lt;path-of-a-local-volume&gt; <br> `local_fs`=&lt;local-volume-fs-type&gt; <br> `nfs_server`=&lt;nfs-server&gt; <br> `nfs_path`=&lt;nfs-path&gt; <br> `csi_driver`=&lt;csi-driver&gt; <br> `csi_volume_handle`=&lt;csi-volume-handle&gt; | STABLE |
11-
| kube_persistentvolume_created | Gauge | Unix Creation Timestamp | seconds | `persistentvolume`=&lt;persistentvolume-name&gt; <br> | EXPERIMENTAL |
11+
| kube_persistentvolume_created | Gauge | Unix creation timestamp | seconds | `persistentvolume`=&lt;persistentvolume-name&gt; <br> | EXPERIMENTAL |
12+
| kube_persistentvolume_deletion_timestamp | Gauge | Unix deletion timestamp | seconds | `persistentvolume`=&lt;persistentvolume-name&gt; <br> | EXPERIMENTAL |
1213

14+
## Useful metrics queries
15+
16+
### How to retrieve non-standard PV state
17+
18+
It is not straightforward to get the PV states for certain cases like "Terminating" since it is not stored behind a field in the `PersistentVolume.Status`.
19+
20+
So to mimic the [logic](https://github.com/kubernetes/kubernetes/blob/v1.27.2/pkg/printers/internalversion/printers.go#L1838) used by the `kubectl` command line, you will need to compose multiple metrics.
21+
22+
Here is an example of a Prometheus rule that can be used to alert on a PV that has been in the `Terminating` state for more than `5m`.
23+
24+
```yaml
25+
groups:
26+
- name: PV state
27+
rules:
28+
- alert: PVBlockedInTerminatingState
29+
expr: kube_persistentvolume_deletion_timestamp * on(persistentvolume) group_left() (kube_persistentvolume_status_phase{phase="Bound"} == 1) > 0
30+
for: 5m
31+
labels:
32+
severity: warning
33+
annotations:
34+
summary: PV {{$labels.persistentvolume}} blocked in Terminating state.
35+
```

internal/store/persistentvolume.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,28 @@ func persistentVolumeMetricFamilies(allowAnnotationsList, allowLabelsList []stri
316316
})
317317
}
318318

319+
return &metric.Family{
320+
Metrics: ms,
321+
}
322+
}),
323+
),
324+
*generator.NewFamilyGeneratorWithStability(
325+
"kube_persistentvolume_deletion_timestamp",
326+
"Unix deletion timestamp",
327+
metric.Gauge,
328+
basemetrics.ALPHA,
329+
"",
330+
wrapPersistentVolumeFunc(func(p *v1.PersistentVolume) *metric.Family {
331+
ms := []*metric.Metric{}
332+
333+
if p.DeletionTimestamp != nil && !p.DeletionTimestamp.IsZero() {
334+
ms = append(ms, &metric.Metric{
335+
LabelKeys: []string{},
336+
LabelValues: []string{},
337+
Value: float64(p.DeletionTimestamp.Unix()),
338+
})
339+
}
340+
319341
return &metric.Family{
320342
Metrics: ms,
321343
}

internal/store/persistentvolume_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,24 @@ func TestPersistentVolumeStore(t *testing.T) {
683683
`,
684684
MetricNames: []string{"kube_persistentvolume_created"},
685685
},
686+
{
687+
Obj: &v1.PersistentVolume{
688+
ObjectMeta: metav1.ObjectMeta{
689+
Name: "test-pv-terminating",
690+
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
691+
DeletionTimestamp: &metav1.Time{Time: time.Unix(1800000000, 0)},
692+
},
693+
Status: v1.PersistentVolumeStatus{
694+
Phase: v1.VolumeBound,
695+
},
696+
},
697+
Want: `
698+
# HELP kube_persistentvolume_deletion_timestamp Unix deletion timestamp
699+
# TYPE kube_persistentvolume_deletion_timestamp gauge
700+
kube_persistentvolume_deletion_timestamp{persistentvolume="test-pv-terminating"} 1.8e+09
701+
`,
702+
MetricNames: []string{"kube_persistentvolume_deletion_timestamp"},
703+
},
686704
}
687705
for i, c := range cases {
688706
c.Func = generator.ComposeMetricGenFuncs(persistentVolumeMetricFamilies(c.AllowAnnotationsList, c.AllowLabelsList))

0 commit comments

Comments
 (0)