You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@heron.apache.org by sa...@apache.org on 2022/05/01 19:35:35 UTC

[incubator-heron] 02/07: [K8s] Added support for Host Path.

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

saadurrahman pushed a commit to branch saadurrahman/3821-Remove-Deprecated-Volumes-K8s-dev
in repository https://gitbox.apache.org/repos/asf/incubator-heron.git

commit 053bb5f044388e30673e78d8afc2bfb880468201
Author: Saad Ur Rahman <sa...@apache.org>
AuthorDate: Sun May 1 15:14:40 2022 -0400

    [K8s] Added support for Host Path.
---
 .../apache/heron/scheduler/kubernetes/Volumes.java | 34 ++++++++++++++++++++
 .../heron/scheduler/kubernetes/VolumesTests.java   | 36 +++++++++++++++++++---
 2 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/heron/schedulers/src/java/org/apache/heron/scheduler/kubernetes/Volumes.java b/heron/schedulers/src/java/org/apache/heron/scheduler/kubernetes/Volumes.java
index 3b597338031..6dba3bf8547 100644
--- a/heron/schedulers/src/java/org/apache/heron/scheduler/kubernetes/Volumes.java
+++ b/heron/schedulers/src/java/org/apache/heron/scheduler/kubernetes/Volumes.java
@@ -40,6 +40,7 @@ final class Volumes {
 
   private Volumes() {
     volumes.put(VolumeType.EmptyDir, new EmptyDirVolumeFactory());
+    volumes.put(VolumeType.HostPath, new HostPathVolumeFactory());
   }
 
   static Volumes get() {
@@ -99,4 +100,37 @@ final class Volumes {
       return volume;
     }
   }
+
+  static class HostPathVolumeFactory implements VolumeFactory {
+
+    /**
+     * Generates an <code>Host Path</code> <code>V1 Volume</code>.
+     * @param volumeName The name of the volume to generate.
+     * @param configs A map of configurations.
+     * @return A fully configured <code>Host Path</code> volume.
+     */
+    @Override
+    public V1Volume create(String volumeName,
+                           Map<KubernetesConstants.VolumeConfigKeys, String> configs) {
+      final V1Volume volume = new V1VolumeBuilder()
+          .withName(volumeName)
+          .withNewHostPath()
+          .endHostPath()
+          .build();
+
+      for (Map.Entry<KubernetesConstants.VolumeConfigKeys, String> config : configs.entrySet()) {
+        switch (config.getKey()) {
+          case type:
+            volume.getHostPath().setType(config.getValue());
+            break;
+          case pathOnHost:
+            volume.getHostPath().setPath(config.getValue());
+            break;
+          default:
+            break;
+        }
+      }
+      return volume;
+    }
+  }
 }
diff --git a/heron/schedulers/tests/java/org/apache/heron/scheduler/kubernetes/VolumesTests.java b/heron/schedulers/tests/java/org/apache/heron/scheduler/kubernetes/VolumesTests.java
index 316947f9267..7e638a839d1 100644
--- a/heron/schedulers/tests/java/org/apache/heron/scheduler/kubernetes/VolumesTests.java
+++ b/heron/schedulers/tests/java/org/apache/heron/scheduler/kubernetes/VolumesTests.java
@@ -38,7 +38,7 @@ public class VolumesTests {
     final String sizeLimit = "1Gi";
     final String path = "/path/to/mount";
     final String subPath = "/sub/path/to/mount";
-    final Map<KubernetesConstants.VolumeConfigKeys, String> configEmptyDir =
+    final Map<KubernetesConstants.VolumeConfigKeys, String> config =
         ImmutableMap.<KubernetesConstants.VolumeConfigKeys, String>builder()
             .put(KubernetesConstants.VolumeConfigKeys.sizeLimit, sizeLimit)
             .put(KubernetesConstants.VolumeConfigKeys.medium, medium)
@@ -48,14 +48,42 @@ public class VolumesTests {
     final V1Volume expectedVolume = new V1VolumeBuilder()
         .withName(volumeName)
         .withNewEmptyDir()
-        .withMedium(medium)
-        .withNewSizeLimit(sizeLimit)
+          .withMedium(medium)
+          .withNewSizeLimit(sizeLimit)
         .endEmptyDir()
         .build();
 
     final V1Volume actualVolume = Volumes.get()
-        .create(Volumes.VolumeType.EmptyDir, volumeName, configEmptyDir);
+        .create(Volumes.VolumeType.EmptyDir, volumeName, config);
 
     Assert.assertEquals("Volume Factory Empty Directory", expectedVolume, actualVolume);
   }
+
+  @Test
+  public void testHostPath() {
+    final String volumeName = "volume-name-host-path";
+    final String type = "DirectoryOrCreate";
+    final String pathOnHost = "path.on.host";
+    final String path = "/path/to/mount";
+    final String subPath = "/sub/path/to/mount";
+    final Map<KubernetesConstants.VolumeConfigKeys, String> config =
+        ImmutableMap.<KubernetesConstants.VolumeConfigKeys, String>builder()
+            .put(KubernetesConstants.VolumeConfigKeys.type, type)
+            .put(KubernetesConstants.VolumeConfigKeys.pathOnHost, pathOnHost)
+            .put(KubernetesConstants.VolumeConfigKeys.path, path)
+            .put(KubernetesConstants.VolumeConfigKeys.subPath, subPath)
+            .build();
+    final V1Volume expectedVolume = new V1VolumeBuilder()
+        .withName(volumeName)
+        .withNewHostPath()
+          .withNewType(type)
+          .withNewPath(pathOnHost)
+        .endHostPath()
+        .build();
+
+    final V1Volume actualVolume = Volumes.get()
+        .create(Volumes.VolumeType.HostPath, volumeName, config);
+
+    Assert.assertEquals("Volume Factory Host Path", expectedVolume, actualVolume);
+  }
 }