Search in sources :

Example 11 with ApiException

use of io.kubernetes.client.ApiException in project incubator-heron by apache.

the class AppsV1beta1Controller method removeContainers.

@Override
public void removeContainers(Set<PackingPlan.ContainerPlan> containersToRemove) {
    final V1beta1StatefulSet statefulSet;
    try {
        statefulSet = getStatefulSet();
    } catch (ApiException ae) {
        final String message = ae.getMessage() + "\ndetails:" + ae.getResponseBody();
        throw new TopologyRuntimeManagementException(message, ae);
    }
    final int currentContainerCount = statefulSet.getSpec().getReplicas();
    final int newContainerCount = currentContainerCount - containersToRemove.size();
    final V1beta1StatefulSetSpec newSpec = new V1beta1StatefulSetSpec();
    newSpec.setReplicas(newContainerCount);
    try {
        doPatch(newSpec);
    } catch (ApiException e) {
        throw new TopologyRuntimeManagementException(e.getMessage() + "\ndetails\n" + e.getResponseBody());
    }
}
Also used : TopologyRuntimeManagementException(com.twitter.heron.scheduler.TopologyRuntimeManagementException) V1beta1StatefulSet(io.kubernetes.client.models.V1beta1StatefulSet) V1beta1StatefulSetSpec(io.kubernetes.client.models.V1beta1StatefulSetSpec) ApiException(io.kubernetes.client.ApiException)

Example 12 with ApiException

use of io.kubernetes.client.ApiException in project incubator-heron by apache.

the class KubernetesCompat method killTopology.

boolean killTopology(String kubernetesUri, String topology, String namespace) {
    final CoreV1Api client = new CoreV1Api(new ApiClient().setBasePath(kubernetesUri));
    // old version deployed topologies as naked pods
    try {
        final String labelSelector = KubernetesConstants.LABEL_TOPOLOGY + "=" + topology;
        final Response response = client.deleteCollectionNamespacedPodCall(namespace, null, null, null, null, labelSelector, null, null, null, null, null, null).execute();
        if (!response.isSuccessful()) {
            LOG.log(Level.SEVERE, "Error killing topology message: " + response.message());
            KubernetesUtils.logResponseBodyIfPresent(LOG, response);
            throw new TopologyRuntimeManagementException(KubernetesUtils.errorMessageFromResponse(response));
        }
    } catch (IOException | ApiException e) {
        LOG.log(Level.SEVERE, "Error killing topology " + e.getMessage());
        if (e instanceof ApiException) {
            LOG.log(Level.SEVERE, "Error details:\n" + ((ApiException) e).getResponseBody());
        }
        return false;
    }
    return true;
}
Also used : Response(com.squareup.okhttp.Response) TopologyRuntimeManagementException(com.twitter.heron.scheduler.TopologyRuntimeManagementException) IOException(java.io.IOException) ApiClient(io.kubernetes.client.ApiClient) CoreV1Api(io.kubernetes.client.apis.CoreV1Api) ApiException(io.kubernetes.client.ApiException)

Example 13 with ApiException

use of io.kubernetes.client.ApiException in project seldon-core by SeldonIO.

the class SeldonDeploymentControllerImpl method createOrReplaceSeldonDeployment.

@Override
public void createOrReplaceSeldonDeployment(SeldonDeployment mlDep) {
    if (mlDep.hasStatus() && mlDep.getStatus().hasState() && mlDep.getStatus().getState().equals(FAILED_STATE_MSG)) {
        logger.warn("Ignoring failed deployment " + mlDep.getMetadata().getName());
        return;
    }
    try {
        SeldonDeployment existing = mlCache.get(mlDep.getMetadata().getName());
        if (existing == null || !existing.getSpec().equals(mlDep.getSpec())) {
            logger.debug("Running updates for " + mlDep.getMetadata().getName());
            SeldonDeployment mlDep2 = operator.defaulting(mlDep);
            operator.validate(mlDep2);
            mlCache.put(mlDep2);
            DeploymentResources resources = operator.createResources(mlDep2);
            ProtoClient client = clientProvider.getProtoClient();
            String namespace = getNamespace(mlDep2);
            createDeployments(client, namespace, resources.deployments);
            removeDeployments(client, namespace, mlDep2, resources.deployments);
            createService(client, namespace, resources.service);
            if (!mlDep.getSpec().equals(mlDep2.getSpec())) {
                logger.debug("Pushing updated SeldonDeployment " + mlDep2.getMetadata().getName() + " back to kubectl");
                crdHandler.updateSeldonDeployment(mlDep2);
            } else
                logger.debug("Not pushing an update as no change to spec for SeldonDeployment " + mlDep2.getMetadata().getName());
        } else {
            mlCache.put(mlDep);
            logger.debug("Only updated cache for " + mlDep.getMetadata().getName());
        }
    } catch (SeldonDeploymentException e) {
        logger.error("Failed to create deployment ", e);
        failDeployment(mlDep, e);
    } catch (ApiException e) {
        logger.error("Kubernetes API exception deploying code:" + e.getCode() + "message:" + e.getResponseBody(), e);
        failDeployment(mlDep, e);
    } catch (IOException e) {
        logger.error("IOException during createReplace ", e);
        failDeployment(mlDep, e);
    }
}
Also used : IOException(java.io.IOException) SeldonDeployment(io.seldon.protos.DeploymentProtos.SeldonDeployment) DeploymentResources(io.seldon.clustermanager.k8s.SeldonDeploymentOperatorImpl.DeploymentResources) ProtoClient(io.kubernetes.client.ProtoClient) ApiException(io.kubernetes.client.ApiException)

Example 14 with ApiException

use of io.kubernetes.client.ApiException in project seldon-core by SeldonIO.

the class KubeCRDHandlerImpl method updateSeldonDeployment.

@Override
public void updateSeldonDeployment(SeldonDeployment mldep) {
    try {
        // Need to remove resourceVersion from the representation used for last-applied-configuration otherwise you will errors subsequently using kubectl
        SeldonDeployment mlDepTmp = SeldonDeployment.newBuilder(mldep).setMetadata(ObjectMeta.newBuilder(mldep.getMetadata()).clearResourceVersion().removeAnnotations("kubectl.kubernetes.io/last-applied-configuration").build()).build();
        // Create string representation of JSON to add as annotation to allow declarative "kubectl apply" commands to work otherwise a replace
        // would remove the last-applied-configuration that kubectl adds.
        String json = SeldonDeploymentUtils.toJson(mlDepTmp, true);
        // Create final version of deployment with annotation
        SeldonDeployment mlDeployment = SeldonDeployment.newBuilder(mldep).setMetadata(ObjectMeta.newBuilder(mldep.getMetadata()).putAnnotations("kubectl.kubernetes.io/last-applied-configuration", json + "\n")).build();
        json = SeldonDeploymentUtils.toJson(mlDeployment, false);
        logger.debug("Updating seldondeployment " + mlDeployment.getMetadata().getName());
        ApiClient client = Config.defaultClient();
        CustomObjectsApi api = new CustomObjectsApi(client);
        api.replaceNamespacedCustomObject(GROUP, VERSION, namespace, KIND_PLURAL, mlDeployment.getMetadata().getName(), json.getBytes());
    } catch (InvalidProtocolBufferException e) {
        logger.error("Failed to update deployment in kubernetes ", e);
    } catch (ApiException e) {
        logger.error("Failed to update deployment in kubernetes ", e);
    } catch (IOException e) {
        logger.error("Failed to get client ", e);
    } finally {
    }
}
Also used : CustomObjectsApi(io.kubernetes.client.apis.CustomObjectsApi) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) ApiClient(io.kubernetes.client.ApiClient) SeldonDeployment(io.seldon.protos.DeploymentProtos.SeldonDeployment) ApiException(io.kubernetes.client.ApiException)

Example 15 with ApiException

use of io.kubernetes.client.ApiException in project seldon-core by SeldonIO.

the class KubeCRDHandlerImpl method getSeldonDeployment.

@Override
public SeldonDeployment getSeldonDeployment(String name) {
    try {
        ApiClient client = Config.defaultClient();
        CustomObjectsApi api = new CustomObjectsApi(client);
        Object resp = api.getNamespacedCustomObject(GROUP, VERSION, namespace, KIND_PLURAL, name);
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(resp);
        try {
            return SeldonDeploymentUtils.jsonToSeldonDeployment(json);
        } catch (InvalidProtocolBufferException e) {
            logger.error("Failed to parse " + json, e);
            return null;
        }
    } catch (ApiException e) {
        return null;
    } catch (IOException e) {
        logger.error("Failed to get client", e);
        return null;
    }
}
Also used : CustomObjectsApi(io.kubernetes.client.apis.CustomObjectsApi) GsonBuilder(com.google.gson.GsonBuilder) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Gson(com.google.gson.Gson) IOException(java.io.IOException) ApiClient(io.kubernetes.client.ApiClient) ApiException(io.kubernetes.client.ApiException)

Aggregations

ApiException (io.kubernetes.client.ApiException)29 IOException (java.io.IOException)11 CallBuilderFactory (oracle.kubernetes.operator.helpers.CallBuilderFactory)9 V1ObjectMeta (io.kubernetes.client.models.V1ObjectMeta)7 Response (com.squareup.okhttp.Response)5 ApiClient (io.kubernetes.client.ApiClient)5 TopologyRuntimeManagementException (com.twitter.heron.scheduler.TopologyRuntimeManagementException)4 V1DeleteOptions (io.kubernetes.client.models.V1DeleteOptions)3 V1Secret (io.kubernetes.client.models.V1Secret)3 V1beta1StatefulSet (io.kubernetes.client.models.V1beta1StatefulSet)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 JSON (io.kubernetes.client.JSON)2 CustomObjectsApi (io.kubernetes.client.apis.CustomObjectsApi)2 V1Namespace (io.kubernetes.client.models.V1Namespace)2 V1Pod (io.kubernetes.client.models.V1Pod)2 V1PodList (io.kubernetes.client.models.V1PodList)2 V1ServiceAccount (io.kubernetes.client.models.V1ServiceAccount)2 V1ServiceAccountList (io.kubernetes.client.models.V1ServiceAccountList)2