How do I create TokenRequest with kube-rs? #988
-
In order to create a token from ServiceAccount, I should create TokenRequest object. But when I use following code... use k8s_openapi::api::authentication::v1::{TokenRequest, TokenRequestSpec};
use kube::{api::Api, core::ObjectMeta, Client};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let client = Client::try_default().await?;
let tokenrequest_api = Api::<TokenRequest>::namespaced(client, "default");
let tokenrequest = tokenrequest_api
.create(
&Default::default(),
&TokenRequest {
metadata: ObjectMeta {
name: Some("default".to_string()),
..Default::default()
},
spec: TokenRequestSpec {
audiences: vec!["api".to_string()],
..Default::default()
},
status: None,
},
)
.await?;
println!("{:#?}", tokenrequest);
Ok(())
} ... with this Cargo.toml ... [package]
name = "playground"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = { version = "1.0.60", features = ["backtrace"] }
k8s-openapi = { version = "0.15.0", features = ["v1_24", "schemars"] }
kube = { version = "0.74.0", default-features = false, features = ["openssl-tls", "client"] }
schemars = "0.8.10"
serde = { version = "1.0.143", features = ["derive"] }
serde_json = "1.0.83"
tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.36"
tracing-subscriber = { version = "0.3.15", features = ["fmt"] } ... I got following error:
When I ran with
It seems like kube-rs use How do I request with correct URL? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
TokenRequest is a subresource and needs to acted on an Unfortunately, You might be able to use the general helpers like |
Beta Was this translation helpful? Give feedback.
-
Workaround before #989 is merged: use k8s_openapi::api::{authentication::v1::TokenRequest, core::v1::ServiceAccount};
use kube::{Client, Resource};
async fn create_token_request(
client: Client,
name: &str,
namespace: &str,
token_request: &TokenRequest,
) -> kube::Result<TokenRequest> {
let url_path = format!(
"{}/{}/token",
ServiceAccount::url_path(
&<ServiceAccount as Resource>::DynamicType::default(),
Some(namespace)
),
name
);
let body = serde_json::to_vec(token_request).map_err(kube::Error::SerdeError)?;
let mut req = http::Request::post(url_path)
.header(http::header::CONTENT_TYPE, "application/json")
.body(body)
.map_err(|e| kube::Error::BuildRequest(kube::core::request::Error::BuildRequest(e)))?;
req.extensions_mut().insert("create_token_request");
client.request(req).await
} |
Beta Was this translation helpful? Give feedback.
TokenRequest is a subresource and needs to acted on an
Api::<ServiceAccount>
with the methods that exists for that.Unfortunately,
TokenRequest
is actually one of those few ones that is missing itscreate_token_request
method #127 (even though it's not particularly hard to add)You might be able to use the general helpers like
Api::patch_subresource
withsubresource_name="token"
and (serverside apply parameters to be equivalent to create), but I think we should add theApi::create_token_request
to subresource.rs ala https://github.com/kube-rs/kube-rs/pull/773/files - along with a genericApi::create_subresource