use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.
the class DbOpLauncherImpl method launchDbOp.
@Override
public void launchDbOp(String dbOpName, String namespace) {
StackGresDbOps dbOps = dbOpsFinder.findByNameAndNamespace(dbOpName, namespace).orElseThrow(() -> new IllegalArgumentException(StackGresDbOps.KIND + " " + dbOpName + " does not exists in namespace " + namespace));
Instance<DatabaseOperationJob> jobImpl = instance.select(new DatabaseOperationLiteral(dbOps.getSpec().getOp()));
if (jobImpl.isResolvable()) {
LOGGER.info("Initializing conditions for SgDbOps {}", dbOps.getMetadata().getName());
var status = Optional.ofNullable(dbOps.getStatus()).or(() -> Optional.of(new StackGresDbOpsStatus())).map(dbOpsStatus -> {
dbOpsStatus.setOpStarted(Instant.now().toString());
dbOpsStatus.setOpRetries(Optional.ofNullable(dbOpsStatus.getOpRetries()).map(opRetries -> opRetries + 1).orElse(0));
dbOpsStatus.setConditions(getStartingConditions());
return dbOpsStatus;
}).orElseThrow();
dbOps.setStatus(status);
final StackGresDbOps initializedDbOps = dbOpsScheduler.update(dbOps);
try {
final int lockPollInterval = Integer.parseInt(DBOPS_LOCK_POLL_INTERVAL.getString());
final int timeout = Integer.parseInt(DBOPS_LOCK_TIMEOUT.getString());
LockRequest lockRequest = ImmutableLockRequest.builder().namespace(initializedDbOps.getMetadata().getNamespace()).serviceAccount(JobsProperty.SERVICE_ACCOUNT.getString()).podName(JobsProperty.POD_NAME.getString()).pollInterval(lockPollInterval).timeout(timeout).lockResourceName(initializedDbOps.getSpec().getSgCluster()).build();
Infrastructure.setDroppedExceptionHandler(err -> LOGGER.error("Dropped exception ", err));
lockAcquirer.lockRun(lockRequest, (targetCluster) -> {
databaseOperationEventEmitter.operationStarted(dbOpName, namespace);
final DatabaseOperationJob databaseOperationJob = jobImpl.get();
Uni<ClusterRestartState> jobUni = databaseOperationJob.runJob(initializedDbOps, targetCluster);
if (initializedDbOps.getSpec().getTimeout() != null) {
jobUni.await().atMost(Duration.parse(initializedDbOps.getSpec().getTimeout()));
} else {
jobUni.await().indefinitely();
}
databaseOperationEventEmitter.operationCompleted(dbOpName, namespace);
});
LOGGER.info("Operation completed for SgDbOp {}", dbOpName);
updateToCompletedConditions(dbOpName, namespace);
} catch (TimeoutException timeoutEx) {
updateToTimeoutConditions(dbOpName, namespace);
databaseOperationEventEmitter.operationTimedOut(dbOpName, namespace);
throw timeoutEx;
} catch (Exception e) {
updateToFailedConditions(dbOpName, namespace);
databaseOperationEventEmitter.operationFailed(dbOpName, namespace);
throw e;
}
} else if (jobImpl.isAmbiguous()) {
throw new IllegalStateException("Multiple implementations of the operation " + dbOps.getSpec().getOp() + " found");
} else {
throw new IllegalStateException("Implementation of operation " + dbOps.getSpec().getOp() + " not found");
}
}
use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.
the class DbOpsImmutableSpecValidator method validate.
@Override
public void validate(StackGresDbOpsReview review) throws ValidationFailed {
switch(review.getRequest().getOperation()) {
case UPDATE:
StackGresDbOps dbOps = review.getRequest().getObject();
StackGresDbOps oldDbOps = review.getRequest().getOldObject();
if (!dbOps.getSpec().equals(oldDbOps.getSpec())) {
fail("spec can not be updated");
}
break;
default:
}
}
use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.
the class FakeDbOpsScheduler method updateStatus.
@Override
public StackGresDbOps updateStatus(StackGresDbOps resource) {
String name = resource.getMetadata().getName();
String namespace = resource.getMetadata().getNamespace();
StackGresDbOps savedOp = kubeDb.getDbOps(name, namespace);
savedOp.setStatus(resource.getStatus());
return kubeDb.addOrReplaceDbOps(savedOp);
}
Aggregations