This is an automated email from the ASF dual-hosted git repository.

qianzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 17db3a90c644b5044e2b101cb431e37c2d23fed5
Author: Qian Zhang <zhq527...@gmail.com>
AuthorDate: Wed Aug 5 17:14:46 2020 +0800

    Implemented the `cleanup` method of `volume/csi` isolator.
    
    Review: https://reviews.apache.org/r/72734
---
 .../mesos/isolators/volume/csi/isolator.cpp        | 78 ++++++++++++++++++++++
 .../mesos/isolators/volume/csi/isolator.hpp        |  4 ++
 2 files changed, 82 insertions(+)

diff --git a/src/slave/containerizer/mesos/isolators/volume/csi/isolator.cpp 
b/src/slave/containerizer/mesos/isolators/volume/csi/isolator.cpp
index 90a526f..d61fe30 100644
--- a/src/slave/containerizer/mesos/isolators/volume/csi/isolator.cpp
+++ b/src/slave/containerizer/mesos/isolators/volume/csi/isolator.cpp
@@ -318,6 +318,84 @@ Future<Option<ContainerLaunchInfo>> 
VolumeCSIIsolatorProcess::_prepare(
 Future<Nothing> VolumeCSIIsolatorProcess::cleanup(
     const ContainerID& containerId)
 {
+  if (!infos.contains(containerId)) {
+    VLOG(1) << "Ignoring cleanup request for unknown container " << 
containerId;
+    return Nothing();
+  }
+
+  hashmap<CSIVolume, int> references;
+  foreachvalue (const Owned<Info>& info, infos) {
+    foreach (const CSIVolume& volume, info->volumes) {
+      if (!references.contains(volume)) {
+        references[volume] = 1;
+      } else {
+        references[volume]++;
+      }
+    }
+  }
+
+  vector<Future<Nothing>> futures;
+
+  foreach (const CSIVolume& volume, infos[containerId]->volumes) {
+    if (references.contains(volume) && references[volume] > 1) {
+      VLOG(1) << "Cannot unpublish the volume with plugin '"
+              << volume.plugin_name() << "' and ID '" << volume.id()
+              << "' for container " << containerId
+              << " since its reference count is " << references[volume];
+      continue;
+    }
+
+    LOG(INFO) << "Unpublishing the volume with plugin '"
+              << volume.plugin_name() << "' and ID '" << volume.id()
+              << "' for container " << containerId;
+
+    // Invoke CSI server to unpublish the volumes.
+    futures.push_back(
+        csiServer->unpublishVolume(volume.plugin_name(), volume.id()));
+  }
+
+  // Erase the `Info` struct of this container before unpublishing the volumes.
+  // This is to ensure the reference count of the volume will not be wrongly
+  // increased if unpublishing volumes fail, otherwise next time when another
+  // container using the same volume is destroyed, we would NOT unpublish the
+  // volume since its reference count would be larger than 1.
+  infos.erase(containerId);
+
+  return await(futures)
+    .then(defer(
+        PID<VolumeCSIIsolatorProcess>(this),
+        &VolumeCSIIsolatorProcess::_cleanup,
+        containerId,
+        lambda::_1));
+}
+
+
+Future<Nothing> VolumeCSIIsolatorProcess::_cleanup(
+    const ContainerID& containerId,
+    const vector<Future<Nothing>>& futures)
+{
+  vector<string> messages;
+  foreach (const Future<Nothing>& future, futures) {
+    if (!future.isReady()) {
+      messages.push_back(future.isFailed() ? future.failure() : "discarded");
+    }
+  }
+
+  if (!messages.empty()) {
+    return Failure(strings::join("\n", messages));
+  }
+
+  const string containerDir = csi::paths::getContainerDir(rootDir, 
containerId);
+  Try<Nothing> rmdir = os::rmdir(containerDir);
+  if (rmdir.isError()) {
+    return Failure(
+        "Failed to remove the container directory at '" +
+        containerDir + "': " + rmdir.error());
+  }
+
+  LOG(INFO) << "Removed the container directory at '" << containerDir
+            << "' for container " << containerId;
+
   return Nothing();
 }
 
diff --git a/src/slave/containerizer/mesos/isolators/volume/csi/isolator.hpp 
b/src/slave/containerizer/mesos/isolators/volume/csi/isolator.hpp
index a70da4f..e05a7b8 100644
--- a/src/slave/containerizer/mesos/isolators/volume/csi/isolator.hpp
+++ b/src/slave/containerizer/mesos/isolators/volume/csi/isolator.hpp
@@ -94,6 +94,10 @@ private:
       const std::vector<Mount>& mounts,
       const std::vector<process::Future<std::string>>& futures);
 
+  process::Future<Nothing> _cleanup(
+      const ContainerID& containerId,
+      const std::vector<process::Future<Nothing>>& futures);
+
   const Flags flags;
   CSIServer* csiServer;
 

Reply via email to