Search in sources :

Example 6 with V1PodTemplateSpec

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

the class JobMasterRequestObject method constructPodTemplate.

/**
 * construct pod template
 */
public static V1PodTemplateSpec constructPodTemplate() {
    V1PodTemplateSpec template = new V1PodTemplateSpec();
    V1ObjectMeta templateMetaData = new V1ObjectMeta();
    HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
    // job master pod
    labels.put("t2-mp", 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 (JobMasterContext.volatileVolumeRequested(config)) {
        double vSize = JobMasterContext.volatileVolumeSize(config);
        V1Volume volatileVolume = RequestObjectBuilder.createVolatileVolume(vSize);
        volumes.add(volatileVolume);
    }
    if (JobMasterContext.persistentVolumeRequested(config)) {
        String claimName = jobID;
        V1Volume persistentVolume = RequestObjectBuilder.createPersistentVolume(claimName);
        volumes.add(persistentVolume);
    }
    podSpec.setVolumes(volumes);
    ArrayList<V1Container> containers = new ArrayList<V1Container>();
    containers.add(constructContainer());
    podSpec.setContainers(containers);
    template.setSpec(podSpec);
    return template;
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1Volume(io.kubernetes.client.openapi.models.V1Volume) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) ArrayList(java.util.ArrayList) V1EmptyDirVolumeSource(io.kubernetes.client.openapi.models.V1EmptyDirVolumeSource) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) IntOrString(io.kubernetes.client.custom.IntOrString) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec)

Example 7 with V1PodTemplateSpec

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

the class JobMasterRequestObject method createStatefulSetObject.

/**
 * create StatefulSet object for a job
 */
public static V1StatefulSet createStatefulSetObject() {
    if (config == null) {
        LOG.severe("JobMasterRequestObject.init method has not been called.");
        return null;
    }
    V1StatefulSet statefulSet = new V1StatefulSet();
    String statefulSetName = KubernetesUtils.createJobMasterStatefulSetName(jobID);
    // set labels for the jm stateful set
    HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
    // job master statefulset
    labels.put("t2-mss", jobID);
    // construct metadata and set for jobID setting
    V1ObjectMeta meta = new V1ObjectMeta();
    meta.setName(statefulSetName);
    meta.setLabels(labels);
    statefulSet.setMetadata(meta);
    // construct JobSpec and set
    V1StatefulSetSpec setSpec = new V1StatefulSetSpec();
    setSpec.serviceName(KubernetesUtils.createJobMasterServiceName(jobID));
    setSpec.setReplicas(1);
    // add selector for the job
    V1LabelSelector selector = new V1LabelSelector();
    selector.putMatchLabelsItem("t2-mp", jobID);
    setSpec.setSelector(selector);
    // construct the pod template
    V1PodTemplateSpec template = constructPodTemplate();
    setSpec.setTemplate(template);
    statefulSet.setSpec(setSpec);
    return statefulSet;
}
Also used : V1StatefulSet(io.kubernetes.client.openapi.models.V1StatefulSet) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1StatefulSetSpec(io.kubernetes.client.openapi.models.V1StatefulSetSpec) IntOrString(io.kubernetes.client.custom.IntOrString) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) V1LabelSelector(io.kubernetes.client.openapi.models.V1LabelSelector)

Example 8 with V1PodTemplateSpec

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

the class V1Controller method loadPodFromTemplate.

/**
 * Initiates the process of locating and loading <code>Pod Template</code> from a <code>ConfigMap</code>.
 * The loaded text is then parsed into a usable <code>Pod Template</code>.
 * @param isExecutor Flag to indicate loading of <code>Pod Template</code> for <code>Executor</code>
 *                   or <code>Manager</code>.
 * @return A <code>Pod Template</code> which is loaded and parsed from a <code>ConfigMap</code>.
 */
@VisibleForTesting
protected V1PodTemplateSpec loadPodFromTemplate(boolean isExecutor) {
    final Pair<String, String> podTemplateConfigMapName = getPodTemplateLocation(isExecutor);
    // Default Pod Template.
    if (podTemplateConfigMapName == null) {
        LOG.log(Level.INFO, "Configuring cluster with the Default Pod Template");
        return new V1PodTemplateSpec();
    }
    if (isPodTemplateDisabled) {
        throw new TopologySubmissionException("Custom Pod Templates are disabled");
    }
    final String configMapName = podTemplateConfigMapName.first;
    final String podTemplateName = podTemplateConfigMapName.second;
    // Attempt to locate ConfigMap with provided Pod Template name.
    try {
        V1ConfigMap configMap = getConfigMap(configMapName);
        if (configMap == null) {
            throw new ApiException(String.format("K8s client unable to locate ConfigMap '%s'", configMapName));
        }
        final Map<String, String> configMapData = configMap.getData();
        if (configMapData != null && configMapData.containsKey(podTemplateName)) {
            // NullPointerException when Pod Template is empty.
            V1PodTemplateSpec podTemplate = ((V1PodTemplate) Yaml.load(configMapData.get(podTemplateName))).getTemplate();
            LOG.log(Level.INFO, String.format("Configuring cluster with the %s.%s Pod Template", configMapName, podTemplateName));
            return podTemplate;
        }
        // Failure to locate Pod Template with provided name.
        throw new ApiException(String.format("Failed to locate Pod Template '%s' in ConfigMap '%s'", podTemplateName, configMapName));
    } catch (ApiException e) {
        KubernetesUtils.logExceptionWithDetails(LOG, e.getMessage(), e);
        throw new TopologySubmissionException(e.getMessage());
    } catch (IOException | ClassCastException | NullPointerException e) {
        final String message = String.format("Error parsing Pod Template '%s' in ConfigMap '%s'", podTemplateName, configMapName);
        KubernetesUtils.logExceptionWithDetails(LOG, message, e);
        throw new TopologySubmissionException(message);
    }
}
Also used : TopologySubmissionException(org.apache.heron.scheduler.TopologySubmissionException) V1PodTemplate(io.kubernetes.client.openapi.models.V1PodTemplate) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) IOException(java.io.IOException) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) ApiException(io.kubernetes.client.openapi.ApiException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with V1PodTemplateSpec

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

the class V1Controller method createStatefulSet.

/**
 * Creates and configures the <code>StatefulSet</code> which the topology's <code>executor</code>s will run in.
 * @param containerResource Passed down to configure the <code>executor</code> resource limits.
 * @param numberOfInstances Used to configure the execution command and ports for the <code>executor</code>.
 * @param isExecutor Flag used to configure components specific to <code>executor</code> and <code>manager</code>.
 * @return A fully configured <code>StatefulSet</code> for the topology's <code>executors</code>.
 */
private V1StatefulSet createStatefulSet(Resource containerResource, int numberOfInstances, boolean isExecutor) {
    final String topologyName = getTopologyName();
    final Config runtimeConfiguration = getRuntimeConfiguration();
    final List<V1Volume> volumes = new LinkedList<>();
    final List<V1VolumeMount> volumeMounts = new LinkedList<>();
    // Collect Persistent Volume Claim configurations from the CLI.
    final Map<String, Map<KubernetesConstants.VolumeConfigKeys, String>> configsPVC = KubernetesContext.getVolumeClaimTemplates(getConfiguration(), isExecutor);
    // Collect all Volume configurations from the CLI and generate Volumes and Volume Mounts.
    createVolumeAndMountsPersistentVolumeClaimCLI(configsPVC, volumes, volumeMounts);
    createVolumeAndMountsHostPathCLI(KubernetesContext.getVolumeHostPath(getConfiguration(), isExecutor), volumes, volumeMounts);
    createVolumeAndMountsEmptyDirCLI(KubernetesContext.getVolumeEmptyDir(getConfiguration(), isExecutor), volumes, volumeMounts);
    createVolumeAndMountsNFSCLI(KubernetesContext.getVolumeNFS(getConfiguration(), isExecutor), volumes, volumeMounts);
    final V1StatefulSet statefulSet = new V1StatefulSet();
    // Setup StatefulSet's metadata.
    final V1ObjectMeta objectMeta = new V1ObjectMeta().name(getStatefulSetName(isExecutor)).labels(getPodLabels(topologyName));
    statefulSet.setMetadata(objectMeta);
    // Create the StatefulSet Spec.
    // Reduce replica count by one for Executors and set to one for Manager.
    final int replicasCount = isExecutor ? Runtime.numContainers(runtimeConfiguration).intValue() - 1 : 1;
    final V1StatefulSetSpec statefulSetSpec = new V1StatefulSetSpec().serviceName(topologyName).replicas(replicasCount);
    // 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 we know which pods to manage.
    final V1LabelSelector selector = new V1LabelSelector().matchLabels(getPodMatchLabels(topologyName));
    statefulSetSpec.setSelector(selector);
    // Create a Pod Template.
    final V1PodTemplateSpec podTemplateSpec = loadPodFromTemplate(isExecutor);
    // Set up Pod Metadata.
    final V1ObjectMeta templateMetaData = new V1ObjectMeta().labels(getPodLabels(topologyName));
    Map<String, String> annotations = new HashMap<>();
    annotations.putAll(getPodAnnotations());
    annotations.putAll(getPrometheusAnnotations());
    templateMetaData.setAnnotations(annotations);
    podTemplateSpec.setMetadata(templateMetaData);
    configurePodSpec(podTemplateSpec, containerResource, numberOfInstances, isExecutor, volumes, volumeMounts);
    statefulSetSpec.setTemplate(podTemplateSpec);
    statefulSet.setSpec(statefulSetSpec);
    statefulSetSpec.setVolumeClaimTemplates(createPersistentVolumeClaims(configsPVC));
    return statefulSet;
}
Also used : HashMap(java.util.HashMap) Config(org.apache.heron.spi.common.Config) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) LinkedList(java.util.LinkedList) V1VolumeMount(io.kubernetes.client.openapi.models.V1VolumeMount) V1Volume(io.kubernetes.client.openapi.models.V1Volume) V1StatefulSet(io.kubernetes.client.openapi.models.V1StatefulSet) V1StatefulSetSpec(io.kubernetes.client.openapi.models.V1StatefulSetSpec) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) V1LabelSelector(io.kubernetes.client.openapi.models.V1LabelSelector) Map(java.util.Map) HashMap(java.util.HashMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap)

Example 10 with V1PodTemplateSpec

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

the class KubectlRolloutTest method testKubectlRolloutHistoryStatefulSetWithRevisionShouldWork.

@Test
public void testKubectlRolloutHistoryStatefulSetWithRevisionShouldWork() throws KubectlException, IOException {
    wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(STATEFUL_SET))))));
    wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(STATEFUL_SET_CONTROLLER_REVISION_LIST))))));
    wireMockRule.stubFor(patch(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(PATCHED_STATEFUL_SET))))));
    V1PodTemplateSpec template = Kubectl.rollout(V1StatefulSet.class).history().apiClient(apiClient).name("foo").namespace("default").revision(2).skipDiscovery().execute();
    wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))));
    wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions"))).withQueryParam("labelSelector", new EqualToPattern("app = bar")));
    wireMockRule.verify(1, patchRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))).withQueryParam("dryRun", new EqualToPattern("All")));
    Assert.assertNotNull(template);
}
Also used : V1StatefulSet(io.kubernetes.client.openapi.models.V1StatefulSet) EqualToPattern(com.github.tomakehurst.wiremock.matching.EqualToPattern) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) Test(org.junit.Test)

Aggregations

V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)12 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)6 Test (org.junit.Test)5 V1LabelSelector (io.kubernetes.client.openapi.models.V1LabelSelector)4 V1StatefulSet (io.kubernetes.client.openapi.models.V1StatefulSet)4 EqualToPattern (com.github.tomakehurst.wiremock.matching.EqualToPattern)3 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)3 V1Container (io.kubernetes.client.openapi.models.V1Container)3 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)3 V1StatefulSetSpec (io.kubernetes.client.openapi.models.V1StatefulSetSpec)3 V1Volume (io.kubernetes.client.openapi.models.V1Volume)3 IntOrString (io.kubernetes.client.custom.IntOrString)2 ApiException (io.kubernetes.client.openapi.ApiException)2 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)2 V1EmptyDirVolumeSource (io.kubernetes.client.openapi.models.V1EmptyDirVolumeSource)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 V1Patch (io.kubernetes.client.custom.V1Patch)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1