Search in sources :

Example 41 with ApiException

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

the class PodWatchUtils method testGetPodList.

/**
 * a test method to see whether kubernetes java client can connect to kubernetes master
 * and get the pod list
 */
public static void testGetPodList(String namespace) {
    if (apiClient == null || coreApi == null) {
        createApiInstances();
    }
    LOG.info("Getting the pod list for the namespace: " + namespace);
    V1PodList list = null;
    try {
        list = coreApi.listNamespacedPod(namespace, null, null, null, null, null, null, null, null, null);
    } catch (ApiException e) {
        String logMessage = "Exception when getting the pod list: \n" + "exCode: " + e.getCode() + "\n" + "responseBody: " + e.getResponseBody();
        LOG.log(Level.SEVERE, logMessage, e);
        throw new RuntimeException(e);
    }
    LOG.info("Number of pods in the received list: " + list.getItems().size());
    for (V1Pod item : list.getItems()) {
        LOG.info(item.getMetadata().getName());
    }
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiException(io.kubernetes.client.openapi.ApiException)

Example 42 with ApiException

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

the class JobLogger method watchPodsToRunningStartLoggers.

/**
 * watch job pods until they become Running and start loggers for each container afterward
 */
private void watchPodsToRunningStartLoggers() {
    String jobPodsLabel = KubernetesUtils.jobLabelSelector(job.getJobId());
    Integer timeoutSeconds = Integer.MAX_VALUE;
    try {
        watcher = Watch.createWatch(KubernetesController.getApiClient(), v1Api.listNamespacedPodCall(namespace, null, null, null, null, jobPodsLabel, null, null, timeoutSeconds, Boolean.TRUE, null), new TypeToken<Watch.Response<V1Pod>>() {
        }.getType());
    } catch (ApiException e) {
        String logMessage = "Exception when watching the pods to get the IPs: \n" + "exCode: " + e.getCode() + "\n" + "responseBody: " + e.getResponseBody();
        LOG.log(Level.SEVERE, logMessage, e);
        throw new RuntimeException(e);
    }
    // we catch this exception and ignore it.
    try {
        for (Watch.Response<V1Pod> item : watcher) {
            if (stopLogger) {
                break;
            }
            // it means that the pod is in the process of being deleted
            if (item.object != null && item.object.getMetadata().getName().startsWith(job.getJobId()) && KubernetesUtils.isPodRunning(item.object)) {
                String podName = item.object.getMetadata().getName();
                String podIP = item.object.getStatus().getPodIP();
                List<V1Container> containers = item.object.getSpec().getContainers();
                if (podName.endsWith("-jm-0")) {
                    String contName = containers.get(0).getName();
                    String id = "job-master-ip" + podIP;
                    WorkerLogger workerLogger = new WorkerLogger(namespace, podName, contName, id, logsDir, v1Api, this);
                    startWorkerLogger(workerLogger);
                    continue;
                }
                for (V1Container container : containers) {
                    int wID = K8sWorkerUtils.calculateWorkerID(job, podName, container.getName());
                    // this means job is scaled up
                    if (wID >= numberOfWorkers) {
                        numberOfWorkers = wID + 1;
                    }
                    String id = "worker" + wID + "-ip" + podIP;
                    WorkerLogger workerLogger = new WorkerLogger(namespace, podName, container.getName(), id, logsDir, v1Api, this);
                    startWorkerLogger(workerLogger);
                }
            }
        }
    } catch (RuntimeException e) {
        if (stopLogger) {
            LOG.fine("JobLogger is stopped.");
            return;
        } else {
            throw e;
        }
    }
    closeWatcher();
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) Watch(io.kubernetes.client.util.Watch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiException(io.kubernetes.client.openapi.ApiException)

Example 43 with ApiException

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

the class JobKillWatcher method run.

/**
 * start the watcher
 */
@Override
public void run() {
    String killParam = "KILL_JOB";
    String cmName = jobID;
    String labelSelector = KubernetesUtils.jobLabelSelector(jobID);
    Integer timeoutSeconds = Integer.MAX_VALUE;
    CoreV1Api v1Api = controller.createCoreV1Api();
    try {
        watcher = Watch.createWatch(controller.getApiClient(), v1Api.listNamespacedConfigMapCall(namespace, null, null, null, null, labelSelector, null, null, timeoutSeconds, Boolean.TRUE, null), new TypeToken<Watch.Response<V1ConfigMap>>() {
        }.getType());
    } catch (ApiException e) {
        String logMessage = "Exception when watching the ConfigMap: \n" + "exCode: " + e.getCode() + "\n" + "responseBody: " + e.getResponseBody();
        LOG.log(Level.SEVERE, logMessage, e);
        throw new RuntimeException(e);
    }
    try {
        for (Watch.Response<V1ConfigMap> item : watcher) {
            // it means that the pod is in the process of being deleted
            if (item.object != null && item.object.getData() != null && item.object.getMetadata().getName().equals(cmName) && item.object.getData().get(killParam) != null) {
                LOG.info("Job Kill parameter received. Killing the job");
                jobMaster.endJob(JobAPI.JobState.KILLED);
                return;
            }
        }
    } catch (RuntimeException e) {
        if (stopWatcher) {
            LOG.fine("Watcher is stopped.");
            return;
        } else {
            throw e;
        }
    } finally {
        try {
            watcher.close();
        } catch (IOException e) {
            LOG.warning("IOException when closing ConfigMapWatcher");
        }
    }
}
Also used : Watch(io.kubernetes.client.util.Watch) IOException(java.io.IOException) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) ApiException(io.kubernetes.client.openapi.ApiException)

Example 44 with ApiException

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

the class V1Controller method deleteStatefulSets.

/**
 * Deletes the StatefulSets for a <code>topology</code>'s <code>Executors</code> and <code>Manager</code>
 * using <code>Label</code>s.
 */
void deleteStatefulSets() {
    try (Response response = appsClient.deleteCollectionNamespacedStatefulSetCall(getNamespace(), null, null, null, null, null, createTopologySelectorLabels(), null, null, null, null, null, null, null, null).execute()) {
        if (!response.isSuccessful()) {
            if (response.code() == HTTP_NOT_FOUND) {
                LOG.log(Level.WARNING, "Tried to delete a non-existent StatefulSets for Topology: " + getTopologyName());
                return;
            }
            LOG.log(Level.SEVERE, "Error when deleting the StatefulSets 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 StatefulSet for Topology: " + getTopologyName());
            return;
        }
        throw new TopologyRuntimeManagementException("Error deleting topology [" + getTopologyName() + "] Kubernetes StatefulSets", e);
    } catch (IOException e) {
        throw new TopologyRuntimeManagementException("Error deleting topology [" + getTopologyName() + "] Kubernetes StatefulSets", e);
    }
    LOG.log(Level.INFO, "StatefulSet 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 45 with ApiException

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

the class V1Controller method removeContainers.

/**
 * Removes a specified number of Pods from a <code>topology</code>'s <code>Executors</code>.
 * @param containersToRemove Set of containers to be removed.
 */
@Override
public void removeContainers(Set<PackingPlan.ContainerPlan> containersToRemove) {
    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 - containersToRemove.size();
    try {
        patchStatefulSetReplicas(newContainerCount);
    } catch (ApiException e) {
        throw new TopologyRuntimeManagementException(e.getMessage() + "\ndetails\n" + e.getResponseBody());
    }
}
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)

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