Search in sources :

Example 1 with V1LabelSelector

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

the class KubernetesUtils method createStatefulSetObjectForJob.

/**
 * TBD: calculate containers per pod or issue error
 * when the numberOfContainers can not be divisible by the number of containersPerPod
 * @param jobName
 * @param resourceRequest
 * @return
 */
public static V1beta2StatefulSet createStatefulSetObjectForJob(String jobName, RequestedResources resourceRequest, long jobFileSize, Config config) {
    V1beta2StatefulSet statefulSet = new V1beta2StatefulSet();
    statefulSet.setApiVersion("apps/v1beta2");
    statefulSet.setKind("StatefulSet");
    // construct metadata and set for jobName setting
    V1ObjectMeta meta = new V1ObjectMeta();
    meta.setName(jobName);
    statefulSet.setMetadata(meta);
    // construct JobSpec and set
    V1beta2StatefulSetSpec setSpec = new V1beta2StatefulSetSpec();
    setSpec.serviceName(createServiceName(jobName));
    // pods will be started in parallel
    // by default they are started sequentially
    setSpec.setPodManagementPolicy("Parallel");
    // number of containers has to be divisible by the containersPerPod
    // all pods will have equal number of containers
    // all pods will be identical
    int containersPerPod = KubernetesContext.containersPerPod(config);
    if (resourceRequest.getNoOfContainers() % containersPerPod != 0) {
        LOG.log(Level.SEVERE, "Number of containers has to be divisible by containersPerPod.\n" + "Number of containers: " + resourceRequest.getNoOfContainers() + "\n" + "containersPerPod: " + containersPerPod + "\n" + "Aborting submission.");
        return null;
    }
    int numberOfPods = resourceRequest.getNoOfContainers() / containersPerPod;
    setSpec.setReplicas(numberOfPods);
    // add selector for the job
    V1LabelSelector selector = new V1LabelSelector();
    String serviceLabel = createServiceLabel(jobName);
    selector.putMatchLabelsItem("app", serviceLabel);
    setSpec.setSelector(selector);
    // construct the pod template
    V1PodTemplateSpec template = constructPodTemplate(resourceRequest.getContainer(), serviceLabel, jobFileSize, config);
    setSpec.setTemplate(template);
    statefulSet.setSpec(setSpec);
    return statefulSet;
}
Also used : V1beta2StatefulSet(io.kubernetes.client.models.V1beta2StatefulSet) V1beta2StatefulSetSpec(io.kubernetes.client.models.V1beta2StatefulSetSpec) V1ObjectMeta(io.kubernetes.client.models.V1ObjectMeta) IntOrString(io.kubernetes.client.custom.IntOrString) V1PodTemplateSpec(io.kubernetes.client.models.V1PodTemplateSpec) V1LabelSelector(io.kubernetes.client.models.V1LabelSelector)

Example 2 with V1LabelSelector

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

the class AppsV1beta1Controller method createStatefulSet.

private V1beta1StatefulSet createStatefulSet(Resource containerResource, int numberOfInstances) {
    final String topologyName = getTopologyName();
    final Config runtimeConfiguration = getRuntimeConfiguration();
    final V1beta1StatefulSet statefulSet = new V1beta1StatefulSet();
    // setup stateful set metadata
    final V1ObjectMeta objectMeta = new V1ObjectMeta();
    objectMeta.name(topologyName);
    statefulSet.metadata(objectMeta);
    // create the stateful set spec
    final V1beta1StatefulSetSpec statefulSetSpec = new V1beta1StatefulSetSpec();
    statefulSetSpec.serviceName(topologyName);
    statefulSetSpec.setReplicas(Runtime.numContainers(runtimeConfiguration).intValue());
    // Parallel pod management tells the StatefulSet controller to launch or terminate
    // all Pods in parallel, and not to wait for Pods to become Running and Ready or completely
    // terminated prior to launching or terminating another Pod.
    statefulSetSpec.setPodManagementPolicy("Parallel");
    // add selector match labels "app=heron" and "topology=topology-name"
    // so the we know which pods to manage
    final V1LabelSelector selector = new V1LabelSelector();
    selector.matchLabels(getMatchLabels(topologyName));
    statefulSetSpec.selector(selector);
    // create a pod template
    final V1PodTemplateSpec podTemplateSpec = new V1PodTemplateSpec();
    // set up pod meta
    final V1ObjectMeta templateMetaData = new V1ObjectMeta().labels(getLabels(topologyName));
    templateMetaData.annotations(getPrometheusAnnotations());
    podTemplateSpec.setMetadata(templateMetaData);
    final List<String> command = getExecutorCommand("$" + ENV_SHARD_ID);
    podTemplateSpec.spec(getPodSpec(command, containerResource, numberOfInstances));
    statefulSetSpec.setTemplate(podTemplateSpec);
    statefulSet.spec(statefulSetSpec);
    return statefulSet;
}
Also used : Config(com.twitter.heron.spi.common.Config) V1ObjectMeta(io.kubernetes.client.models.V1ObjectMeta) V1PodTemplateSpec(io.kubernetes.client.models.V1PodTemplateSpec) V1LabelSelector(io.kubernetes.client.models.V1LabelSelector) V1beta1StatefulSet(io.kubernetes.client.models.V1beta1StatefulSet) V1beta1StatefulSetSpec(io.kubernetes.client.models.V1beta1StatefulSetSpec)

Aggregations

V1LabelSelector (io.kubernetes.client.models.V1LabelSelector)2 V1ObjectMeta (io.kubernetes.client.models.V1ObjectMeta)2 V1PodTemplateSpec (io.kubernetes.client.models.V1PodTemplateSpec)2 Config (com.twitter.heron.spi.common.Config)1 IntOrString (io.kubernetes.client.custom.IntOrString)1 V1beta1StatefulSet (io.kubernetes.client.models.V1beta1StatefulSet)1 V1beta1StatefulSetSpec (io.kubernetes.client.models.V1beta1StatefulSetSpec)1 V1beta2StatefulSet (io.kubernetes.client.models.V1beta2StatefulSet)1 V1beta2StatefulSetSpec (io.kubernetes.client.models.V1beta2StatefulSetSpec)1