Skip to content

Commit

Permalink
Merge pull request #246 from edio/feature/proxy
Browse files Browse the repository at this point in the history
Implement accessing Kubernetes API via proxy
  • Loading branch information
clux authored Jun 3, 2020
2 parents 2843481 + b37a5a5 commit cf4773e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
31 changes: 31 additions & 0 deletions kube/examples/proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[macro_use] extern crate log;
use k8s_openapi::api::core::v1::Namespace;

use kube::{api::{Api, ListParams}, Client, Config};
use kube::config::KubeConfigOptions;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "info,kube=debug");
env_logger::init();

let proxy_url = std::env::var("http_proxy").ok();
if let Some(p) = &proxy_url {
info!("http_proxy is {}", p);
} else {
warn!("You can set HTTP(s) proxy for this example with http_proxy environment variable");
}

let mut config = Config::from_kubeconfig(&KubeConfigOptions::default()).await?;
let proxy = proxy_url.map(|url| reqwest::Proxy::https(&url)).map_or(Ok(None), |p| p.map(Some))?;
let config = proxy.map(|p| config.proxy(p)).unwrap_or(config);
let client = Client::new(config);

// Verify we can access kubernetes through proxy
let ns_api: Api<Namespace> = Api::all(client);
let namespaces = ns_api.list(&ListParams::default()).await?;
assert!(namespaces.items.len() > 0);
info!("Found {} namespaces", namespaces.items.len());

Ok(())
}
4 changes: 4 additions & 0 deletions kube/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ impl From<Config> for reqwest::ClientBuilder {
fn from(config: Config) -> Self {
let mut builder = Self::new();

if let Some(i) = &config.proxy {
builder = builder.proxy(i.clone())
}

if let Some(i) = config.identity() {
builder = builder.identity(i)
}
Expand Down
19 changes: 19 additions & 0 deletions kube/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub struct Config {
pub timeout: std::time::Duration,
/// Whether to accept invalid ceritifacts
pub accept_invalid_certs: bool,
/// Proxy to send requests to Kubernetes API through
pub(crate) proxy: Option<reqwest::Proxy>,
/// The identity to use for communicating with the Kubernetes API
/// along wit the password to decrypt it.
///
Expand All @@ -91,6 +93,20 @@ pub struct Config {
}

impl Config {
/// Return a copy of this config with proxy configured
///
/// ```rust
/// # fn main() {
/// # async fn run() -> Result<(), Box< dyn std::error::Error>> {
/// let mut config = kube::Config::from_kubeconfig(&kube::config::KubeConfigOptions::default()).await?;
/// let proxy = reqwest::Proxy::http("https://localhost:8080")?;
/// let config = config.proxy(proxy);
/// # Ok(())
/// # }}
/// ```
pub fn proxy(&mut self, proxy: reqwest::Proxy) -> Self {
Config { proxy: Some(proxy), ..(self.clone()) }
}
/// Construct a new config where only the `cluster_url` is set by the user.
/// and everything else receives a default value.
///
Expand All @@ -104,6 +120,7 @@ impl Config {
headers: HeaderMap::new(),
timeout: DEFAULT_TIMEOUT,
accept_invalid_certs: false,
proxy: None,
identity: None,
auth_header: Authentication::None,
}
Expand Down Expand Up @@ -163,6 +180,7 @@ impl Config {
headers: HeaderMap::new(),
timeout: DEFAULT_TIMEOUT,
accept_invalid_certs: false,
proxy: None,
identity: None,
auth_header: Authentication::Token(format!("Bearer {}", token)),
})
Expand Down Expand Up @@ -226,6 +244,7 @@ impl Config {
headers: HeaderMap::new(),
timeout: DEFAULT_TIMEOUT,
accept_invalid_certs,
proxy: None,
identity: identity.map(|i| (i, String::from(IDENTITY_PASSWORD))),
auth_header: load_auth_header(&loader)?,
})
Expand Down

0 comments on commit cf4773e

Please sign in to comment.