Search in sources :

Example 46 with ApiException

use of io.kubernetes.client.openapi.ApiException 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 47 with ApiException

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

the class LeaderElectingControllerTest method testLeaderElectingController.

@Test(timeout = 90000)
public void testLeaderElectingController() throws ApiException, InterruptedException {
    AtomicReference<LeaderElectionRecord> record = new AtomicReference<>();
    record.set(new LeaderElectionRecord());
    Semaphore latch = new Semaphore(2);
    Semaphore controllerLatch = new Semaphore(2);
    when(mockLock.identity()).thenReturn("foo");
    when(mockLock.get()).thenThrow(new ApiException("Record Not Found", HttpURLConnection.HTTP_NOT_FOUND, null, null)).thenReturn(record.get());
    doAnswer(invocationOnMock -> {
        record.set(invocationOnMock.getArgument(0));
        latch.release();
        return true;
    }).when(mockLock).create(any());
    doAnswer(invocationOnMock -> {
        latch.release();
        return false;
    }).when(mockLock).update(any());
    doAnswer(invocationOnMock -> {
        controllerLatch.release();
        return null;
    }).when(mockController).run();
    doAnswer(invocationOnMock -> {
        controllerLatch.release();
        return null;
    }).when(mockController).shutdown();
    LeaderElectingController leaderElectingController = new LeaderElectingController(new LeaderElector(new LeaderElectionConfig(mockLock, Duration.ofMillis(300), Duration.ofMillis(200), Duration.ofMillis(100))), mockController);
    latch.acquire(2);
    controllerLatch.acquire(2);
    Thread controllerThread = new Thread(leaderElectingController::run);
    controllerThread.start();
    latch.acquire(2);
    controllerThread.interrupt();
    verify(mockLock, times(1)).create(any());
    verify(mockLock, atLeastOnce()).update(any());
    controllerLatch.acquire(2);
    verify(mockController, times(1)).run();
    verify(mockController, times(1)).shutdown();
}
Also used : LeaderElectionRecord(io.kubernetes.client.extended.leaderelection.LeaderElectionRecord) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 48 with ApiException

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

the class KubectlGetTest method testGetDefaultNamespaceOnePodForbiddenShouldThrowException.

@Test
public void testGetDefaultNamespaceOnePodForbiddenShouldThrowException() {
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/default/pods/foo1")).willReturn(aResponse().withStatus(403).withBody(apiClient.getJSON().serialize(new V1Status().code(403)))));
    try {
        V1Pod getPod = Kubectl.get(V1Pod.class).apiClient(apiClient).skipDiscovery().namespace(// no namespace specified
        "default").name("foo1").execute();
    } catch (KubectlException e) {
        assertTrue(e.getCause() instanceof ApiException);
        return;
    } finally {
        wireMockRule.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo1")));
    }
    fail();
}
Also used : V1Status(io.kubernetes.client.openapi.models.V1Status) V1Pod(io.kubernetes.client.openapi.models.V1Pod) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 49 with ApiException

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

the class KubectlCreate method execute.

@Override
public ApiType execute() throws KubectlException {
    refreshDiscovery();
    GenericKubernetesApi<ApiType, KubernetesListObject> api = getGenericApi();
    if (ModelMapper.isNamespaced(this.targetObj.getClass())) {
        String targetNamespace = namespace != null ? namespace : Strings.isNullOrEmpty(targetObj.getMetadata().getNamespace()) ? Namespaces.NAMESPACE_DEFAULT : targetObj.getMetadata().getNamespace();
        try {
            return api.create(targetNamespace, targetObj, new CreateOptions()).throwsApiException().getObject();
        } catch (ApiException e) {
            throw new KubectlException(e);
        }
    } else {
        try {
            return api.create(targetObj, new CreateOptions()).throwsApiException().getObject();
        } catch (ApiException e) {
            throw new KubectlException(e);
        }
    }
}
Also used : KubernetesListObject(io.kubernetes.client.common.KubernetesListObject) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) CreateOptions(io.kubernetes.client.util.generic.options.CreateOptions) ApiException(io.kubernetes.client.openapi.ApiException)

Example 50 with ApiException

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

the class KubectlLog method execute.

@Override
public InputStream execute() throws KubectlException {
    validate();
    PodLogs logs = new PodLogs(apiClient);
    String ns = (this.namespace == null ? "default" : this.namespace);
    try {
        return logs.streamNamespacedPodLog(ns, this.name, this.container);
    } catch (ApiException | IOException ex) {
        throw new KubectlException(ex);
    }
}
Also used : PodLogs(io.kubernetes.client.PodLogs) IOException(java.io.IOException) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

ApiException (io.kubernetes.client.openapi.ApiException)82 V1Pod (io.kubernetes.client.openapi.models.V1Pod)21 IOException (java.io.IOException)21 V1PodList (io.kubernetes.client.openapi.models.V1PodList)18 Test (org.junit.Test)18 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)17 KubectlException (io.kubernetes.client.extended.kubectl.exception.KubectlException)14 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)13 ApiClient (io.kubernetes.client.openapi.ApiClient)10 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)10 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)8 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)8 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)8 V1Status (io.kubernetes.client.openapi.models.V1Status)8 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)8 Watch (io.kubernetes.client.util.Watch)8 List (java.util.List)8 Configuration (io.kubernetes.client.openapi.Configuration)6 HashMap (java.util.HashMap)6 V1Patch (io.kubernetes.client.custom.V1Patch)5