-
Notifications
You must be signed in to change notification settings - Fork 3
/
provisioner.rs
313 lines (262 loc) · 12.9 KB
/
provisioner.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::collections::{BTreeMap};
use std::path::PathBuf;
use chrono::Utc;
use color_eyre::eyre::{bail, eyre};
use color_eyre::Result;
use k8s_openapi::api::core::v1::{LocalVolumeSource, NodeSelector, NodeSelectorRequirement, NodeSelectorTerm, PersistentVolume, PersistentVolumeClaim, PersistentVolumeClaimSpec, PersistentVolumeSpec, ResourceRequirements, VolumeNodeAffinity};
use k8s_openapi::api::storage::v1::StorageClass;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{ObjectMeta};
use kube::{Api, Client, Config, Resource, ResourceExt};
use kube::api::{ListParams, Patch, PatchParams, PostParams};
use kube::api::entry::Entry;
use rand::{Rng, thread_rng};
use rand::distributions::Alphanumeric;
use crate::config::*;
use crate::btrfs_volume_metadata::BtrfsVolumeMetadata;
use crate::btrfs_wrapper::BtrfsWrapper;
use crate::controller::storage_class_utils::is_controlling_storage_class;
use crate::ext::{PathBufExt, ProvisionerResourceExt};
use crate::quantity_parser::QuantityParser;
pub struct Provisioner {
/// The Kubernetes client to use, created in [Provisioner::create]
client: Client,
/// The name of the Node this Provisioner runs on
node_name: String,
}
impl Provisioner {
/// Creates and returns a new [Provisioner].
///
/// This method first tries to get the Kubernetes client credentials from ~/.kube/config and
/// tries the in-cluster service account if it doesn't find any.
pub async fn create(node_name: String) -> Result<Self> {
let client = Client::try_default()
.await
.or_else(|_| Client::try_from(Config::incluster_env().expect("Failed to load in-cluster Kube config")))
.expect("Failed to create Kube client");
Ok(Provisioner {
client,
node_name,
})
}
/// Provisions a PV by a PVC name
pub async fn provision_persistent_volume_by_claim_name(&self, claim_namespace: &str, claim_name: &str) -> Result<()> {
let persistent_volume_claims = Api::<PersistentVolumeClaim>::namespaced(self.client(), claim_namespace);
let claim = persistent_volume_claims.get(claim_name).await?;
self.provision_persistent_volume(&claim).await
}
/// Provisions a PV by a PVC
pub async fn provision_persistent_volume(&self, claim: &PersistentVolumeClaim) -> Result<()> {
let client = self.client();
let persistent_volumes = Api::<PersistentVolume>::all(client);
// Check that the PVC has a storage request
if let PersistentVolumeClaim {
spec: Some(
PersistentVolumeClaimSpec {
storage_class_name: Some(storage_class_name),
resources: Some(
ResourceRequirements {
requests: Some(requests), ..
}
), ..
}
), ..
} = &claim {
let storage_request = requests.get("storage").ok_or_else(|| eyre!("PVC {} does not have a storage request", claim.full_name()))?;
let storage_request_bytes = storage_request.to_bytes()?.ok_or_else(|| eyre!("Failed to parse storage request: '{}'", storage_request.0))?;
println!("Provisioning claim {}", claim.full_name());
let pv_name = self.generate_pv_name_for_claim(claim).await?;
let btrfs_wrapper = BtrfsWrapper::new();
let btrfs_volume_metadata = BtrfsVolumeMetadata::from_pv_name(&pv_name)?;
let volume_path_str = btrfs_volume_metadata.path.as_str()?;
if !Provisioner::get_host_path(&[VOLUMES_DIR.as_str()])?.exists() {
bail!("The root volumes directory at {} does not exist. Please create it or mount a btrfs filesystem yourself.", VOLUMES_DIR.as_str());
}
println!("Creating btrfs subvolume at {}", volume_path_str);
if btrfs_volume_metadata.host_path.exists() {
bail!("Cannot create btrfs subvolume, file/directory exists!");
}
btrfs_wrapper.subvolume_create(volume_path_str)?;
println!("Enabling Quota on {}", volume_path_str);
btrfs_wrapper.quota_enable(volume_path_str)?;
println!("Setting Quota limit on {} to {} bytes", volume_path_str, storage_request_bytes);
btrfs_wrapper.qgroup_limit(storage_request_bytes as u64, volume_path_str)?;
println!("Triggering subvolume rescan");
btrfs_wrapper.quota_rescan_wait(volume_path_str)?;
println!("Creating PersistentVolume {}", pv_name);
let mut annotations: BTreeMap<String, String> = BTreeMap::new();
annotations.insert(PROVISIONED_BY_ANNOTATION_KEY.into(), PROVISIONER_NAME.into());
persistent_volumes.create(&PostParams::default(), &PersistentVolume {
metadata: ObjectMeta {
annotations: Some(annotations),
name: Some(pv_name.clone()),
finalizers: Some(vec![FINALIZER_NAME.into()]),
..Default::default()
},
spec: Some(PersistentVolumeSpec {
local: Some(LocalVolumeSource {
path: volume_path_str.into(),
..LocalVolumeSource::default()
}),
claim_ref: Some(claim.object_ref(&())),
access_modes: Some(vec![String::from("ReadWriteOnce")]),
capacity: Some(requests.clone()),
storage_class_name: Some(storage_class_name.to_owned()),
node_affinity: Some(VolumeNodeAffinity {
required: Some(NodeSelector {
node_selector_terms: vec![NodeSelectorTerm {
match_expressions: Some(vec![NodeSelectorRequirement {
key: NODE_HOSTNAME_KEY.into(),
operator: "In".into(),
values: Some(vec![self.node_name.to_owned()]),
}]),
..Default::default()
}]
})
}),
..Default::default()
}),
..Default::default()
}).await?;
println!("Created volume {}", pv_name);
} else {
bail!("PVC {} does not have resource requests", claim.full_name());
}
Ok(())
}
/// Deletes a PV by name
pub async fn delete_persistent_volume_by_name(&self, volume_name: &str) -> Result<()> {
let persistent_volumes = Api::<PersistentVolume>::all(self.client());
let volume = persistent_volumes.get(volume_name).await?;
self.delete_persistent_volume(&volume).await
}
/// Deletes a PV
pub async fn delete_persistent_volume(&self, volume: &PersistentVolume) -> Result<()> {
let persistent_volumes = Api::<PersistentVolume>::all(self.client());
if let PersistentVolume {
metadata: ObjectMeta {
finalizers: Some(finalizers),
..
},
spec: Some(
PersistentVolumeSpec {
storage_class_name: Some(
storage_class_name
), ..
}
), ..
} = &volume {
if !is_controlling_storage_class(self.client(), storage_class_name).await? {
bail!("StorageClass {} is not controlled by btrfs-provisioner", volume.name_any());
}
let finalizer_index = finalizers
.iter()
.position(|f| f == FINALIZER_NAME)
.ok_or_else(|| eyre!("Finalizer {} not present on volume", FINALIZER_NAME))?;
println!("Deleting PersistentVolume {}", volume.name_any());
let btrfs_volume_metadata = BtrfsVolumeMetadata::from_pv_name(&volume.name_any())?;
let volume_path_str = btrfs_volume_metadata.path.as_str()?;
if !btrfs_volume_metadata.host_path.exists() {
bail!("Volume {} does not exist", volume_path_str);
}
let btrfs_wrapper = BtrfsWrapper::new();
match btrfs_wrapper.get_qgroup(volume_path_str) {
Ok(qgroup) => {
println!("Destroying qgroup {}", qgroup);
btrfs_wrapper.qgroup_destroy(&qgroup, volume_path_str)?;
}
Err(e) => {
println!("Could not detect a qgroup for volume {}: {}", volume_path_str, e)
}
}
if *ARCHIVE_ON_DELETE {
println!("Archiving on PV deletion is enabled, archiving volume...");
let volume_dir_name = btrfs_volume_metadata.path.file_name().ok_or_else(|| eyre!("Could not determine volume directory name"))?;
let mut new_path = btrfs_volume_metadata.path.clone();
new_path.set_file_name(format!("_archive-{}-{}", Utc::now().timestamp(), volume_dir_name.to_str().unwrap()));
let new_path_str = new_path.to_str().unwrap();
println!("Moving from {} to {}", volume_path_str, new_path_str);
btrfs_wrapper.mv(volume_path_str, new_path_str)?;
} else {
println!("Deleting subvolume {}", volume_path_str);
btrfs_wrapper.subvolume_delete(volume_path_str)?;
}
println!("Removing finalizer");
let finalizer_path = format!("/metadata/finalizers/{}", finalizer_index);
persistent_volumes.patch(
&volume.name_any(),
&PatchParams::default(),
&Patch::<json_patch::Patch>::Json(serde_json::from_value(serde_json::json!([
{
"op": "remove",
"path": finalizer_path
}
]))?),
).await?;
Ok(())
} else {
bail!("StorageClass name is empty");
}
}
/// Initializes the Node this Provisioner runs on
pub async fn initialize_node(&self) -> Result<()> {
let storage_classes = Api::<StorageClass>::all(self.client());
let volumes_dir_host_path = Provisioner::get_host_path(&[&VOLUMES_DIR])?;
if !volumes_dir_host_path.exists() {
bail!("Volumes root path '{}' does not exist on this node, please create it manually.", *VOLUMES_DIR);
}
if *STORAGE_CLASS_PER_NODE_ENABLED {
println!("Creating StorageClass for node {}", &self.node_name);
if let [existing_storage_class] = storage_classes.list(&ListParams {
label_selector: Some(format!("{}={}", STORAGE_CLASS_CONTROLLING_NODE_LABEL_NAME, &self.node_name)),
limit: Some(1),
..ListParams::default()
}).await?.items.as_slice() {
bail!("StorageClass for node {} already exists: {}", &self.node_name, existing_storage_class.name_any());
}
storage_classes.create(&PostParams::default(), &StorageClass {
provisioner: PROVISIONER_NAME.into(),
allow_volume_expansion: Some(false),
metadata: ObjectMeta {
name: Some(STORAGE_CLASS_PER_NODE_NAME_PATTERN.to_owned().replace("{}", &self.node_name)),
labels: Some(BTreeMap::from([
(STORAGE_CLASS_CONTROLLING_NODE_LABEL_NAME.into(), self.node_name.to_owned())
])),
..ObjectMeta::default()
},
..StorageClass::default()
}).await?;
}
Ok(())
}
/// Returns the absolute path to an absolute path in the host filesystem
pub fn get_host_path(path: &[&str]) -> Result<PathBuf> {
let mut path_buf = PathBuf::new();
if let Ok(path) = std::env::var(HOST_FS_ENV_NAME) {
path_buf.push(path);
}
for part in path {
path_buf.push(part.trim_start_matches('/'));
}
Ok(path_buf)
}
/// Returns a copy of the Kubernetes client
fn client(&self) -> Client {
self.client.clone()
}
/// Generates a unique PV name for a PVC
async fn generate_pv_name_for_claim(&self, claim: &PersistentVolumeClaim) -> Result<String> {
let client = self.client();
let persistent_volumes = Api::<PersistentVolume>::all(client);
loop {
let rand_string: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(5)
.map(|u| char::from(u).to_ascii_lowercase())
.collect();
let generated_name = format!("{}-{}-{}", claim.namespace().unwrap_or_else(|| "default".into()), claim.name_any(), rand_string);
if let Entry::Vacant(_) = persistent_volumes.entry(&generated_name).await? {
return Ok(generated_name);
}
}
}
}