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 example for raw API Server requests #1330

Merged
merged 5 commits into from
Oct 30, 2023

Conversation

mateiidavid
Copy link
Contributor

@mateiidavid mateiidavid commented Oct 28, 2023

Motivation

I noticed a couple of questions around querying the metrics API that a kubelet exposes. A number of different use cases aside from stat queries may require raw requests to the API Server (in the style of kubectl get --raw).

It does not make much sense to extend the Request API to cover this use case (since an abstraction would not simplify anything). Users can, however, make use of the http::Request type and use an instantiated client to interface with the API Server. There's a lack of material here, so I thought I'd include an example that other people may reference.

Solution

The example builds the equivalent of kubectl top nodes (in a simpler, and dumber way) using raw HTTP requests. The data may not be as exact as what kubectl top offers, but it serves as an example on how data may be queried.

If this isn't needed, I'm happy to put it in a gist somewhere, no time wasted so no worries there :) It is an unsolicited PR.

:; kubectl top node
NAME                  CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
k3d-agent-0-0         34m          0%     65Mi            0%
k3d-source-server-0   168m         1%     1516Mi          4%



:; cargo run -p kube-examples --example request_raw
NAME                    MEMORY(bytes)     MEMORY%   CPU(cores)   CPU%
k3d-source-server-0     1914Mi            6%        195m         1%
k3d-agent-0-0           60Mi              0%        67m          0%

I noticed a couple of questions around querying the metrics API that a
kubelet exposes. A number of different use cases aside from stat queries
may require raw requests to the API Server (in the style of `kubectl get --raw`).

It does not make much sense to extend the Request API to cover this use
case (since an abstraction would not simplify anything). Users can,
however, make use of the `http::Request` type and use an instantiated
client to interface with the API Server. There's a lack of material
here, so I thought I'd include an example that other people may
reference.

The example builds the equivalent of `kubectl top nodes` (in a simpler,
and dumber way) using raw HTTP requests. The data may not be as exact as
what `kubectl top` offers, but it serves as an example on how data may
be queried.

Signed-off-by: Matei David <[email protected]>
@codecov
Copy link

codecov bot commented Oct 28, 2023

Codecov Report

Merging #1330 (44980d4) into main (9c81f1f) will decrease coverage by 0.2%.
Report is 4 commits behind head on main.
The diff coverage is n/a.

❗ Current head 44980d4 differs from pull request most recent head a3a7f43. Consider uploading reports for the commit a3a7f43 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1330     +/-   ##
=======================================
- Coverage   72.4%   72.1%   -0.2%     
=======================================
  Files         75      75             
  Lines       6343    6377     +34     
=======================================
+ Hits        4586    4597     +11     
- Misses      1757    1780     +23     

see 5 files with indirect coverage changes

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

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

thanks for this, saw you were providing helpful answers.

i like this example, it's advanced enough to be useful, and not too complicated either.

there are a few nits i've suggested to make it more readable, but this is definitely parseable.

examples/request_raw.rs Show resolved Hide resolved
examples/request_raw.rs Outdated Show resolved Hide resolved
examples/request_raw.rs Outdated Show resolved Hide resolved
examples/request_raw.rs Outdated Show resolved Hide resolved
examples/request_raw.rs Outdated Show resolved Hide resolved
examples/request_raw.rs Outdated Show resolved Hide resolved
mateiidavid and others added 4 commits October 30, 2023 17:47
* Remove redundant `.items` acess.
* Rename NodeMetric to NodeMetrics
* std::cmp instead of manually comparing.

Co-authored-by: Eirik A <[email protected]>
Signed-off-by: Matei David <[email protected]>
Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

@clux clux added this to the 0.87.0 milestone Oct 30, 2023
@clux clux added the changelog-add changelog added category for prs label Oct 30, 2023
@clux clux merged commit 17f1cb5 into kube-rs:main Oct 30, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-add changelog added category for prs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants