Skip to content

Commit 92090b1

Browse files
committed
Move overrides into #[kube(crates)], rename schema_mode to schema
Signed-off-by: Teo Klestrup Röijezon <[email protected]>
1 parent 3083d34 commit 92090b1

File tree

5 files changed

+65
-39
lines changed

5 files changed

+65
-39
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ UNRELEASED
1212
- Replaced `kube::Error::RequestValidation(String)` variant with `kube::Error::BuildRequest(kube::core::request::Error)`. This variant includes possible errors when building an HTTP request as described above, and contains errors that was previously grouped under `kube::Error::SerdeError` and `kube::Error::HttpError`.
1313
- Removed `impl From<T> for kube::Error` for the following types: `std::io::Error`, `hyper::Error`, `tower::BoxError`, `std::string::FromUtf8Error`, `http::Error`, `http::uri::InvalidUri`, `serde_json::Error`, `openssl::error::ErrorStack`, `kube::core::Error`, `kube::error::ConfigError`, `kube::error::DisoveryError`, `kube::error::OAuthError`.
1414
- Changed variants of error enums in `kube::runtime`. Replaced `snafu` with `thiserror`.
15-
* BREAKING: Replaced feature `kube-derive/schema` with attribute `#[kube(schema_mode)]` - #690
16-
- If you currently disable default `kube-derive` default features to avoid automatic schema generation, add `#[kube(schema_mode = "disabled")]` to your spec struct instead
15+
* BREAKING: Replaced feature `kube-derive/schema` with attribute `#[kube(schema)]` - #690
16+
- If you currently disable default `kube-derive` default features to avoid automatic schema generation, add `#[kube(schema = "disabled")]` to your spec struct instead
17+
* BREAKING: Moved `CustomResource` derive crate overrides into subattribute `#[kube(crates(...))]` - #690
18+
- Replace `#[kube(kube_core = .., k8s_openapi = .., schema = .., serde = .., serde_json = ..)]` with `#[kube(crates(kube_core = .., k8s_openapi = .., schema = .., serde = .., serde_json = ..))]`
1719

1820
0.63.2 / 2021-10-28
1921
===================

examples/crd_derive_custom_schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
1313
version = "v1",
1414
kind = "Bar",
1515
namespaced,
16-
schema_mode = "custom"
16+
schema = "custom"
1717
)]
1818
pub struct MyBar {
1919
bars: u32,

examples/crd_derive_no_schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
1414
version = "v1",
1515
kind = "Bar",
1616
namespaced,
17-
schema_mode = "disabled"
17+
schema = "disabled"
1818
)]
1919
pub struct MyBar {
2020
bars: u32,

kube-derive/src/custom_resource.rs

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct KubeAttrs {
2424
#[darling(multiple, rename = "derive")]
2525
derives: Vec<String>,
2626
#[darling(default)]
27-
schema_mode: Option<SchemaMode>,
27+
schema: Option<SchemaMode>,
2828
#[darling(default)]
2929
status: Option<String>,
3030
#[darling(multiple, rename = "category")]
@@ -35,35 +35,56 @@ struct KubeAttrs {
3535
printcolums: Vec<String>,
3636
#[darling(default)]
3737
scale: Option<String>,
38-
#[darling(default = "default_kube_core")]
38+
#[darling(default)]
39+
crates: Crates,
40+
}
41+
42+
#[derive(Debug, FromMeta)]
43+
struct Crates {
44+
#[darling(default = "Self::default_kube_core")]
3945
kube_core: Path,
40-
#[darling(default = "default_k8s_openapi")]
46+
#[darling(default = "Self::default_k8s_openapi")]
4147
k8s_openapi: Path,
42-
#[darling(default = "default_schemars")]
48+
#[darling(default = "Self::default_schemars")]
4349
schemars: Path,
44-
#[darling(default = "default_serde")]
50+
#[darling(default = "Self::default_serde")]
4551
serde: Path,
46-
#[darling(default = "default_serde_json")]
52+
#[darling(default = "Self::default_serde_json")]
4753
serde_json: Path,
4854
}
4955

50-
fn default_apiext() -> String {
51-
"v1".to_owned()
52-
}
53-
fn default_kube_core() -> Path {
54-
parse_quote! { ::kube::core } // by default must work well with people using facade crate
55-
}
56-
fn default_k8s_openapi() -> Path {
57-
parse_quote! { ::k8s_openapi }
58-
}
59-
fn default_schemars() -> Path {
60-
parse_quote! { ::schemars }
56+
// Default is required when the subattribute isn't mentioned at all
57+
// Delegate to darling rather than deriving, so that we can piggyback off the `#[darling(default)]` clauses
58+
impl Default for Crates {
59+
fn default() -> Self {
60+
Self::from_list(&[]).unwrap()
61+
}
6162
}
62-
fn default_serde() -> Path {
63-
parse_quote! { ::serde }
63+
64+
impl Crates {
65+
fn default_kube_core() -> Path {
66+
parse_quote! { ::kube::core } // by default must work well with people using facade crate
67+
}
68+
69+
fn default_k8s_openapi() -> Path {
70+
parse_quote! { ::k8s_openapi }
71+
}
72+
73+
fn default_schemars() -> Path {
74+
parse_quote! { ::schemars }
75+
}
76+
77+
fn default_serde() -> Path {
78+
parse_quote! { ::serde }
79+
}
80+
81+
fn default_serde_json() -> Path {
82+
parse_quote! { ::serde_json }
83+
}
6484
}
65-
fn default_serde_json() -> Path {
66-
parse_quote! { ::serde_json }
85+
86+
fn default_apiext() -> String {
87+
"v1".to_owned()
6788
}
6889

6990
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -130,7 +151,7 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
130151
version,
131152
namespaced,
132153
derives,
133-
schema_mode,
154+
schema: schema_mode,
134155
status,
135156
plural,
136157
singular,
@@ -139,11 +160,14 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
139160
printcolums,
140161
apiextensions,
141162
scale,
142-
kube_core,
143-
k8s_openapi,
144-
schemars,
145-
serde,
146-
serde_json,
163+
crates:
164+
Crates {
165+
kube_core,
166+
k8s_openapi,
167+
schemars,
168+
serde,
169+
serde_json,
170+
},
147171
} = kube_attrs;
148172

149173
let struct_name = kind_struct.unwrap_or_else(|| kind.clone());

kube-derive/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ mod custom_resource;
8787
/// ### `#[kube(struct = "StructName")]`
8888
/// Customize the name of the generated root struct (defaults to `kind`).
8989
///
90-
/// ### `#[kube(kube_core = "::kube::core")]`
90+
/// ### `#[kube(crates(kube_core = "::kube::core"))]`
9191
/// Customize the crate name the generated code will reach into (defaults to `::kube::core`).
9292
/// Should be one of `kube::core`, `kube_client::core` or `kube_core`.
9393
///
94-
/// ### `#[kube(k8s_openapi = "::k8s_openapi")]`
94+
/// ### `#[kube(crates(k8s_openapi = "::k8s_openapi"))]`
9595
/// Customize the crate name the generated code will use for [`k8s_openapi`](https://docs.rs/k8s-openapi/) (defaults to `::k8s_openapi`).
9696
///
97-
/// ### `#[kube(schemars = "::schemars")]`
97+
/// ### `#[kube(crates(schemars = "::schemars"))]`
9898
/// Customize the crate name the generated code will use for [`schemars`](https://docs.rs/schemars/) (defaults to `::schemars`).
9999
///
100-
/// ### `#[kube(serde = "::serde")]`
100+
/// ### `#[kube(crates(serde = "::serde"))]`
101101
/// Customize the crate name the generated code will use for [`serde`](https://docs.rs/serde/) (defaults to `::serde`).
102102
///
103-
/// ### `#[kube(serde_json = "::serde_json")]`
103+
/// ### `#[kube(crates(serde_json = "::serde_json"))]`
104104
/// Customize the crate name the generated code will use for [`serde_json`](https://docs.rs/serde_json/) (defaults to `::serde_json`).
105105
///
106106
/// ### `#[kube(status = "StatusStructName")]`
@@ -111,7 +111,7 @@ mod custom_resource;
111111
/// Adding `#[kube(derive = "PartialEq")]` is required if you want your generated
112112
/// top level type to be able to `#[derive(PartialEq)]`
113113
///
114-
/// ### `#[kube(schema_mode = "mode")]`
114+
/// ### `#[kube(schema = "mode")]`
115115
/// Defines whether the `JsonSchema` of the top level generated type should be used when generating a `CustomResourceDefinition`.
116116
///
117117
/// Legal values:
@@ -124,7 +124,7 @@ mod custom_resource;
124124
///
125125
/// Defaults to `"disabled"` when `apiextensions = "v1beta1"`, otherwise `"derived"`.
126126
///
127-
/// NOTE: `apiextensions = "v1"` `CustomResourceDefinition`s require a schema. If `schema_mode = "disabled"` then
127+
/// NOTE: `apiextensions = "v1"` `CustomResourceDefinition`s require a schema. If `schema = "disabled"` then
128128
/// `Self::crd()` will not be installable into the cluster as-is.
129129
///
130130
/// ### `#[kube(scale = r#"json"#)]`
@@ -206,7 +206,7 @@ mod custom_resource;
206206
///
207207
/// See [kubernetes openapi validation](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation) for the format of the OpenAPI v3 schemas.
208208
///
209-
/// If you have to override a lot, [you can opt-out of schema-generation entirely](#kubeschema_mode--mode)
209+
/// If you have to override a lot, [you can opt-out of schema-generation entirely](#kubeschema--mode)
210210
///
211211
/// ## Advanced Features
212212
/// - **embedding k8s-openapi types** can be done by enabling the `schemars` feature of `k8s-openapi` from [`0.13.0`](https://github.com/Arnavion/k8s-openapi/blob/master/CHANGELOG.md#v0130-2021-08-09)

0 commit comments

Comments
 (0)