Search in sources :

Example 16 with V1Container

use of io.kubernetes.client.openapi.models.V1Container in project java by kubernetes-client.

the class FluentExample method main.

public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    CoreV1Api api = new CoreV1Api();
    V1Pod pod = new V1PodBuilder().withNewMetadata().withName("apod").endMetadata().withNewSpec().addNewContainer().withName("www").withImage("nginx").endContainer().endSpec().build();
    api.createNamespacedPod("default", pod, null, null, null, null);
    V1Pod pod2 = new V1Pod().metadata(new V1ObjectMeta().name("anotherpod")).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name("www").image("nginx"))));
    api.createNamespacedPod("default", pod2, null, null, null, null);
    V1PodList list = api.listNamespacedPod("default", null, null, null, null, null, null, null, null, null, null);
    for (V1Pod item : list.getItems()) {
        System.out.println(item.getMetadata().getName());
    }
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1PodBuilder(io.kubernetes.client.openapi.models.V1PodBuilder) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec)

Example 17 with V1Container

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

Example 18 with V1Container

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

the class RequestObjectBuilder method constructPodTemplate.

/**
 * construct pod template
 */
public static V1PodTemplateSpec constructPodTemplate(ComputeResource computeResource) {
    V1PodTemplateSpec template = new V1PodTemplateSpec();
    V1ObjectMeta templateMetaData = new V1ObjectMeta();
    HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
    // worker pod
    labels.put("t2-wp", jobID);
    templateMetaData.setLabels(labels);
    template.setMetadata(templateMetaData);
    V1PodSpec podSpec = new V1PodSpec();
    podSpec.setTerminationGracePeriodSeconds(0L);
    ArrayList<V1Volume> volumes = new ArrayList<>();
    V1Volume memoryVolume = new V1Volume();
    memoryVolume.setName(KubernetesConstants.POD_MEMORY_VOLUME_NAME);
    V1EmptyDirVolumeSource volumeSource1 = new V1EmptyDirVolumeSource();
    volumeSource1.setMedium("Memory");
    memoryVolume.setEmptyDir(volumeSource1);
    volumes.add(memoryVolume);
    // create it if the requested disk space is positive
    if (computeResource.getDiskGigaBytes() > 0) {
        double volumeSize = computeResource.getDiskGigaBytes() * computeResource.getWorkersPerPod();
        V1Volume volatileVolume = createVolatileVolume(volumeSize);
        volumes.add(volatileVolume);
    }
    if (SchedulerContext.persistentVolumeRequested(config)) {
        String claimName = jobID;
        V1Volume persistentVolume = createPersistentVolume(claimName);
        volumes.add(persistentVolume);
    }
    // if openmpi is used, we initialize a Secret volume on each pod
    if (SchedulerContext.usingOpenMPI(config)) {
        String secretName = KubernetesContext.secretName(config);
        V1Volume secretVolume = createSecretVolume(secretName);
        volumes.add(secretVolume);
    }
    podSpec.setVolumes(volumes);
    int containersPerPod = computeResource.getWorkersPerPod();
    // if openmpi is used, we initialize only one container for each pod
    if (SchedulerContext.usingOpenMPI(config)) {
        containersPerPod = 1;
    }
    ArrayList<V1Container> containers = new ArrayList<V1Container>();
    for (int i = 0; i < containersPerPod; i++) {
        containers.add(constructContainer(computeResource, i));
    }
    podSpec.setContainers(containers);
    if (computeResource.getIndex() == 0) {
        constructAffinity(podSpec);
    }
    template.setSpec(podSpec);
    return template;
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) ArrayList(java.util.ArrayList) V1EmptyDirVolumeSource(io.kubernetes.client.openapi.models.V1EmptyDirVolumeSource) V1Container(io.kubernetes.client.openapi.models.V1Container) V1Volume(io.kubernetes.client.openapi.models.V1Volume) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec)

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