Skip to content

Fix pod and container level swap metrics for CRI #132065

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

Merged
merged 1 commit into from
Jun 25, 2025

Conversation

yuanwang04
Copy link
Contributor

@yuanwang04 yuanwang04 commented Jun 3, 2025

What type of PR is this?

/kind bug

What this PR does / why we need it:

Fixes the missing pod-level and container-level swap metrics from the CRI provider.

Swap metrics are introduced in #118865. For the CRI provider, the swap data is only scraped from the listPodStatsPartiallyFromCRI function. However, the resource metrics are scraped from the ListPodCPUAndMemoryStats function. This PR added the similar code changes to the ListPodCPUAndMemoryStats function so that the swap metrics can be reported correctly.

Also added the swap related metrics to the e2e test to assert these metrics exist.

Which issue(s) this PR fixes:

Fixes #131915

Special notes for your reviewer:

Also added the swap metrics to the e2e test suite

Does this PR introduce a user-facing change?

Fixed the bug when swap related metrics were not available in `/metrics/resource` endpoint.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 3, 2025
@k8s-ci-robot
Copy link
Contributor

Welcome @yuanwang04!

It looks like this is your first PR to kubernetes/kubernetes 🎉. 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/kubernetes 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
Copy link
Contributor

Hi @yuanwang04. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/kubelet area/test sig/node Categorizes an issue or PR as relevant to SIG Node. sig/testing Categorizes an issue or PR as relevant to SIG Testing. labels Jun 3, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. label Jun 3, 2025
@ajaysundark
Copy link
Contributor

ajaysundark commented Jun 4, 2025

/cc

@SergeyKanzhelev
Copy link
Member

/ok-to-test
/assign @haircommander
/priority important-soon
/triage accepted

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jun 4, 2025
@k8s-ci-robot k8s-ci-robot removed the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Jun 4, 2025
@SergeyKanzhelev SergeyKanzhelev moved this from Triage to Archive-it in SIG Node CI/Test Board Jun 4, 2025
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it also need a addCRIPodSwapUsage method here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iholder101 I don't see the Swap stats are being populated from the reference to CRI's ListPodSandboxStats function. Do you remember if this is a limitation of the CRI that Swap related data have to be retrieved from the cadvisor / cgroup directly?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey there! Sorry for the late reply.

In #118865, I was adding a comment regarding that:

Special notes for your reviewer:

AFAICT, cadvisor is being used to fetch the metric data. If it is not available through cadvisor, it's possible to fallback into gathering CRI data. Unfortunately, it doesn't seem currently possible to do so (here) since memory usage is not part of the CRI-API. In follow-up work, I think it should be added as well, as I don't see a reason to why CRIs wouldn't be able to provide this information.

While this was true back then, a swapUsage struct is now part of the CRI-API.

Therefore:

Does it also need a addCRIPodSwapUsage method here?

@ajaysundark, I think so, yes.

Do you remember if this is a limitation of the CRI that Swap related data have to be retrieved from the cadvisor / cgroup directly?

Prior to my work, this wasn't part of the CRI-API. Now it is. I'm not aware of any further limitation.

@haircommander can you please confirm that CRIs are implementing the swapUsage API?

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/cri-o/cri-o/blob/155deb668c7b54a0bed35f2e091d4f3067c93208/internal/lib/stats/stats_server_linux.go#L328-L333 looks like CRI-O does, but to return those you'd need to turn on the CRI stats feature, so it doesn't OOTB

Copy link
Contributor

Choose a reason for hiding this comment

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

@haircommander Could you help me understand the status of the CRI stats provider. This feature still seems to be alpha: https://kubernetes.io/docs/reference/instrumentation/cri-pod-container-metrics/?

containerd doesn't seem to support swapUsage in stats yet. I raised a bug to follow-up on this: containerd/containerd#12026.

Copy link
Contributor

Choose a reason for hiding this comment

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

CRI stats provider is used by default in containerd right now. there are two different halves of it. the legacy "partial" CRI stats (where the remainder are populated by cadvisor) vs the new (2371) full CRI stats. it's possible swap stats are only populated in the "full" case

@yuanwang04
Copy link
Contributor Author

yuanwang04 commented Jun 12, 2025

@haircommander @kannon92 Could you help to review this PR? Thanks! I think we might also want to backport this fix to at least 1.33 so Swap can be monitored appropriately, cc @ajaysundark

@iholder101
Copy link
Contributor

iholder101 commented Jun 15, 2025

However, the resource metrics are scraped from the ListPodCPUAndMemoryStats function. This PR added the similar code changes to the ListPodCPUAndMemoryStats function so that the swap metrics can be reported correctly.

For the record, originally, I wasn't sure if swap should be considered as "CPU and memory" stats.
I wasn't aware that the /metrics/resource/ endpoint relies solely on these.

If so, I think this is the right direction 👍
Thank you @yuanwang04!

@iholder101
Copy link
Contributor

@haircommander @kannon92 Could you help to review this PR? Thanks! I think we might also want to backport this fix to at least 1.33 so Swap can be monitored appropriately, cc @ajaysundark

I tend to think this should be possible, if we treat this as a bug fix.

@haircommander
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 24, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 205b8efb109b34df96a8735c9728881234977da7

@yujuhong
Copy link
Contributor

Need a release note

Copy link
Member

@SergeyKanzhelev SergeyKanzhelev left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: SergeyKanzhelev, yuanwang04

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

The pull request process is described here

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 25, 2025
@SergeyKanzhelev
Copy link
Member

/release-note-edit

Fixed the bug when swap related metrics were not available in `/metrics/resource` endpoint.

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Jun 25, 2025
@yuanwang04
Copy link
Contributor Author

/retest

Error is from resource_quota test, seems irrelevant from this change

 I0625 21:26:18.631640 67863 resource_quota.go:1615] Unexpected error: 
      <*errors.StatusError | 0xc00203bb80>: 
      pods "terminating-pod" is forbidden: exceeded quota: quota-terminating, requested: pods=1, used: pods=1, limited: pods=1
      {
          ErrStatus: 
              code: 403
              details:
                kind: pods
                name: terminating-pod
              message: 'pods "terminating-pod" is forbidden: exceeded quota: quota-terminating,
                requested: pods=1, used: pods=1, limited: pods=1'
              metadata: {}
              reason: Forbidden
              status: Failure,
      }
  [FAILED] in [It] - k8s.io/kubernetes/test/e2e/apimachinery/resource_quota.go:1615 @ 06/25/25 21:26:18.631

@k8s-ci-robot k8s-ci-robot merged commit 1e59323 into kubernetes:master Jun 25, 2025
14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.34 milestone Jun 25, 2025
@github-project-automation github-project-automation bot moved this from Archive-it to Done in SIG Node CI/Test Board Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kubelet area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Development

Successfully merging this pull request may close these issues.

Kubelet Swap metrics are missing
7 participants