Search in sources :

Example 1 with V1PodSpecBuilder

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

the class V1ControllerTest method testConfigureTolerations.

@Test
public void testConfigureTolerations() {
    final V1Toleration keptToleration = new V1Toleration().key("kept toleration").operator("Some Operator").effect("Some Effect").tolerationSeconds(5L);
    final List<V1Toleration> expectedTolerationBase = Collections.unmodifiableList(V1Controller.getTolerations());
    final List<V1Toleration> inputTolerationsBase = Collections.unmodifiableList(Arrays.asList(new V1Toleration().key(KubernetesConstants.TOLERATIONS.get(0)).operator("replace").effect("replace"), new V1Toleration().key(KubernetesConstants.TOLERATIONS.get(1)).operator("replace").effect("replace"), keptToleration));
    // Null Tolerations. This is the default case.
    final V1PodSpec podSpecNullTolerations = new V1PodSpecBuilder().build();
    v1ControllerWithPodTemplate.configureTolerations(podSpecNullTolerations);
    Assert.assertTrue("Pod Spec has null TOLERATIONS and should be set to Heron's defaults", CollectionUtils.containsAll(podSpecNullTolerations.getTolerations(), expectedTolerationBase));
    // Empty Tolerations.
    final V1PodSpec podSpecWithEmptyTolerations = new V1PodSpecBuilder().withTolerations(new LinkedList<>()).build();
    v1ControllerWithPodTemplate.configureTolerations(podSpecWithEmptyTolerations);
    Assert.assertTrue("Pod Spec has empty TOLERATIONS and should be set to Heron's defaults", CollectionUtils.containsAll(podSpecWithEmptyTolerations.getTolerations(), expectedTolerationBase));
    // Toleration overriding.
    final V1PodSpec podSpecWithTolerations = new V1PodSpecBuilder().withTolerations(inputTolerationsBase).build();
    final List<V1Toleration> expectedTolerationsOverriding = new LinkedList<>(expectedTolerationBase);
    expectedTolerationsOverriding.add(keptToleration);
    v1ControllerWithPodTemplate.configureTolerations(podSpecWithTolerations);
    Assert.assertTrue("Pod Spec has TOLERATIONS and should be overridden with Heron's defaults", CollectionUtils.containsAll(podSpecWithTolerations.getTolerations(), expectedTolerationsOverriding));
}
Also used : V1Toleration(io.kubernetes.client.openapi.models.V1Toleration) V1PodSpecBuilder(io.kubernetes.client.openapi.models.V1PodSpecBuilder) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with V1PodSpecBuilder

use of io.kubernetes.client.openapi.models.V1PodSpecBuilder 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 3 with V1PodSpecBuilder

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

the class V1ControllerTest method testConfigurePodWithVolumesAndMountsFromCLI.

@Test
public void testConfigurePodWithVolumesAndMountsFromCLI() {
    final String volumeNameClashing = "clashing-volume";
    final String volumeMountNameClashing = "original-volume-mount";
    V1Volume baseVolume = new V1VolumeBuilder().withName(volumeNameClashing).withNewPersistentVolumeClaim().withClaimName("Original Base Claim Name").endPersistentVolumeClaim().build();
    V1VolumeMount baseVolumeMount = new V1VolumeMountBuilder().withName(volumeMountNameClashing).withMountPath("/original/mount/path").build();
    V1Volume clashingVolume = new V1VolumeBuilder().withName(volumeNameClashing).withNewPersistentVolumeClaim().withClaimName("Clashing Claim Replaced").endPersistentVolumeClaim().build();
    V1VolumeMount clashingVolumeMount = new V1VolumeMountBuilder().withName(volumeMountNameClashing).withMountPath("/clashing/mount/path").build();
    V1Volume secondaryVolume = new V1VolumeBuilder().withName("secondary-volume").withNewPersistentVolumeClaim().withClaimName("Original Secondary Claim Name").endPersistentVolumeClaim().build();
    V1VolumeMount secondaryVolumeMount = new V1VolumeMountBuilder().withName("secondary-volume-mount").withMountPath("/secondary/mount/path").build();
    // Test case container.
    // Input: [0] Pod Spec to modify, [1] Heron container to modify, [2] List of Volumes
    // [3] List of Volume Mounts.
    // Output: The expected <V1PodSpec> and <V1Container>.
    final List<TestTuple<Object[], Pair<V1PodSpec, V1Container>>> testCases = new LinkedList<>();
    // No Persistent Volume Claim.
    final V1PodSpec podSpecEmptyCase = new V1PodSpecBuilder().withVolumes(baseVolume).build();
    final V1Container executorEmptyCase = new V1ContainerBuilder().withVolumeMounts(baseVolumeMount).build();
    final V1PodSpec expectedEmptyPodSpec = new V1PodSpecBuilder().withVolumes(baseVolume).build();
    final V1Container expectedEmptyExecutor = new V1ContainerBuilder().withVolumeMounts(baseVolumeMount).build();
    testCases.add(new TestTuple<>("Empty", new Object[] { podSpecEmptyCase, executorEmptyCase, new LinkedList<>(), new LinkedList<>() }, new Pair<>(expectedEmptyPodSpec, expectedEmptyExecutor)));
    // Non-clashing Persistent Volume Claim.
    final V1PodSpec podSpecNoClashCase = new V1PodSpecBuilder().withVolumes(baseVolume).build();
    final V1Container executorNoClashCase = new V1ContainerBuilder().withVolumeMounts(baseVolumeMount).build();
    final V1PodSpec expectedNoClashPodSpec = new V1PodSpecBuilder().addToVolumes(baseVolume).addToVolumes(secondaryVolume).build();
    final V1Container expectedNoClashExecutor = new V1ContainerBuilder().addToVolumeMounts(baseVolumeMount).addToVolumeMounts(secondaryVolumeMount).build();
    testCases.add(new TestTuple<>("No Clash", new Object[] { podSpecNoClashCase, executorNoClashCase, Collections.singletonList(secondaryVolume), Collections.singletonList(secondaryVolumeMount) }, new Pair<>(expectedNoClashPodSpec, expectedNoClashExecutor)));
    // Clashing Persistent Volume Claim.
    final V1PodSpec podSpecClashCase = new V1PodSpecBuilder().withVolumes(baseVolume).build();
    final V1Container executorClashCase = new V1ContainerBuilder().withVolumeMounts(baseVolumeMount).build();
    final V1PodSpec expectedClashPodSpec = new V1PodSpecBuilder().addToVolumes(clashingVolume).addToVolumes(secondaryVolume).build();
    final V1Container expectedClashExecutor = new V1ContainerBuilder().addToVolumeMounts(clashingVolumeMount).addToVolumeMounts(secondaryVolumeMount).build();
    testCases.add(new TestTuple<>("Clashing", new Object[] { podSpecClashCase, executorClashCase, Arrays.asList(clashingVolume, secondaryVolume), Arrays.asList(clashingVolumeMount, secondaryVolumeMount) }, new Pair<>(expectedClashPodSpec, expectedClashExecutor)));
    // Testing loop.
    for (TestTuple<Object[], Pair<V1PodSpec, V1Container>> testCase : testCases) {
        v1ControllerWithPodTemplate.configurePodWithVolumesAndMountsFromCLI((V1PodSpec) testCase.input[0], (V1Container) testCase.input[1], (List<V1Volume>) testCase.input[2], (List<V1VolumeMount>) testCase.input[3]);
        Assert.assertEquals("Pod Specs match " + testCase.description, testCase.input[0], testCase.expected.first);
        Assert.assertEquals("Executors match " + testCase.description, testCase.input[1], testCase.expected.second);
    }
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) V1PodSpecBuilder(io.kubernetes.client.openapi.models.V1PodSpecBuilder) LinkedList(java.util.LinkedList) V1VolumeMount(io.kubernetes.client.openapi.models.V1VolumeMount) V1VolumeBuilder(io.kubernetes.client.openapi.models.V1VolumeBuilder) V1Container(io.kubernetes.client.openapi.models.V1Container) V1Volume(io.kubernetes.client.openapi.models.V1Volume) TestTuple(org.apache.heron.scheduler.kubernetes.KubernetesUtils.TestTuple) V1ContainerBuilder(io.kubernetes.client.openapi.models.V1ContainerBuilder) V1VolumeMountBuilder(io.kubernetes.client.openapi.models.V1VolumeMountBuilder) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) Pair(org.apache.heron.common.basics.Pair) Test(org.junit.Test)

Aggregations

V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)3 V1PodSpecBuilder (io.kubernetes.client.openapi.models.V1PodSpecBuilder)3 LinkedList (java.util.LinkedList)3 Test (org.junit.Test)3 V1Volume (io.kubernetes.client.openapi.models.V1Volume)2 V1VolumeBuilder (io.kubernetes.client.openapi.models.V1VolumeBuilder)2 Matchers.anyString (org.mockito.Matchers.anyString)2 V1Container (io.kubernetes.client.openapi.models.V1Container)1 V1ContainerBuilder (io.kubernetes.client.openapi.models.V1ContainerBuilder)1 V1Toleration (io.kubernetes.client.openapi.models.V1Toleration)1 V1VolumeMount (io.kubernetes.client.openapi.models.V1VolumeMount)1 V1VolumeMountBuilder (io.kubernetes.client.openapi.models.V1VolumeMountBuilder)1 Pair (org.apache.heron.common.basics.Pair)1 TestTuple (org.apache.heron.scheduler.kubernetes.KubernetesUtils.TestTuple)1 Config (org.apache.heron.spi.common.Config)1