Skip to content

Commit

Permalink
try to move status attribute to take a path
Browse files Browse the repository at this point in the history
Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed Feb 26, 2025
1 parent 8876639 commit ad7db3e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
53 changes: 39 additions & 14 deletions examples/crd_derive_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,33 @@ mod v1 {
use super::*;
// spec that is forwards compatible with v2 (can upgrade by truncating)
#[derive(CustomResource, Serialize, Deserialize, Default, Debug, Clone, JsonSchema)]
#[kube(group = "kube.rs", version = "v1", kind = "ManyDerive", namespaced)]
#[kube(
group = "kube.rs",
version = "v1",
kind = "ManyDerive",
status = "ManyDeriveStatus",
namespaced
)]
pub struct ManyDeriveSpec {
pub name: String,
pub oldprop: u32,
}
#[derive(Serialize, Deserialize, Default, Debug, Clone, JsonSchema)]
pub struct ManyDeriveStatus {
pub condition: Option<bool>,
}
}
mod v2 {
// spec that is NOT backwards compatible with v1 (cannot retrieve oldprop if truncated)
use super::*;
#[derive(CustomResource, Serialize, Deserialize, Default, Debug, Clone, JsonSchema)]
#[kube(group = "kube.rs", version = "v2", kind = "ManyDerive", namespaced)]
#[kube(
group = "kube.rs",
version = "v2",
kind = "ManyDerive",
namespaced,
status = "v1::ManyDeriveStatus" // cross-referencing from v1 module
)]
pub struct ManyDeriveSpec {
pub name: String,
pub extra: Option<String>,
Expand All @@ -49,20 +65,26 @@ async fn main() -> anyhow::Result<()> {
let v2api: Api<v2::ManyDerive> = Api::default_namespaced(client.clone());

// create a v1 version
let v1m = v1::ManyDerive::new("old", v1::ManyDeriveSpec {
name: "i am old".into(),
oldprop: 5,
});
let v1m = v1::ManyDerive::new(
"old",
v1::ManyDeriveSpec {
name: "i am old".into(),
oldprop: 5,
},
);
let oldvarv1 = v1api.patch("old", &ssapply, &Patch::Apply(&v1m)).await?;
info!("old instance on v1: {:?}", oldvarv1.spec);
let oldvarv2 = v2api.get("old").await?;
info!("old instance on v2 truncates: {:?}", oldvarv2.spec);

// create a v2 version
let v2m = v2::ManyDerive::new("new", v2::ManyDeriveSpec {
name: "i am new".into(),
extra: Some("hi".into()),
});
let v2m = v2::ManyDerive::new(
"new",
v2::ManyDeriveSpec {
name: "i am new".into(),
extra: Some("hi".into()),
},
);
let newvarv2 = v2api.patch("new", &ssapply, &Patch::Apply(&v2m)).await?;
info!("new instance on v2 is force downgraded: {:?}", newvarv2.spec); // no extra field
let cannot_fetch_as_old = v1api.get("new").await.unwrap_err();
Expand All @@ -83,10 +105,13 @@ async fn main() -> anyhow::Result<()> {

// note we can apply old versions without them being truncated to the v2 schema
// in our case this means we cannot fetch them with our v1 schema (breaking change to not have oldprop)
let v1m2 = v1::ManyDerive::new("old", v1::ManyDeriveSpec {
name: "i am old2".into(),
oldprop: 5,
});
let v1m2 = v1::ManyDerive::new(
"old",
v1::ManyDeriveSpec {
name: "i am old2".into(),
oldprop: 5,
},
);
let v1err = v1api
.patch("old", &ssapply, &Patch::Apply(&v1m2))
.await
Expand Down
16 changes: 8 additions & 8 deletions kube-derive/src/custom_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct KubeAttrs {
#[darling(multiple, rename = "derive")]
derives: Vec<String>,
schema: Option<SchemaMode>,
status: Option<String>,
status: Option<Path>,
#[darling(multiple, rename = "category")]
categories: Vec<String>,
#[darling(multiple, rename = "shortname")]
Expand Down Expand Up @@ -808,28 +808,28 @@ struct StatusInformation {
/// returns: A `StatusInformation` struct
fn process_status(
root_ident: &Ident,
status: &Option<String>,
status: &Option<Path>,
visibility: &Visibility,
kube_core: &Path,
) -> StatusInformation {
if let Some(status_name) = &status {
let ident = format_ident!("{}", status_name);
if let Some(status_path) = &status {
let pth = quote! { #status_path };
StatusInformation {
field: quote! {
#[serde(skip_serializing_if = "Option::is_none")]
#visibility status: Option<#ident>,
#visibility status: Option<#pth>,
},
default: quote! { status: None, },
impl_hasstatus: quote! {
impl #kube_core::object::HasStatus for #root_ident {

type Status = #ident;
type Status = #pth;

fn status(&self) -> Option<&#ident> {
fn status(&self) -> Option<&#pth> {
self.status.as_ref()
}

fn status_mut(&mut self) -> &mut Option<#ident> {
fn status_mut(&mut self) -> &mut Option<#pth> {
&mut self.status
}
}
Expand Down

0 comments on commit ad7db3e

Please sign in to comment.