Search in sources :

Example 1 with V1ObjectFieldSelector

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

the class KubernetesUtils method constructContainer.

/**
 * construct a container
 * @param containerIndex
 * @param reqContainer
 * @param jobFileSize
 * @param containerPort
 * @param config
 * @return
 */
public static V1Container constructContainer(int containerIndex, ResourceContainer reqContainer, long jobFileSize, int containerPort, Config config) {
    // construct container and add it to podSpec
    V1Container container = new V1Container();
    String containerName = createContainerName(containerIndex);
    container.setName(containerName);
    container.setImage(TWISTER2_DOCKER_IMAGE);
    // by default: IfNotPresent
    // can be set to Always from client.yaml
    container.setImagePullPolicy(KubernetesContext.imagePullPolicy(config));
    // container.setArgs(Arrays.asList("1000000")); parameter to the main method
    // container.setCommand(Arrays.asList("java", TWISTER2_WORKER_CLASS));
    V1EnvVar var1 = new V1EnvVar().name(KubernetesField.JOB_PACKAGE_FILENAME + "").value(SchedulerContext.jobPackageFileName(config));
    V1EnvVar var2 = new V1EnvVar().name(KubernetesField.JOB_PACKAGE_FILE_SIZE + "").value(jobFileSize + "");
    V1EnvVar var3 = new V1EnvVar().name(KubernetesField.CONTAINER_NAME + "").value(containerName);
    V1EnvVar var4 = new V1EnvVar().name(KubernetesField.USER_JOB_JAR_FILE + "").value(SchedulerContext.userJobJarFile(config));
    V1EnvVar var5 = new V1EnvVar().name(KubernetesField.JOB_DESCRIPTION_FILE + "").value(SchedulerContext.jobDescriptionFile(config));
    // POD_IP with downward API
    V1ObjectFieldSelector fieldSelector = new V1ObjectFieldSelector();
    fieldSelector.setFieldPath("status.podIP");
    V1EnvVarSource varSource = new V1EnvVarSource();
    varSource.setFieldRef(fieldSelector);
    V1EnvVar var6 = new V1EnvVar().name(KubernetesField.POD_IP + "").valueFrom(varSource);
    container.setEnv(Arrays.asList(var1, var2, var3, var4, var5, var6));
    V1ResourceRequirements resReq = new V1ResourceRequirements();
    resReq.putRequestsItem("cpu", reqContainer.getNoOfCpus() + "");
    resReq.putRequestsItem("memory", reqContainer.getMemoryMegaBytes() + "Mi");
    container.setResources(resReq);
    V1VolumeMount volumeMount = new V1VolumeMount();
    volumeMount.setName(POD_SHARED_VOLUME_NAME);
    volumeMount.setMountPath(POD_SHARED_VOLUME);
    container.setVolumeMounts(Arrays.asList(volumeMount));
    V1ContainerPort port = new V1ContainerPort().name("port1").containerPort(containerPort);
    port.setProtocol("TCP");
    container.setPorts(Arrays.asList(port));
    return container;
}
Also used : V1Container(io.kubernetes.client.models.V1Container) V1EnvVarSource(io.kubernetes.client.models.V1EnvVarSource) V1ContainerPort(io.kubernetes.client.models.V1ContainerPort) V1ObjectFieldSelector(io.kubernetes.client.models.V1ObjectFieldSelector) IntOrString(io.kubernetes.client.custom.IntOrString) V1EnvVar(io.kubernetes.client.models.V1EnvVar) V1ResourceRequirements(io.kubernetes.client.models.V1ResourceRequirements) V1VolumeMount(io.kubernetes.client.models.V1VolumeMount)

Example 2 with V1ObjectFieldSelector

use of io.kubernetes.client.models.V1ObjectFieldSelector in project incubator-heron by apache.

the class AppsV1beta1Controller method getContainer.

private V1Container getContainer(List<String> executorCommand, Resource resource, int numberOfInstances) {
    final Config configuration = getConfiguration();
    final V1Container container = new V1Container().name("executor");
    // set up the container images
    container.setImage(KubernetesContext.getExecutorDockerImage(configuration));
    // set up the container command
    container.setCommand(executorCommand);
    if (KubernetesContext.hasImagePullPolicy(configuration)) {
        container.setImagePullPolicy(KubernetesContext.getKubernetesImagePullPolicy(configuration));
    }
    // setup the environment variables for the container
    final V1EnvVar envVarHost = new V1EnvVar();
    envVarHost.name(KubernetesConstants.ENV_HOST).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath(KubernetesConstants.POD_IP)));
    final V1EnvVar envVarPodName = new V1EnvVar();
    envVarPodName.name(KubernetesConstants.ENV_POD_NAME).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath(KubernetesConstants.POD_NAME)));
    container.setEnv(Arrays.asList(envVarHost, envVarPodName));
    // set container resources
    final V1ResourceRequirements resourceRequirements = new V1ResourceRequirements();
    final Map<String, String> requests = new HashMap<>();
    requests.put(KubernetesConstants.MEMORY, KubernetesUtils.Megabytes(resource.getRam()));
    requests.put(KubernetesConstants.CPU, Double.toString(resource.getCpu()));
    resourceRequirements.setRequests(requests);
    container.setResources(resourceRequirements);
    // set container ports
    final boolean debuggingEnabled = TopologyUtils.getTopologyRemoteDebuggingEnabled(Runtime.topology(getRuntimeConfiguration()));
    container.setPorts(getContainerPorts(debuggingEnabled, numberOfInstances));
    // setup volume mounts
    mountVolumeIfPresent(container);
    return container;
}
Also used : V1Container(io.kubernetes.client.models.V1Container) V1EnvVarSource(io.kubernetes.client.models.V1EnvVarSource) HashMap(java.util.HashMap) Config(com.twitter.heron.spi.common.Config) V1ObjectFieldSelector(io.kubernetes.client.models.V1ObjectFieldSelector) V1EnvVar(io.kubernetes.client.models.V1EnvVar) V1ResourceRequirements(io.kubernetes.client.models.V1ResourceRequirements)

Aggregations

V1Container (io.kubernetes.client.models.V1Container)2 V1EnvVar (io.kubernetes.client.models.V1EnvVar)2 V1EnvVarSource (io.kubernetes.client.models.V1EnvVarSource)2 V1ObjectFieldSelector (io.kubernetes.client.models.V1ObjectFieldSelector)2 V1ResourceRequirements (io.kubernetes.client.models.V1ResourceRequirements)2 Config (com.twitter.heron.spi.common.Config)1 IntOrString (io.kubernetes.client.custom.IntOrString)1 V1ContainerPort (io.kubernetes.client.models.V1ContainerPort)1 V1VolumeMount (io.kubernetes.client.models.V1VolumeMount)1 HashMap (java.util.HashMap)1