Skip to content

Commit

Permalink
#200: Crash prevention and status tracking for AppDefinitions
Browse files Browse the repository at this point in the history
Enable checking and storing of AppDefinitions' handling in the operator
  • Loading branch information
lucas-koehler committed Aug 3, 2023
1 parent c8ac69e commit a9b14ff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

import static org.eclipse.theia.cloud.common.util.LogMessageUtil.formatLogMessage;

import java.util.Optional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.theia.cloud.common.k8s.client.TheiaCloudClient;
import org.eclipse.theia.cloud.common.k8s.resource.AppDefinition;
import org.eclipse.theia.cloud.common.k8s.resource.AppDefinitionSpec;
import org.eclipse.theia.cloud.common.k8s.resource.AppDefinitionStatus;
import org.eclipse.theia.cloud.common.k8s.resource.OperatorStatus;
import org.eclipse.theia.cloud.common.k8s.resource.ResourceStatus;
import org.eclipse.theia.cloud.operator.handler.AppDefinitionHandler;
import org.eclipse.theia.cloud.operator.handler.IngressPathProvider;
import org.eclipse.theia.cloud.operator.handler.util.TheiaCloudIngressUtil;
Expand All @@ -46,33 +51,37 @@ public boolean appDefinitionAdded(AppDefinition appDefinition, String correlatio
} catch (Throwable ex) {
LOGGER.error(formatLogMessage(correlationId,
"An unexpected exception occurred while adding AppDefinition: " + appDefinition), ex);
// TODO update status
// client.appDefinitions().updateStatus(correlationId, appDefinition, status ->
// {
// status.setOperatorStatus(OperatorStatus.ERROR);
// status.setOperatorMessage("Unexpected error. Please check the logs for correlationId " + correlationId);
// });
client.appDefinitions().updateStatus(correlationId, appDefinition, status -> {
status.setOperatorStatus(OperatorStatus.ERROR);
status.setOperatorMessage("Unexpected error. Please check the logs for correlationId " + correlationId);
});
return false;
}
}

protected boolean doAppDefinitionAdded(AppDefinition appDefinition, String correlationId) {
LOGGER.info(formatLogMessage(correlationId, "Handling " + appDefinition));

// TODO Check current session status and ignore if handling failed before
// Optional<AppDefinitionStatus> status = Optional.ofNullable(appDefinition.getStatus());
// String operatorStatus = status.map(ResourceStatus::getOperatorStatus).orElse(OperatorStatus.NEW);
// if (OperatorStatus.ERROR.equals(operatorStatus) || OperatorStatus.HANDLING.equals(operatorStatus)) {
// LOGGER.warn(formatLogMessage(correlationId,
// "AppDefinition could not be handled before and is skipped now. Current status: " + operatorStatus
// + ". AppDefinition: " + appDefinition));
// return false;
// }

// TODO Set app definition status to being handled
// client.appDefinitions().updateStatus(correlationId, appDefinition, s -> {
// s.setOperatorStatus(OperatorStatus.HANDLING);
// });
// Check current session status and ignore if handling failed or finished before
Optional<AppDefinitionStatus> status = Optional.ofNullable(appDefinition.getStatus());
String operatorStatus = status.map(ResourceStatus::getOperatorStatus).orElse(OperatorStatus.NEW);
if (OperatorStatus.HANDLED.equals(operatorStatus)) {
LOGGER.trace(formatLogMessage(correlationId,
"AppDefinition was successfully handled before and is skipped now. AppDefinition: "
+ appDefinition));
return true;
}
if (OperatorStatus.ERROR.equals(operatorStatus) || OperatorStatus.HANDLING.equals(operatorStatus)) {
LOGGER.warn(formatLogMessage(correlationId,
"AppDefinition could not be handled before and is skipped now. Current status: " + operatorStatus
+ ". AppDefinition: " + appDefinition));
return false;
}

// Set app definition status to being handled
client.appDefinitions().updateStatus(correlationId, appDefinition, s -> {
s.setOperatorStatus(OperatorStatus.HANDLING);
});

AppDefinitionSpec spec = appDefinition.getSpec();
String appDefinitionResourceName = appDefinition.getMetadata().getName();
Expand All @@ -83,20 +92,18 @@ protected boolean doAppDefinitionAdded(AppDefinition appDefinition, String corre
LOGGER.error(formatLogMessage(correlationId,
"Expected ingress '" + spec.getIngressname() + "' for app definition '" + appDefinitionResourceName
+ "' does not exist. Abort handling app definition."));
// TODO update status
// client.appDefinitions().updateStatus(correlationId, appDefinition, s -> {
// s.setOperatorStatus(OperatorStatus.ERROR);
// s.setOperatorMessage("Ingress does not exist.");
// });
client.appDefinitions().updateStatus(correlationId, appDefinition, s -> {
s.setOperatorStatus(OperatorStatus.ERROR);
s.setOperatorMessage("Ingress does not exist.");
});
return false;
} else {
LOGGER.trace(formatLogMessage(correlationId, "Ingress available already"));
}

// TODO update status
// client.appDefinitions().updateStatus(correlationId, appDefinition, s -> {
// s.setOperatorStatus(OperatorStatus.HANDLED);
// });
client.appDefinitions().updateStatus(correlationId, appDefinition, s -> {
s.setOperatorStatus(OperatorStatus.HANDLED);
});
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean workspaceAdded(Workspace workspace, String correlationId) {
protected boolean doWorkspaceAdded(Workspace workspace, String correlationId) {
LOGGER.info(formatLogMessage(correlationId, "Handling " + workspace));

// Check current session status and ignore if handling failed before
// Check current session status and ignore if handling failed or finished before
Optional<WorkspaceStatus> status = Optional.ofNullable(workspace.getStatus());
String operatorStatus = status.map(ResourceStatus::getOperatorStatus).orElse(OperatorStatus.NEW);
if (OperatorStatus.HANDLED.equals(operatorStatus)) {
Expand Down

0 comments on commit a9b14ff

Please sign in to comment.