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());
}
}
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();
}
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");
}
}
}
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.");
}
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());
}
}
Aggregations