Skip to content

Commit 52e4392

Browse files
authored
Doc tweaks and 2 missing impls pre release (#1573)
* Doc tweaks and 2 missing impls pre release Mostly doc stuff that was inconsistent or looked unprofessional. In particular, the lib re-export ones now seem to concatenate the docstrings, so have made these cleaner on both end. Cleaned up the cert check example a little bit to more showcase the problem. Missing impls: - Derive + Clone on marker `Namespace` and `Cluster` for unstable client_ext Signed-off-by: clux <[email protected]> * redundant word Signed-off-by: clux <[email protected]> * more missing links Signed-off-by: clux <[email protected]> --------- Signed-off-by: clux <[email protected]>
1 parent 33a01c2 commit 52e4392

File tree

8 files changed

+45
-64
lines changed

8 files changed

+45
-64
lines changed

examples/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ cargo run --example crd_api
6969
cargo run --example crd_derive
7070
cargo run --example crd_derive_schema
7171
cargo run --example crd_derive_no_schema --no-default-features --features=openssl-tls,latest
72-
# collect kube-root configmaps from each namespace, with strictly typed serialization on ca.crt key
73-
cargo run --example cert_check
72+
cargo run --example cert_check # showcases partial typing with Resource derive
7473
```
7574

76-
The last one opts out from the default `schema` feature from `kube-derive` (and thus the need for you to derive/impl `JsonSchema`).
75+
The `no_schema` one opts out from the default `schema` feature from `kube-derive` (and thus the need for you to derive/impl `JsonSchema`).
7776

7877
**However**: without the `schema` feature, it's left **up to you to fill in a valid openapi v3 schema**, as schemas are **required** for [v1::CustomResourceDefinitions](https://docs.rs/k8s-openapi/0.10.0/k8s_openapi/apiextensions_apiserver/pkg/apis/apiextensions/v1/struct.CustomResourceDefinition.html), and the generated crd will be rejected by the apiserver if it's missing. As the last example shows, you can do this directly without `schemars`.
7978

examples/cert_check.rs

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,34 @@ use k8s_openapi::{
77
use kube::{
88
api::ObjectMeta,
99
client::scope::{Cluster, Namespace},
10-
Client, Resource, ResourceExt,
10+
Client, Resource,
1111
};
1212
use serde::{Deserialize, Serialize};
1313
use tracing::*;
1414

15-
use thiserror::Error;
16-
17-
#[derive(Debug, Error)]
18-
enum Error {
19-
#[error("Failed to open client: {0}")]
20-
ClientSetup(#[source] kube::Error),
21-
#[error("Failed to list namespaces: {0}")]
22-
NamespaceList(#[source] kube::Error),
23-
#[error("Failed to get ConfigMap: {0}")]
24-
FetchFailed(#[from] kube::Error),
25-
#[error("Expected certificate key in ConfigMap: {0}")]
26-
MissingKey(#[from] serde_json::Error),
27-
}
28-
29-
// Variant of ConfigMap that only accepts ConfigMaps with a CA certificate
30-
// to demonstrate manual implementation
31-
#[derive(Serialize, Deserialize, Debug, Clone)]
32-
struct CaConfigMapManual {
33-
metadata: ObjectMeta,
34-
data: CaConfigMapData,
35-
}
36-
15+
// Our own way of representing data - partially typed in 2 ways
16+
// For a ConfigMap variant that only accepts CA certificates
3717
#[derive(Serialize, Deserialize, Debug, Clone)]
3818
struct CaConfigMapData {
3919
#[serde(rename = "ca.crt")]
4020
ca_crt: String,
4121
}
4222

43-
// Variant of ConfigMap that only accepts ConfigMaps with a CA certificate
44-
// with inherited resource implementation
23+
// Method 1 :: inherit resource implementation from k8s_openapi's ConfigMap
4524
#[derive(Resource, Serialize, Deserialize, Debug, Clone)]
4625
#[resource(inherit = ConfigMap)]
4726
struct CaConfigMap {
4827
metadata: ObjectMeta,
4928
data: CaConfigMapData,
5029
}
5130

52-
// Display of a manual implementation
31+
// Method 2 :: manual Resource implementation
32+
#[derive(Serialize, Deserialize, Debug, Clone)]
33+
struct CaConfigMapManual {
34+
metadata: ObjectMeta,
35+
data: CaConfigMapData,
36+
}
37+
// Method 2 :: manual Resource implementation
5338
impl Resource for CaConfigMapManual {
5439
type DynamicType = ();
5540
type Scope = NamespaceResourceScope;
@@ -79,33 +64,24 @@ impl Resource for CaConfigMapManual {
7964
}
8065
}
8166

82-
8367
#[tokio::main]
84-
async fn main() -> Result<(), Error> {
68+
async fn main() -> anyhow::Result<()> {
8569
tracing_subscriber::fmt::init();
8670

87-
let client = Client::try_default().await.map_err(Error::ClientSetup)?;
88-
let namespaces = client
89-
.list::<Ns>(&Default::default(), &Cluster)
90-
.await
91-
.map_err(Error::NamespaceList)?;
71+
let client = Client::try_default().await?;
72+
let namespaces = client.list::<Ns>(&Default::default(), &Cluster).await?;
73+
let kube_root = "kube-root-ca.crt";
9274

9375
for ns in namespaces {
76+
let ns = Namespace::try_from(&ns)?;
9477
// Equivalent ways to GET using different structs and different Resource impls, with added field validation on top.
95-
let _ca: ConfigMap = client
96-
.get("kube-root-ca.crt", &Namespace::from(ns.name_any()))
97-
.await?;
98-
let _ca: CaConfigMapManual = client
99-
.get("kube-root-ca.crt", &Namespace::from(ns.name_any()))
100-
.await?;
101-
let ca: CaConfigMap = client
102-
.get("kube-root-ca.crt", &Namespace::from(ns.name_any()))
103-
.await?;
104-
info!(
105-
"Found correct root ca config map in {}: {}",
106-
ns.name_any(),
107-
ca.name_any()
108-
);
78+
let ca1: ConfigMap = client.get(kube_root, &ns).await?;
79+
let ca2: CaConfigMapManual = client.get(kube_root, &ns).await?;
80+
let ca3: CaConfigMap = client.get(kube_root, &ns).await?;
81+
info!("Found {kube_root} in {ns:?} with all 3 methods");
82+
debug!("ca1: {ca1:?}");
83+
debug!("ca2: {ca2:?}");
84+
debug!("ca3: {ca3:?}");
10985
}
11086

11187
Ok(())

kube-client/src/client/client_ext.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ pub trait ObjectUrl<K> {
3939
}
4040

4141
/// Marker type for cluster level queries
42+
#[derive(Debug, Clone)]
4243
pub struct Cluster;
4344
/// Namespace newtype for namespace level queries
4445
///
4546
/// You can create this directly, or convert `From` a `String` / `&str`, or `TryFrom` an `k8s_openapi::api::core::v1::Namespace`
47+
#[derive(Debug, Clone)]
4648
pub struct Namespace(String);
4749

4850
/// Referenced object name resolution

kube-client/src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A basic API client for interacting with the Kubernetes API
1+
//! API client for interacting with the Kubernetes API
22
//!
33
//! The [`Client`] uses standard kube error handling.
44
//!

kube-client/src/config/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ pub enum LoadDataError {
118118
NoBase64DataOrFile,
119119
}
120120

121-
/// Configuration object detailing things like cluster URL, default namespace, root certificates, and timeouts.
121+
/// Configuration object for accessing a Kuernetes cluster
122+
///
123+
/// The configurable parameters for connecting like cluster URL, default namespace, root certificates, and timeouts.
124+
/// Normally created implicitly through [`Config::infer`] or [`Client::try_default`](crate::Client::try_default).
122125
///
123126
/// # Usage
124127
/// Construct a [`Config`] instance by using one of the many constructors.

kube-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Crate with types and traits necessary for interacting with the Kubernetes API
1+
//! Types and traits necessary for interacting with the Kubernetes API
22
//!
33
//! This crate provides the minimal apimachinery necessary to make requests to the kubernetes API.
44
//!

kube-derive/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,13 @@ pub fn derive_custom_resource(input: proc_macro::TokenStream) -> proc_macro::Tok
312312

313313
/// A custom derive for inheriting Resource impl for the type.
314314
///
315-
/// This will generate a [`kube::Resource`] trait implementation, which inherits the specified
316-
/// resources trait implementation.
315+
/// This will generate a [`kube::Resource`] trait implementation,
316+
/// inheriting from a specified resource trait implementation.
317317
///
318-
/// Such implementation allows to add strict typing to some typical resources like `Secret` or `ConfigMap`,
318+
/// This allows strict typing to some typical resources like `Secret` or `ConfigMap`,
319319
/// in cases when implementing CRD is not desirable or it does not fit the use-case.
320320
///
321-
/// This object can be used with [`kube::Api`].
321+
/// Once derived, the type can be used with [`kube::Api`].
322322
///
323323
/// # Example
324324
///
@@ -351,6 +351,11 @@ pub fn derive_custom_resource(input: proc_macro::TokenStream) -> proc_macro::Tok
351351
/// ```
352352
/// // impl kube::Resource for FooMap { .. }
353353
/// ```
354+
/// [`kube`]: https://docs.rs/kube
355+
/// [`kube::Api`]: https://docs.rs/kube/*/kube/struct.Api.html
356+
/// [`kube::Resource`]: https://docs.rs/kube/*/kube/trait.Resource.html
357+
/// [`kube::core::ApiResource`]: https://docs.rs/kube/*/kube/core/struct.ApiResource.html
358+
/// [`kube::CustomResourceExt`]: https://docs.rs/kube/*/kube/trait.CustomResourceExt.html
354359
#[proc_macro_derive(Resource, attributes(resource))]
355360
pub fn derive_resource(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
356361
resource::derive(proc_macro2::TokenStream::from(input)).into()

kube/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! - [`client`] with the Kubernetes [`Client`] and its layers
1111
//! - [`config`] for cluster [`Config`]
1212
//! - [`api`] with the generic Kubernetes [`Api`]
13-
//! - [`derive`](kube_derive) with the [`CustomResource`] derive for building controllers types
13+
//! - [`derive`](kube_derive) with the [`CustomResource`] / [`Resource`](kube_derive::Resource) derive for building controllers types
1414
//! - [`runtime`] with a [`Controller`](crate::runtime::Controller) / [`watcher`](crate::runtime::watcher()) / [`reflector`](crate::runtime::reflector::reflector) / [`Store`](crate::runtime::reflector::Store)
1515
//! - [`core`] with generics from `apimachinery`
1616
//!
@@ -160,7 +160,6 @@ cfg_error! {
160160
pub type Result<T, E = Error> = std::result::Result<T, E>;
161161
}
162162

163-
/// Re-exports from [`kube-derive`](kube_derive)
164163
#[cfg(feature = "derive")]
165164
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
166165
pub use kube_derive::CustomResource;
@@ -169,24 +168,21 @@ pub use kube_derive::CustomResource;
169168
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
170169
pub use kube_derive::Resource;
171170

172-
/// Re-exports from `kube-runtime`
173171
#[cfg(feature = "runtime")]
174172
#[cfg_attr(docsrs, doc(cfg(feature = "runtime")))]
175173
#[doc(inline)]
176174
pub use kube_runtime as runtime;
177175

178176
pub use crate::core::{CustomResourceExt, Resource, ResourceExt};
179-
/// Re-exports from `kube_core`
180-
#[doc(inline)]
181-
pub use kube_core as core;
177+
#[doc(inline)] pub use kube_core as core;
182178

183179
// Mock tests for the runtime
184180
#[cfg(test)]
185181
#[cfg(all(feature = "derive", feature = "runtime"))]
186182
mod mock_tests;
187183

188184
pub mod prelude {
189-
//! A "prelude" for kube client crate. Reduces the number of duplicated imports.
185+
//! A prelude for kube. Reduces the number of duplicated imports.
190186
//!
191187
//! This prelude is similar to the standard library's prelude in that you'll
192188
//! almost always want to import its entire contents, but unlike the

0 commit comments

Comments
 (0)