Search in sources :

Example 1 with CustomObjectsApi

use of io.kubernetes.client.apis.CustomObjectsApi 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 2 with CustomObjectsApi

use of io.kubernetes.client.apis.CustomObjectsApi 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)

Example 3 with CustomObjectsApi

use of io.kubernetes.client.apis.CustomObjectsApi 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 4 with CustomObjectsApi

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

the class DeploymentWatcher method watchSeldonSeldonDeployments.

public int watchSeldonSeldonDeployments(int resourceVersion, int resourceVersionProcessed) throws ApiException, JsonProcessingException, IOException {
    String rs = null;
    if (resourceVersion > 0)
        rs = "" + resourceVersion;
    logger.info("Watching with rs " + rs);
    CustomObjectsApi api = new CustomObjectsApi();
    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.info(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;
                    String jsonModified = removeCreationTimestampField(jsonInString);
                    SeldonDeployment.Builder mlBuilder = SeldonDeployment.newBuilder();
                    ProtoBufUtils.updateMessageBuilderFromJson(mlBuilder, jsonModified);
                    this.processWatch(mlBuilder.build(), item.type);
                }
            }
        }
    } catch (RuntimeException e) {
        if (e.getCause() instanceof SocketTimeoutException)
            return maxResourceVersion;
        else
            throw e;
    }
    return maxResourceVersion;
}
Also used : CustomObjectsApi(io.kubernetes.client.apis.CustomObjectsApi) GsonBuilder(com.google.gson.GsonBuilder) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Gson(com.google.gson.Gson) JsonNode(com.fasterxml.jackson.databind.JsonNode) SocketTimeoutException(java.net.SocketTimeoutException) TypeToken(com.google.gson.reflect.TypeToken) Watch(io.kubernetes.client.util.Watch) SeldonDeployment(io.seldon.protos.DeploymentProtos.SeldonDeployment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

CustomObjectsApi (io.kubernetes.client.apis.CustomObjectsApi)4 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 ApiClient (io.kubernetes.client.ApiClient)3 JsonFactory (com.fasterxml.jackson.core.JsonFactory)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 TypeToken (com.google.gson.reflect.TypeToken)2 ApiException (io.kubernetes.client.ApiException)2 Watch (io.kubernetes.client.util.Watch)2 SeldonDeployment (io.seldon.protos.DeploymentProtos.SeldonDeployment)2 IOException (java.io.IOException)2 SocketTimeoutException (java.net.SocketTimeoutException)2