From 492a3818a8211be889bfc268ca81cc6f81ee97dc Mon Sep 17 00:00:00 2001 From: Ho Kim Date: Fri, 2 Jun 2023 01:18:23 +0900 Subject: [PATCH] Release shared PVs before login --- vine/storage/src/lib.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/vine/storage/src/lib.rs b/vine/storage/src/lib.rs index d160f373..d15ff114 100644 --- a/vine/storage/src/lib.rs +++ b/vine/storage/src/lib.rs @@ -144,7 +144,7 @@ async fn clone_pv( let api = Api::::all(kube.clone()); let target_name = format!("{source_name}-{target_namespace}"); if let Some(pv) = api.get_opt(&target_name).await? { - return Ok(pv); + return release_pv(&api, pv, pp).await; } // get original PV @@ -220,6 +220,29 @@ async fn clone_pv( .map_err(|error| anyhow!("failed to create a PV ({source_name} => {target_name}): {error}")) } +async fn release_pv( + api: &Api, + mut pv: PersistentVolume, + pp: &PostParams, +) -> Result { + if let Some(spec) = &mut pv.spec { + // apply patch + if spec.claim_ref.take().is_none() { + // skip if already patched + return Ok(pv); + } + } + + // save + let name = pv.name_any(); + match api.replace(&name, pp, &pv).await { + Ok(_) => Ok(pv), + Err(error) => { + bail!("failed to release the PV ({name}): {error}") + } + } +} + /// On this PV, change the `persistentVolumeReclaimPolicy` parameter to `Retain` /// to avoid it from being deleted when you will delete PVCs. ///