Skip to content

Add TryFrom conversions for Kubeconfig -> Config -> Client#1801

Merged
clux merged 2 commits into
kube-rs:mainfrom
Danil-Grigorev:from-kubeconfig
Aug 10, 2025
Merged

Add TryFrom conversions for Kubeconfig -> Config -> Client#1801
clux merged 2 commits into
kube-rs:mainfrom
Danil-Grigorev:from-kubeconfig

Conversation

@Danil-Grigorev
Copy link
Copy Markdown
Member

@Danil-Grigorev Danil-Grigorev commented Aug 7, 2025

Motivation

This implementation allows kubeconfig to be directly translated into client using try-from implementation.

Previously, user was required to perform a multi-step manipulation to use custom provided kubeconfig without any modification of context or user, which may not be needed if the kubeconfig was constructed as ready to be used, and multiple sources of kubeconfig are used simultaneously.

Solution

Implement TryFrom trate for the Kubeconfig to Client conversion.

let config = Kubeconfig::default();
let cfg = Config::from_custom_kubeconfig(config, &KubeConfigOptions::default()).await?;
let client = Client::try_from(cfg)?;

while new approach is a more straightforward version of this, skipping the configuration step:

let config = Kubeconfig::default();
let client = Client::try_from(config)?;

@Danil-Grigorev Danil-Grigorev force-pushed the from-kubeconfig branch 2 times, most recently from 6d127bc to 6444847 Compare August 7, 2025 13:01
@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 7, 2025

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.3%. Comparing base (66083d4) to head (069ccef).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
kube-client/src/config/mod.rs 83.4% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1801     +/-   ##
=======================================
+ Coverage   75.1%   76.3%   +1.2%     
=======================================
  Files         84      84             
  Lines       7792    7899    +107     
=======================================
+ Hits        5851    6024    +173     
+ Misses      1941    1875     -66     
Files with missing lines Coverage Δ
kube-client/src/client/mod.rs 80.8% <100.0%> (+5.2%) ⬆️
kube-client/src/config/file_loader.rs 77.5% <100.0%> (+2.5%) ⬆️
kube-client/src/config/mod.rs 55.8% <83.4%> (+2.5%) ⬆️

... and 42 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
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.

i think this looks great.

one minor request; you can use it in examples/kubectl to show it off! (it is using the more awkward kubeconfigoptions)

}

async fn new_from_loader(loader: ConfigLoader) -> Result<Self, KubeconfigError> {
fn new_from_loader(loader: ConfigLoader) -> Result<Self, KubeconfigError> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is technically breaking, and we have avoided changing this in the past due to us potentially loading files via tokio::fs.

i guess it is required for the try_from though, so it's probably worth it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

All I could find is #1336, and I think in this instance it is a private method signature change, so it shouldn't be? Since it is not a part of any trait and used only internally? My thought on this was that in case async is needed later, it can be duplicated in a separate method.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ah, you are right, this is internal. i'll change it to changelog-add.

Comment thread kube-client/src/config/mod.rs
@clux clux added the changelog-change changelog change category for prs label Aug 9, 2025
@clux clux added this to the next-major milestone Aug 9, 2025
@clux clux added changelog-add changelog added category for prs and removed changelog-change changelog change category for prs labels Aug 10, 2025
@clux clux merged commit e8ae086 into kube-rs:main Aug 10, 2025
16 of 17 checks passed
@clux clux mentioned this pull request Sep 7, 2025
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