Expand description
Bindings for the Kubernetes client API, generated from the OpenAPI spec.
Each supported version of Kubernetes is represented by a feature name (like v1_9
). Only one such feature can be enabled at a time.
These docs have been generated with the v1_32
feature enabled. To see docs for one of the other supported versions, please generate the docs locally with cargo doc --features 'v1_<>'
§Examples
§Resources
This example creates an instance of api::core::v1::PodSpec
with no other properties set, and pretty-prints it.
use k8s_openapi::api::core::v1 as api;
fn main() {
let pod_spec: api::PodSpec = Default::default();
println!("{pod_spec:#?}");
}
§Crate features
This crate contains several v1_*
features. Enabling one of the v1_*
features selects which version of the Kubernetes API server this crate should target.
For example, enabling the v1_50
feature means the crate will only contain the API exposed by Kubernetes 1.50. It will not expose API
that were removed in 1.50 or earlier, nor any API added in 1.51 or later.
One and only one of the v1_*
features must be enabled at the same time, otherwise the crate will not compile. This ensures that all crates in the crate graph
use the same types. If it was possible for one library crate to use api::core::v1::Pod
corresponding to v1.50 and another to use the type
corresponding to v1.51, an application would not be able to use the same Pod
value with both.
Thus, it is recommended that only application crates must enable one of the v1_*
features, corresponding to the version of Kubernetes
that the application wants to support.
# For application crates
[dependencies]
k8s-openapi = { version = "...", features = ["v1_50"] }
If you’re writing a library crate, your crate must not enable any features of k8s-openapi
directly. The choice of which feature to enable
must be left to any application crates that use your library. This ensures that all k8s-openapi
-using dependencies in that application crate’s dependency graph
use the same set of k8s-openapi
types and are interoperable.
If your library crate has tests or examples, you should also add a dev-dependency on k8s-openapi
in addition to the direct dependency,
and enable a version feature only for that dev-dependency.
# For library crates
[dependencies]
k8s-openapi = "..."
[dev-dependencies]
k8s-openapi = { version = "...", features = ["v1_50"] }
However, commands like cargo check
and cargo doc
do not build dev dependencies, so they will not enable the feature and will fail to build. There are two ways
you can resolve this:
-
Add a feature to your library that enables one of the k8s-openapi
v1_*
features, and then remember to enable this feature when running such commands.[features] __check = ["k8s-openapi/v1_50"]
$ cargo check --features __check
-
Define the
K8S_OPENAPI_ENABLED_VERSION
env var when running such commands:$ K8S_OPENAPI_ENABLED_VERSION=1.50 cargo check
§Conditional compilation
As the previous section explained, library crates must not enable any version features in their k8s-openapi
dependency. However, your library crate may
need to know about which version gets selected eventually.
For example:
-
Your crate creates a
PodSecurityContext
and wants to set thesupplemental_groups_policy
field. This field is only available in Kubernetes 1.31+, so you want your crate to fail to compile if a lower feature was enabled. -
Your crate creates a
PodSecurityContext
and wants to set thesupplemental_groups_policy
field, but it’s okay to not set it when compiling for older versions.
There are two ways for your crate to determine which feature of k8s-openapi
is enabled:
-
The
k8s-openapi
crate exportsk8s_if_*
macros, which either expand to their contents or don’t. See the docs of the macros for more details.With these macros, the two cases above would be solved like this:
-
ⓘ
// The compile_error!() is only emitted if 1.30 or lower is selected. k8s_openapi::k8s_if_le_1_30! { compile_error!("This crate requires the v1_31 (or higher) feature to be enabled on the k8s-openapi crate."); } ... let pod_security_context = k8s_openapi::api::core::v1::PodSecurityContext { supplemental_groups_policy: ..., ... };
-
ⓘ
let mut pod_security_context = k8s_openapi::api::core::v1::PodSecurityContext { ... }; k8s_openapi::k8s_if_ge_1_31! { pod_security_context.supplemental_groups_policy = ...; }
-
-
The
k8s-openapi
crate emits the selected version number as metadata that your crate can read in a build script from theDEP_K8S_OPENAPI_*_VERSION
env var.// Your crate's build.rs fn main() { let k8s_openapi_version: u32 = std::env::vars_os() .find_map(|(key, value)| { let key = key.into_string().ok()?; if key.starts_with("DEP_K8S_OPENAPI_") && key.ends_with("_VERSION") { let value = value.into_string().ok()?; Some(value) } else { None } }).expect("DEP_K8S_OPENAPI_*_VERSION must have been set by k8s-openapi") .parse().expect("DEP_K8S_OPENAPI_*_VERSION is malformed"); // k8s_openapi_version has the format 0x00_MM_NN_00. // // - MM is the major version. // - NN is the minor version. // // Thus, if the v1_31 feature was enabled, k8s_openapi_version would be 0x00_01_1F_00 // The build script can now do arbitrary things with the information. // For example, it could define custom cfgs: if k8s_openapi_version >= 0x00_01_1F_00 { println!(r#"cargo::rustc-cfg=k8s_pod_security_context_supports_supplemental_groups_policy"#); } // ... or emit new source code files under OUT_DIR, or anything else a build script can do. }
With this cfg, the two cases above would be solved like this:
-
ⓘ
// The compile_error!() is only emitted if 1.30 or lower is selected. #[cfg(not(k8s_pod_security_context_supports_supplemental_groups_policy))] compile_error!("This crate requires the v1_31 (or higher) feature to be enabled on the k8s-openapi crate."); ... let pod_security_context = k8s_openapi::api::core::v1::PodSecurityContext { supplemental_groups_policy: ..., ... };
-
ⓘ
let pod_security_context = k8s_openapi::api::core::v1::PodSecurityContext { #[cfg(not(k8s_pod_security_context_supports_supplemental_groups_policy))] supplemental_groups_policy: ..., ... };
-
Note that both approaches require your crate to have a direct dependency on the k8s-openapi
crate. Neither approach is available if your crate
only has a transitive dependency on the k8s-openapi
crate.
The macros approach is easier to use since it doesn’t require a build script.
The build script method lets you emit arbitrary cfgs, emit arbitrary source code, and generally gives you more options, at the cost of needing a build script.
cfg()
s can be used in places where macros cannot, such as how the second example above shows it being used on a single field in a struct literal.
§Custom resource definitions
The k8s-openapi-derive
crate provides a custom derive for generating types
for custom resources. See that crate’s docs for more information.
Re-exports§
pub use chrono;
pub use serde;
pub use serde_json;
pub use serde_value;
Modules§
- Strategies for merging collections.
Macros§
- This macro evaluates to its contents if the
v1_28
feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_29
feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_30
feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_31
feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_32
feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_28
or higher feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_29
or higher feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_30
or higher feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_31
or higher feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_32
or higher feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_28
or lower feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_29
or lower feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_30
or lower feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_31
or lower feature is enabled, otherwise it evaluates to nothing. - This macro evaluates to its contents if the
v1_32
or lower feature is enabled, otherwise it evaluates to nothing. - A macro that emits a
match
expr with the given test expression and arms. The match arms can be annotated with the other conditional compilation macros in this crate so that they’re only emitted if the predicate is true.
Structs§
- A wrapper around a list of bytes.
- Indicates that a
Resource
is cluster-scoped. - List is a list of resources.
- Indicates that a
Resource
is namespace-scoped. - Indicates that a
Resource
is neither cluster-scoped nor namespace-scoped.
Traits§
- A trait applies to types that support deep merging.
- A trait applied to all Kubernetes resources that can be part of a corresponding list.
- A trait applied to all Kubernetes resources that have metadata.
- A trait applied to all Kubernetes resources.
- The scope of a
Resource
.
Functions§
- Extracts the API version of the given resource value.
- Extracts the group of the given resource value.
- Extracts the kind of the given resource value.
- Extracts the version of the given resource value.