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 {
}
}
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;
}
}
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;
}
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;
}
Aggregations