Skip to content

feat: Add access mode metric for PersistentVolumes#2823

Open
viragvoros wants to merge 3 commits intokubernetes:mainfrom
viragvoros:feature/pv-access-mode-metric
Open

feat: Add access mode metric for PersistentVolumes#2823
viragvoros wants to merge 3 commits intokubernetes:mainfrom
viragvoros:feature/pv-access-mode-metric

Conversation

@viragvoros
Copy link
Copy Markdown

What this PR does / why we need it:
This PR adds a kube_persistentvolume_access_mode metric that exposes the access modes of PersistentVolume objects, in a way that mirrors the existing PVC access mode metric.

At the moment, if we want to filter or aggregate PersistentVolumes by access mode (e.g. ReadWriteOnce, ReadWriteMany, ReadOnlyMany), we have to rely on kube_persistentvolumeclaim_access_mode and join through
PVC metrics. That forces us to scrape the PVC access mode metrics even when we only care about PVs, and increases metric cardinality.

By exposing a dedicated PV access mode metric with a small label set, we can:

  • filter and aggregate PVs directly by access mode
  • avoid scraping PVC access mode metrics in setups where they are not
    otherwise needed,
    while still keeping metric cardinality low.

Details of the change

  • Add kube_persistentvolume_access_mode metric in the PV store:
    • creates one sample for each access mode a PersistentVolume lists
    • access_mode label uses the standard Kubernetes access
      modes (ReadWriteOnce, ReadOnlyMany, ReadWriteMany, etc.)
    • metric follows a similar style as the existing PVC access mode metric
  • Update internal/store/persistentvolume_test.go
    • add test coverage for the new PV access mode metric
  • Update PV metrics documentation

How does this change affect the cardinality of KSM:
Previously, dashboards and alerts that needed PV access mode information had to join against PVC access mode metrics and therefore scrape kube_persistentvolumeclaim_access_mode, which may have higher cardinality in large clusters.
With kube_persistentvolume_access_mode, users can query PV access modes directly from the PV metrics.

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Dec 2, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Dec 2, 2025
@k8s-ci-robot k8s-ci-robot requested a review from mrueg December 2, 2025 17:06
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

Welcome @viragvoros!

It looks like this is your first PR to kubernetes/kube-state-metrics 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kube-state-metrics has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Dec 2, 2025
@github-project-automation github-project-automation Bot moved this to Needs Triage in SIG Instrumentation Dec 2, 2025
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 2, 2025
@viragvoros viragvoros force-pushed the feature/pv-access-mode-metric branch from 2b6ab10 to 17c531d Compare December 4, 2025 10:44
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Dec 8, 2025
@dgrisonnet
Copy link
Copy Markdown
Member

/assign
/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Dec 12, 2025
Expose a kube_persistentvolume_access_mode metric that mirrors the
existing PVC access mode metric. This allows consumers to filter and
aggregate PersistentVolumes by access mode directly, without having to
scrape kube_persistentvolumeclaim_access_mode.

Using a small set of access-mode labels keeps cardinality low while
still allowing PV metrics to be sliced by access mode.

Signed-off-by: vvoeroes <virag.voeroes@inovex.de>
@viragvoros viragvoros force-pushed the feature/pv-access-mode-metric branch from 17c531d to d01f7e8 Compare December 15, 2025 15:29
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: viragvoros
Once this PR has been reviewed and has the lgtm label, please ask for approval from dgrisonnet. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mrueg
Copy link
Copy Markdown
Member

mrueg commented Dec 19, 2025

I wonder if it's worth adding a new metric here or enhancing kube_persistentvolume_volume_mode instead?

@viragvoros
Copy link
Copy Markdown
Author

I considered merging access_mode into the existing kube_persistentvolume_volume_mode metric, but I think keeping them separate is the better choice.
volumeMode and accessMode represent different shapes in the Kubernetes API. A PersistentVolume has exactly one volumeMode, while it may have multiple accessModes. Combining them would require emitting one sample per access mode, which means the volume mode metric would no longer represent a single property of the PV.

There is also a cardinality consideration for metric consumers. If only a combined {volumemode, access_mode} metric existed, users who are interested only in access modes would still have to deal with the extra volume mode label. This increases the number of series they need to aggregate over and makes otherwise simple queries (e.g. counting PVs by access mode) more expensive than using a dedicated access mode metric.

Users who need both dimensions can still correlate them via PV labels.

Comment thread docs/metrics/storage/persistentvolume-metrics.md Outdated
@mrueg mrueg requested review from Copilot and removed request for logicalhan March 10, 2026 10:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new PersistentVolume metric (kube_persistentvolume_access_mode) to expose PV access modes directly (mirroring the existing PVC access mode metric), along with test coverage and documentation updates.

Changes:

  • Add kube_persistentvolume_access_mode metric generation in the PV store.
  • Extend PV store tests to validate emitted access mode samples.
  • Document the new PV access mode metric in the storage metrics docs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
internal/store/persistentvolume.go Introduces a new metric family generator for PV access modes and wires it into the PV metric families list.
internal/store/persistentvolume_test.go Adds a test case asserting the new metric output for a PV with multiple access modes.
docs/metrics/storage/persistentvolume-metrics.md Documents the new kube_persistentvolume_access_mode metric in the PV metrics table.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +500 to +504
ms = append(ms, &metric.Metric{
LabelKeys: []string{"access_mode"},
LabelValues: []string{string(am)},
Value: 1,
})
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The docs state that only the four official Kubernetes access modes are emitted, but this implementation emits whatever values appear in p.Spec.AccessModes without filtering. To keep behavior aligned with the docs (and to avoid potentially exposing unexpected/invalid values), restrict emission to the known modes (ReadWriteOnce, ReadOnlyMany, ReadWriteMany, ReadWriteOncePod) or update the docs to match the actual behavior.

Suggested change
ms = append(ms, &metric.Metric{
LabelKeys: []string{"access_mode"},
LabelValues: []string{string(am)},
Value: 1,
})
switch am {
case v1.ReadWriteOnce, v1.ReadOnlyMany, v1.ReadWriteMany, v1.ReadWriteOncePod:
ms = append(ms, &metric.Metric{
LabelKeys: []string{"access_mode"},
LabelValues: []string{string(am)},
Value: 1,
})
default:
// Ignore any non-standard or invalid access mode values to align with documented behavior.
}

Copilot uses AI. Check for mistakes.
| kube_persistentvolume_deletion_timestamp | Gauge | Unix deletion timestamp | seconds | `persistentvolume`=&lt;persistentvolume-name&gt; <br> | EXPERIMENTAL |
| kube_persistentvolume_csi_attributes | Gauge | CSI attributes of the Persistent Volume, disabled by default, manage with [--metric-opt-in-list](../../developer/cli-arguments.md)) | | `persistentvolume`=&lt;persistentvolume-name&gt; <br> `csi_mounter`=&lt;csi-mounter&gt; <br> `csi_map_options`=&lt;csi-map-options&gt; | EXPERIMENTAL |
| kube_persistentvolume_volume_mode | Gauge | Volume Mode information for the PersistentVolume. | | `persistentvolume`=&lt;persistentvolume-name&gt; <br>`volumemode`=&lt;volumemode&gt; | EXPERIMENTAL |
| kube_persistentvolume_access_mode | Gauge | Access modes of the PersistentVolume. Only the four official access modes are emitted. | | `persistentvolume`=&lt;persistentvolume-name&gt; <br>`access_mode`=<ReadWriteOnce \| ReadOnlyMany \| ReadWriteMany \| ReadWriteOncePod> | STABLE |
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

In the metrics table, the access_mode value list is written using raw angle brackets (<...>), which can be interpreted as HTML and render incorrectly. Consider escaping the brackets (&lt;/&gt;) to match the formatting used elsewhere in this doc, and ensure the wording matches the actual emission behavior (currently claims only official modes are emitted).

Copilot uses AI. Check for mistakes.
@viragvoros
Copy link
Copy Markdown
Author

Hi @mrueg, thank you for the review. I updated the new metric to match the guidance for newly introduced metrics. Also fixed the docs formatting and removed the sentence that said only the four official access modes are emitted, as the implementation exposes the values in spec.accessModes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

5 participants