Skip to content

Commit

Permalink
Build kubernetes client for check access
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Detjens <[email protected]>
  • Loading branch information
detjensrobert committed Jun 16, 2024
1 parent e59ee72 commit 3995ca7
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/access_handlers/kube.rs
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);
}

0 comments on commit 3995ca7

Please sign in to comment.