Search in sources :

Example 16 with ApiClient

use of io.kubernetes.client.ApiClient in project weblogic-kubernetes-operator by oracle.

the class CallBuilder method listService.

/* Services */
/**
 * List services
 * @param namespace Namespace
 * @return List of services
 * @throws ApiException API Exception
 */
public V1ServiceList listService(String namespace) throws ApiException {
    String _continue = "";
    ApiClient client = helper.take();
    try {
        return new CoreV1Api(client).listNamespacedService(namespace, pretty, _continue, fieldSelector, includeUninitialized, labelSelector, limit, resourceVersion, timeoutSeconds, watch);
    } finally {
        helper.recycle(client);
    }
}
Also used : ApiClient(io.kubernetes.client.ApiClient) CoreV1Api(io.kubernetes.client.apis.CoreV1Api)

Example 17 with ApiClient

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

the class SeldonDeploymentWatcher method watchSeldonMLDeployments.

public int watchSeldonMLDeployments(int resourceVersion, int resourceVersionProcessed) throws ApiException, JsonProcessingException, IOException {
    String rs = null;
    if (resourceVersion > 0)
        rs = "" + resourceVersion;
    ApiClient client = Config.defaultClient();
    CustomObjectsApi api = new CustomObjectsApi(client);
    String namespace = StringUtils.isEmpty(this.clusterManagerProperites.getNamespace()) ? "default" : this.clusterManagerProperites.getNamespace();
    logger.debug("Watching with rs " + rs + " in namespace " + namespace);
    Watch<Object> watch = Watch.createWatch(client, api.listNamespacedCustomObjectCall("machinelearning.seldon.io", "v1alpha1", namespace, "seldondeployments", null, null, rs, true, null, null), new TypeToken<Watch.Response<Object>>() {
    }.getType());
    int maxResourceVersion = resourceVersion;
    try {
        for (Watch.Response<Object> item : watch) {
            Gson gson = new GsonBuilder().create();
            String jsonInString = gson.toJson(item.object);
            logger.debug(String.format("%s\n : %s%n", item.type, jsonInString));
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getFactory();
            JsonParser parser = factory.createParser(jsonInString);
            JsonNode actualObj = mapper.readTree(parser);
            if (actualObj.has("kind") && actualObj.get("kind").asText().equals("Status")) {
                logger.warn("Possible old resource version found - resetting");
                return 0;
            } else {
                int resourceVersionNew = actualObj.get("metadata").get("resourceVersion").asInt();
                if (resourceVersionNew <= resourceVersionProcessed) {
                    logger.warn("Looking at already processed request - skipping");
                } else {
                    if (resourceVersionNew > maxResourceVersion)
                        maxResourceVersion = resourceVersionNew;
                    try {
                        this.processWatch(SeldonDeploymentUtils.jsonToSeldonDeployment(jsonInString), item.type);
                    } catch (InvalidProtocolBufferException e) {
                        // TODO : update status of seldondeployment to show error
                        logger.warn("Failed to parse SeldonDelployment " + jsonInString, e);
                    }
                }
            }
        }
    } catch (RuntimeException e) {
        if (e.getCause() instanceof SocketTimeoutException)
            return maxResourceVersion;
        else
            throw e;
    } finally {
        watch.close();
    }
    return maxResourceVersion;
}
Also used : CustomObjectsApi(io.kubernetes.client.apis.CustomObjectsApi) GsonBuilder(com.google.gson.GsonBuilder) JsonFactory(com.fasterxml.jackson.core.JsonFactory) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Gson(com.google.gson.Gson) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiClient(io.kubernetes.client.ApiClient) SocketTimeoutException(java.net.SocketTimeoutException) TypeToken(com.google.gson.reflect.TypeToken) Watch(io.kubernetes.client.util.Watch) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 18 with ApiClient

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

the class DeploymentWatcher method watchDeployments.

public int watchDeployments(int resourceVersion, int resourceVersionProcessed) throws ApiException, IOException {
    String rs = null;
    if (resourceVersion > 0)
        rs = "" + resourceVersion;
    logger.debug("Watching with rs " + rs);
    int maxResourceVersion = resourceVersion;
    ApiClient client = Config.defaultClient();
    ExtensionsV1beta1Api api = new ExtensionsV1beta1Api(client);
    Watch<ExtensionsV1beta1Deployment> watch = Watch.createWatch(client, api.listNamespacedDeploymentCall(namespace, null, null, null, false, SeldonDeploymentOperatorImpl.LABEL_SELDON_TYPE_KEY + "=" + SeldonDeploymentOperatorImpl.LABEL_SELDON_TYPE_VAL, null, rs, 10, true, null, null), new TypeToken<Watch.Response<ExtensionsV1beta1Deployment>>() {
    }.getType());
    try {
        for (Watch.Response<ExtensionsV1beta1Deployment> item : watch) {
            int resourceVersionNew = Integer.parseInt(item.object.getMetadata().getResourceVersion());
            if (resourceVersionNew <= resourceVersionProcessed) {
                logger.warn("Looking at already processed request - skipping");
            } else {
                if (resourceVersionNew > maxResourceVersion)
                    maxResourceVersion = resourceVersionNew;
                if (item.object.getMetadata().getOwnerReferences() == null) {
                    logger.warn("Found possible seldon controlled deployment which has no owner reference. Ignoring.");
                } else {
                    switch(item.type) {
                        case "ADDED":
                        case "MODIFIED":
                            for (V1OwnerReference ownerRef : item.object.getMetadata().getOwnerReferences()) {
                                if (ownerRef.getKind().equals(KubeCRDHandlerImpl.KIND) && item.object.getStatus() != null) {
                                    String mlDepName = ownerRef.getName();
                                    String depName = item.object.getMetadata().getName();
                                    statusUpdater.updateStatus(mlDepName, depName, item.object.getStatus().getReplicas(), item.object.getStatus().getReadyReplicas());
                                }
                            }
                            break;
                        case "DELETED":
                            for (V1OwnerReference ownerRef : item.object.getMetadata().getOwnerReferences()) {
                                if (ownerRef.getKind().equals(KubeCRDHandlerImpl.KIND) && item.object.getStatus() != null) {
                                    String mlDepName = ownerRef.getName();
                                    String depName = item.object.getMetadata().getName();
                                    statusUpdater.removeStatus(mlDepName, depName);
                                }
                            }
                            break;
                        default:
                            logger.error("Unknown type " + item.type);
                    }
                // for modified get owner reference and determine which predictor it is
                // get the MLDeployment from API or local cache and update status
                // put this logic in new class
                }
            }
        }
    } catch (RuntimeException e) {
        if (e.getCause() instanceof SocketTimeoutException)
            return maxResourceVersion;
        else
            throw e;
    } finally {
        watch.close();
    }
    return maxResourceVersion;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) TypeToken(com.google.gson.reflect.TypeToken) ExtensionsV1beta1Api(io.kubernetes.client.apis.ExtensionsV1beta1Api) ExtensionsV1beta1Deployment(io.kubernetes.client.models.ExtensionsV1beta1Deployment) Watch(io.kubernetes.client.util.Watch) V1OwnerReference(io.kubernetes.client.models.V1OwnerReference) ApiClient(io.kubernetes.client.ApiClient)

Aggregations

ApiClient (io.kubernetes.client.ApiClient)18 CoreV1Api (io.kubernetes.client.apis.CoreV1Api)9 ApiException (io.kubernetes.client.ApiException)4 IOException (java.io.IOException)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 CustomObjectsApi (io.kubernetes.client.apis.CustomObjectsApi)3 ExtensionsV1beta1Api (io.kubernetes.client.apis.ExtensionsV1beta1Api)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 TypeToken (com.google.gson.reflect.TypeToken)2 Watch (io.kubernetes.client.util.Watch)2 SocketTimeoutException (java.net.SocketTimeoutException)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Response (com.squareup.okhttp.Response)1 TopologyRuntimeManagementException (com.twitter.heron.scheduler.TopologyRuntimeManagementException)1 ExtensionsV1beta1Deployment (io.kubernetes.client.models.ExtensionsV1beta1Deployment)1 ExtensionsV1beta1DeploymentList (io.kubernetes.client.models.ExtensionsV1beta1DeploymentList)1