-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ResourceInherit derive macro 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]> * Rename ResourceInherit to Resource - Display usage in errorbounded CM watcher Signed-off-by: Danil-Grigorev <[email protected]> * Reverse order resource/inherit Signed-off-by: Danil-Grigorev <[email protected]> * Add cert_check example Signed-off-by: Danil-Grigorev <[email protected]> * Example fmt fixes Signed-off-by: Danil-Grigorev <[email protected]> * Review: comment and cargo fix Signed-off-by: Danil-Grigorev <[email protected]> * Examples readme fix Signed-off-by: Danil-Grigorev <[email protected]> --------- Signed-off-by: Danil-Grigorev <[email protected]> Co-authored-by: Eirik A <[email protected]>
- Loading branch information
1 parent
c975335
commit f45ac81
Showing
8 changed files
with
357 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use std::borrow::Cow; | ||
|
||
use k8s_openapi::{ | ||
api::core::v1::{ConfigMap, Namespace as Ns}, | ||
NamespaceResourceScope, | ||
}; | ||
use kube::{ | ||
api::ObjectMeta, | ||
client::scope::{Cluster, Namespace}, | ||
Client, Resource, ResourceExt, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use tracing::*; | ||
|
||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
enum Error { | ||
#[error("Failed to open client: {0}")] | ||
ClientSetup(#[source] kube::Error), | ||
#[error("Failed to list namespaces: {0}")] | ||
NamespaceList(#[source] kube::Error), | ||
#[error("Failed to get ConfigMap: {0}")] | ||
FetchFailed(#[from] kube::Error), | ||
#[error("Expected certificate key in ConfigMap: {0}")] | ||
MissingKey(#[from] serde_json::Error), | ||
} | ||
|
||
// Variant of ConfigMap that only accepts ConfigMaps with a CA certificate | ||
// to demonstrate manual implementation | ||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
struct CaConfigMapManual { | ||
metadata: ObjectMeta, | ||
data: CaConfigMapData, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
struct CaConfigMapData { | ||
#[serde(rename = "ca.crt")] | ||
ca_crt: String, | ||
} | ||
|
||
// Variant of ConfigMap that only accepts ConfigMaps with a CA certificate | ||
// with inherited resource implementation | ||
#[derive(Resource, Serialize, Deserialize, Debug, Clone)] | ||
#[resource(inherit = ConfigMap)] | ||
struct CaConfigMap { | ||
metadata: ObjectMeta, | ||
data: CaConfigMapData, | ||
} | ||
|
||
// Display of a manual implementation | ||
impl Resource for CaConfigMapManual { | ||
type DynamicType = (); | ||
type Scope = NamespaceResourceScope; | ||
|
||
fn kind(&(): &Self::DynamicType) -> Cow<'_, str> { | ||
Cow::Borrowed("ConfigMap") | ||
} | ||
|
||
fn group(&(): &Self::DynamicType) -> Cow<'_, str> { | ||
Cow::Borrowed("") | ||
} | ||
|
||
fn version(&(): &Self::DynamicType) -> Cow<'_, str> { | ||
Cow::Borrowed("v1") | ||
} | ||
|
||
fn plural(&(): &Self::DynamicType) -> Cow<'_, str> { | ||
Cow::Borrowed("configmaps") | ||
} | ||
|
||
fn meta(&self) -> &ObjectMeta { | ||
&self.metadata | ||
} | ||
|
||
fn meta_mut(&mut self) -> &mut ObjectMeta { | ||
&mut self.metadata | ||
} | ||
} | ||
|
||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
tracing_subscriber::fmt::init(); | ||
|
||
let client = Client::try_default().await.map_err(Error::ClientSetup)?; | ||
let namespaces = client | ||
.list::<Ns>(&Default::default(), &Cluster) | ||
.await | ||
.map_err(Error::NamespaceList)?; | ||
|
||
for ns in namespaces { | ||
// Equivalent ways to GET using different structs and different Resource impls, with added field validation on top. | ||
let _ca: ConfigMap = client | ||
.get("kube-root-ca.crt", &Namespace::from(ns.name_any())) | ||
.await?; | ||
let _ca: CaConfigMapManual = client | ||
.get("kube-root-ca.crt", &Namespace::from(ns.name_any())) | ||
.await?; | ||
let ca: CaConfigMap = client | ||
.get("kube-root-ca.crt", &Namespace::from(ns.name_any())) | ||
.await?; | ||
info!( | ||
"Found correct root ca config map in {}: {}", | ||
ns.name_any(), | ||
ca.name_any() | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// 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(resource))] | ||
struct InheritAttrs { | ||
inherit: syn::Path, | ||
#[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() | ||
} | ||
} | ||
|
||
impl Crates { | ||
fn default_kube_core() -> Path { | ||
parse_quote! { ::kube::core } // by default must work well with people using facade crate | ||
} | ||
|
||
fn default_k8s_openapi() -> Path { | ||
parse_quote! { ::k8s_openapi } | ||
} | ||
} | ||
|
||
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, | ||
}; | ||
// Limit derive to structs | ||
match derive_input.data { | ||
Data::Struct(_) | Data::Enum(_) => {} | ||
_ => { | ||
return syn::Error::new_spanned(&derive_input.ident, r#"Unions can not #[derive(Resource)]"#) | ||
.to_compile_error() | ||
} | ||
} | ||
let kube_attrs = match InheritAttrs::from_derive_input(&derive_input) { | ||
Err(err) => return err.write_errors(), | ||
Ok(attrs) => attrs, | ||
}; | ||
|
||
let InheritAttrs { | ||
inherit: resource, | ||
crates: Crates { | ||
kube_core, | ||
k8s_openapi, | ||
}, | ||
.. | ||
} = kube_attrs; | ||
|
||
let rootident = derive_input.ident; | ||
|
||
let inherit_resource = quote! { | ||
impl #kube_core::Resource for #rootident { | ||
type DynamicType = <#resource as #kube_core::Resource>::DynamicType; | ||
type Scope = <#resource as #kube_core::Resource>::Scope; | ||
|
||
fn group(_: &<#resource as #kube_core::Resource>::DynamicType) -> std::borrow::Cow<'_, str> { | ||
#resource::group(&Default::default()).into_owned().into() | ||
} | ||
|
||
fn kind(_: &<#resource as #kube_core::Resource>::DynamicType) -> std::borrow::Cow<'_, str> { | ||
#resource::kind(&Default::default()).into_owned().into() | ||
} | ||
|
||
fn version(_: &<#resource as #kube_core::Resource>::DynamicType) -> std::borrow::Cow<'_, str> { | ||
#resource::version(&Default::default()).into_owned().into() | ||
} | ||
|
||
fn api_version(_: &<#resource as #kube_core::Resource>::DynamicType) -> std::borrow::Cow<'_, str> { | ||
#resource::api_version(&Default::default()).into_owned().into() | ||
} | ||
|
||
fn plural(_: &<#resource as #kube_core::Resource>::DynamicType) -> 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! { | ||
#inherit_resource | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_inherit() { | ||
let input = quote! { | ||
#[derive(Resource)] | ||
#[resource(inherit = "ConfigMap")] | ||
struct Foo { metadata: ObjectMeta } | ||
}; | ||
|
||
let input = syn::parse2(input).unwrap(); | ||
InheritAttrs::from_derive_input(&input).unwrap(); | ||
} | ||
} |
Oops, something went wrong.