Search in sources :

Example 1 with V1ContainerPort

use of io.kubernetes.client.openapi.models.V1ContainerPort in project twister2 by DSC-SPIDAL.

the class RequestObjectBuilder method constructContainer.

/**
 * construct a container
 */
public static V1Container constructContainer(ComputeResource computeResource, int containerIndex) {
    // construct container and add it to podSpec
    V1Container container = new V1Container();
    String containerName = KubernetesUtils.createContainerName(containerIndex);
    container.setName(containerName);
    String containerImage = KubernetesContext.twister2DockerImageForK8s(config);
    if (containerImage == null) {
        throw new RuntimeException("Container Image name is null. Config parameter: " + "twister2.resource.kubernetes.docker.image can not be null");
    }
    container.setImage(containerImage);
    container.setImagePullPolicy(KubernetesContext.imagePullPolicy(config));
    String startScript = null;
    double cpuPerContainer = computeResource.getCpu();
    int ramPerContainer = computeResource.getRamMegaBytes();
    if (SchedulerContext.usingOpenMPI(config)) {
        startScript = "./init_openmpi.sh";
        cpuPerContainer = cpuPerContainer * computeResource.getWorkersPerPod();
        ramPerContainer = ramPerContainer * computeResource.getWorkersPerPod();
    } else {
        startScript = "./init.sh";
    }
    container.setCommand(Arrays.asList("/bin/bash"));
    container.setArgs(Arrays.asList("-c", startScript));
    V1ResourceRequirements resReq = new V1ResourceRequirements();
    if (KubernetesContext.bindWorkerToCPU(config)) {
        resReq.putLimitsItem("cpu", new Quantity(String.format("%.2f", cpuPerContainer)));
        resReq.putLimitsItem("memory", new Quantity(ramPerContainer + "Mi"));
    } else {
        resReq.putRequestsItem("cpu", new Quantity(String.format("%.2f", cpuPerContainer)));
        resReq.putRequestsItem("memory", new Quantity(ramPerContainer + "Mi"));
    }
    container.setResources(resReq);
    ArrayList<V1VolumeMount> volumeMounts = new ArrayList<>();
    V1VolumeMount memoryVolumeMount = new V1VolumeMount();
    memoryVolumeMount.setName(KubernetesConstants.POD_MEMORY_VOLUME_NAME);
    memoryVolumeMount.setMountPath(KubernetesConstants.POD_MEMORY_VOLUME);
    volumeMounts.add(memoryVolumeMount);
    if (computeResource.getDiskGigaBytes() > 0) {
        V1VolumeMount volatileVolumeMount = new V1VolumeMount();
        volatileVolumeMount.setName(KubernetesConstants.POD_VOLATILE_VOLUME_NAME);
        volatileVolumeMount.setMountPath(KubernetesConstants.POD_VOLATILE_VOLUME);
        volumeMounts.add(volatileVolumeMount);
    }
    if (SchedulerContext.persistentVolumeRequested(config)) {
        V1VolumeMount persVolumeMount = new V1VolumeMount();
        persVolumeMount.setName(KubernetesConstants.PERSISTENT_VOLUME_NAME);
        persVolumeMount.setMountPath(KubernetesConstants.PERSISTENT_VOLUME_MOUNT);
        volumeMounts.add(persVolumeMount);
    }
    // mount Secret object as a volume
    if (SchedulerContext.usingOpenMPI(config)) {
        V1VolumeMount persVolumeMount = new V1VolumeMount();
        persVolumeMount.setName(KubernetesConstants.SECRET_VOLUME_NAME);
        persVolumeMount.setMountPath(KubernetesConstants.SECRET_VOLUME_MOUNT);
        volumeMounts.add(persVolumeMount);
    }
    container.setVolumeMounts(volumeMounts);
    int containerPort = KubernetesContext.workerBasePort(config) + containerIndex * (SchedulerContext.numberOfAdditionalPorts(config) + 1);
    V1ContainerPort port = new V1ContainerPort();
    // currently not used
    port.name("port11");
    port.containerPort(containerPort);
    port.setProtocol(KubernetesContext.workerTransportProtocol(config));
    container.setPorts(Arrays.asList(port));
    int jvmMemory = (int) (computeResource.getRamMegaBytes() * KubernetesContext.jvmMemoryFraction(config));
    container.setEnv(constructEnvironmentVariables(containerName, containerPort, jvmMemory));
    return container;
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1ContainerPort(io.kubernetes.client.openapi.models.V1ContainerPort) ArrayList(java.util.ArrayList) Quantity(io.kubernetes.client.custom.Quantity) V1ResourceRequirements(io.kubernetes.client.openapi.models.V1ResourceRequirements) V1VolumeMount(io.kubernetes.client.openapi.models.V1VolumeMount)

Example 2 with V1ContainerPort

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

the class V1Controller method getDebuggingPorts.

/**
 * Generate the debugging ports required by Heron.
 * @param numberOfInstances The number of debugging ports to generate.
 * @return A list of configured debugging <code>ports</code>.
 */
@VisibleForTesting
protected static List<V1ContainerPort> getDebuggingPorts(int numberOfInstances) {
    List<V1ContainerPort> ports = new LinkedList<>();
    IntStream.range(0, numberOfInstances).forEach(i -> {
        final String portName = KubernetesConstants.JVM_REMOTE_DEBUGGER_PORT_NAME + "-" + i;
        final V1ContainerPort port = new V1ContainerPort().name(portName).containerPort(KubernetesConstants.JVM_REMOTE_DEBUGGER_PORT + i);
        ports.add(port);
    });
    return ports;
}
Also used : V1ContainerPort(io.kubernetes.client.openapi.models.V1ContainerPort) LinkedList(java.util.LinkedList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with V1ContainerPort

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

the class V1Controller method getExecutorPorts.

/**
 * Generates a list of <code>ports</code> required by Heron to function.
 * @return A list of configured <code>ports</code> required by Heron to function.
 */
@VisibleForTesting
protected static List<V1ContainerPort> getExecutorPorts() {
    List<V1ContainerPort> ports = new LinkedList<>();
    KubernetesConstants.EXECUTOR_PORTS.forEach((p, v) -> {
        final V1ContainerPort port = new V1ContainerPort().name(p.getName()).containerPort(v);
        ports.add(port);
    });
    return ports;
}
Also used : V1ContainerPort(io.kubernetes.client.openapi.models.V1ContainerPort) LinkedList(java.util.LinkedList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with V1ContainerPort

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

use of io.kubernetes.client.openapi.models.V1ContainerPort in project twister2 by DSC-SPIDAL.

the class JobMasterRequestObject method constructContainer.

/**
 * construct a container
 */
public static V1Container constructContainer() {
    // construct container and add it to podSpec
    V1Container container = new V1Container();
    container.setName("twister2-job-master-0");
    String containerImage = KubernetesContext.twister2DockerImageForK8s(config);
    if (containerImage == null) {
        throw new RuntimeException("Container Image name is null. Config parameter: " + "twister2.resource.kubernetes.docker.image can not be null");
    }
    container.setImage(containerImage);
    container.setImagePullPolicy(KubernetesContext.imagePullPolicy(config));
    container.setCommand(Arrays.asList("/bin/bash"));
    container.setArgs(Arrays.asList("-c", "./init.sh"));
    int jmRam = JobMasterContext.jobMasterRAM(config) + 128;
    V1ResourceRequirements resReq = new V1ResourceRequirements();
    resReq.putRequestsItem("cpu", new Quantity(JobMasterContext.jobMasterCpu(config) + ""));
    resReq.putRequestsItem("memory", new Quantity(jmRam + "Mi"));
    container.setResources(resReq);
    ArrayList<V1VolumeMount> volumeMounts = new ArrayList<>();
    V1VolumeMount memoryVolumeMount = new V1VolumeMount();
    memoryVolumeMount.setName(KubernetesConstants.POD_MEMORY_VOLUME_NAME);
    memoryVolumeMount.setMountPath(KubernetesConstants.POD_MEMORY_VOLUME);
    volumeMounts.add(memoryVolumeMount);
    if (JobMasterContext.volatileVolumeRequested(config)) {
        V1VolumeMount volatileVolumeMount = new V1VolumeMount();
        volatileVolumeMount.setName(KubernetesConstants.POD_VOLATILE_VOLUME_NAME);
        volatileVolumeMount.setMountPath(KubernetesConstants.POD_VOLATILE_VOLUME);
        volumeMounts.add(volatileVolumeMount);
    }
    if (JobMasterContext.persistentVolumeRequested(config)) {
        V1VolumeMount persVolumeMount = new V1VolumeMount();
        persVolumeMount.setName(KubernetesConstants.PERSISTENT_VOLUME_NAME);
        persVolumeMount.setMountPath(KubernetesConstants.PERSISTENT_VOLUME_MOUNT);
        volumeMounts.add(persVolumeMount);
    }
    container.setVolumeMounts(volumeMounts);
    V1ContainerPort port = new V1ContainerPort();
    port.name("job-master-port");
    port.containerPort(JobMasterContext.jobMasterPort(config));
    port.setProtocol("TCP");
    container.setPorts(Arrays.asList(port));
    container.setEnv(constructEnvironmentVariables(JobMasterContext.jobMasterRAM(config)));
    return container;
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1ContainerPort(io.kubernetes.client.openapi.models.V1ContainerPort) ArrayList(java.util.ArrayList) Quantity(io.kubernetes.client.custom.Quantity) IntOrString(io.kubernetes.client.custom.IntOrString) V1ResourceRequirements(io.kubernetes.client.openapi.models.V1ResourceRequirements) V1VolumeMount(io.kubernetes.client.openapi.models.V1VolumeMount)

Aggregations

V1ContainerPort (io.kubernetes.client.openapi.models.V1ContainerPort)5 V1Container (io.kubernetes.client.openapi.models.V1Container)3 LinkedList (java.util.LinkedList)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Quantity (io.kubernetes.client.custom.Quantity)2 V1ResourceRequirements (io.kubernetes.client.openapi.models.V1ResourceRequirements)2 V1VolumeMount (io.kubernetes.client.openapi.models.V1VolumeMount)2 ArrayList (java.util.ArrayList)2 IntOrString (io.kubernetes.client.custom.IntOrString)1 V1ContainerBuilder (io.kubernetes.client.openapi.models.V1ContainerBuilder)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1