pub async fn create_client_builder(options: ConfigOptions) -> Result<(ClientBuilder,KubeConfigLoader)> {
    let kubeconfig = utils::find_kubeconfig()
        .map_err(|e| Error::KubeConfig(format!("Unable to load file: {}", e)))?;

    let loader =
        KubeConfigLoader::load(kubeconfig, options.context, options.cluster, options.user).await?;

    let token = match &loader.user.token {
        Some(token) => Some(token.clone()),
        None => {
            if let Some(exec) = &loader.user.exec {
                let creds = exec::auth_exec(exec)?;
                let status = creds
                    .status
                    .ok_or_else(|| Error::KubeConfig("exec-plugin response did not contain a status".into()))?;
                status.token
            } else {
                None
            }
        }
    };

    let mut client_builder = Client::builder();

    if let Some(bundle) = loader.ca_bundle() {
        for ca in bundle? {
            let cert = Certificate::from_der(&ca.to_der().map_err(|e| Error::SslError(format!("{}", e)))?)
                .map_err(Error::ReqwestError)?;
            client_builder = client_builder.add_root_certificate(cert);
        }
    }
    match loader.p12(" ") {
        Ok(p12) => {
            let der = p12.to_der().map_err(|e| Error::SslError(format!("{}", e)))?;
            let req_p12 = Identity::from_pkcs12_der(&der, " ")
                .map_err(Error::ReqwestError)?;
            client_builder = client_builder.identity(req_p12);
        }
        Err(_) => {
            // last resort only if configs ask for it, and no client certs
            if let Some(true) = loader.cluster.insecure_skip_tls_verify {
                client_builder = client_builder.danger_accept_invalid_certs(true);
            }
        }
    }

    let mut headers = header::HeaderMap::new();

    match (
        utils::data_or_file(&token, &loader.user.token_file),
        (&loader.user.username, &loader.user.password),
    ) {
        (Ok(token), _) => {
            headers.insert(
                header::AUTHORIZATION,
                header::HeaderValue::from_str(&format!("Bearer {}", token))
                    .map_err(|e| Error::KubeConfig(format!("Invalid bearer token: {}", e)))?,
            );
        }
        (_, (Some(u), Some(p))) => {
            let encoded = base64::encode(&format!("{}:{}", u, p));
            headers.insert(
                header::AUTHORIZATION,
                header::HeaderValue::from_str(&format!("Basic {}", encoded))
                    .map_err(|e| Error::KubeConfig(format!("Invalid bearer token: {}", e)))?,
            );
        }
        _ => {}
    }

    Ok((client_builder.default_headers(headers), loader))

}
/// Returns a client builder and config loader, based on the cluster information from the kubeconfig file.
///
/// This allows to create your custom reqwest client for using with the cluster API.
Returns a client builder and config loader, based on the cluster information from the kubeconfig file.
This allows to create your custom reqwest client for using with the cluster API.
Returns a client builder and config loader based on the cluster information from the kubeconfig file . This allows to create your custom reqwest client for using with the cluster API .
pub async fn create_client_builder(options: ConfigOptions) -> Result<(ClientBuilder,KubeConfigLoader)> {
    let kubeconfig = utils::find_kubeconfig()
        .map_err(|e| Error::KubeConfig(format!("Unable to load file: {}", e)))?;
    let loader =
        KubeConfigLoader::load(kubeconfig, options.context, options.cluster, options.user).await?;
    let token = match &loader.user.token {
        Some(token) => Some(token.clone()),
        None => {
            if let Some(exec) = &loader.user.exec {
                let creds = exec::auth_exec(exec)?;
                let status = creds
                    .status
                    .ok_or_else(|| Error::KubeConfig("exec-plugin response did not contain a status".into()))?;
                status.token
            } else {
                None
            }
        }
    };
    let mut client_builder = Client::builder();
    if let Some(bundle) = loader.ca_bundle() {
        for ca in bundle? {
            let cert = Certificate::from_der(&ca.to_der().map_err(|e| Error::SslError(format!("{}", e)))?)
                .map_err(Error::ReqwestError)?;
            client_builder = client_builder.add_root_certificate(cert);
        }
    }
    match loader.p12(" ") {
        Ok(p12) => {
            let der = p12.to_der().map_err(|e| Error::SslError(format!("{}", e)))?;
            let req_p12 = Identity::from_pkcs12_der(&der, " ")
                .map_err(Error::ReqwestError)?;
            client_builder = client_builder.identity(req_p12);
        }
        Err(_) => {
            // last resort only if configs ask for it, and no client certs
            if let Some(true) = loader.cluster.insecure_skip_tls_verify {
                client_builder = client_builder.danger_accept_invalid_certs(true);
            }
        }
    }
    let mut headers = header::HeaderMap::new();
    match (
        utils::data_or_file(&token, &loader.user.token_file),
        (&loader.user.username, &loader.user.password),
    ) {
        (Ok(token), _) => {
            headers.insert(
                header::AUTHORIZATION,
                header::HeaderValue::from_str(&format!("Bearer {}", token))
                    .map_err(|e| Error::KubeConfig(format!("Invalid bearer token: {}", e)))?,
            );
        }
        (_, (Some(u), Some(p))) => {
            let encoded = base64::encode(&format!("{}:{}", u, p));
            headers.insert(
                header::AUTHORIZATION,
                header::HeaderValue::from_str(&format!("Basic {}", encoded))
                    .map_err(|e| Error::KubeConfig(format!("Invalid bearer token: {}", e)))?,
            );
        }
        _ => {}
    }
    Ok((client_builder.default_headers(headers), loader))
}
pub async fn create_client_builder ( options : ConfigOptions ) -> Result < ( ClientBuilder , KubeConfigLoader ) > { let kubeconfig = utils :: find_kubeconfig ( ) . map_err ( | e | Error :: KubeConfig ( format ! ( " " e ) ) ) ? ; let loader = KubeConfigLoader :: load ( kubeconfig , options . context , options . cluster , options . user ) . await ? ; let token = match & loader . user . token { Some ( token ) => Some ( token . clone ( ) ) , None => { if let Some ( exec ) = & loader . user . exec { let creds = exec :: auth_exec ( exec ) ? ; let status = creds . status . ok_or_else ( | | Error :: KubeConfig ( " " . into ( ) ) ) ? ; status . token } else { None } } } ; let mut client_builder = Client :: builder ( ) ; if let Some ( bundle ) = loader . ca_bundle ( ) { for ca in bundle ? { let cert = Certificate :: from_der ( & ca . to_der ( ) . map_err ( | e | Error :: SslError ( format ! ( " " e ) ) ) ? ) . map_err ( Error :: ReqwestError ) ? ; client_builder = client_builder . add_root_certificate ( cert ) ; } } match loader . p12 ( " " ) { Ok ( p12 ) => { let der = p12 . to_der ( ) . map_err ( | e | Error :: SslError ( format ! ( " " e ) ) ) ? ; let req_p12 = Identity :: from_pkcs12_der ( & der , " " ) . map_err ( Error :: ReqwestError ) ? ; client_builder = client_builder . identity ( req_p12 ) ; } Err ( _ ) => { // last resort only if configs ask for it, and no client certs if let Some ( true ) = loader . cluster . insecure_skip_tls_verify { client_builder = client_builder . danger_accept_invalid_certs ( true ) ; } } } let mut headers = header :: HeaderMap :: new ( ) ; match ( utils :: data_or_file ( & token , & loader . user . token_file ) , ( & loader . user . username , & loader . user . password ) , ) { ( Ok ( token ) , _ ) => { headers . insert ( header :: AUTHORIZATION , header :: HeaderValue :: from_str ( & format ! ( " " token ) ) . map_err ( | e | Error :: KubeConfig ( format ! ( " " e ) ) ) ? , ) ; } ( _ , ( Some ( u ) , Some ( p ) ) ) => { let encoded = base64 :: encode ( & format ! ( " " u p ) ) ; headers . insert ( header :: AUTHORIZATION , header :: HeaderValue :: from_str ( & format ! ( " " encoded ) ) . map_err ( | e | Error :: KubeConfig ( format ! ( " " e ) ) ) ? , ) ; } _ => { } } Ok ( ( client_builder . default_headers ( headers ) , loader ) ) }
Returns a client builder and config loader, based on the cluster information from the kubeconfig file.
Returns a client builder and config loader based on the cluster information from the kubeconfig file .
options
ConfigOptions