use of io.kubernetes.client.openapi.ApiException in project pravega by pravega.
the class K8sClient method downloadLogs.
/**
* Download logs of the specified pod.
*
* @param fromPod Pod logs to be copied.
* @param toFile Destination file of the logs.
* @return A Future which completes once the download operation completes.
*/
public CompletableFuture<Void> downloadLogs(final V1Pod fromPod, final String toFile) {
final AtomicInteger retryCount = new AtomicInteger(0);
return Retry.withExpBackoff(LOG_DOWNLOAD_INIT_DELAY_MS, 10, LOG_DOWNLOAD_RETRY_COUNT, RETRY_MAX_DELAY_MS).retryingOn(TestFrameworkException.class).throwingOn(RuntimeException.class).runInExecutor(() -> {
final String podName = fromPod.getMetadata().getName();
log.debug("Download logs from pod {}", podName);
try {
@Cleanup InputStream logStream = logUtility.streamNamespacedPodLog(fromPod);
// On every retry this method attempts to download the complete pod logs from from K8s api-server. Due to the
// amount of logs for a pod and the K8s cluster configuration it can so happen that the K8s api-server can
// return truncated logs. Hence, every retry attempt does not overwrite the previously downloaded logs for
// the pod.
String logFile = toFile + "-" + retryCount.incrementAndGet() + ".log";
Files.copy(logStream, Paths.get(logFile));
log.info("Logs downloaded from pod {} to {}", podName, logFile);
} catch (ApiException | IOException e) {
log.warn("Retryable error while downloading logs from pod {}. Error message: {} ", podName, e.getMessage());
throw new TestFrameworkException(TestFrameworkException.Type.RequestFailed, "Error while downloading logs");
}
}, executor);
}
use of io.kubernetes.client.openapi.ApiException in project pravega by pravega.
the class K8sClient method createAndUpdateCustomObject.
/**
* This is used to update a custom object. This is useful to modify the custom object configuration, number of
* instances is one type of configuration. If the object does not exist then a new object is created.
* @param customResourceGroup Custom resource group.
* @param version version.
* @param namespace Namespace.
* @param plural Plural of the CRD.
* @param request Actual request.
* @return A Future representing the status of create/update.
*/
@SuppressWarnings("unchecked")
public CompletableFuture<Object> createAndUpdateCustomObject(String customResourceGroup, String version, String namespace, String plural, Map<String, Object> request) {
CustomObjectsApi api = new CustomObjectsApi();
// Fetch the name of the custom object.
String name = ((Map<String, String>) request.get("metadata")).get("name");
return getCustomObject(customResourceGroup, version, namespace, plural, name).thenCompose(o -> {
log.info("Instance {} of custom resource {} exists, update it with the new request", name, customResourceGroup);
try {
// patch object
K8AsyncCallback<Object> cb1 = new K8AsyncCallback<>("patchCustomObject");
PatchUtils.patch(CustomObjectsApi.class, () -> api.patchNamespacedCustomObjectCall(customResourceGroup, version, namespace, plural, name, request, cb1), V1Patch.PATCH_FORMAT_JSON_MERGE_PATCH);
return cb1.getFuture();
} catch (ApiException e) {
throw Exceptions.sneakyThrow(e);
}
}).exceptionally(t -> {
log.warn("Exception while trying to fetch instance {} of custom resource {}, try to create it. Details: {}", name, customResourceGroup, t.getMessage());
try {
// create object
K8AsyncCallback<Object> cb = new K8AsyncCallback<>("createCustomObject");
api.createNamespacedCustomObjectAsync(customResourceGroup, version, namespace, plural, request, PRETTY_PRINT, cb);
return cb.getFuture();
} catch (ApiException e) {
throw Exceptions.sneakyThrow(e);
}
});
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class PatchExample method main.
public static void main(String[] args) throws IOException {
try {
AppsV1Api api = new AppsV1Api(ClientBuilder.standard().build());
V1Deployment body = Configuration.getDefaultApiClient().getJSON().deserialize(jsonDeploymentStr, V1Deployment.class);
// create a deployment
V1Deployment deploy1 = api.createNamespacedDeployment("default", body, null, null, null, null);
System.out.println("original deployment" + deploy1);
// json-patch a deployment
V1Deployment deploy2 = PatchUtils.patch(V1Deployment.class, () -> api.patchNamespacedDeploymentCall("hello-node", "default", new V1Patch(jsonPatchStr), null, null, null, // field-manager is optional
null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, api.getApiClient());
System.out.println("json-patched deployment" + deploy2);
// strategic-merge-patch a deployment
V1Deployment deploy3 = PatchUtils.patch(V1Deployment.class, () -> api.patchNamespacedDeploymentCall("hello-node", "default", new V1Patch(strategicMergePatchStr), null, null, // field-manager is optional
null, null, null, null), V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, api.getApiClient());
System.out.println("strategic-merge-patched deployment" + deploy3);
// apply-yaml a deployment, server side apply is available by default after kubernetes v1.16
// or opt-in by turning on the feature gate for v1.14 or v1.15.
// https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply
V1Deployment deploy4 = PatchUtils.patch(V1Deployment.class, () -> api.patchNamespacedDeploymentCall("hello-node", "default", new V1Patch(applyYamlStr), null, null, // field-manager is required for server-side apply
"example-field-manager", null, true, null), V1Patch.PATCH_FORMAT_APPLY_YAML, api.getApiClient());
System.out.println("application/apply-patch+yaml deployment" + deploy4);
} catch (ApiException e) {
System.out.println(e.getResponseBody());
e.printStackTrace();
}
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class KubectlAnnotate method execute.
@Override
public ApiType execute() throws KubectlException {
verifyArguments();
refreshDiscovery();
final ApiType currentObj;
if (isNamespaced(apiTypeClass)) {
try {
currentObj = getGenericApi().get(namespace, name).throwsApiException().getObject();
} catch (ApiException e) {
throw new KubectlException(e);
}
} else {
try {
currentObj = getGenericApi().get(name).throwsApiException().getObject();
} catch (ApiException e) {
throw new KubectlException(e);
}
}
Annotations.addAnnotations(currentObj, addingAnnotations);
final KubernetesApiResponse<ApiType> updateResponse;
try {
return getGenericApi().update(currentObj).throwsApiException().getObject();
} catch (ApiException e) {
throw new KubectlException(e);
}
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class LeaderElectorTest method deleteConfigMapLockResource.
private void deleteConfigMapLockResource() throws Exception {
try {
CoreV1Api coreV1Api = new CoreV1Api(apiClient);
coreV1Api.deleteNamespacedConfigMap(LOCK_RESOURCE_NAME, NAMESPACE, null, null, null, null, null, null);
} catch (ApiException ex) {
if (ex.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
throw ex;
}
}
}
Aggregations