Skip to content

Commit

Permalink
fix(csi-controller/gc): list volumes with pagination
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Castro <[email protected]>
  • Loading branch information
tiagolobocastro committed Jan 24, 2024
1 parent 4576447 commit e4a5f56
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
18 changes: 12 additions & 6 deletions control-plane/csi-driver/src/bin/controller/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ impl IoEngineApiClient {
}
}

/// Token used to list volumes with pagination.
pub(crate) enum ListToken {
String(String),
Number(isize),
}

impl IoEngineApiClient {
/// List all nodes available in IoEngine cluster.
pub(crate) async fn list_nodes(&self) -> Result<Vec<Node>, ApiClientError> {
Expand Down Expand Up @@ -178,17 +184,17 @@ impl IoEngineApiClient {
pub(crate) async fn list_volumes(
&self,
max_entries: i32,
starting_token: String,
starting_token: ListToken,
) -> Result<Volumes, ApiClientError> {
let max_entries = max_entries as isize;
let starting_token = if starting_token.is_empty() {
0
} else {
starting_token.parse::<isize>().map_err(|_| {
let starting_token = match starting_token {
ListToken::String(starting_token) if starting_token.is_empty() => 0,
ListToken::String(starting_token) => starting_token.parse::<isize>().map_err(|_| {
ApiClientError::InvalidArgument(
"Failed to parse starting token as an isize".to_string(),
)
})?
})?,
ListToken::Number(starting_token) => starting_token,
};

let response = self
Expand Down
6 changes: 4 additions & 2 deletions control-plane/csi-driver/src/bin/controller/controller.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{ApiClientError, CreateVolumeTopology, CsiControllerConfig, IoEngineApiClient};
use crate::{
client::ListToken, ApiClientError, CreateVolumeTopology, CsiControllerConfig, IoEngineApiClient,
};

use csi_driver::context::{CreateParams, PublishParams};
use rpc::csi::{volume_content_source::Type, Topology as CsiTopology, *};
Expand Down Expand Up @@ -652,7 +654,7 @@ impl rpc::csi::controller_server::Controller for CsiControllerSvc {
let vt_mapper = VolumeTopologyMapper::init().await?;

let volumes = IoEngineApiClient::get_client()
.list_volumes(max_entries, args.starting_token)
.list_volumes(max_entries, ListToken::String(args.starting_token))
.await
.map_err(|e| Status::internal(format!("Failed to list volumes, error = {e:?}")))?;

Expand Down
25 changes: 19 additions & 6 deletions control-plane/csi-driver/src/bin/controller/pvwatcher.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::client::ListToken;
use futures::TryStreamExt;
use k8s_openapi::api::core::v1::PersistentVolume;
use kube::{
Expand Down Expand Up @@ -100,15 +101,27 @@ impl PvGarbageCollector {
/// 2. to tackle k8s bug where volumes are leaked when PV deletion is attempted before
/// PVC deletion.
async fn delete_orphan_volumes(&self) {
match self.rest_client.list_volumes(0, "".to_string()).await {
Ok(volume_list) => {
for vol in volume_list.entries {
if self.is_vol_orphaned(&vol.spec.uuid).await {
self.delete_volume(vol.spec.uuid).await;
let max_entries = 200;
let mut starting_token = Some(0);
while let Some(token) = starting_token {
match self
.rest_client
.list_volumes(max_entries, ListToken::Number(token))
.await
{
Ok(volumes) => {
starting_token = volumes.next_token;
for vol in volumes.entries {
if self.is_vol_orphaned(&vol.spec.uuid).await {
self.delete_volume(vol.spec.uuid).await;
}
}
}
Err(error) => {
error!(?error, "Unable to list volumes");
return;
}
}
Err(error) => error!(?error, "Unable to list volumes"),
}
}

Expand Down
1 change: 1 addition & 0 deletions control-plane/csi-driver/src/bin/controller/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Connected for UnixStream {
}
}

// Not sure why we need the inner fields, probably worth checking if we can remove them.
#[derive(Clone, Debug)]
#[allow(unused)]
struct UdsConnectInfo {
Expand Down

0 comments on commit e4a5f56

Please sign in to comment.