-
-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kube-derive
does not create required schemas
field for CustomResourceDefinition
#264
Comments
Oh, interesting. I seem to recall testing and managed to get it working with the stable v1 on 1.18 and 1.17 without a schema, but will do some more testing. At any rate, it is possible that sticking to v1beta1 is a reasonable stop-gap solution until we have schemas (#129) |
Nevermind removal has been moved to 1.22, so we got time |
Hmm. With kube 0.40 and 1.18 I get this when using #[kube(apiextensions = "v1beta1")] on my CRD:
is anything else needed for this? |
Hm. It shouldn't need anything else. It could be a bug. You could try to grab your crd via Oh, and if you are posting it to kubernetes via a
not the stable one ^ |
@clux, thanks, I was using the stable CRD type... |
Ah, good. Yeah, that is a bit of a footgun at the moment... |
Unfortunately, this problem is still present in I have a custom resource: #[derive(CustomResource, Debug, Clone, Deserialize, Serialize)]
#[kube(group = "h2o.ai", version = "v1", kind = "H2O", namespaced)]
#[kube(shortname = "h2o", namespaced)]
pub struct H2OSpec {
pub nodes: u32,
pub resources: Resources,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Resources {
pub cpu: u32,
pub memory: String,
#[serde(rename = "memoryPercentage", skip_serializing_if = "Option::is_none")]
pub memory_percentage: Option<u8>,
} And the output of let h2o_crd: CustomResourceDefinition = H2O::crd();
println!("{}", serde_yaml::to_string(&h2o_crd).unwrap()); is apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: h2os.h2o.ai
spec:
group: h2o.ai
names:
kind: H2O
plural: h2os
shortNames:
- h2o
singular: h2o
scope: Namespaced
versions:
- name: v1
served: true
storage: true There is no schema, therefore when trying to create the resource in Kuernetes, a validation error occurs:
Fortunately, the workaround is easy, just create the YAML myself: const H2O_RESOURCE_TEMPLATE: &str = r#"
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: h2os.h2o.ai
spec:
group: h2o.ai
names:
kind: H2O
plural: h2os
singular: h2o
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
nodes:
type: integer
resources:
type: object
properties:
cpu:
type: integer
minimum: 1
memory:
type: string
pattern: "^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$"
memoryPercentage:
type: integer
minimum: 1
maximum: 100
required: ["cpu", "memory"]
required: ["nodes", "resources"]
"#; And the create the CRD manually with a simple call to serde: return serde_yaml::from_str(H2O_RESOURCE_TEMPLATE).unwrap(); |
Is anyone working on this? I thought // in crd()
let schema = schemars::schema_for!(Self);
// definitions is a map of struct's name to schema.
let spec_schema = &schema.definitions.get(stringify!(#ident)).unwrap();
serde_json::json!({
// ...
"spec": {
// ...
"versions": [{
// ...
"schema": {
"openAPIV3Schema": {
"type": "object",
"properties": {
"spec": spec_schema,
}
}
}
}],
}
}) (Requires adding However, this fails when there are optional fields (e.g., I also found @clux Thanks for creating |
@kazk did you generating schema with custom settings? I think this function should work. |
@MikailBag Thanks! Changing to let gen = schemars::gen::SchemaSettings::openapi3().into_generator();
let schema = gen.into_root_schema_for::<Self>();
let spec_schema = &schema.definitions.get(stringify!(#ident)).unwrap(); worked 🎉
use k8s_openapi::Resource;
use kube::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
/// Our spec for Foo
///
/// A struct with our chosen Kind will be created for us, using the following kube attrs
#[derive(CustomResource, Serialize, Deserialize, Default, Debug, PartialEq, Clone, JsonSchema)]
#[kube(
group = "clux.dev",
version = "v1",
kind = "Foo",
namespaced,
status = "FooStatus",
derive = "PartialEq",
derive = "Default",
shortname = "f",
scale = r#"{"specReplicasPath":".spec.replicas", "statusReplicasPath":".status.replicas"}"#,
printcolumn = r#"{"name":"Spec", "type":"string", "description":"name of foo", "jsonPath":".spec.name"}"#
)]
#[kube(apiextensions = "v1")]
pub struct MyFoo {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
info: Option<String>,
}
fn main() {
let crd = serde_json::to_string_pretty(&Foo::crd()).unwrap();
println!("Foo CRD: \n{}", crd);
} Output{
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "CustomResourceDefinition",
"metadata": {
"name": "foos.clux.dev"
},
"spec": {
"group": "clux.dev",
"names": {
"kind": "Foo",
"plural": "foos",
"shortNames": [
"f"
],
"singular": "foo"
},
"scope": "Namespaced",
"versions": [
{
"name": "v1",
"schema": {
"openAPIV3Schema": {
"properties": {
"spec": {
"description": "Our spec for Foo\n\nA struct with our chosen Kind will be created for us, using the following kube attrs",
"properties": {
"info": {
"nullable": true,
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"name"
],
"type": "object"
}
},
"type": "object"
}
},
"served": true,
"storage": true
}
]
}
} |
Schema is generated with `schemars`. Also fixed the location of `subresources` and `additionalPrinterColumns`. See kube-rs#264
Schema is generated with `schemars`. Also fixed the location of `subresources` and `additionalPrinterColumns`. See kube-rs#264
Schema is generated with `schemars`. `inline_subschemas` option is used to avoid definitions in schema. `meta_schema` is set to `None` to prevent including `$schema` which is not allowed by Kubernetes. This support nested structs and enums, but does not support structural schemas for some of the more complex spec types. The schema of the status subresource is included if present. Also fixed the location of `subresources` and `additionalPrinterColumns`. Closes kube-rs#264
With
apiextensions.k8s.io/v1
the definition of a structural schema is mandatory for CustomResourceDefinitions, while inv1beta1
this is still optional.ref: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/
Therefore should
kube-derive
only be allowed to createapiextensions.k8s.io/v1beta1
CRD files?The text was updated successfully, but these errors were encountered: