Search in sources :

Example 1 with CustomObjectsApi

use of io.kubernetes.client.openapi.apis.CustomObjectsApi in project pravega by pravega.

the class K8sClient method deleteCustomObject.

/**
 * Delete Custom Object for a given resource group.
 * @param customResourceGroup Custom resource group.
 * @param version Version.
 * @param namespace Namespace.
 * @param plural Plural of the CRD.
 * @param name Name of the object.
 * @return Future which completes once the delete request is accepted.
 */
@SneakyThrows(ApiException.class)
public CompletableFuture<Object> deleteCustomObject(String customResourceGroup, String version, String namespace, String plural, String name) {
    CustomObjectsApi api = new CustomObjectsApi();
    V1DeleteOptions options = new V1DeleteOptions();
    options.setOrphanDependents(false);
    K8AsyncCallback<Object> callback = new K8AsyncCallback<>("getCustomObject-" + customResourceGroup);
    api.deleteNamespacedCustomObjectAsync(customResourceGroup, version, namespace, plural, name, 0, false, null, options, callback);
    return callback.getFuture();
}
Also used : V1DeleteOptions(io.kubernetes.client.openapi.models.V1DeleteOptions) CustomObjectsApi(io.kubernetes.client.openapi.apis.CustomObjectsApi) SneakyThrows(lombok.SneakyThrows)

Example 2 with CustomObjectsApi

use of io.kubernetes.client.openapi.apis.CustomObjectsApi 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);
        }
    });
}
Also used : PodLogs(io.kubernetes.client.PodLogs) TypeToken(com.google.gson.reflect.TypeToken) SneakyThrows(lombok.SneakyThrows) Retry(io.pravega.common.util.Retry) V1beta1CustomResourceDefinition(io.kubernetes.client.openapi.models.V1beta1CustomResourceDefinition) Cleanup(lombok.Cleanup) V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1PodStatus(io.kubernetes.client.openapi.models.V1PodStatus) TestFrameworkException(io.pravega.test.system.framework.TestFrameworkException) ApiextensionsV1beta1Api(io.kubernetes.client.openapi.apis.ApiextensionsV1beta1Api) Futures.exceptionallyExpecting(io.pravega.common.concurrent.Futures.exceptionallyExpecting) V1beta1ClusterRole(io.kubernetes.client.openapi.models.V1beta1ClusterRole) V1Patch(io.kubernetes.client.custom.V1Patch) Configuration(io.kubernetes.client.openapi.Configuration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) V1beta1ClusterRoleBinding(io.kubernetes.client.openapi.models.V1beta1ClusterRoleBinding) V1ServiceAccount(io.kubernetes.client.openapi.models.V1ServiceAccount) V1DeleteOptions(io.kubernetes.client.openapi.models.V1DeleteOptions) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) V1Secret(io.kubernetes.client.openapi.models.V1Secret) CONFLICT(javax.ws.rs.core.Response.Status.CONFLICT) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) RbacAuthorizationV1beta1Api(io.kubernetes.client.openapi.apis.RbacAuthorizationV1beta1Api) V1Status(io.kubernetes.client.openapi.models.V1Status) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Optional(java.util.Optional) ApiCallback(io.kubernetes.client.openapi.ApiCallback) PatchUtils(io.kubernetes.client.util.PatchUtils) Futures(io.pravega.common.concurrent.Futures) V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) Exceptions(io.pravega.common.Exceptions) Watch(io.kubernetes.client.util.Watch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) ApiClient(io.kubernetes.client.openapi.ApiClient) ApiException(io.kubernetes.client.openapi.ApiException) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) V1ContainerStateTerminated(io.kubernetes.client.openapi.models.V1ContainerStateTerminated) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) V1ContainerStatus(io.kubernetes.client.openapi.models.V1ContainerStatus) V1Namespace(io.kubernetes.client.openapi.models.V1Namespace) V1beta1Role(io.kubernetes.client.openapi.models.V1beta1Role) JsonSyntaxException(com.google.gson.JsonSyntaxException) Files(java.nio.file.Files) CustomObjectsApi(io.kubernetes.client.openapi.apis.CustomObjectsApi) IOException(java.io.IOException) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Config(io.kubernetes.client.util.Config) TimeUnit(java.util.concurrent.TimeUnit) ConnectionFailed(io.pravega.test.system.framework.TestFrameworkException.Type.ConnectionFailed) Paths(java.nio.file.Paths) V1ContainerState(io.kubernetes.client.openapi.models.V1ContainerState) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) V1beta1RoleBinding(io.kubernetes.client.openapi.models.V1beta1RoleBinding) SECONDS(java.util.concurrent.TimeUnit.SECONDS) V1Pod(io.kubernetes.client.openapi.models.V1Pod) InputStream(java.io.InputStream) CustomObjectsApi(io.kubernetes.client.openapi.apis.CustomObjectsApi) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) ApiException(io.kubernetes.client.openapi.ApiException)

Example 3 with CustomObjectsApi

use of io.kubernetes.client.openapi.apis.CustomObjectsApi in project pravega by pravega.

the class K8sClient method createCustomObject.

/**
 * Create a Custom object for a Custom Resource Definition (CRD). This is useful while interacting with operators.
 * @param customResourceGroup Custom resource group.
 * @param version Version.
 * @param namespace Namespace.
 * @param plural plural of the CRD.
 * @param request Actual request.
 * @return Future representing the custom object creation.
 */
@SneakyThrows(ApiException.class)
public CompletableFuture<Object> createCustomObject(String customResourceGroup, String version, String namespace, String plural, Map<String, Object> request) {
    CustomObjectsApi api = new CustomObjectsApi();
    K8AsyncCallback<Object> callback = new K8AsyncCallback<>("createCustomObject-" + customResourceGroup);
    api.createNamespacedCustomObjectAsync(customResourceGroup, version, namespace, plural, request, PRETTY_PRINT, callback);
    return callback.getFuture();
}
Also used : CustomObjectsApi(io.kubernetes.client.openapi.apis.CustomObjectsApi) SneakyThrows(lombok.SneakyThrows)

Example 4 with CustomObjectsApi

use of io.kubernetes.client.openapi.apis.CustomObjectsApi in project pravega by pravega.

the class K8sClient method getCustomObject.

/**
 * Fetch Custom Object for a given custom resource group.
 * @param customResourceGroup Custom resource group.
 * @param version Version.
 * @param namespace Namespace.
 * @param plural Plural of the CRD.
 * @param name Name of the object.
 * @return A future which returns the details of the object. The future completes exceptionally if the object is not present.
 */
@SneakyThrows(ApiException.class)
public CompletableFuture<Object> getCustomObject(String customResourceGroup, String version, String namespace, String plural, String name) {
    CustomObjectsApi api = new CustomObjectsApi();
    K8AsyncCallback<Object> callback = new K8AsyncCallback<>("getCustomObject-" + customResourceGroup);
    api.getNamespacedCustomObjectAsync(customResourceGroup, version, namespace, plural, name, callback);
    return callback.getFuture();
}
Also used : CustomObjectsApi(io.kubernetes.client.openapi.apis.CustomObjectsApi) SneakyThrows(lombok.SneakyThrows)

Aggregations

CustomObjectsApi (io.kubernetes.client.openapi.apis.CustomObjectsApi)4 SneakyThrows (lombok.SneakyThrows)4 V1DeleteOptions (io.kubernetes.client.openapi.models.V1DeleteOptions)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 TypeToken (com.google.gson.reflect.TypeToken)1 PodLogs (io.kubernetes.client.PodLogs)1 V1Patch (io.kubernetes.client.custom.V1Patch)1 ApiCallback (io.kubernetes.client.openapi.ApiCallback)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 ApiException (io.kubernetes.client.openapi.ApiException)1 Configuration (io.kubernetes.client.openapi.Configuration)1 ApiextensionsV1beta1Api (io.kubernetes.client.openapi.apis.ApiextensionsV1beta1Api)1 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)1 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)1 RbacAuthorizationV1beta1Api (io.kubernetes.client.openapi.apis.RbacAuthorizationV1beta1Api)1 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)1 V1ContainerState (io.kubernetes.client.openapi.models.V1ContainerState)1 V1ContainerStateTerminated (io.kubernetes.client.openapi.models.V1ContainerStateTerminated)1 V1ContainerStatus (io.kubernetes.client.openapi.models.V1ContainerStatus)1