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