-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build kubernetes client for check access
Signed-off-by: Robert Detjens <[email protected]>
- Loading branch information
1 parent
e59ee72
commit 3995ca7
Showing
1 changed file
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,42 @@ | ||
use anyhow::{Error, Result}; | ||
use kube; | ||
|
||
use crate::configparser::{config, CONFIG}; | ||
|
||
/// kubernetes access checks | ||
pub fn check(profile: &config::ProfileConfig) -> Result<()> { | ||
// Ok(()) | ||
Err(Error::msg("bad kube!")) | ||
// we need to make sure that: | ||
// a) can talk to the cluster | ||
// b) have the right permissions (a la `kubectl auth can-i`) | ||
|
||
// build a client | ||
let client = client(profile); | ||
|
||
return Ok(()); | ||
} | ||
|
||
/// Returns K8S Client for selected profile | ||
async fn client(profile: &config::ProfileConfig) -> Result<kube::Client> { | ||
// make sure the profile exists | ||
|
||
// read in kubeconfig from given kubeconfig (or default) | ||
// (use kube::Config to specify context) | ||
let options = kube::config::KubeConfigOptions { | ||
context: Some(profile.kubecontext.to_owned()), | ||
cluster: None, | ||
user: None, | ||
}; | ||
|
||
let client_config = match &profile.kubeconfig { | ||
Some(kc_path) => { | ||
let kc = kube::config::Kubeconfig::read_from(kc_path)?; | ||
kube::Config::from_custom_kubeconfig(kc, &options).await? | ||
} | ||
None => kube::Config::from_kubeconfig(&options).await?, | ||
}; | ||
|
||
// client::try_from returns a Result, but the Error is not compatible | ||
// with anyhow::Error, so assign this with ? and return Ok() separately | ||
let client = kube::Client::try_from(client_config)?; | ||
return Ok(client); | ||
} |