Search in sources :

Example 11 with V1Container

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

the class V1ControllerTest method testConfigureContainerPorts.

@Test
public void testConfigureContainerPorts() {
    final String portNamekept = "random-port-to-be-kept";
    final int portNumberkept = 1111;
    final int numInstances = 3;
    final List<V1ContainerPort> expectedPortsBase = Collections.unmodifiableList(V1Controller.getExecutorPorts());
    final List<V1ContainerPort> debugPorts = Collections.unmodifiableList(V1Controller.getDebuggingPorts(numInstances));
    final List<V1ContainerPort> inputPortsBase = Collections.unmodifiableList(Arrays.asList(new V1ContainerPort().name("server-port-to-replace").containerPort(KubernetesConstants.SERVER_PORT), new V1ContainerPort().name("shell-port-to-replace").containerPort(KubernetesConstants.SHELL_PORT), new V1ContainerPort().name(portNamekept).containerPort(portNumberkept)));
    // Null ports. This is the default case.
    final V1Container inputContainerWithNullPorts = new V1ContainerBuilder().build();
    v1ControllerWithPodTemplate.configureContainerPorts(false, 0, inputContainerWithNullPorts);
    Assert.assertTrue("Server and/or shell PORTS for container with null ports list", CollectionUtils.containsAll(inputContainerWithNullPorts.getPorts(), expectedPortsBase));
    // Empty ports.
    final V1Container inputContainerWithEmptyPorts = new V1ContainerBuilder().withPorts(new LinkedList<>()).build();
    v1ControllerWithPodTemplate.configureContainerPorts(false, 0, inputContainerWithEmptyPorts);
    Assert.assertTrue("Server and/or shell PORTS for container with empty ports list", CollectionUtils.containsAll(inputContainerWithEmptyPorts.getPorts(), expectedPortsBase));
    // Port overriding.
    final List<V1ContainerPort> inputPorts = new LinkedList<>(inputPortsBase);
    final V1Container inputContainerWithPorts = new V1ContainerBuilder().withPorts(inputPorts).build();
    final List<V1ContainerPort> expectedPortsOverriding = new LinkedList<>(expectedPortsBase);
    expectedPortsOverriding.add(new V1ContainerPort().name(portNamekept).containerPort(portNumberkept));
    v1ControllerWithPodTemplate.configureContainerPorts(false, 0, inputContainerWithPorts);
    Assert.assertTrue("Server and/or shell PORTS for container should be overwritten.", CollectionUtils.containsAll(inputContainerWithPorts.getPorts(), expectedPortsOverriding));
    // Port overriding with debug ports.
    final List<V1ContainerPort> inputPortsWithDebug = new LinkedList<>(debugPorts);
    inputPortsWithDebug.addAll(inputPortsBase);
    final V1Container inputContainerWithDebug = new V1ContainerBuilder().withPorts(inputPortsWithDebug).build();
    final List<V1ContainerPort> expectedPortsDebug = new LinkedList<>(expectedPortsBase);
    expectedPortsDebug.add(new V1ContainerPort().name(portNamekept).containerPort(portNumberkept));
    expectedPortsDebug.addAll(debugPorts);
    v1ControllerWithPodTemplate.configureContainerPorts(true, numInstances, inputContainerWithDebug);
    Assert.assertTrue("Server and/or shell with debug PORTS for container should be overwritten.", CollectionUtils.containsAll(inputContainerWithDebug.getPorts(), expectedPortsDebug));
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1ContainerPort(io.kubernetes.client.openapi.models.V1ContainerPort) V1ContainerBuilder(io.kubernetes.client.openapi.models.V1ContainerBuilder) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 12 with V1Container

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

the class V1ControllerTest method testConfigureContainerResources.

@Test
public void testConfigureContainerResources() {
    final boolean isExecutor = true;
    final Resource resourceDefault = new Resource(9, ByteAmount.fromMegabytes(19000), ByteAmount.fromMegabytes(99000));
    final Resource resourceCustom = new Resource(4, ByteAmount.fromMegabytes(34000), ByteAmount.fromMegabytes(400000));
    final Quantity defaultRAM = Quantity.fromString(KubernetesUtils.Megabytes(resourceDefault.getRam()));
    final Quantity defaultCPU = Quantity.fromString(Double.toString(KubernetesUtils.roundDecimal(resourceDefault.getCpu(), 3)));
    final Quantity customRAM = Quantity.fromString(KubernetesUtils.Megabytes(resourceCustom.getRam()));
    final Quantity customCPU = Quantity.fromString(Double.toString(KubernetesUtils.roundDecimal(resourceCustom.getCpu(), 3)));
    final Quantity customDisk = Quantity.fromString(KubernetesUtils.Megabytes(resourceCustom.getDisk()));
    final Config configNoLimit = Config.newBuilder().put(KubernetesContext.KUBERNETES_RESOURCE_REQUEST_MODE, "NOT_SET").build();
    final Config configWithLimit = Config.newBuilder().put(KubernetesContext.KUBERNETES_RESOURCE_REQUEST_MODE, "EQUAL_TO_LIMIT").build();
    final V1ResourceRequirements expectDefaultRequirements = new V1ResourceRequirements().putLimitsItem(KubernetesConstants.MEMORY, defaultRAM).putLimitsItem(KubernetesConstants.CPU, defaultCPU);
    final V1ResourceRequirements expectCustomRequirements = new V1ResourceRequirements().putLimitsItem(KubernetesConstants.MEMORY, defaultRAM).putLimitsItem(KubernetesConstants.CPU, defaultCPU).putLimitsItem("disk", customDisk);
    final V1ResourceRequirements customRequirements = new V1ResourceRequirements().putLimitsItem(KubernetesConstants.MEMORY, customRAM).putLimitsItem(KubernetesConstants.CPU, customCPU).putLimitsItem("disk", customDisk);
    // Default. Null resources.
    V1Container containerNull = new V1ContainerBuilder().build();
    v1ControllerWithPodTemplate.configureContainerResources(containerNull, configNoLimit, resourceDefault, isExecutor);
    Assert.assertTrue("Default LIMITS should be set in container with null LIMITS", containerNull.getResources().getLimits().entrySet().containsAll(expectDefaultRequirements.getLimits().entrySet()));
    // Empty resources.
    V1Container containerEmpty = new V1ContainerBuilder().withNewResources().endResources().build();
    v1ControllerWithPodTemplate.configureContainerResources(containerEmpty, configNoLimit, resourceDefault, isExecutor);
    Assert.assertTrue("Default LIMITS should be set in container with empty LIMITS", containerNull.getResources().getLimits().entrySet().containsAll(expectDefaultRequirements.getLimits().entrySet()));
    // Custom resources.
    V1Container containerCustom = new V1ContainerBuilder().withResources(customRequirements).build();
    v1ControllerWithPodTemplate.configureContainerResources(containerCustom, configNoLimit, resourceDefault, isExecutor);
    Assert.assertTrue("Custom LIMITS should be set in container with custom LIMITS", containerCustom.getResources().getLimits().entrySet().containsAll(expectCustomRequirements.getLimits().entrySet()));
    // Custom resources with request.
    V1Container containerRequests = new V1ContainerBuilder().withResources(customRequirements).build();
    v1ControllerWithPodTemplate.configureContainerResources(containerRequests, configWithLimit, resourceDefault, isExecutor);
    Assert.assertTrue("Custom LIMITS should be set in container with custom LIMITS and REQUEST", containerRequests.getResources().getLimits().entrySet().containsAll(expectCustomRequirements.getLimits().entrySet()));
    Assert.assertTrue("Custom REQUEST should be set in container with custom LIMITS and REQUEST", containerRequests.getResources().getRequests().entrySet().containsAll(expectCustomRequirements.getLimits().entrySet()));
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) Config(org.apache.heron.spi.common.Config) Resource(org.apache.heron.spi.packing.Resource) Quantity(io.kubernetes.client.custom.Quantity) V1ContainerBuilder(io.kubernetes.client.openapi.models.V1ContainerBuilder) V1ResourceRequirements(io.kubernetes.client.openapi.models.V1ResourceRequirements) Test(org.junit.Test)

Example 13 with V1Container

use of io.kubernetes.client.openapi.models.V1Container 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)

Example 14 with V1Container

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

the class V1ControllerTest method testConfigureContainerResourcesCLI.

@Test
public void testConfigureContainerResourcesCLI() {
    final boolean isExecutor = true;
    final String customLimitMEMStr = "120Gi";
    final String customLimitCPUStr = "5";
    final String customRequestMEMStr = "100Mi";
    final String customRequestCPUStr = "4";
    final Resource resources = new Resource(6, ByteAmount.fromMegabytes(34000), ByteAmount.fromGigabytes(400));
    final Quantity customLimitMEM = Quantity.fromString(customLimitMEMStr);
    final Quantity customLimitCPU = Quantity.fromString(customLimitCPUStr);
    final Quantity customRequestMEM = Quantity.fromString(customRequestMEMStr);
    final Quantity customRequestCPU = Quantity.fromString(customRequestCPUStr);
    final Config config = Config.newBuilder().put(String.format(KubernetesContext.KUBERNETES_RESOURCE_LIMITS_PREFIX + KubernetesConstants.CPU, KubernetesConstants.EXECUTOR_NAME), customLimitCPUStr).put(String.format(KubernetesContext.KUBERNETES_RESOURCE_LIMITS_PREFIX + KubernetesConstants.MEMORY, KubernetesConstants.EXECUTOR_NAME), customLimitMEMStr).put(String.format(KubernetesContext.KUBERNETES_RESOURCE_REQUESTS_PREFIX + KubernetesConstants.CPU, KubernetesConstants.EXECUTOR_NAME), customRequestCPUStr).put(String.format(KubernetesContext.KUBERNETES_RESOURCE_REQUESTS_PREFIX + KubernetesConstants.MEMORY, KubernetesConstants.EXECUTOR_NAME), customRequestMEMStr).put(KubernetesContext.KUBERNETES_RESOURCE_REQUEST_MODE, "EQUAL_TO_LIMIT").build();
    final V1Container expected = new V1ContainerBuilder().withNewResources().addToLimits(KubernetesConstants.CPU, customLimitCPU).addToLimits(KubernetesConstants.MEMORY, customLimitMEM).addToRequests(KubernetesConstants.CPU, customRequestCPU).addToRequests(KubernetesConstants.MEMORY, customRequestMEM).endResources().build();
    final V1Container actual = new V1Container();
    v1ControllerWithPodTemplate.configureContainerResources(actual, config, resources, isExecutor);
    Assert.assertEquals("Container Resources are set from CLI.", expected, actual);
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) Config(org.apache.heron.spi.common.Config) Resource(org.apache.heron.spi.packing.Resource) Quantity(io.kubernetes.client.custom.Quantity) V1ContainerBuilder(io.kubernetes.client.openapi.models.V1ContainerBuilder) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 15 with V1Container

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

the class V1ControllerTest method testMountVolumeIfPresent.

@Test
public void testMountVolumeIfPresent() {
    final String pathDefault = "config-host-volume-path";
    final String pathNameDefault = "config-host-volume-name";
    final Config configWithVolumes = Config.newBuilder().put(KubernetesContext.KUBERNETES_CONTAINER_VOLUME_MOUNT_NAME, pathNameDefault).put(KubernetesContext.KUBERNETES_CONTAINER_VOLUME_MOUNT_PATH, pathDefault).build();
    final V1Controller controllerWithMounts = new V1Controller(configWithVolumes, RUNTIME);
    final V1VolumeMount volumeDefault = new V1VolumeMountBuilder().withName(pathNameDefault).withMountPath(pathDefault).build();
    final V1VolumeMount volumeCustom = new V1VolumeMountBuilder().withName("custom-volume-mount").withMountPath("should-be-kept").build();
    final List<V1VolumeMount> expectedMountsDefault = Collections.singletonList(volumeDefault);
    final List<V1VolumeMount> expectedMountsCustom = Arrays.asList(volumeCustom, volumeDefault);
    final List<V1VolumeMount> volumeMountsCustomList = Arrays.asList(new V1VolumeMountBuilder().withName(pathNameDefault).withMountPath("should-be-replaced").build(), volumeCustom);
    // No Volume Mounts set.
    V1Controller controllerDoNotSetMounts = new V1Controller(Config.newBuilder().build(), RUNTIME);
    V1Container containerNoSetMounts = new V1Container();
    controllerDoNotSetMounts.mountVolumeIfPresent(containerNoSetMounts);
    Assert.assertNull(containerNoSetMounts.getVolumeMounts());
    // Default. Null Volume Mounts.
    V1Container containerNull = new V1ContainerBuilder().build();
    controllerWithMounts.mountVolumeIfPresent(containerNull);
    Assert.assertTrue("Default VOLUME MOUNTS should be set in container with null VOLUME MOUNTS", CollectionUtils.containsAll(expectedMountsDefault, containerNull.getVolumeMounts()));
    // Empty Volume Mounts.
    V1Container containerEmpty = new V1ContainerBuilder().withVolumeMounts(new LinkedList<>()).build();
    controllerWithMounts.mountVolumeIfPresent(containerEmpty);
    Assert.assertTrue("Default VOLUME MOUNTS should be set in container with empty VOLUME MOUNTS", CollectionUtils.containsAll(expectedMountsDefault, containerEmpty.getVolumeMounts()));
    // Custom Volume Mounts.
    V1Container containerCustom = new V1ContainerBuilder().withVolumeMounts(volumeMountsCustomList).build();
    controllerWithMounts.mountVolumeIfPresent(containerCustom);
    Assert.assertTrue("Default VOLUME MOUNTS should be set in container with custom VOLUME MOUNTS", CollectionUtils.containsAll(expectedMountsCustom, containerCustom.getVolumeMounts()));
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) Config(org.apache.heron.spi.common.Config) V1ContainerBuilder(io.kubernetes.client.openapi.models.V1ContainerBuilder) Matchers.anyString(org.mockito.Matchers.anyString) V1VolumeMountBuilder(io.kubernetes.client.openapi.models.V1VolumeMountBuilder) LinkedList(java.util.LinkedList) V1VolumeMount(io.kubernetes.client.openapi.models.V1VolumeMount) Test(org.junit.Test)

Aggregations

V1Container (io.kubernetes.client.openapi.models.V1Container)18 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)9 Test (org.junit.Test)8 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)7 V1ContainerBuilder (io.kubernetes.client.openapi.models.V1ContainerBuilder)6 V1Pod (io.kubernetes.client.openapi.models.V1Pod)5 V1VolumeMount (io.kubernetes.client.openapi.models.V1VolumeMount)5 Quantity (io.kubernetes.client.custom.Quantity)4 V1Volume (io.kubernetes.client.openapi.models.V1Volume)4 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 Config (org.apache.heron.spi.common.Config)4 Matchers.anyString (org.mockito.Matchers.anyString)4 ApiClient (io.kubernetes.client.openapi.ApiClient)3 ApiException (io.kubernetes.client.openapi.ApiException)3 V1ContainerPort (io.kubernetes.client.openapi.models.V1ContainerPort)3 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)3 V1ResourceRequirements (io.kubernetes.client.openapi.models.V1ResourceRequirements)3 IntOrString (io.kubernetes.client.custom.IntOrString)2 V1Patch (io.kubernetes.client.custom.V1Patch)2