k8s_openapi/lib.rs
1#![warn(rust_2018_idioms)]
2#![allow(
3 clippy::doc_markdown,
4 clippy::doc_lazy_continuation,
5 clippy::doc_overindented_list_items,
6 clippy::match_single_binding,
7 clippy::module_name_repetitions,
8 clippy::similar_names,
9 clippy::single_match_else,
10 rustdoc::bare_urls,
11)]
12
13// `schemars::json_schema!` expansion hits recursion limit.
14#![cfg_attr(feature = "schemars", recursion_limit = "256")]
15
16//! Bindings for the Kubernetes client API, generated from the OpenAPI spec.
17//!
18//! Each supported version of Kubernetes is represented by a feature name (like `v1_9`). Only one such feature can be enabled at a time.
19//!
20//! These docs have been generated with the `
21
22#![cfg_attr(k8s_openapi_enabled_version="1.33", doc = "v1_33")]
23#![cfg_attr(k8s_openapi_enabled_version="1.34", doc = "v1_34")]
24#![cfg_attr(k8s_openapi_enabled_version="1.35", doc = "v1_35")]
25#![cfg_attr(k8s_openapi_enabled_version="1.36", doc = "v1_36")]
26#![cfg_attr(k8s_openapi_enabled_version="1.37", doc = "v1_37")]
27
28//! ` feature enabled. To see docs for one of the other supported versions, please generate the docs locally with `cargo doc --features 'v1_<>'`
29//!
30//!
31//! # Examples
32//!
33//! ## Resources
34//!
35//! This example creates an instance of [`api::core::v1::PodSpec`] with no other properties set, and pretty-prints it.
36//!
37//! ```rust
38//! use k8s_openapi::api::core::v1 as api;
39//!
40//! fn main() {
41//! let pod_spec: api::PodSpec = Default::default();
42//! println!("{pod_spec:#?}");
43//! }
44//! ```
45//!
46//!
47//! # Crate features
48//!
49//! This crate contains several `v1_*` features. Enabling one of the `v1_*` features selects which version of the Kubernetes API server this crate should target.
50//! 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
51//! that were removed in 1.50 or earlier, nor any API added in 1.51 or later.
52//!
53//! 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
54//! 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
55//! corresponding to v1.51, an application would not be able to use the same `Pod` value with both.
56//!
57//! Thus, it is recommended that only application crates must enable one of the `v1_*` features, corresponding to the version of Kubernetes
58//! that the application wants to support.
59//!
60//! ```toml
61//! # For application crates
62//!
63//! [dependencies]
64//! k8s-openapi = { version = "...", features = ["v1_50"] }
65//! ```
66//!
67//! 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
68//! 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
69//! use the same set of `k8s-openapi` types and are interoperable.
70//!
71//! If your library crate has tests or examples, you should also add a dev-dependency on `k8s-openapi` in addition to the direct dependency,
72//! and enable a version feature only for that dev-dependency.
73//!
74//! ```toml
75//! # For library crates
76//!
77//! [dependencies]
78//! k8s-openapi = "..."
79//!
80//! [dev-dependencies]
81//! k8s-openapi = { version = "...", features = ["v1_50"] }
82//! ```
83//!
84//! 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
85//! you can resolve this:
86//!
87//! 1. 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.
88//!
89//! ```toml
90//! [features]
91//! __check = ["k8s-openapi/v1_50"]
92//! ```
93//!
94//! ```sh
95//! $ cargo check --features __check
96//! ```
97//!
98//! 1. Define the `K8S_OPENAPI_ENABLED_VERSION` env var when running such commands:
99//!
100//! ```sh
101//! $ K8S_OPENAPI_ENABLED_VERSION=1.50 cargo check
102//! ```
103//!
104//!
105//! # Conditional compilation
106//!
107//! As the previous section explained, library crates must not enable any version features in their `k8s-openapi` dependency. However, your library crate may
108//! need to know about which version gets selected eventually.
109//!
110//! For example:
111//!
112//! 1. Your crate creates a `PodSpec` and wants to set the `hostname_override` field. This field is only available in Kubernetes 1.34+,
113//! so you want your crate to fail to compile if a lower feature was enabled.
114//!
115//! 1. Your crate creates a `PodSpec` and wants to set the `hostname_override` field, but it's okay to not set it when compiling for older versions.
116//!
117//! There are two ways for your crate to determine which feature of `k8s-openapi` is enabled:
118//!
119//! 1. The `k8s-openapi` crate exports [`k8s_if_*` macros,](#macros) which either expand to their contents or don't. See the docs of the macros for more details.
120//!
121//! With these macros, the two cases above would be solved like this:
122//!
123//! - ```rust,ignore
124//! // The compile_error!() is only emitted if 1.33 or lower is selected.
125//! k8s_openapi::k8s_if_le_1_33! {
126//! compile_error!("This crate requires the v1_34 (or higher) feature to be enabled on the k8s-openapi crate.");
127//! }
128//!
129//! ...
130//!
131//! let pod_spec = k8s_openapi::api::core::v1::PodSpec {
132//! hostname_override: ...,
133//! ...
134//! };
135//! ```
136//!
137//! - ```rust,ignore
138//! let mut pod_spec = k8s_openapi::api::core::v1::PodSpec {
139//! ...
140//! };
141//!
142//! k8s_openapi::k8s_if_ge_1_34! {
143//! pod_spec.hostname_override = ...;
144//! }
145//! ```
146//!
147//! 1. The `k8s-openapi` crate emits the selected version number as metadata that your crate can read in a build script
148//! from the `DEP_K8S_OPENAPI_*_VERSION` env var.
149//!
150//! ```rust,no_run
151//! // Your crate's build.rs
152//!
153//! fn main() {
154//! let k8s_openapi_version: u32 =
155//! std::env::vars_os()
156//! .find_map(|(key, value)| {
157//! let key = key.into_string().ok()?;
158//! if key.starts_with("DEP_K8S_OPENAPI_") && key.ends_with("_VERSION") {
159//! let value = value.into_string().ok()?;
160//! Some(value)
161//! }
162//! else {
163//! None
164//! }
165//! }).expect("DEP_K8S_OPENAPI_*_VERSION must have been set by k8s-openapi")
166//! .parse().expect("DEP_K8S_OPENAPI_*_VERSION is malformed");
167//!
168//! // k8s_openapi_version has the format 0x00_MM_NN_00.
169//! //
170//! // - MM is the major version.
171//! // - NN is the minor version.
172//! //
173//! // Thus, if the v1_34 feature was enabled, k8s_openapi_version would be 0x00_01_22_00
174//!
175//! // The build script can now do arbitrary things with the information.
176//! // For example, it could define custom cfgs:
177//! if k8s_openapi_version >= 0x00_01_22_00 {
178//! println!("cargo::rustc-cfg=k8s_pod_spec_supports_hostname_override");
179//! }
180//!
181//! // ... or emit new source code files under OUT_DIR, or anything else a build script can do.
182//! }
183//! ```
184//!
185//! With this cfg, the two cases above would be solved like this:
186//!
187//! - ```rust,ignore
188//! // The compile_error!() is only emitted if 1.33 or lower is selected.
189//! #[cfg(not(k8s_pod_spec_supports_hostname_override))]
190//! compile_error!("This crate requires the v1_34 (or higher) feature to be enabled on the k8s-openapi crate.");
191//!
192//! ...
193//!
194//! let pod_spec = k8s_openapi::api::core::v1::PodSpec {
195//! hostname_override: ...,
196//! ...
197//! };
198//! ```
199//!
200//! - ```rust,ignore
201//! let pod_spec = k8s_openapi::api::core::v1::PodSpec {
202//! #[cfg(not(k8s_pod_spec_supports_hostname_override))]
203//! hostname_override: ...,
204//! ...
205//! };
206//! ```
207//!
208//! Note that both approaches require your crate to have a direct dependency on the `k8s-openapi` crate. Neither approach is available if your crate
209//! only has a transitive dependency on the `k8s-openapi` crate.
210//!
211//! The macros approach is easier to use since it doesn't require a build script.
212//!
213//! 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.
214//! `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.
215//!
216//!
217//! # Custom resource definitions
218//!
219//! The [`k8s-openapi-derive` crate](https://crates.io/crates/k8s-openapi-derive) provides a custom derive for generating types
220//! for custom resources. See that crate's docs for more information.
221
222#![no_std]
223
224#[cfg(not(feature = "std"))]
225extern crate alloc as std;
226#[cfg(feature = "std")]
227extern crate std;
228
229pub use jiff;
230#[cfg(feature = "schemars")]
231pub use schemars;
232pub use serde;
233pub use serde_json;
234
235
236#[path = "byte_string.rs"]
237mod _byte_string;
238pub use _byte_string::ByteString;
239
240#[path = "deep_merge.rs"]
241mod _deep_merge;
242pub use self::_deep_merge::{DeepMerge, strategies as merge_strategies};
243
244#[path = "resource.rs"]
245mod _resource;
246pub use _resource::{
247 Resource,
248 ResourceScope, ClusterResourceScope, NamespaceResourceScope, SubResourceScope,
249 ListableResource,
250 Metadata,
251 api_version, group, kind, version,
252};
253
254#[cfg(k8s_openapi_enabled_version="1.33")] mod v1_33;
255#[cfg(k8s_openapi_enabled_version="1.33")] pub use self::v1_33::*;
256
257#[cfg(k8s_openapi_enabled_version="1.34")] mod v1_34;
258#[cfg(k8s_openapi_enabled_version="1.34")] pub use self::v1_34::*;
259
260#[cfg(k8s_openapi_enabled_version="1.35")] mod v1_35;
261#[cfg(k8s_openapi_enabled_version="1.35")] pub use self::v1_35::*;
262
263#[cfg(k8s_openapi_enabled_version="1.36")] mod v1_36;
264#[cfg(k8s_openapi_enabled_version="1.36")] pub use self::v1_36::*;
265
266#[cfg(k8s_openapi_enabled_version="1.37")] mod v1_37;
267#[cfg(k8s_openapi_enabled_version="1.37")] pub use self::v1_37::*;
268
269include!(concat!(env!("OUT_DIR"), "/conditional_compilation_macros.rs"));