Search in sources :

Example 1 with ApiException

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

the class V1Controller method submit.

/**
 * Configures all components required by a <code>topology</code> and submits it to the Kubernetes scheduler.
 * @param packingPlan Used to configure the StatefulSets <code>Resource</code>s and replica count.
 * @return Success indicator.
 */
@Override
boolean submit(PackingPlan packingPlan) {
    final String topologyName = getTopologyName();
    if (!topologyName.equals(topologyName.toLowerCase())) {
        throw new TopologySubmissionException("K8S scheduler does not allow upper case topology's.");
    }
    final Resource containerResource = getContainerResource(packingPlan);
    final V1Service topologyService = createTopologyService();
    try {
        coreClient.createNamespacedService(getNamespace(), topologyService, null, null, null);
    } catch (ApiException e) {
        KubernetesUtils.logExceptionWithDetails(LOG, "Error creating topology service", e);
        throw new TopologySubmissionException(e.getMessage());
    }
    // Find the max number of instances in a container so that we can open
    // enough ports if remote debugging is enabled.
    int numberOfInstances = 0;
    for (PackingPlan.ContainerPlan containerPlan : packingPlan.getContainers()) {
        numberOfInstances = Math.max(numberOfInstances, containerPlan.getInstances().size());
    }
    final V1StatefulSet executors = createStatefulSet(containerResource, numberOfInstances, true);
    final V1StatefulSet manager = createStatefulSet(containerResource, numberOfInstances, false);
    try {
        appsClient.createNamespacedStatefulSet(getNamespace(), executors, null, null, null);
        appsClient.createNamespacedStatefulSet(getNamespace(), manager, null, null, null);
    } catch (ApiException e) {
        final String message = String.format("Error creating topology: %s%n", e.getResponseBody());
        KubernetesUtils.logExceptionWithDetails(LOG, message, e);
        throw new TopologySubmissionException(message);
    }
    return true;
}
Also used : TopologySubmissionException(org.apache.heron.scheduler.TopologySubmissionException) V1StatefulSet(io.kubernetes.client.openapi.models.V1StatefulSet) PackingPlan(org.apache.heron.spi.packing.PackingPlan) Resource(org.apache.heron.spi.packing.Resource) V1Service(io.kubernetes.client.openapi.models.V1Service) ApiException(io.kubernetes.client.openapi.ApiException)

Example 2 with ApiException

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

the class V1Controller method deleteService.

/**
 * Deletes the headless <code>Service</code> for a <code>topology</code>'s <code>Executors</code>
 * and <code>Manager</code> using the <code>topology</code>'s name.
 */
void deleteService() {
    try (Response response = coreClient.deleteNamespacedServiceCall(getTopologyName(), getNamespace(), null, null, 0, null, KubernetesConstants.DELETE_OPTIONS_PROPAGATION_POLICY, null, null).execute()) {
        if (!response.isSuccessful()) {
            if (response.code() == HTTP_NOT_FOUND) {
                LOG.log(Level.WARNING, "Deleting non-existent Kubernetes headless service for Topology: " + getTopologyName());
                return;
            }
            LOG.log(Level.SEVERE, "Error when deleting the Service of the job [" + getTopologyName() + "] in namespace [" + getNamespace() + "]");
            LOG.log(Level.SEVERE, "Error killing topology message:" + response.message());
            KubernetesUtils.logResponseBodyIfPresent(LOG, response);
            throw new TopologyRuntimeManagementException(KubernetesUtils.errorMessageFromResponse(response));
        }
    } catch (ApiException e) {
        if (e.getCode() == HTTP_NOT_FOUND) {
            LOG.log(Level.WARNING, "Tried to delete a non-existent Kubernetes service for Topology: " + getTopologyName());
            return;
        }
        throw new TopologyRuntimeManagementException("Error deleting topology [" + getTopologyName() + "] Kubernetes service", e);
    } catch (IOException e) {
        throw new TopologyRuntimeManagementException("Error deleting topology [" + getTopologyName() + "] Kubernetes service", e);
    }
    LOG.log(Level.INFO, "Headless Service for the Job [" + getTopologyName() + "] in namespace [" + getNamespace() + "] is deleted.");
}
Also used : Response(okhttp3.Response) TopologyRuntimeManagementException(org.apache.heron.scheduler.TopologyRuntimeManagementException) IOException(java.io.IOException) ApiException(io.kubernetes.client.openapi.ApiException)

Example 3 with ApiException

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

the class V1Controller method addContainers.

/**
 * Adds a specified number of Pods to a <code>topology</code>'s <code>Executors</code>.
 * @param containersToAdd Set of containers to be added.
 * @return The passed in <code>Packing Plan</code>.
 */
@Override
public Set<PackingPlan.ContainerPlan> addContainers(Set<PackingPlan.ContainerPlan> containersToAdd) {
    final V1StatefulSet statefulSet;
    try {
        statefulSet = getStatefulSet();
    } catch (ApiException ae) {
        final String message = ae.getMessage() + "\ndetails:" + ae.getResponseBody();
        throw new TopologyRuntimeManagementException(message, ae);
    }
    final V1StatefulSetSpec v1StatefulSet = Objects.requireNonNull(statefulSet.getSpec());
    final int currentContainerCount = Objects.requireNonNull(v1StatefulSet.getReplicas());
    final int newContainerCount = currentContainerCount + containersToAdd.size();
    try {
        patchStatefulSetReplicas(newContainerCount);
    } catch (ApiException ae) {
        throw new TopologyRuntimeManagementException(ae.getMessage() + "\ndetails\n" + ae.getResponseBody());
    }
    return containersToAdd;
}
Also used : TopologyRuntimeManagementException(org.apache.heron.scheduler.TopologyRuntimeManagementException) V1StatefulSet(io.kubernetes.client.openapi.models.V1StatefulSet) V1StatefulSetSpec(io.kubernetes.client.openapi.models.V1StatefulSetSpec) ApiException(io.kubernetes.client.openapi.ApiException)

Example 4 with ApiException

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

the class V1Controller method removePersistentVolumeClaims.

/**
 * Removes all Persistent Volume Claims associated with a specific topology, if they exist.
 * It looks for the following:
 * metadata:
 *   labels:
 *     topology: <code>topology-name</code>
 *     onDemand: <code>true</code>
 */
private void removePersistentVolumeClaims() {
    final String name = getTopologyName();
    final StringBuilder selectorLabel = new StringBuilder();
    // Generate selector label.
    for (Map.Entry<String, String> label : getPersistentVolumeClaimLabels(name).entrySet()) {
        if (selectorLabel.length() != 0) {
            selectorLabel.append(",");
        }
        selectorLabel.append(label.getKey()).append("=").append(label.getValue());
    }
    // Remove all dynamically backed Persistent Volume Claims.
    try {
        V1Status status = coreClient.deleteCollectionNamespacedPersistentVolumeClaim(getNamespace(), null, null, null, null, null, selectorLabel.toString(), null, null, null, null, null, null, null);
        LOG.log(Level.INFO, String.format("Removing automatically generated Persistent Volume Claims for `%s`:%n%s", name, status.getMessage()));
    } catch (ApiException e) {
        final String message = String.format("Failed to connect to K8s cluster to delete Persistent " + "Volume Claims for topology `%s`. A manual clean-up is required.%n%s", name, e.getMessage());
        LOG.log(Level.WARNING, message);
        throw new TopologyRuntimeManagementException(message);
    }
}
Also used : TopologyRuntimeManagementException(org.apache.heron.scheduler.TopologyRuntimeManagementException) V1Status(io.kubernetes.client.openapi.models.V1Status) Map(java.util.Map) HashMap(java.util.HashMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) ApiException(io.kubernetes.client.openapi.ApiException)

Example 5 with ApiException

use of io.kubernetes.client.openapi.ApiException in project pravega by pravega.

the class K8sClient method createNamespace.

/**
 * Method used to create a namespace. This blocks until the namespace is created.
 * @param namespace Namespace to be created.
 * @return V1Namespace.
 */
@SneakyThrows(ApiException.class)
public V1Namespace createNamespace(final String namespace) {
    CoreV1Api api = new CoreV1Api();
    try {
        V1Namespace existing = api.readNamespace(namespace, PRETTY_PRINT, Boolean.FALSE, Boolean.FALSE);
        if (existing != null) {
            log.info("Namespace {} already exists, ignoring namespace create operation.", namespace);
            return existing;
        }
    } catch (ApiException ignore) {
    // ignore exception and proceed with Namespace creation.
    }
    V1Namespace body = new V1Namespace();
    // Set the required api version and kind of resource
    body.setApiVersion("v1");
    body.setKind("Namespace");
    // Setup the standard object metadata
    V1ObjectMeta meta = new V1ObjectMeta();
    meta.setName(namespace);
    body.setMetadata(meta);
    return api.createNamespace(body, PRETTY_PRINT, DRY_RUN, FIELD_MANAGER);
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Namespace(io.kubernetes.client.openapi.models.V1Namespace) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ApiException(io.kubernetes.client.openapi.ApiException) SneakyThrows(lombok.SneakyThrows)

Aggregations

ApiException (io.kubernetes.client.openapi.ApiException)71 V1Pod (io.kubernetes.client.openapi.models.V1Pod)18 Test (org.junit.Test)18 IOException (java.io.IOException)17 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)15 V1PodList (io.kubernetes.client.openapi.models.V1PodList)15 KubectlException (io.kubernetes.client.extended.kubectl.exception.KubectlException)14 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)13 ApiClient (io.kubernetes.client.openapi.ApiClient)9 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)9 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 List (java.util.List)7 Watch (io.kubernetes.client.util.Watch)6 HashMap (java.util.HashMap)6 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)5 Configuration (io.kubernetes.client.openapi.Configuration)5 JSON (io.kubernetes.client.openapi.JSON)5