Commit decbbc45 authored by Ľuboslav Pivarč's avatar Ľuboslav Pivarč
Browse files

erorr handling in listall fixed && refactor constants

parent 0009c8d5
package cz.muni.ics.edirex.cbioondemandK8S;
import com.cronutils.builder.CronBuilder;
import com.cronutils.mapper.CronMapper;
import com.cronutils.model.Cron;
import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
......@@ -13,13 +11,11 @@ import com.google.gson.JsonElement;
import cz.muni.ics.edirex.cbioondemandK8S.model.IdentifierCRD;
import io.kubernetes.client.JSON;
import io.kubernetes.client.util.Yaml;
import org.springframework.scheduling.support.CronSequenceGenerator;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Date;
......@@ -31,6 +27,9 @@ import static com.cronutils.model.field.expression.FieldExpressionFactory.on;
*/
@Component
public class Utils {
public static final long ONE_MINUTE_IN_MILLIS = 60000;
public static final long HOUR = ONE_MINUTE_IN_MILLIS * 60;
public static final long FIVE_MINUTES = 60 * 5;
private static Gson gson = new Gson();
......
......@@ -63,6 +63,9 @@ public class API {
@GetMapping("/cbioondemands")
public ResponseEntity listInstancesForUser(User user){
List<Instance> instances = cBioOnDemand.listInstancesForUser(user);
if (instances == null){
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<>(instances, HttpStatus.OK);
}
}
......@@ -23,9 +23,7 @@ import java.util.UUID;
@Service
public class KubernetesImpl implements CBioOnDemandService {
static final long ONE_MINUTE_IN_MILLIS = 60000;
private static final long FIVE_MINUTES = 60 * 5;
private final long HOUR = ONE_MINUTE_IN_MILLIS * 60;
private final long TIME_TO_LIVE = Utils.HOUR;
private KubernetesCalls api;
private RoutingAAI routing;
......@@ -38,8 +36,8 @@ public class KubernetesImpl implements CBioOnDemandService {
private IdentifierCRD identifier;
private final String NAMESPACE = "cbio-on-demand";
private final String LABELS = "app=cbio, type=ondemand";
private final String DBLABELS = "app=cbioDB, type=ondemand";
private final String APP_LABELS = "app=cbio, type=ondemand";
private final String DB_LABELS = "app=cbioDB, type=ondemand";
public KubernetesImpl(KubernetesCalls api, RoutingAAI routing) throws IOException {
this.api = api;
......@@ -75,7 +73,7 @@ public class KubernetesImpl implements CBioOnDemandService {
fallBack(hostname, cbioService);
return null;
}
return new Instance(instanceID, user, HOUR, url, Status.creating);
return new Instance(instanceID, user, TIME_TO_LIVE, url, Status.creating);
}
private void fallBack(String hostname, String cbioService) {
......@@ -86,7 +84,7 @@ public class KubernetesImpl implements CBioOnDemandService {
}
public Instance status(Instance instance){
String labels = LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
String labels = APP_LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
List<V1beta1CronJob> result = api.getCroneJobs(labels, NAMESPACE);
if (result.size()!=1)
......@@ -106,7 +104,7 @@ public class KubernetesImpl implements CBioOnDemandService {
if(!statusDecided(status)){
instance.fillReport(timeToExpire, status);
}else {
if (timeToExpire < FIVE_MINUTES){
if (timeToExpire < Utils.FIVE_MINUTES){
instance.fillReport(timeToExpire, Status.removing);
}else {
......@@ -128,13 +126,15 @@ public class KubernetesImpl implements CBioOnDemandService {
}
public Instance extend(Instance instance){
String labels = LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
String labels = APP_LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
return updateCronjob(instance, labels);
}
public List<Instance> listInstancesForUser(User user) {
String labelsRS = LABELS + ", user=" + user.getUserId();
String labelsRS = APP_LABELS + ", user=" + user.getUserId();
List<V1ReplicaSet> result = api.getReplicaSets(labelsRS, NAMESPACE);
if (result == null)
return null;
if(result.size() == 0)
return new ArrayList<>();
List<Instance> returns = new ArrayList<>();
......@@ -142,7 +142,7 @@ public class KubernetesImpl implements CBioOnDemandService {
for (V1ReplicaSet replicaSet : result){
Map<String, String> labels = replicaSet.getMetadata().getLabels();
String label = LABELS +", user=" + user.getUserId() + ", instance=" +
String label = APP_LABELS +", user=" + user.getUserId() + ", instance=" +
replicaSet.getMetadata().getLabels().get("instance");
long expireTime = getExpireDate(label);
......@@ -197,7 +197,7 @@ public class KubernetesImpl implements CBioOnDemandService {
V1beta1CronJob job = Utils.deepCopy(cronJob, V1beta1CronJob.class);
addLabelsAndENV(job, userID, instanceID);
Date now = new Date();
setSchedule(job, now.getTime() + HOUR);
setSchedule(job, now.getTime() + TIME_TO_LIVE);
V1beta1CronJob result = api.createCroneJob(job, NAMESPACE);
if( result != null)
return result.getMetadata().getName();
......@@ -358,7 +358,7 @@ public class KubernetesImpl implements CBioOnDemandService {
}
private boolean deleteRemoveJob(Instance instance) {
String labels = LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
String labels = APP_LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
List<V1beta1CronJob> result =api.getCroneJobs(labels, NAMESPACE);
if(result == null)
return false;
......@@ -369,10 +369,10 @@ public class KubernetesImpl implements CBioOnDemandService {
}
private boolean deleteServices(Instance instance) {
String labels = LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
String labels = APP_LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
boolean cbioService = deleteService(labels);
String dbLabels = DBLABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
String dbLabels = DB_LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
boolean dbService = deleteService(dbLabels);
return cbioService && dbService;
......@@ -391,10 +391,10 @@ public class KubernetesImpl implements CBioOnDemandService {
}
private boolean deleteReplicaSets(Instance instance) {
String labels = LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
String labels = APP_LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
boolean cbioRS = deleteReplicaSet(labels);
String dbLabels = DBLABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
String dbLabels = DB_LABELS + ", user=" + instance.getUser().getUserId() + ", instance=" + instance.getId();
boolean dbRS = deleteReplicaSet(dbLabels);
return cbioRS && dbRS;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment