Skip to content

Commit 7efbcd3

Browse files
committed
get kubernetes to accept my post/patch/delete requests - fixes ynqa#16
1 parent 5397e00 commit 7efbcd3

File tree

5 files changed

+242
-83
lines changed

5 files changed

+242
-83
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ edition = "2018"
1717
base64 = "0.9.3"
1818
dirs = "1.0.4"
1919
failure = "0.1.2"
20-
reqwest = "0.9.2"
20+
reqwest = "0.9.17"
2121
serde = "1.0.90"
2222
serde_derive = "1.0.90"
2323
serde_json = "1.0.39"

examples/crd_api.rs

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use serde_json::json;
55

66
use kube::{
7-
api::{Api, PostResponse, PostParams, Object},
7+
api::{Api, PostResponse, PostParams, DeleteParams, Object, PropagationPolicy, Void},
88
client::APIClient,
99
config,
1010
};
@@ -23,11 +23,16 @@ pub struct FooSpec {
2323
info: String,
2424
}
2525

26-
#[derive(Deserialize, Serialize, Clone, Debug)]
26+
#[derive(Deserialize, Serialize, Clone, Debug, Default)]
2727
pub struct FooStatus {
2828
is_bad: bool,
2929
}
3030

31+
// shorthands
32+
type Foo = Object<FooSpec, FooStatus>;
33+
type FooMeta = Object<Void, Void>;
34+
type FullCrd = Object<CrdSpec, CrdStatus>;
35+
3136
fn main() -> Result<(), failure::Error> {
3237
std::env::set_var("RUST_LOG", "info,kube=trace");
3338
env_logger::init();
@@ -37,6 +42,20 @@ fn main() -> Result<(), failure::Error> {
3742
// Manage the CRD
3843
let crds = Api::v1beta1CustomResourceDefinition();
3944

45+
// Delete any old versions of it first:
46+
let dp = DeleteParams {
47+
propagation_policy: Some(PropagationPolicy::Foreground),
48+
..Default::default()
49+
};
50+
//let req = crds.delete("foos.clux.dev", &dp)?;
51+
//if let Ok(res) = client.request::<FullCrd>(req) {
52+
// info!("Deleted {}: ({:?})", res.metadata.name, res.status.conditions.unwrap().last());
53+
// use std::{thread, time};
54+
// let five_secs = time::Duration::from_millis(5000);
55+
// thread::sleep(five_secs);
56+
// // even foreground policy doesn't seem to block here..
57+
//}
58+
4059
// Create the CRD so we can create Foos in kube
4160
let foocrd = json!({
4261
"metadata": {
@@ -50,49 +69,68 @@ fn main() -> Result<(), failure::Error> {
5069
"plural": "foos",
5170
"singular": "foo",
5271
"kind": "Foo",
53-
}
54-
}
72+
},
73+
"subresources": {
74+
"status": {}
75+
},
76+
},
5577
});
5678

79+
info!("Creating CRD foos.clux.dev");
5780
let pp = PostParams::default();
5881
let req = crds.create(&pp, serde_json::to_vec(&foocrd)?)?;
59-
if let Ok(res) = client.request::<Object<CrdSpec, CrdStatus>>(req) {
82+
if let Ok(res) = client.request::<FullCrd>(req) {
6083
info!("Created {}", res.metadata.name);
6184
debug!("Created CRD: {:?}", res.spec);
6285
} else {
63-
// TODO: need error code here for ease
86+
// TODO: need error code here for ease - 409 common
6487
}
6588

66-
6789
// Manage the Foo CR
68-
let foos = Api::customResource("foos.clux.dev").version("v1");
90+
let foos = Api::customResource("foos")
91+
.version("v1")
92+
.group("clux.dev")
93+
.within("dev");
6994

70-
// Create some Foos
95+
// Create Foo baz
96+
info!("Creating Foo instance baz");
7197
let f1 = json!({
72-
"metadata": { "name": "baz" },
73-
"spec": FooSpec { name: "baz".into(), info: "unpatched baz".into() }
98+
"apiVersion": "clux.dev/v1",
99+
"kind": "Foo",
100+
"metadata": { "name": "baz", "info": "old" },
74101
});
75-
foos.create(&pp, serde_json::to_vec(&f1)?)?;
102+
let req = foos.create(&pp, serde_json::to_vec(&f1)?)?;
103+
let o = client.request::<FooMeta>(req)?;
104+
info!("Created {}", o.metadata.name);
105+
106+
// Modify a Foo baz with a Patch
107+
info!("Patch Foo instance baz");
108+
let patch = json!({
109+
"spec": { "info": "patched baz" }
110+
});
111+
let req = foos.patch("baz", &pp, serde_json::to_vec(&patch)?)?;
112+
let o = client.request::<Foo>(req)?;
113+
info!("Patched {} with new name: {}", o.metadata.name, o.spec.name);
76114

115+
info!("Create Foo instance qux");
77116
let f2 = json!({
117+
"apiVersion": "clux.dev/v1",
118+
"kind": "Foo",
78119
"metadata": { "name": "qux" },
79-
"spec": FooSpec { name: "qux".into(), info: "unpatched qux".into() }
120+
"spec": FooSpec { name: "qux".into(), info: "unpatched qux".into() },
121+
"status": FooStatus::default(),
80122
});
81-
foos.create(&pp, serde_json::to_vec(&f2)?)?;
82-
123+
let req = foos.create(&pp, serde_json::to_vec(&f2)?)?;
124+
let o = client.request::<FooMeta>(req)?;
125+
info!("Created {}", o.metadata.name);
83126

84-
// Modify a Foo with a Patch
85-
//let patch = json!( info => "patched baz" );
86-
//let req = foos.patch("baz", &pp, serde_json::to_vec(&patch)?)?;
87-
//client.request::<PostResponse<Object<FooSpec, FooStatus>>>(req)?;
88127

89-
// shorthand
90-
type Foo = Object<FooSpec, FooStatus>;
91-
// TODO: request should return statuscode as a better useability!
92-
93-
// Set its status:
94-
let fs = FooStatus { is_bad: true };
95-
let req = foos.replace_status("baz", &pp, serde_json::to_vec(&fs)?)?;
128+
// Update status on -qux
129+
info!("Replace Status on Foo instance qux");
130+
let fs = json!({
131+
"status": FooStatus { is_bad: true }
132+
});
133+
let req = foos.replace_status("qux", &pp, serde_json::to_vec(&fs)?)?;
96134
let res = client.request::<Foo>(req)?;
97135
info!("Replaced status {:?} for {}", res.status, res.metadata.name);
98136

0 commit comments

Comments
 (0)