Search in sources :

Example 1 with Registry

use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class DeprovisionRegistryWorker method finallyExecute.

@Transactional
@Override
public void finallyExecute(Task aTask, WorkerContext ctl, Optional<Exception> error) throws RegistryNotFoundException, RegistryStorageConflictException {
    DeprovisionRegistryTask task = (DeprovisionRegistryTask) aTask;
    Optional<RegistryData> registry = storage.getRegistryById(task.getRegistryId());
    if (registry.isPresent()) {
        var reg = registry.get();
        // Failure - Could not delete tenant or update status
        // Try updating status to failed, otherwise user can retry.
        reg.setStatus(RegistryStatusValueDto.FAILED.value());
        // TODO Add failed_reason
        storage.createOrUpdateRegistry(reg);
        log.warn("Failed to deprovision Registry: {}", registry);
    } else {
        // SUCCESS
        log.debug("Registry (ID = {}) has been deleted.", task.getRegistryId());
    }
}
Also used : DeprovisionRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.DeprovisionRegistryTask) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) Transactional(javax.transaction.Transactional)

Example 2 with Registry

use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class DeprovisionRegistryWorker method execute.

@Transactional
@Override
public void execute(Task aTask, WorkerContext ctl) throws RegistryStorageConflictException, RegistryNotFoundException, AccountManagementServiceException, TenantManagerServiceException {
    var task = (DeprovisionRegistryTask) aTask;
    var registryOptional = storage.getRegistryById(task.getRegistryId());
    if (registryOptional.isPresent()) {
        // FAILURE POINT 1
        var registry = registryOptional.get();
        RegistryDeploymentData registryDeployment = registry.getRegistryDeployment();
        // FAILURE POINT 2
        if (task.getRegistryTenantId() == null) {
            final var tenantId = registry.getId();
            TenantManagerConfig tenantManagerConfig = Utils.createTenantManagerConfig(registryDeployment);
            try {
                tms.deleteTenant(tenantManagerConfig, tenantId);
                log.debug("Tenant id='{}' delete request send.", tenantId);
            } catch (TenantNotFoundServiceException ex) {
                log.info("Tenant id='{}' does not exist (already deleted?).", tenantId);
            }
            task.setRegistryTenantId(tenantId);
        }
        /* Return AMS entitlement
             * FAILURE POINT 3
             * Recovery: We recover by setting the registry status to failed so we don't lose information
             *   and the process can be initiated again.
             * Reentrancy: If the registry was already returned, (i.e. failed in #3)
             *   we need to continue without raising an error, otherwise we will keep retrying.
             */
        if (!task.isAmsSuccess()) {
            final String subscriptionId = registry.getSubscriptionId();
            // TODO Workaround: Remove this once we have RHOSRTrial working.
            if (subscriptionId != null && RegistryInstanceTypeValueDto.of(registry.getInstanceType()) != RegistryInstanceTypeValueDto.EVAL) {
                try {
                    ams.deleteSubscription(subscriptionId);
                } catch (SubscriptionNotFoundServiceException ex) {
                    log.info("Subscription ID '{}' for tenant ID '{}' does not exist (already deleted?).", subscriptionId, task.getRegistryTenantId());
                }
            } else {
                log.debug("Deleting an eval instance {} without calling AMS.", registry.getId());
            }
            task.setAmsSuccess(true);
            log.debug("Subscription (id='{}') for Registry (id='{}') deleted.", subscriptionId, registry.getId());
        }
        /* Delete the registry from DB
             * FAILURE POINT 4
             * Recovery: We set the status to failed so it can be retried.
             * Reentrancy: This is the last step, so nothing to do.
             */
        storage.deleteRegistry(registry.getId());
    } else {
        log.warn("Registry id='{}' not found. Stopping.", task.getRegistryId());
        ctl.stop();
    }
}
Also used : DeprovisionRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.DeprovisionRegistryTask) TenantNotFoundServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException) SubscriptionNotFoundServiceException(org.bf2.srs.fleetmanager.spi.ams.SubscriptionNotFoundServiceException) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) TenantManagerConfig(org.bf2.srs.fleetmanager.spi.tenants.model.TenantManagerConfig) Transactional(javax.transaction.Transactional)

Example 3 with Registry

use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class EvalInstanceExpirationRegistryWorker method execute.

@Transactional
@Override
public void execute(Task aTask, WorkerContext ctx) {
    EvalInstanceExpirationRegistryTask task = (EvalInstanceExpirationRegistryTask) aTask;
    // Nothing to do, the heavy lifting is done by the deprovisioning task.
    log.debug("Initiating deprovisioning of an eval instance, registry id = '{}'", task.getRegistryId());
    ctx.delay(() -> tasks.submit(DeprovisionRegistryTask.builder().registryId(task.getRegistryId()).build()));
}
Also used : EvalInstanceExpirationRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.EvalInstanceExpirationRegistryTask) Transactional(javax.transaction.Transactional)

Example 4 with Registry

use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class StartDeprovisionRegistryWorker method finallyExecute.

@Transactional
@Override
public void finallyExecute(Task aTask, WorkerContext ctl, Optional<Exception> error) throws RegistryNotFoundException, RegistryStorageConflictException {
    StartDeprovisionRegistryTask task = (StartDeprovisionRegistryTask) aTask;
    Optional<RegistryData> registryOptional = storage.getRegistryById(task.getRegistryId());
    if (registryOptional.isPresent()) {
        var registry = registryOptional.get();
        // SUCCESS STATE
        if (RegistryStatusValueDto.DEPROVISIONING_DELETING.value().equals(registry.getStatus()))
            return;
        // FAILURE
        // Nothing to do, user can retry
        log.warn("Failed to start deprovisioning of Registry '{}'. Check the status to see if the instance is stuck.", registry);
    } else {
        log.warn("Could not find Registry (ID = {}).", task.getRegistryId());
    }
}
Also used : StartDeprovisionRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.StartDeprovisionRegistryTask) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) Transactional(javax.transaction.Transactional)

Example 5 with Registry

use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class ProvisionRegistryTenantWorker method execute.

@Transactional
@Override
public void execute(Task aTask, WorkerContext ctl) throws RegistryStorageConflictException, TenantManagerServiceException {
    // TODO Split along failure points?
    ProvisionRegistryTenantTask task = (ProvisionRegistryTenantTask) aTask;
    Optional<RegistryData> registryOptional = storage.getRegistryById(task.getRegistryId());
    // NOTE: Failure point 1
    if (registryOptional.isEmpty()) {
        ctl.retry();
    }
    RegistryData registry = registryOptional.get();
    RegistryDeploymentData registryDeployment = registry.getRegistryDeployment();
    // NOTE: Failure point 2
    if (registryDeployment == null) {
        // Either the schedule task didn't run yet, or we are in trouble
        ctl.retry();
    }
    String registryUrl = registryDeployment.getRegistryDeploymentUrl();
    // New approach: configure the deployment URL with a replacement like:  https://TENANT_ID.shrd.sr.openshift.com
    if (registryUrl.contains("TENANT_ID")) {
        registryUrl = registryUrl.replace("TENANT_ID", registry.getId());
    } else {
        // Old approach: configure the deployment URL without a replacement, and just add "/t/TENANT_ID" to the end of it.
        if (!registryUrl.endsWith("/")) {
            registryUrl += "/";
        }
        registryUrl += "t/" + registry.getId();
    }
    registry.setRegistryUrl(registryUrl);
    // Avoid accidentally creating orphan tenants
    if (task.getRegistryTenantId() == null) {
        CreateTenantRequest tenantRequest = CreateTenantRequest.builder().tenantId(registry.getId()).createdBy(registry.getOwner()).organizationId(registry.getOrgId()).resources(plansService.determineQuotaPlan(registry.getOrgId()).getResources()).build();
        TenantManagerConfig tenantManager = Utils.createTenantManagerConfig(registryDeployment);
        // NOTE: Failure point 4
        tmClient.createTenant(tenantManager, tenantRequest);
        task.setRegistryTenantId(registry.getId());
    }
    // Add expiration task if this is an eval instance
    if (isEvalInstance(registry.getInstanceType())) {
        var expiration = Instant.now().plus(Duration.ofSeconds(evalLifetimeSeconds));
        log.debug("Scheduling an expiration task for the eval instance {} to be executed at {}", registry, expiration);
        ctl.delay(() -> tasks.submit(EvalInstanceExpirationRegistryTask.builder().registryId(registry.getId()).schedule(TaskSchedule.builder().firstExecuteAt(expiration).build()).build()));
    }
    // NOTE: Failure point 5
    registry.setStatus(RegistryStatusValueDto.READY.value());
    storage.createOrUpdateRegistry(registry);
// TODO This task is (temporarily) not used. Enable when needed.
// Update status to available in the heartbeat task, which should run ASAP
// ctl.delay(() -> tasks.submit(RegistryHeartbeatTask.builder().registryId(registry.getId()).build()));
}
Also used : CreateTenantRequest(org.bf2.srs.fleetmanager.spi.tenants.model.CreateTenantRequest) ProvisionRegistryTenantTask(org.bf2.srs.fleetmanager.execution.impl.tasks.ProvisionRegistryTenantTask) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) TenantManagerConfig(org.bf2.srs.fleetmanager.spi.tenants.model.TenantManagerConfig) Transactional(javax.transaction.Transactional)

Aggregations

RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)13 Transactional (javax.transaction.Transactional)8 Test (org.junit.jupiter.api.Test)7 AccountInfo (org.bf2.srs.fleetmanager.spi.common.model.AccountInfo)6 TenantNotFoundServiceException (org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException)5 RegistryDeploymentData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData)5 Secret (io.fabric8.kubernetes.api.model.Secret)4 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)4 ManagedConnector (org.bf2.cos.fleetshard.api.ManagedConnector)4 TenantManagerClient (io.apicurio.multitenant.client.TenantManagerClient)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Optional (java.util.Optional)3 ApplicationScoped (javax.enterprise.context.ApplicationScoped)3 Inject (javax.inject.Inject)3 ConnectorDeployment (org.bf2.cos.fleet.manager.model.ConnectorDeployment)3 FleetShardSyncConfig (org.bf2.cos.fleetshard.sync.FleetShardSyncConfig)3 FleetManagerClient (org.bf2.cos.fleetshard.sync.client.FleetManagerClient)3 FleetShardClient (org.bf2.cos.fleetshard.sync.client.FleetShardClient)3