Search in sources :

Example 16 with V1ConfigMap

use of io.kubernetes.client.openapi.models.V1ConfigMap in project pravega by pravega.

the class K8sClient method createConfigMap.

/**
 * Create ConfigMap.
 * @param namespace The namespace where the ConfigMap should be created.
 * @param binding The cluster ConfigMap.
 * @return A future indicating the status of the ConfigMap operation.
 */
@SneakyThrows(ApiException.class)
public CompletableFuture<V1ConfigMap> createConfigMap(String namespace, V1ConfigMap binding) {
    CoreV1Api api = new CoreV1Api();
    K8AsyncCallback<V1ConfigMap> callback = new K8AsyncCallback<>("createConfigMap-" + binding.getMetadata().getName());
    api.createNamespacedConfigMapAsync(namespace, binding, PRETTY_PRINT, DRY_RUN, FIELD_MANAGER, callback);
    return exceptionallyExpecting(callback.getFuture(), isConflict, null);
}
Also used : CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) SneakyThrows(lombok.SneakyThrows)

Example 17 with V1ConfigMap

use of io.kubernetes.client.openapi.models.V1ConfigMap in project pravega by pravega.

the class K8sClient method getConfigMap.

/**
 * Method to get V1ConfigMap.
 * @param name Name of the ConfigMap.
 * @param namespace Namespace on which the pod(s) reside.
 * @return Future representing the V1ConfigMap.
 */
@SneakyThrows(ApiException.class)
public CompletableFuture<V1ConfigMap> getConfigMap(String name, String namespace) {
    CoreV1Api api = new CoreV1Api();
    K8AsyncCallback<V1ConfigMap> callback = new K8AsyncCallback<>("readNamespacedConfigMap-" + name);
    api.readNamespacedConfigMapAsync(name, namespace, PRETTY_PRINT, false, false, callback);
    return callback.getFuture();
}
Also used : CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) SneakyThrows(lombok.SneakyThrows)

Example 18 with V1ConfigMap

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

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

the class V1ControllerTest method testLoadPodFromTemplateBadTargetConfigMap.

@Test
public void testLoadPodFromTemplateBadTargetConfigMap() {
    // ConfigMap with target ConfigMap and an invalid Pod Template.
    final V1ConfigMap configMapInvalidPod = new V1ConfigMapBuilder().withNewMetadata().withName(CONFIGMAP_NAME).endMetadata().addToData(POD_TEMPLATE_NAME, "Dummy Value").build();
    // ConfigMap with target ConfigMaps and an empty Pod Template.
    final V1ConfigMap configMapEmptyPod = new V1ConfigMapBuilder().withNewMetadata().withName(CONFIGMAP_NAME).endMetadata().addToData(POD_TEMPLATE_NAME, "").build();
    // Test case container.
    // Input: ConfigMap to setup mock V1Controller, Boolean flag for executor/manager switch.
    // Output: The expected error message.
    final List<TestTuple<Pair<V1ConfigMap, Boolean>, String>> testCases = new LinkedList<>();
    testCases.add(new TestTuple<>("Executor invalid Pod Template", new Pair<>(configMapInvalidPod, true), "Error parsing"));
    testCases.add(new TestTuple<>("Manager invalid Pod Template", new Pair<>(configMapInvalidPod, false), "Error parsing"));
    testCases.add(new TestTuple<>("Executor empty Pod Template", new Pair<>(configMapEmptyPod, true), "Error parsing"));
    testCases.add(new TestTuple<>("Manager empty Pod Template", new Pair<>(configMapEmptyPod, false), "Error parsing"));
    // Test loop.
    for (TestTuple<Pair<V1ConfigMap, Boolean>, String> testCase : testCases) {
        doReturn(testCase.input.first).when(v1ControllerWithPodTemplate).getConfigMap(anyString());
        String message = "";
        try {
            v1ControllerWithPodTemplate.loadPodFromTemplate(testCase.input.second);
        } catch (TopologySubmissionException e) {
            message = e.getMessage();
        }
        Assert.assertTrue(testCase.description, message.contains(testCase.expected));
    }
}
Also used : V1ConfigMapBuilder(io.kubernetes.client.openapi.models.V1ConfigMapBuilder) TopologySubmissionException(org.apache.heron.scheduler.TopologySubmissionException) TestTuple(org.apache.heron.scheduler.kubernetes.KubernetesUtils.TestTuple) Matchers.anyString(org.mockito.Matchers.anyString) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) LinkedList(java.util.LinkedList) Pair(org.apache.heron.common.basics.Pair) Test(org.junit.Test)

Example 20 with V1ConfigMap

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

the class V1ControllerTest method testLoadPodFromTemplateNoConfigMap.

@Test
public void testLoadPodFromTemplateNoConfigMap() {
    final List<TestTuple<Boolean, String>> testCases = new LinkedList<>();
    testCases.add(new TestTuple<>("Executor no ConfigMap", true, "Failed to locate Pod Template"));
    testCases.add(new TestTuple<>("Manager no ConfigMap", false, "Failed to locate Pod Template"));
    for (TestTuple<Boolean, String> testCase : testCases) {
        doReturn(new V1ConfigMap()).when(v1ControllerWithPodTemplate).getConfigMap(anyString());
        String message = "";
        try {
            v1ControllerWithPodTemplate.loadPodFromTemplate(testCase.input);
        } catch (TopologySubmissionException e) {
            message = e.getMessage();
        }
        Assert.assertTrue(testCase.description, message.contains(testCase.expected));
    }
}
Also used : TopologySubmissionException(org.apache.heron.scheduler.TopologySubmissionException) TestTuple(org.apache.heron.scheduler.kubernetes.KubernetesUtils.TestTuple) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) Test(org.junit.Test)

Aggregations

V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)35 Test (org.junit.Test)11 Type (java.lang.reflect.Type)8 V1ConfigMapBuilder (io.kubernetes.client.openapi.models.V1ConfigMapBuilder)6 TopologySubmissionException (org.apache.heron.scheduler.TopologySubmissionException)6 Matchers.anyString (org.mockito.Matchers.anyString)6 ApiException (io.kubernetes.client.openapi.ApiException)5 LinkedList (java.util.LinkedList)5 TestTuple (org.apache.heron.scheduler.kubernetes.KubernetesUtils.TestTuple)5 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)4 JobConfig (edu.iu.dsc.tws.api.JobConfig)3 Config (edu.iu.dsc.tws.api.config.Config)3 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)3 HashMap (java.util.HashMap)3 Pair (org.apache.heron.common.basics.Pair)3 EqualToPattern (com.github.tomakehurst.wiremock.matching.EqualToPattern)2 Twister2RuntimeException (edu.iu.dsc.tws.api.exceptions.Twister2RuntimeException)2 KubernetesController (edu.iu.dsc.tws.rsched.schedulers.k8s.KubernetesController)2 JSON (io.kubernetes.client.openapi.JSON)2 V1ConfigMapList (io.kubernetes.client.openapi.models.V1ConfigMapList)2