My use case here is kubefed. The system is built around Federated* CRDs, for instance FederatedJob. These objects have some metadata about which clusters to replicate to, and essentially just wrap the Spec for the object types they represent. The following is the natural way to do this, but it fails because JobSpec doesn't implement JsonSchema. I saw the example using a manual schema, so there's definitely a path forward there, but it would be a lot cleaner if it was possible to use the existing k8s-openapi objects. I also hacked around with adding a feature to the k8s-openapi crate to derive JsonSchema on the appropriate objects and it looks like that's maybe possible, but I was hoping there was a shortcut somewhere that I'm missing. If not (or if this issue belongs in the k8s-openapi repo), fine to close this. Thanks for reading.
use serde::{Serialize, Deserialize};
use kube_derive::CustomResource;
use schemars::JsonSchema;
use k8s_openapi::api::batch::v1::JobSpec;
#[derive(Serialize, Deserialize, Default, Clone, Debug, JsonSchema)]
struct FederatedJobTemplate {
spec: JobSpec
}
#[derive(Serialize, Deserialize, Default, Clone, Debug, JsonSchema)]
struct ClusterPlacement {
clusters: HashMap<String, String>
}
#[derive(CustomResource, Serialize, Deserialize, Default, Clone, Debug, JsonSchema)]
#[kube(group = "types.kubefed.io", version = "v1beta1", kind = "FederatedJob", namespaced)]
pub struct FederatedJobSpec {
template: FederatedJobTemplate,
placement: ClusterPlacement
}
Yeah, there is an open issue in k8s-openapi for exactly this at https://github.com/Arnavion/k8s-openapi/issues/86 - it looks a bit complicated and don't think anyone has had time to dedicate a lot of energy towards it yet.
On the positive side, at least #[schemars(schema_with = "some_schema_generating_fn")] works and schemars is suddenly receiving a lot of work by the author again. So hopefully at some point :crossed_fingers:
Something like https://github.com/Arnavion/k8s-openapi/issues/86#issuecomment-777912231 might work, but I haven't figured out how the code generation works, and haven't had much time recently.
Presumably this should be doable now with k8s-openapi 0.13.0 which contains https://github.com/Arnavion/k8s-openapi/pull/105
It has been released under the schemars feature.
Yeah, I confirmed it by removing schema_with in one of our examples.
Just enable schemars feature in k8s-openapi 0.13.0 and it should work.
Ok, going to close this with a doc change to kube-derive then, as this can be done from 0.59 (which bumps k8s-openapi).
Nice, works like a charm with my example code as well. Thank you @kazk @clux for your work on this!
Most helpful comment
Nice, works like a charm with my example code as well. Thank you @kazk @clux for your work on this!