Skip to content

Commit

Permalink
fix(k8s): use provided kube-config path
Browse files Browse the repository at this point in the history
Ensure we always use the provided kube-config path.

Signed-off-by: Tiago Castro <tiagolobocastro@gmail.com>
  • Loading branch information
tiagolobocastro committed Nov 7, 2024
1 parent 4ec8c08 commit 1670d8c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
3 changes: 1 addition & 2 deletions k8s/forward/examples/http-forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ async fn main() -> anyhow::Result<()> {

let selector = kube_forward::TargetSelector::svc_label("app", "api-rest");
let target = kube_forward::Target::new(selector, "http", "mayastor");
let uri = kube_forward::HttpForward::new(target, None)
.await?
let uri = kube_forward::HttpForward::new(target, None, kube::Client::try_default().await?)
.uri()
.await?;

Expand Down
2 changes: 1 addition & 1 deletion k8s/forward/examples/port-forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ async fn main() -> anyhow::Result<()> {

let selector = kube_forward::TargetSelector::svc_label("app", "api-rest");
let target = kube_forward::Target::new(selector, "http", "mayastor");
let pf = kube_forward::PortForward::new(target, 30011).await?;
let pf = kube_forward::PortForward::new(target, 30011, kube::Client::try_default().await?);

let (_, handle) = pf.port_forward().await?;
handle.await?;
Expand Down
13 changes: 7 additions & 6 deletions k8s/forward/src/http_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ impl HttpForward {
/// Return a new `Self`.
/// # Arguments
/// * `target` - the target we'll forward to
pub async fn new<SO: Into<Option<Scheme>>>(
pub fn new<SO: Into<Option<Scheme>>>(
target: crate::Target,
scheme: SO,
) -> anyhow::Result<Self> {
let client = kube::Client::try_default().await?;
client: kube::Client,
) -> Self {
let namespace = target.namespace.name_any();

Ok(Self {
Self {
target,
pod_api: Api::namespaced(client.clone(), &namespace),
svc_api: Api::namespaced(client, &namespace),
scheme: scheme.into().unwrap_or(Scheme::HTTP),
})
}
}

/// Returns the `hyper::Uri` that can be used to proxy with the kubeapi server.
Expand All @@ -70,7 +70,8 @@ impl HttpForward {
/// ```ignore
/// let selector = kube_forward::TargetSelector::svc_label("app", "api-rest");
/// let target = kube_forward::Target::new(selector, "http", "mayastor");
/// let pf = kube_forward::HttpForward::new(target, None).await?;
/// let client = kube::Client::try_default().await?;
/// let pf = kube_forward::HttpForward::new(target, None, client).await?;
///
/// let uri = pf.uri().await?;
/// tracing::info!(%uri, "generated kube-api");
Expand Down
13 changes: 5 additions & 8 deletions k8s/forward/src/port_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use kube::{
/// ```ignore
/// let selector = kube_forward::TargetSelector::pod_label("app", "etcd");
/// let target = kube_forward::Target::new(selector, "client", "mayastor");
/// let pf = kube_forward::PortForward::new(target, 35003).await?;
/// let client = kube::Client::try_default().await?;
/// let pf = kube_forward::PortForward::new(target, 35003, client).await?;
///
/// let (_port, handle) = pf.port_forward().await?;
/// handle.await?;
Expand All @@ -36,19 +37,15 @@ impl PortForward {
/// # Arguments
/// * `target` - the target we'll forward to
/// * `local_port` - specific local port to use, if Some
pub async fn new(
target: crate::Target,
local_port: impl Into<Option<u16>>,
) -> anyhow::Result<Self> {
let client = Client::try_default().await?;
pub fn new(target: crate::Target, local_port: impl Into<Option<u16>>, client: Client) -> Self {
let namespace = target.namespace.name_any();

Ok(Self {
Self {
target,
local_port: local_port.into(),
pod_api: Api::namespaced(client.clone(), &namespace),
svc_api: Api::namespaced(client, &namespace),
})
}
}

/// The specified local port, or 0.
Expand Down
34 changes: 23 additions & 11 deletions k8s/proxy/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,13 @@ impl ConfigBuilder<ApiRest> {
}
/// Tries to build an HTTP `Configuration` from the current self.
async fn build_http(self) -> anyhow::Result<Configuration> {
let uri = kube_forward::HttpForward::new(self.target, Some(self.scheme.into()))
.await?
.uri()
.await?;
let config = super::config_from_kubeconfig(self.kube_config).await?;
let client = kube::Client::try_from(config)?;
let uri =
kube_forward::HttpForward::new(self.target, Some(self.scheme.into()), client.clone())
.uri()
.await?;

let proxy = kube_forward::HttpProxy::new(client);

let config = Configuration::builder()
Expand All @@ -208,7 +209,10 @@ impl ConfigBuilder<ApiRest> {
}
/// Tries to build a TCP `Configuration` from the current self.
async fn build_tcp(self) -> anyhow::Result<Configuration> {
let pf = kube_forward::PortForward::new(self.target, None).await?;
let config = super::config_from_kubeconfig(self.kube_config).await?;
let client = kube::Client::try_from(config)?;

let pf = kube_forward::PortForward::new(self.target, None, client);

let (port, _handle) = pf.port_forward().await?;

Expand All @@ -235,7 +239,10 @@ impl ConfigBuilder<ApiRest> {
impl ConfigBuilder<Etcd> {
/// Tries to build a TCP `Configuration` from the current self.
pub async fn build(self) -> anyhow::Result<Uri> {
let pf = kube_forward::PortForward::new(self.target, None).await?;
let config = super::config_from_kubeconfig(self.kube_config).await?;
let client = kube::Client::try_from(config)?;

let pf = kube_forward::PortForward::new(self.target, None, client);

let (port, _handle) = pf.port_forward().await?;

Expand Down Expand Up @@ -279,12 +286,14 @@ impl ConfigBuilder<Loki> {
}
/// Tries to build an HTTP `Configuration` from the current self.
async fn build_http(self) -> anyhow::Result<(Uri, LokiClient)> {
let uri = kube_forward::HttpForward::new(self.target, Some(self.scheme.into()))
.await?
.uri()
.await?;
let config = super::config_from_kubeconfig(self.kube_config).await?;
let client = kube::Client::try_from(config)?;

let uri =
kube_forward::HttpForward::new(self.target, Some(self.scheme.into()), client.clone())
.uri()
.await?;

let proxy = kube_forward::HttpProxy::new(client);

let service = tower::ServiceBuilder::new()
Expand All @@ -294,7 +303,10 @@ impl ConfigBuilder<Loki> {
}
/// Tries to build a TCP `Configuration` from the current self.
async fn build_tcp(self) -> anyhow::Result<(Uri, LokiClient)> {
let pf = kube_forward::PortForward::new(self.target, None).await?;
let config = super::config_from_kubeconfig(self.kube_config).await?;
let client = kube::Client::try_from(config)?;

let pf = kube_forward::PortForward::new(self.target, None, client);

let (port, _handle) = pf.port_forward().await?;

Expand Down

0 comments on commit 1670d8c

Please sign in to comment.