Skip to content

Add Kubernetes Metrics API support to utils module#2512

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/integrate-kubernetes-metrics-api
Draft

Add Kubernetes Metrics API support to utils module#2512
Copilot wants to merge 4 commits intomasterfrom
copilot/integrate-kubernetes-metrics-api

Conversation

Copy link
Contributor

Copilot AI commented Feb 8, 2026

Adds native support for querying pod and node resource metrics (CPU/memory) from the metrics.k8s.io API, enabling monitoring and autoscaling workflows without external dependencies.

Changes

  • New module kubernetes/utils/metrics.py with three functions:

    • get_nodes_metrics(api_client) - cluster-wide node metrics
    • get_pods_metrics(api_client, namespace, label_selector=None) - namespace-scoped pod metrics with optional label filtering
    • get_pods_metrics_in_all_namespaces(api_client, namespaces, label_selector=None) - multi-namespace aggregation with per-namespace error isolation
  • Testing: 8 unit tests covering success paths, validation, filtering, and error handling

  • Documentation: API reference, usage patterns, and troubleshooting guide in kubernetes/docs/metrics.md

  • Example: Working demonstration in examples/metrics_example.py

Usage

from kubernetes import client, config, utils

config.load_kube_config()
api = client.ApiClient()

# Node metrics
nodes = utils.get_nodes_metrics(api)
for node in nodes['items']:
    print(f"{node['metadata']['name']}: {node['usage']}")

# Pod metrics with label filtering
pods = utils.get_pods_metrics(api, 'production', 'app=nginx')
for pod in pods['items']:
    for container in pod['containers']:
        print(f"{container['name']}: {container['usage']}")

# Multi-namespace with error handling
results = utils.get_pods_metrics_in_all_namespaces(
    api, ['default', 'kube-system']
)

Requirements

Requires metrics-server installed in target cluster. Uses existing CustomObjectsApi to access metrics.k8s.io/v1beta1 endpoints.

Original prompt

This section details on the original issue you should resolve

<issue_title>[feat]: Integrate Kubernetes Metrics API into the Python Client</issue_title>
<issue_description>What is the feature and why do you need it:

The feature is the integration of the Kubernetes Metrics API into the Python client. This addition will allow users to retrieve metrics data directly through the client, enabling more streamlined monitoring and auto-scaling workflows. Currently, the client lacks native support for querying metrics data, making it necessary to rely on external tools or libraries.

Describe the solution you'd like to see:

I envision the addition of a new module or extension to the existing client specifically dedicated to handling metrics data retrieval. This module should include methods for querying various types of metrics, such as pod metrics, node metrics, and custom metrics. It should also be designed to seamlessly integrate with the existing client structure, providing a consistent user experience.

The solution should ensure compatibility with different versions of Kubernetes to support users on various clusters. Additionally, comprehensive documentation and examples should be provided to guide users in effectively utilizing the new metrics functionalities.

Overall, the goal is to empower users with the ability to access and utilize metrics data directly through the Python client, eliminating the need for external dependencies and enhancing the client's capabilities in monitoring and scaling applications.

</issue_description>

<agent_instructions>Please implement support for easy access to pod and node metrics, you can look at similar code in Java here: https://github.com/kubernetes-client/java/blob/master/util/src/main/java/io/kubernetes/client/Metrics.java
</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@roycaihw /help

@Parvezkhan0 Could you elaborate what Kubernetes Metrics API is?</comment_new>
<comment_new>@k8s-ci-robot
@roycaihw:
This request has been marked as needing help from a contributor.

Guidelines

Please ensure that the issue body includes answers to the following questions:

  • Why are we solving this issue?
  • To address this issue, are there any code changes? If there are code changes, what needs to be done in the code and what places can the assignee treat as reference points?
  • Does this issue have zero to low barrier of entry?
  • How can the assignee reach out to you for help?

For more details on the requirements of such an issue, please see here and ensure that they are met.

If this request no longer meets these requirements, the label can be removed
by commenting with the /remove-help command.

Details

In response to this:

/help

@Parvezkhan0 Could you elaborate what Kubernetes Metrics API is?

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/test-infra repository.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@k8s-ci-robot
Copy link
Contributor

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

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 do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 8, 2026
Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Copilot
Once this PR has been reviewed and has the lgtm label, please assign yliaog for approval. 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

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 8, 2026
Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 8, 2026
Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
Copilot AI changed the title [WIP] Integrate Kubernetes Metrics API into Python client Add Kubernetes Metrics API support to utils module Feb 8, 2026
Copilot AI requested a review from brendandburns February 8, 2026 17:45
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. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feat]: Integrate Kubernetes Metrics API into the Python Client

3 participants