Skip to content

Commit

Permalink
Add ResourceInherit derive macro
Browse files Browse the repository at this point in the history
Allows to generate Resource trait implementation for types which proxy
another type internally.

ConfigMaps and Secrets can be strictly typed on the client side. While
using DeserializeGuard, resources can be listed and watched, skipping
invalid resources.

Signed-off-by: Danil-Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev committed Sep 1, 2024
1 parent 7ff120a commit 6072a33
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 0 deletions.
47 changes: 47 additions & 0 deletions kube-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate proc_macro;
#[macro_use] extern crate quote;

mod custom_resource;
mod resource_inherit;

/// A custom derive for kubernetes custom resource definitions.
///
Expand Down Expand Up @@ -308,3 +309,49 @@ mod custom_resource;
pub fn derive_custom_resource(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
custom_resource::derive(proc_macro2::TokenStream::from(input)).into()
}

/// A custom derive for inheriting Resource impl for the type.
///
/// This will generate a [`kube::Resource`] trait implementation, which inherits the specified
/// resources trait implementation.
///
/// Such implementation allows to add strict typing to some typical resources like `Secret` or `ConfigMap`,
/// in cases when implementing CRD is not desirable or it does not fit the use-case.
///
/// This object can be used with [`kube::Api`].
///
/// # Example
///
/// ```rust
/// use kube::api::ObjectMeta;
/// use k8s_openapi::api::core::v1::ConfigMap;
/// use kube_derive::ResourceInherit;
/// use kube::Client;
///
/// #[derive(ResourceInherit)]
/// #[inherit(
/// resource = "ConfigMap",
/// namespaced,
/// )]
/// struct FooMap {
/// metadata: ObjectMeta,
/// data: Option<FooMapSpec>,
/// }
///
/// struct FooMapSpec {
/// field: String,
/// }
///
/// # let client: Client = todo!();
/// # let api: Api<FooMap> = Api::default_namespaced(client);
/// # let config_map = api.get("with-field")
/// ```
///
/// The example above will generate:
/// ```no_run
/// impl kube::Resource for FooMap { .. }
/// ```
#[proc_macro_derive(ResourceInherit, attributes(inherit))]
pub fn derive_resource_inherit(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
resource_inherit::derive(proc_macro2::TokenStream::from(input)).into()

Check warning on line 356 in kube-derive/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/lib.rs#L355-L356

Added lines #L355 - L356 were not covered by tests
}
121 changes: 121 additions & 0 deletions kube-derive/src/resource_inherit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Generated by darling macros, out of our control
#![allow(clippy::manual_unwrap_or_default)]

use darling::{FromDeriveInput, FromMeta};
use syn::{parse_quote, Data, DeriveInput, Path};

/// Values we can parse from #[kube(attrs)]
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(inherit))]
struct InheritAttrs {
resource: syn::Path,
#[darling(default)]
namespaced: bool,
#[darling(default)]
crates: Crates,
}

#[derive(Debug, FromMeta)]
struct Crates {
#[darling(default = "Self::default_kube_core")]
kube_core: Path,
#[darling(default = "Self::default_k8s_openapi")]
k8s_openapi: Path,
}

// Default is required when the subattribute isn't mentioned at all
// Delegate to darling rather than deriving, so that we can piggyback off the `#[darling(default)]` clauses
impl Default for Crates {
fn default() -> Self {
Self::from_list(&[]).unwrap()

Check warning on line 30 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L29-L30

Added lines #L29 - L30 were not covered by tests
}
}

impl Crates {
fn default_kube_core() -> Path {
parse_quote! { ::kube::core } // by default must work well with people using facade crate

Check warning on line 36 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L35-L36

Added lines #L35 - L36 were not covered by tests
}

fn default_k8s_openapi() -> Path {
parse_quote! { ::k8s_openapi }

Check warning on line 40 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L39-L40

Added lines #L39 - L40 were not covered by tests
}
}

pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let derive_input: DeriveInput = match syn::parse2(input) {
Err(err) => return err.to_compile_error(),
Ok(di) => di,

Check warning on line 47 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L44-L47

Added lines #L44 - L47 were not covered by tests
};
// Limit derive to structs
match derive_input.data {

Check warning on line 50 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L50

Added line #L50 was not covered by tests
Data::Struct(_) | Data::Enum(_) => {}
_ => {
return syn::Error::new_spanned(&derive_input.ident, r#"Unions can not #[derive(Resource)]"#)

Check warning on line 53 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L53

Added line #L53 was not covered by tests
.to_compile_error()
}
}
let kube_attrs = match InheritAttrs::from_derive_input(&derive_input) {
Err(err) => return err.write_errors(),
Ok(attrs) => attrs,

Check warning on line 59 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L57-L59

Added lines #L57 - L59 were not covered by tests
};

let InheritAttrs {
resource,
namespaced,

Check warning on line 64 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L63-L64

Added lines #L63 - L64 were not covered by tests
crates: Crates {
kube_core,
k8s_openapi,

Check warning on line 67 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L66-L67

Added lines #L66 - L67 were not covered by tests
..
},
..
} = kube_attrs;

let rootident = derive_input.ident;

Check warning on line 73 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L73

Added line #L73 was not covered by tests

let scope_quote = if namespaced {
quote! { #kube_core::NamespaceResourceScope }

Check warning on line 76 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L75-L76

Added lines #L75 - L76 were not covered by tests
} else {
quote! { #kube_core::ClusterResourceScope }

Check warning on line 78 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L78

Added line #L78 was not covered by tests
};

// let inherit = quote! { #inherit };
let inherit_resource = quote! {

Check warning on line 82 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L82

Added line #L82 was not covered by tests
impl #kube_core::Resource for #rootident {
type DynamicType = ();
type Scope = #scope_quote;

fn group(_: &()) -> std::borrow::Cow<'_, str> {
#resource::group(&Default::default()).into_owned().into()
}

fn kind(_: &()) -> std::borrow::Cow<'_, str> {
#resource::kind(&Default::default()).into_owned().into()
}

fn version(_: &()) -> std::borrow::Cow<'_, str> {
#resource::version(&Default::default()).into_owned().into()
}

fn api_version(_: &()) -> std::borrow::Cow<'_, str> {
#resource::api_version(&Default::default()).into_owned().into()
}

fn plural(_: &()) -> std::borrow::Cow<'_, str> {
#resource::plural(&Default::default()).into_owned().into()
}

fn meta(&self) -> &#k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta {
&self.metadata
}

fn meta_mut(&mut self) -> &mut #k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta {
&mut self.metadata
}
}
};

// Concat output
quote! {

Check warning on line 118 in kube-derive/src/resource_inherit.rs

View check run for this annotation

Codecov / codecov/patch

kube-derive/src/resource_inherit.rs#L118

Added line #L118 was not covered by tests
#inherit_resource
}
}
55 changes: 55 additions & 0 deletions kube-derive/tests/resource_inherit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use k8s_openapi::api::core::v1::ConfigMap;
use k8s_openapi::api::core::v1::Secret;
use k8s_openapi::ByteString;
use kube::api::ObjectMeta;
use kube_derive::ResourceInherit;

#[derive(ResourceInherit, Default)]
#[inherit(resource = "ConfigMap", namespaced)]
struct TypedMap {
metadata: ObjectMeta,
data: Option<TypedData>,
}

#[derive(Default)]
struct TypedData {
field: String,
}

#[derive(ResourceInherit, Default)]
#[inherit(resource = "Secret", namespaced)]
struct TypedSecret {
metadata: ObjectMeta,
data: Option<TypedSecretData>,
}

#[derive(Default)]
struct TypedSecretData {
field: ByteString,
}

#[cfg(test)]
mod tests {
use kube::Resource;

use crate::TypedMap;
use crate::TypedSecret;

#[test]
fn test_parse_config_map_default() {
TypedMap::default();
assert_eq!(TypedMap::kind(&()), "ConfigMap");
assert_eq!(TypedMap::api_version(&()), "v1");
assert_eq!(TypedMap::group(&()), "");
assert_eq!(TypedMap::plural(&()), "configmaps");
}

#[test]
fn test_parse_secret_default() {
TypedSecret::default();
assert_eq!(TypedSecret::kind(&()), "Secret");
assert_eq!(TypedSecret::api_version(&()), "v1");
assert_eq!(TypedSecret::group(&()), "");
assert_eq!(TypedSecret::plural(&()), "secrets");
}
}

0 comments on commit 6072a33

Please sign in to comment.