Search in sources :

Example 1 with V1VolumeBuilder

use of io.kubernetes.client.openapi.models.V1VolumeBuilder in project heron by twitter.

the class V1ControllerTest method testCreateVolumeAndMountsNFSCLI.

@Test
public void testCreateVolumeAndMountsNFSCLI() {
    final String volumeName = "volume-name-nfs";
    final String server = "nfs.server.address";
    final String pathOnNFS = "path.on.host";
    final String readOnly = "true";
    final String path = "/path/to/mount";
    final String subPath = "/sub/path/to/mount";
    // NFS.
    final Map<String, Map<VolumeConfigKeys, String>> config = ImmutableMap.of(volumeName, new HashMap<VolumeConfigKeys, String>() {

        {
            put(VolumeConfigKeys.server, server);
            put(VolumeConfigKeys.readOnly, readOnly);
            put(VolumeConfigKeys.pathOnNFS, pathOnNFS);
            put(VolumeConfigKeys.path, path);
            put(VolumeConfigKeys.subPath, subPath);
        }
    });
    final List<V1Volume> expectedVolumes = Collections.singletonList(new V1VolumeBuilder().withName(volumeName).withNewNfs().withServer(server).withPath(pathOnNFS).withReadOnly(Boolean.parseBoolean(readOnly)).endNfs().build());
    final List<V1VolumeMount> expectedMounts = Collections.singletonList(new V1VolumeMountBuilder().withName(volumeName).withMountPath(path).withSubPath(subPath).withReadOnly(true).build());
    List<V1Volume> actualVolumes = new LinkedList<>();
    List<V1VolumeMount> actualMounts = new LinkedList<>();
    v1ControllerPodTemplate.createVolumeAndMountsNFSCLI(config, actualVolumes, actualMounts);
    Assert.assertEquals("NFS Volume populated", expectedVolumes, actualVolumes);
    Assert.assertEquals("NFS Volume Mount populated", expectedMounts, actualMounts);
}
Also used : V1VolumeBuilder(io.kubernetes.client.openapi.models.V1VolumeBuilder) VolumeConfigKeys(org.apache.heron.scheduler.kubernetes.KubernetesConstants.VolumeConfigKeys) V1Volume(io.kubernetes.client.openapi.models.V1Volume) Matchers.anyString(org.mockito.Matchers.anyString) V1VolumeMountBuilder(io.kubernetes.client.openapi.models.V1VolumeMountBuilder) HashMap(java.util.HashMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedList(java.util.LinkedList) V1VolumeMount(io.kubernetes.client.openapi.models.V1VolumeMount) Test(org.junit.Test)

Example 2 with V1VolumeBuilder

use of io.kubernetes.client.openapi.models.V1VolumeBuilder in project heron by twitter.

the class V1ControllerTest method testCreateVolumeAndMountsEmptyDirCLI.

@Test
public void testCreateVolumeAndMountsEmptyDirCLI() {
    final String volumeName = "volume-name-empty-dir";
    final String medium = "Memory";
    final String sizeLimit = "1Gi";
    final String path = "/path/to/mount";
    final String subPath = "/sub/path/to/mount";
    // Empty Dir.
    final Map<String, Map<VolumeConfigKeys, String>> config = ImmutableMap.of(volumeName, new HashMap<VolumeConfigKeys, String>() {

        {
            put(VolumeConfigKeys.sizeLimit, sizeLimit);
            put(VolumeConfigKeys.medium, "Memory");
            put(VolumeConfigKeys.path, path);
            put(VolumeConfigKeys.subPath, subPath);
        }
    });
    final List<V1Volume> expectedVolumes = Collections.singletonList(new V1VolumeBuilder().withName(volumeName).withNewEmptyDir().withMedium(medium).withNewSizeLimit(sizeLimit).endEmptyDir().build());
    final List<V1VolumeMount> expectedMounts = Collections.singletonList(new V1VolumeMountBuilder().withName(volumeName).withMountPath(path).withSubPath(subPath).build());
    List<V1Volume> actualVolumes = new LinkedList<>();
    List<V1VolumeMount> actualMounts = new LinkedList<>();
    v1ControllerPodTemplate.createVolumeAndMountsEmptyDirCLI(config, actualVolumes, actualMounts);
    Assert.assertEquals("Empty Dir Volume populated", expectedVolumes, actualVolumes);
    Assert.assertEquals("Empty Dir Volume Mount populated", expectedMounts, actualMounts);
}
Also used : V1VolumeBuilder(io.kubernetes.client.openapi.models.V1VolumeBuilder) VolumeConfigKeys(org.apache.heron.scheduler.kubernetes.KubernetesConstants.VolumeConfigKeys) V1Volume(io.kubernetes.client.openapi.models.V1Volume) Matchers.anyString(org.mockito.Matchers.anyString) V1VolumeMountBuilder(io.kubernetes.client.openapi.models.V1VolumeMountBuilder) HashMap(java.util.HashMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedList(java.util.LinkedList) V1VolumeMount(io.kubernetes.client.openapi.models.V1VolumeMount) Test(org.junit.Test)

Example 3 with V1VolumeBuilder

use of io.kubernetes.client.openapi.models.V1VolumeBuilder in project heron by twitter.

the class V1ControllerTest method testCreatePersistentVolumeClaimVolumesAndMounts.

@Test
public void testCreatePersistentVolumeClaimVolumesAndMounts() {
    final String volumeNameOne = "VolumeNameONE";
    final String volumeNameTwo = "VolumeNameTWO";
    final String claimNameOne = "claim-name-one";
    final String claimNameTwo = "OnDemand";
    final String mountPathOne = "/mount/path/ONE";
    final String mountPathTwo = "/mount/path/TWO";
    final String mountSubPathTwo = "/mount/sub/path/TWO";
    Map<String, Map<VolumeConfigKeys, String>> mapOfOpts = ImmutableMap.of(volumeNameOne, ImmutableMap.of(VolumeConfigKeys.claimName, claimNameOne, VolumeConfigKeys.path, mountPathOne), volumeNameTwo, ImmutableMap.of(VolumeConfigKeys.claimName, claimNameTwo, VolumeConfigKeys.path, mountPathTwo, VolumeConfigKeys.subPath, mountSubPathTwo));
    final V1Volume volumeOne = new V1VolumeBuilder().withName(volumeNameOne).withNewPersistentVolumeClaim().withClaimName(claimNameOne).endPersistentVolumeClaim().build();
    final V1Volume volumeTwo = new V1VolumeBuilder().withName(volumeNameTwo).withNewPersistentVolumeClaim().withClaimName(claimNameTwo).endPersistentVolumeClaim().build();
    final V1VolumeMount volumeMountOne = new V1VolumeMountBuilder().withName(volumeNameOne).withMountPath(mountPathOne).build();
    final V1VolumeMount volumeMountTwo = new V1VolumeMountBuilder().withName(volumeNameTwo).withMountPath(mountPathTwo).withSubPath(mountSubPathTwo).build();
    // Test case container.
    // Input: Map of Volume configurations.
    // Output: The expected lists of Volumes and Volume Mounts.
    final List<TestTuple<Map<String, Map<VolumeConfigKeys, String>>, Pair<List<V1Volume>, List<V1VolumeMount>>>> testCases = new LinkedList<>();
    // Default case: No PVC provided.
    testCases.add(new TestTuple<>("Generated an empty list of Volumes", new HashMap<>(), new Pair<>(new LinkedList<>(), new LinkedList<>())));
    // PVC Provided.
    final Pair<List<V1Volume>, List<V1VolumeMount>> expectedFull = new Pair<>(new LinkedList<>(Arrays.asList(volumeOne, volumeTwo)), new LinkedList<>(Arrays.asList(volumeMountOne, volumeMountTwo)));
    testCases.add(new TestTuple<>("Generated a list of Volumes", mapOfOpts, new Pair<>(expectedFull.first, expectedFull.second)));
    // Testing loop.
    for (TestTuple<Map<String, Map<VolumeConfigKeys, String>>, Pair<List<V1Volume>, List<V1VolumeMount>>> testCase : testCases) {
        List<V1Volume> actualVolume = new LinkedList<>();
        List<V1VolumeMount> actualVolumeMount = new LinkedList<>();
        v1ControllerPodTemplate.createVolumeAndMountsPersistentVolumeClaimCLI(testCase.input, actualVolume, actualVolumeMount);
        Assert.assertTrue(testCase.description, (testCase.expected.first).containsAll(actualVolume));
        Assert.assertTrue(testCase.description + " Mounts", (testCase.expected.second).containsAll(actualVolumeMount));
    }
}
Also used : VolumeConfigKeys(org.apache.heron.scheduler.kubernetes.KubernetesConstants.VolumeConfigKeys) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) V1VolumeMount(io.kubernetes.client.openapi.models.V1VolumeMount) V1VolumeBuilder(io.kubernetes.client.openapi.models.V1VolumeBuilder) V1Volume(io.kubernetes.client.openapi.models.V1Volume) TestTuple(org.apache.heron.scheduler.kubernetes.KubernetesUtils.TestTuple) LinkedList(java.util.LinkedList) List(java.util.List) V1VolumeMountBuilder(io.kubernetes.client.openapi.models.V1VolumeMountBuilder) HashMap(java.util.HashMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Pair(org.apache.heron.common.basics.Pair) Test(org.junit.Test)

Example 4 with V1VolumeBuilder

use of io.kubernetes.client.openapi.models.V1VolumeBuilder in project heron by twitter.

the class V1ControllerTest method testAddVolumesIfPresent.

@Test
public void testAddVolumesIfPresent() {
    final String pathDefault = "config-host-volume-path";
    final String pathNameDefault = "config-host-volume-name";
    final Config configWithVolumes = Config.newBuilder().put(KubernetesContext.KUBERNETES_VOLUME_NAME, pathNameDefault).put(KubernetesContext.KUBERNETES_VOLUME_TYPE, Volumes.HOST_PATH).put(KubernetesContext.KUBERNETES_VOLUME_HOSTPATH_PATH, pathDefault).build();
    final V1Controller controllerWithVol = new V1Controller(configWithVolumes, RUNTIME);
    final V1Volume volumeDefault = new V1VolumeBuilder().withName(pathNameDefault).withNewHostPath().withNewPath(pathDefault).endHostPath().build();
    final V1Volume volumeToBeKept = new V1VolumeBuilder().withName("volume-to-be-kept-name").withNewHostPath().withNewPath("volume-to-be-kept-path").endHostPath().build();
    final List<V1Volume> customVolumeList = Arrays.asList(new V1VolumeBuilder().withName(pathNameDefault).withNewHostPath().withNewPath("this-path-must-be-replaced").endHostPath().build(), volumeToBeKept);
    final List<V1Volume> expectedDefault = Collections.singletonList(volumeDefault);
    final List<V1Volume> expectedCustom = Arrays.asList(volumeDefault, volumeToBeKept);
    // No Volumes set.
    V1Controller controllerDoNotSetVolumes = new V1Controller(Config.newBuilder().build(), RUNTIME);
    V1PodSpec podSpecNoSetVolumes = new V1PodSpec();
    controllerDoNotSetVolumes.addVolumesIfPresent(podSpecNoSetVolumes);
    Assert.assertNull(podSpecNoSetVolumes.getVolumes());
    // Default. Null Volumes.
    V1PodSpec podSpecNull = new V1PodSpecBuilder().build();
    controllerWithVol.addVolumesIfPresent(podSpecNull);
    Assert.assertTrue("Default VOLUMES should be set in container with null VOLUMES", CollectionUtils.containsAll(expectedDefault, podSpecNull.getVolumes()));
    // Empty Volumes list
    V1PodSpec podSpecEmpty = new V1PodSpecBuilder().withVolumes(new LinkedList<>()).build();
    controllerWithVol.addVolumesIfPresent(podSpecEmpty);
    Assert.assertTrue("Default VOLUMES should be set in container with empty VOLUMES", CollectionUtils.containsAll(expectedDefault, podSpecEmpty.getVolumes()));
    // Custom Volumes list
    V1PodSpec podSpecCustom = new V1PodSpecBuilder().withVolumes(customVolumeList).build();
    controllerWithVol.addVolumesIfPresent(podSpecCustom);
    Assert.assertTrue("Default VOLUMES should be set in container with custom VOLUMES", CollectionUtils.containsAll(expectedCustom, podSpecCustom.getVolumes()));
}
Also used : V1VolumeBuilder(io.kubernetes.client.openapi.models.V1VolumeBuilder) V1Volume(io.kubernetes.client.openapi.models.V1Volume) Config(org.apache.heron.spi.common.Config) Matchers.anyString(org.mockito.Matchers.anyString) V1PodSpecBuilder(io.kubernetes.client.openapi.models.V1PodSpecBuilder) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 5 with V1VolumeBuilder

use of io.kubernetes.client.openapi.models.V1VolumeBuilder in project pravega by pravega.

the class K8SequentialExecutor method getTestPod.

private V1Pod getTestPod(String className, String methodName, String podName) {
    log.info("Running test pod with security enabled :{}, transport enabled: {}", Utils.AUTH_ENABLED, Utils.TLS_AND_AUTH_ENABLED);
    V1Pod pod = new V1PodBuilder().withNewMetadata().withName(podName).withNamespace(NAMESPACE).withLabels(ImmutableMap.of("POD_NAME", podName, "app", APP)).endMetadata().withNewSpec().withServiceAccountName(SERVICE_ACCOUNT).withAutomountServiceAccountToken(true).withVolumes(new V1VolumeBuilder().withName("task-pv-storage").withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSourceBuilder().withClaimName("task-pv-claim").build()).build()).addNewContainer().withName(// container name is same as that of the pod.
    podName).withImage(TEST_POD_IMAGE).withImagePullPolicy("IfNotPresent").withCommand("/bin/sh").withArgs("-c", "java" + getArgs() + " -cp /data/test-collection.jar io.pravega.test.system.SingleJUnitTestRunner " + className + "#" + methodName + /*+ " > server.log 2>&1 */
    "; exit $?").withVolumeMounts(new V1VolumeMountBuilder().withMountPath("/data").withName("task-pv-storage").build()).endContainer().withRestartPolicy("Never").endSpec().build();
    if (Utils.TLS_AND_AUTH_ENABLED) {
        pod = new V1PodBuilder(pod).editSpec().withVolumes(new V1VolumeBuilder().withName("tls-certs").withSecret(new V1SecretVolumeSourceBuilder().withSecretName(Utils.TLS_SECRET_NAME).build()).build()).editContainer(0).withVolumeMounts(new V1VolumeMountBuilder().withMountPath(Utils.TLS_MOUNT_PATH).withName("tls-secret").build()).endContainer().endSpec().build();
    }
    return pod;
}
Also used : V1VolumeBuilder(io.kubernetes.client.openapi.models.V1VolumeBuilder) V1SecretVolumeSourceBuilder(io.kubernetes.client.openapi.models.V1SecretVolumeSourceBuilder) V1PodBuilder(io.kubernetes.client.openapi.models.V1PodBuilder) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1VolumeMountBuilder(io.kubernetes.client.openapi.models.V1VolumeMountBuilder) V1PersistentVolumeClaimVolumeSourceBuilder(io.kubernetes.client.openapi.models.V1PersistentVolumeClaimVolumeSourceBuilder)

Aggregations

V1VolumeBuilder (io.kubernetes.client.openapi.models.V1VolumeBuilder)10 V1Volume (io.kubernetes.client.openapi.models.V1Volume)9 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 V1VolumeMountBuilder (io.kubernetes.client.openapi.models.V1VolumeMountBuilder)6 LinkedList (java.util.LinkedList)6 Test (org.junit.Test)6 Matchers.anyString (org.mockito.Matchers.anyString)6 V1VolumeMount (io.kubernetes.client.openapi.models.V1VolumeMount)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 VolumeConfigKeys (org.apache.heron.scheduler.kubernetes.KubernetesConstants.VolumeConfigKeys)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)2 V1PodSpecBuilder (io.kubernetes.client.openapi.models.V1PodSpecBuilder)2 Pair (org.apache.heron.common.basics.Pair)2 TestTuple (org.apache.heron.scheduler.kubernetes.KubernetesUtils.TestTuple)2 Quantity (io.kubernetes.client.custom.Quantity)1 V1Container (io.kubernetes.client.openapi.models.V1Container)1 V1ContainerBuilder (io.kubernetes.client.openapi.models.V1ContainerBuilder)1