diff --git a/k8s/forward/examples/http-forward.rs b/k8s/forward/examples/http-forward.rs index ded2d3664..ae0756929 100644 --- a/k8s/forward/examples/http-forward.rs +++ b/k8s/forward/examples/http-forward.rs @@ -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?; diff --git a/k8s/forward/examples/port-forward.rs b/k8s/forward/examples/port-forward.rs index b6be97e68..9edd1fb48 100644 --- a/k8s/forward/examples/port-forward.rs +++ b/k8s/forward/examples/port-forward.rs @@ -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?; diff --git a/k8s/forward/src/http_forward.rs b/k8s/forward/src/http_forward.rs index cbb98417b..73093fc4d 100644 --- a/k8s/forward/src/http_forward.rs +++ b/k8s/forward/src/http_forward.rs @@ -32,19 +32,19 @@ impl HttpForward { /// Return a new `Self`. /// # Arguments /// * `target` - the target we'll forward to - pub async fn new>>( + pub fn new>>( target: crate::Target, scheme: SO, - ) -> anyhow::Result { - 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. @@ -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"); diff --git a/k8s/forward/src/port_forward.rs b/k8s/forward/src/port_forward.rs index fb692edf5..5859bcb70 100644 --- a/k8s/forward/src/port_forward.rs +++ b/k8s/forward/src/port_forward.rs @@ -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?; @@ -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>, - ) -> anyhow::Result { - let client = Client::try_default().await?; + pub fn new(target: crate::Target, local_port: impl Into>, 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. diff --git a/k8s/proxy/src/proxy.rs b/k8s/proxy/src/proxy.rs index 3b6976243..45a153574 100644 --- a/k8s/proxy/src/proxy.rs +++ b/k8s/proxy/src/proxy.rs @@ -190,12 +190,13 @@ impl ConfigBuilder { } /// Tries to build an HTTP `Configuration` from the current self. async fn build_http(self) -> anyhow::Result { - 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() @@ -208,7 +209,10 @@ impl ConfigBuilder { } /// Tries to build a TCP `Configuration` from the current self. async fn build_tcp(self) -> anyhow::Result { - 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?; @@ -235,7 +239,10 @@ impl ConfigBuilder { impl ConfigBuilder { /// Tries to build a TCP `Configuration` from the current self. pub async fn build(self) -> anyhow::Result { - 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?; @@ -279,12 +286,14 @@ impl ConfigBuilder { } /// 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() @@ -294,7 +303,10 @@ impl ConfigBuilder { } /// 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?;