-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
107 lines (88 loc) · 2.89 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::time::Duration;
use kube::{
api::{Api, DeleteParams, PostParams},
Client, CustomResource, ResourceExt,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tokio::time::sleep;
use tracing::*;
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, Default, JsonSchema)]
#[kube(
group = "apps.kubeblocks.io",
version = "v1alpha1",
kind = "Cluster",
namespaced
)]
#[serde(rename_all = "camelCase")]
pub struct ClusterSpec {
cluster_definition_ref: String,
cluster_version_ref: String,
component_specs: Vec<ComponentSpec>,
termination_policy: String,
}
#[derive(Deserialize, Serialize, Clone, Debug, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ComponentSpec {
component_def_ref: String,
name: String,
replicas: i32,
}
fn new_redis_cluster(name: &str) -> Cluster {
Cluster::new(
name,
ClusterSpec {
cluster_definition_ref: "redis".to_owned(),
cluster_version_ref: "redis-7.0.6".to_owned(),
component_specs: vec![
ComponentSpec {
component_def_ref: "redis".to_owned(),
name: "redis".to_owned(),
replicas: 1,
},
ComponentSpec {
component_def_ref: "redis-twemproxy".to_owned(),
name: "redis-twemproxy".to_owned(),
replicas: 1,
},
ComponentSpec {
component_def_ref: "redis-sentinel".to_owned(),
name: "redis-sentinel".to_owned(),
replicas: 3,
},
],
termination_policy: "WipeOut".to_owned(),
},
)
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let client = Client::try_default().await?;
let name = "hello-rust-sdk";
let cluster = new_redis_cluster(name);
info!("{:#}", serde_yaml::to_string(&cluster).unwrap());
create(&client, &cluster).await?;
// delete(&client, name).await?;
Ok(())
}
pub async fn create(client: &Client, cluster: &Cluster) -> anyhow::Result<()> {
let api: Api<Cluster> = Api::default_namespaced(client.clone());
let params = PostParams::default();
let cluster = api.create(¶ms, &cluster).await?;
info!("Created: {:?}", cluster.name_any());
// Wait for the api to catch up
sleep(Duration::from_secs(2)).await;
Ok(())
}
pub async fn delete(client: &Client, name: &str) -> anyhow::Result<()> {
let api: Api<Cluster> = Api::default_namespaced(client.clone());
api.delete(name, &DeleteParams::default())
.await
.unwrap()
.map_left(|o| info!("Deleting: {:?}", o.name_any()))
.map_right(|s| info!("Deleted: {:?}", s));
// Wait for the api to catch up
sleep(Duration::from_secs(2)).await;
Ok(())
}