#![warn(rust_2018_idioms)]
#![deny(clippy::all, clippy::pedantic)]
#![allow(
clippy::default_trait_access,
clippy::derive_partial_eq_without_eq,
clippy::doc_markdown,
clippy::large_enum_variant,
clippy::match_single_binding,
clippy::missing_errors_doc,
clippy::module_name_repetitions,
clippy::must_use_candidate,
clippy::single_match_else,
clippy::too_many_lines,
clippy::type_complexity,
rustdoc::bare_urls,
)]
#![cfg_attr(k8s_openapi_enabled_version="1.20", doc = "v1_20")]
#![cfg_attr(k8s_openapi_enabled_version="1.21", doc = "v1_21")]
#![cfg_attr(k8s_openapi_enabled_version="1.22", doc = "v1_22")]
#![cfg_attr(k8s_openapi_enabled_version="1.23", doc = "v1_23")]
#![cfg_attr(k8s_openapi_enabled_version="1.24", doc = "v1_24")]
#![cfg_attr(k8s_openapi_enabled_version="1.25", doc = "v1_25")]
#![cfg_attr(k8s_openapi_enabled_version="1.26", doc = "v1_26")]
#![cfg_attr(k8s_openapi_enabled_version="1.27", doc = "v1_27")]
#![cfg_attr(feature = "api", doc = r#"
(This requires the `api` feature to be enabled. The feature is enabled by default. See ["Crate features"](#crate-features) below for more details.)
This example executes the [`api::core::v1::Pod::list`] API operation to list all pods inside a namespace.
It demonstrates the common patterns implemented by all API operation functions in this crate:
1. The API function has required parameters and optional parameters. All optional parameters are taken as a single struct with optional fields.
Specifically for the [`api::core::v1::Pod::list`] operation, the `namespace` parameter is required and taken by the function itself,
while other optional parameters like `field_selector` are fields of the [`ListOptional`] struct. An instance of
this struct is taken as the last parameter of `Pod::list`. This struct impls [`Default`] so that you can just pass in `Default::default()`
if you don't want to specify values for any of the optional parameters.
Some API operations have a single common type for optional parameters:
- All create API take optional parameters using the [`CreateOptional`] struct.
- All delete API take optional parameters using the [`DeleteOptional`] struct.
- All list API take optional parameters using the [`ListOptional`] struct.
- All patch API take optional parameters using the [`PatchOptional`] struct.
- All replace API take optional parameters using the [`ReplaceOptional`] struct.
- All watch API take optional parameters using the [`WatchOptional`] struct.
- All delete-collection API take optional parameters using the [`DeleteOptional`] struct for delete options and the [`ListOptional`] struct for list options.
Other API functions have their own `Optional` structs with fields corresponding to the specific parameters for those functions,
such as [`api::core::v1::ReadPodLogOptional`] for [`api::core::v1::Pod::read_log`]
1. The function returns an [`http::Request`] value with the URL path, query string, and request body filled out according to the parameters
given to the function. The function does *not* execute this request. You can execute this `http::Request` using any HTTP client library you want to use.
It does not matter whether you use a synchronous client like `reqwest`, or an asynchronous client like `hyper`, or a mock client that returns bytes
read from a test file.
1. For each API operation function, there is a corresponding response type. For `Pod::list` this is [`ListResponse`]`<`[`api::core::v1::Pod`]`>`.
This is an enum with variants for each of the possible HTTP status codes that the operation can return, and contains the data that the API server would
return corresponding to that status code. For example, the list-namespaced-pod operation returns a pod list with HTTP 200 OK, so one of the variants of
that type is `Ok(`[`List`]`<`[`api::core::v1::Pod`]`>)`
1. The response types impl the [`Response`] trait, which contains a single [`Response::try_from_parts`] function. This function takes an [`http::StatusCode`]
and a `&u8` byte buffer, and tries to parse the byte buffer as the response type. For example, if you executed the request and received an HTTP 200 OK response
with some bytes, you could call `<ListResponse<Pod> as Response>::try_from_parts(status_code, buf)` and expect to get
`Ok(ListResponse::<Pod>::Ok(pod_list))` from it.
Once again, this design ensures that the crate is not tied to a specific HTTP client library or interface. It does not matter how you execute the HTTP request,
nor whether your library is synchronous or asynchronous, since every HTTP client library gives you a way to get the HTTP response status code and the bytes
of the response body.
1. The API operation function also returns another value next to the `http::Request`. This value is a function that takes an [`http::StatusCode`] and returns
a [`ResponseBody`]`<ListResponse<Pod>>`. As mentioned above, `Response::try_from_parts` requires you to maintain a byte buffer for the response body.
`ResponseBody` is a helper that maintains such a buffer internally. It provides an `append_slice()` function to append slices to this internal buffer,
and a `parse()` function to parse the buffer as the expected type (`ListResponse<Pod>` in this case).
It is not *necessary* to use the `ResponseBody` returned by the API operation function to parse the response. The `ResponseBody::parse` function is
only a wrapper around the underlying `Response::try_from_parts` function, and handles growing and shrinking its inner buffer as necessary. It also
helps ensure that the response body is parsed as the *correct* type for the operation, `ListResponse<Pod>` in this case, and not some other type.
However, you can instead use your own byte buffer instead of the `ResponseBody` value and call `ListResponse<Pod>::try_from_parts` yourself.
1. The response types are enums with variants corresponding to HTTP status codes. For example, the `ListResponse<Pod>::Ok` variant corresponds to the
HTTP 200 response of the list-namespaced-pod API.
Each response enum also has an `Other` variant, that is yielded when the response status code does not match any of the other variants.
This variant has a `Result<Option<`[`serde_json::Value`]`>, `[`serde_json::Error`]`>` value.
If the response body is empty, this value will be `Ok(None)`.
If the response body is not empty, this value will be an `Ok(Some(value))` or `Err(err)` from attempting to parse that body as a `serde_json::Value`.
If you expect the response body to be a specific JSON type such as [`apimachinery::pkg::apis::meta::v1::Status`], you can use the `serde_json::Value`
as a [`serde::Deserializer`] like `let status = <Status as Deserialize>::deserialize(value)?;`. On the other hand, if you expect the response body to not be
a JSON value, then ignore the `Err(err)` and parse the raw bytes of the response into the appropriate type.
Also see the `get_single_value` and `get_multiple_values` functions in
[the `k8s-openapi-tests` directory in the repository](https://github.com/Arnavion/k8s-openapi/tree/master/k8s-openapi-tests/src)
for examples of how to use a synchronous client with this style of API.
```rust,no_run
// Re-export of the http crate since it's used in the public API
use k8s_openapi::http;
use k8s_openapi::api::core::v1 as api;
# struct Response;
# impl Response {
# fn status_code(&self) -> http::StatusCode {
# unimplemented!()
# }
# fn read_into(&self, _buf: &mut [u8]) -> std::io::Result<usize> {
# unimplemented!()
# }
# }
#
// Assume `execute` is some function that takes an `http::Request` and
// executes it synchronously or asynchronously to get a response. This is
// provided by your HTTP client library.
//
// Note that the `http::Request` values returned by API operation functions
// only have a URL path, query string and request body filled out. That is,
// they do *not* have a URL host. So the real `execute` implementation
// would first mutate the URL of the request to an absolute URL with
// the API server's authority, add authorization headers, etc before
// actually executing it.
fn execute(req: http::Request<Vec<u8>>) -> Response { unimplemented!(); }
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a `http::Request` to list all the pods in the
// "kube-system" namespace.
let (request, response_body) =
api::Pod::list("kube-system", Default::default())?;
// Execute the request and get a response.
// If this is an asynchronous operation, you would await
// or otherwise yield to the event loop here.
let response = execute(request);
// Got a status code from executing the request.
let status_code: http::StatusCode = response.status_code();
// Construct the `ResponseBody<ListResponse<Pod>>` using the
// constructor returned by the API function.
let mut response_body = response_body(status_code);
// Buffer used for each read from the HTTP response.
let mut buf = Box::new([0_u8; 4096]);
let pod_list = loop {
// Read some bytes from the HTTP response into the buffer.
// If this is an asynchronous operation, you would await or
// yield to the event loop here.
let read = response.read_into(&mut *buf)?;
// `buf` now contains some data read from the response. Append it
// to the `ResponseBody` and try to parse it into
// the response type.
response_body.append_slice(&buf[..read]);
let response = response_body.parse();
match response {
// Successful response (HTTP 200 and parsed successfully)
Ok(k8s_openapi::ListResponse::Ok(pod_list)) =>
break pod_list,
// Some unexpected response
// (not HTTP 200, but still parsed successfully)
Ok(other) => return Err(format!(
"expected Ok but got {status_code} {other:?}").into()),
// Need more response data.
// Read more bytes from the response into the `ResponseBody`
Err(k8s_openapi::ResponseError::NeedMoreData) => continue,
// Some other error, like the response body being
// malformed JSON or invalid UTF-8.
Err(err) => return Err(format!(
"error: {status_code} {err:?}").into()),
}
};
for pod in pod_list.items {
println!("{pod:#?}",);
}
Ok(())
}
```
"#)]
#![cfg_attr(not(feature = "api"), doc = r#"
The `api` feature has been disabled, so the client API is not available. See ["Crate features"](#crate-features) below for more details.
"#)]
#![cfg_attr(feature = "api", doc = "[`api::core::v1::Pod::read`]")]
#![cfg_attr(not(feature = "api"), doc = "`api::core::v1::Pod::read`")]
pub use chrono;
#[cfg(feature = "api")]
pub use http;
#[cfg(feature = "api")]
pub use percent_encoding;
#[cfg(feature = "schemars")]
pub use schemars;
pub use serde;
pub use serde_json;
pub use serde_value;
#[cfg(feature = "api")]
pub use url;
#[cfg(feature = "api")]
#[path = "api.rs"]
mod _api;
#[cfg(feature = "api")]
pub use _api::{
RequestError,
Response, ResponseBody, ResponseError,
percent_encoding2,
};
#[path = "byte_string.rs"]
mod _byte_string;
pub use _byte_string::ByteString;
#[path = "deep_merge.rs"]
mod _deep_merge;
pub use self::_deep_merge::{DeepMerge, strategies as merge_strategies};
#[path = "resource.rs"]
mod _resource;
pub use _resource::{
Resource,
ResourceScope, ClusterResourceScope, NamespaceResourceScope, SubResourceScope,
ListableResource,
Metadata,
api_version, group, kind, version,
};
#[cfg(k8s_openapi_enabled_version="1.20")] mod v1_20;
#[cfg(k8s_openapi_enabled_version="1.20")] pub use self::v1_20::*;
#[cfg(k8s_openapi_enabled_version="1.21")] mod v1_21;
#[cfg(k8s_openapi_enabled_version="1.21")] pub use self::v1_21::*;
#[cfg(k8s_openapi_enabled_version="1.22")] mod v1_22;
#[cfg(k8s_openapi_enabled_version="1.22")] pub use self::v1_22::*;
#[cfg(k8s_openapi_enabled_version="1.23")] mod v1_23;
#[cfg(k8s_openapi_enabled_version="1.23")] pub use self::v1_23::*;
#[cfg(k8s_openapi_enabled_version="1.24")] mod v1_24;
#[cfg(k8s_openapi_enabled_version="1.24")] pub use self::v1_24::*;
#[cfg(k8s_openapi_enabled_version="1.25")] mod v1_25;
#[cfg(k8s_openapi_enabled_version="1.25")] pub use self::v1_25::*;
#[cfg(k8s_openapi_enabled_version="1.26")] mod v1_26;
#[cfg(k8s_openapi_enabled_version="1.26")] pub use self::v1_26::*;
#[cfg(k8s_openapi_enabled_version="1.27")] mod v1_27;
#[cfg(k8s_openapi_enabled_version="1.27")] pub use self::v1_27::*;
include!(concat!(env!("OUT_DIR"), "/conditional_compilation_macros.rs"));