k8s_openapi/
lib.rs

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