Skip to content
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

Generate schema for CRD v1 #348

Merged
merged 3 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ snafu = { version = "0.6.8", features = ["futures"] }
either = "1.6.0"
# Some configuration tweaking require reqwest atm
reqwest = { version = "0.10.8", default-features = false, features = ["json", "gzip", "stream"] }
schemars = "0.8.0"

[[example]]
name = "configmapgen_controller"
Expand All @@ -56,6 +57,10 @@ path = "crd_apply.rs"
name = "crd_derive"
path = "crd_derive.rs"

[[example]]
name = "crd_derive_schema"
path = "crd_derive_schema.rs"

[[example]]
name = "crd_reflector"
path = "crd_reflector.rs"
Expand Down
3 changes: 2 additions & 1 deletion examples/configmapgen_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use kube::{
Api, Client, CustomResource,
};
use kube_runtime::controller::{Context, Controller, ReconcilerAction};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use snafu::{Backtrace, OptionExt, ResultExt, Snafu};
use std::collections::BTreeMap;
Expand All @@ -32,7 +33,7 @@ enum Error {
},
}

#[derive(CustomResource, Debug, Clone, Deserialize, Serialize)]
#[derive(CustomResource, Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[kube(group = "nullable.se", version = "v1", kind = "ConfigMapGenerator")]
#[kube(shortname = "cmg", namespaced)]
struct ConfigMapGeneratorSpec {
Expand Down
5 changes: 3 additions & 2 deletions examples/crd_api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[macro_use] extern crate log;
use either::Either::{Left, Right};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::time::Duration;
Expand All @@ -14,7 +15,7 @@ use kube::{
};

// Own custom resource
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug)]
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[kube(group = "clux.dev", version = "v1", kind = "Foo", namespaced)]
#[kube(apiextensions = "v1beta1")]
#[kube(status = "FooStatus")]
Expand All @@ -26,7 +27,7 @@ pub struct FooSpec {
replicas: i32,
}

#[derive(Deserialize, Serialize, Clone, Debug, Default)]
#[derive(Deserialize, Serialize, Clone, Debug, Default, JsonSchema)]
pub struct FooStatus {
is_bad: bool,
replicas: i32,
Expand Down
5 changes: 3 additions & 2 deletions examples/crd_apply.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[macro_use] extern crate log;
use futures::{StreamExt, TryStreamExt};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use apiexts::CustomResourceDefinition;
Expand All @@ -14,7 +15,7 @@ use kube::{
// Please test against Kubernetes 1.16.X!

// Own custom resource
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug)]
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[kube(group = "clux.dev", version = "v1", kind = "Foo", namespaced)]
#[kube(status = "FooStatus")]
#[kube(apiextensions = "v1beta1")] // remove this if using Kubernetes >= 1.17
Expand All @@ -25,7 +26,7 @@ pub struct FooSpec {
replicas: i32,
}

#[derive(Deserialize, Serialize, Clone, Debug, Default)]
#[derive(Deserialize, Serialize, Clone, Debug, Default, JsonSchema)]
pub struct FooStatus {
is_bad: bool,
replicas: i32,
Expand Down
80 changes: 62 additions & 18 deletions examples/crd_derive.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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)]
#[derive(CustomResource, Serialize, Deserialize, Default, Debug, PartialEq, Clone, JsonSchema)]
#[kube(
group = "clux.dev",
version = "v1",
Expand All @@ -18,14 +19,14 @@ use serde::{Deserialize, Serialize};
scale = r#"{"specReplicasPath":".spec.replicas", "statusReplicasPath":".status.replicas"}"#,
printcolumn = r#"{"name":"Spec", "type":"string", "description":"name of foo", "jsonPath":".spec.name"}"#
)]
#[kube(apiextensions = "v1beta1")] // kubernetes <= 1.16
#[kube(apiextensions = "v1")]
pub struct MyFoo {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
info: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema)]
pub struct FooStatus {
is_bad: bool,
}
Expand All @@ -49,7 +50,7 @@ fn verify_crd() {
use serde_json::{self, json};
let crd = Foo::crd();
let output = json!({
"apiVersion": "apiextensions.k8s.io/v1beta1",
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "CustomResourceDefinition",
"metadata": {
"name": "foos.clux.dev"
Expand All @@ -62,26 +63,69 @@ fn verify_crd() {
"shortNames": ["f"],
"singular": "foo"
},
"additionalPrinterColumns": [
{
"description": "name of foo",
"JSONPath": ".spec.name",
"name": "Spec",
"type": "string"
}
],
"scope": "Namespaced",
"versions": [
{
"name": "v1",
"served": true,
"storage": true
"storage": true,
"additionalPrinterColumns": [
{
"description": "name of foo",
"jsonPath": ".spec.name",
"name": "Spec",
"type": "string"
}
],
"schema": {
"openAPIV3Schema": {
"description": "Auto-generated derived type for MyFoo via `CustomResource`",
"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"
},
"status": {
"nullable": true,
"properties": {
"is_bad": {
"type": "boolean"
}
},
"required": [
"is_bad"
],
"type": "object"
}
},
"required": [
"spec"
],
"title": "Foo",
"type": "object"
}
},
"subresources": {
"scale": {
"specReplicasPath": ".spec.replicas",
"statusReplicasPath": ".status.replicas"
},
"status": {}
},
}
],
"subresources": {
"scale": {"specReplicasPath":".spec.replicas", "statusReplicasPath":".status.replicas"},
"status": {}
}
]
}
});
let outputcrd = serde_json::from_value(output).expect("expected output is valid");
Expand Down
Loading