Search in sources :

Example 6 with Registry

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

the class ProvisionRegistryTenantWorker method finallyExecute.

@Transactional
@Override
public void finallyExecute(Task aTask, WorkerContext ctl, Optional<Exception> error) throws RegistryNotFoundException, RegistryStorageConflictException, SubscriptionNotFoundServiceException, AccountManagementServiceException, TenantManagerServiceException {
    ProvisionRegistryTenantTask task = (ProvisionRegistryTenantTask) aTask;
    RegistryData registry = storage.getRegistryById(task.getRegistryId()).orElse(null);
    RegistryDeploymentData registryDeployment = null;
    if (registry != null)
        registryDeployment = registry.getRegistryDeployment();
    // SUCCESS STATE
    if (registry != null && registry.getRegistryUrl() != null)
        return;
    // Cleanup orphan susbcription, if it's null, it's not needed since it will likely be an eval instance
    if (registry != null && registryDeployment != null && registry.getSubscriptionId() != null) {
        accountManagementService.deleteSubscription(registry.getSubscriptionId());
    }
    // Cleanup orphan tenant
    if (registry != null && registryDeployment != null && task.getRegistryTenantId() != null) {
        try {
            tmClient.deleteTenant(Utils.createTenantManagerConfig(registryDeployment), registry.getId());
        } catch (TenantNotFoundServiceException e) {
            log.warn("Could not delete tenant '{}'. Tenant does not exist and may have been already deleted.", registry.getId());
        }
    }
    // Remove registry entity
    if (registry != null) {
        storage.deleteRegistry(registry.getId());
    }
}
Also used : TenantNotFoundServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException) 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) Transactional(javax.transaction.Transactional)

Example 7 with Registry

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

the class ScheduleRegistryWorker method execute.

@Transactional
@Override
public void execute(Task aTask, WorkerContext ctl) throws RegistryStorageConflictException {
    ScheduleRegistryTask task = (ScheduleRegistryTask) aTask;
    Optional<RegistryData> registryOptional = storage.getRegistryById(task.getRegistryId());
    if (registryOptional.isEmpty()) {
        // NOTE: Failure point 1
        ctl.retry();
    }
    RegistryData registry = registryOptional.get();
    List<RegistryDeploymentData> eligibleRegistryDeployments = storage.getAllRegistryDeployments().stream().filter(rd -> RegistryDeploymentStatusValue.of(rd.getStatus().getValue()) == RegistryDeploymentStatusValue.AVAILABLE).collect(toList());
    if (eligibleRegistryDeployments.isEmpty()) {
        // NOTE: Failure point 2
        // TODO How to report it better?
        log.warn("Could not schedule registry with ID {}. No deployments are available.", registry.getId());
        // We can wait here longer, somebody needs to create a deployment
        ctl.retry(100);
    }
    // Schedule to a random registry deployment
    // TODO Improve & use a specific scheduling strategy
    RegistryDeploymentData registryDeployment = eligibleRegistryDeployments.get(ThreadLocalRandom.current().nextInt(eligibleRegistryDeployments.size()));
    // TODO only available
    log.info("Scheduling {} to {}.", registry, registryDeployment);
    registry.setRegistryDeployment(registryDeployment);
    registry.setStatus(RegistryStatusValueDto.PROVISIONING.value());
    // NOTE: Failure point 3
    storage.createOrUpdateRegistry(registry);
    ctl.delay(() -> tasks.submit(ProvisionRegistryTenantTask.builder().registryId(registry.getId()).build()));
}
Also used : RegistryDeploymentStatusValue(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentStatusValue) Task(org.bf2.srs.fleetmanager.execution.manager.Task) SCHEDULE_REGISTRY_W(org.bf2.srs.fleetmanager.execution.impl.workers.WorkerType.SCHEDULE_REGISTRY_W) Logger(org.slf4j.Logger) Transactional(javax.transaction.Transactional) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) LoggerFactory(org.slf4j.LoggerFactory) TaskManager(org.bf2.srs.fleetmanager.execution.manager.TaskManager) RegistryStatusValueDto(org.bf2.srs.fleetmanager.rest.service.model.RegistryStatusValueDto) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) RegistryStorageConflictException(org.bf2.srs.fleetmanager.storage.RegistryStorageConflictException) Inject(javax.inject.Inject) WorkerContext(org.bf2.srs.fleetmanager.execution.manager.WorkerContext) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ProvisionRegistryTenantTask(org.bf2.srs.fleetmanager.execution.impl.tasks.ProvisionRegistryTenantTask) RegistryNotFoundException(org.bf2.srs.fleetmanager.storage.RegistryNotFoundException) SCHEDULE_REGISTRY_T(org.bf2.srs.fleetmanager.execution.impl.tasks.TaskType.SCHEDULE_REGISTRY_T) ResourceStorage(org.bf2.srs.fleetmanager.storage.ResourceStorage) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Optional(java.util.Optional) ScheduleRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.ScheduleRegistryTask) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ScheduleRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.ScheduleRegistryTask) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) Transactional(javax.transaction.Transactional)

Example 8 with Registry

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

the class CheckDeletePermissionsInterceptor method intercept.

@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
    if (isResolvable(securityIdentity)) {
        final AccountInfo accountInfo = authService.extractAccountInfo();
        final Optional<RegistryData> registry = storage.getRegistryById(context.getParameters()[0].toString());
        if (userCanDeleteInstance(accountInfo, registry)) {
            return context.proceed();
        }
    } else {
        return context.proceed();
    }
    log.info("Attempt to delete registry instance without the proper permissions");
    throw new ForbiddenException();
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) AccountInfo(org.bf2.srs.fleetmanager.spi.common.model.AccountInfo) AroundInvoke(javax.interceptor.AroundInvoke)

Example 9 with Registry

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

the class RegistryDeploymentServiceImpl method init.

@Override
public void init() throws IOException, RegistryDeploymentStorageConflictException, RegistryDeploymentNotFoundException {
    if (deploymentsConfigFile.isEmpty()) {
        return;
    }
    log.info("Loading registry deployments config file from {}", deploymentsConfigFile.get().getAbsolutePath());
    YAMLMapper mapper = new YAMLMapper();
    RegistryDeploymentsConfigList deploymentsConfigList = mapper.readValue(deploymentsConfigFile.get(), RegistryDeploymentsConfigList.class);
    List<RegistryDeploymentCreate> staticDeployments = deploymentsConfigList.getDeployments();
    Set<String> names = new HashSet<>();
    List<String> duplicatedNames = staticDeployments.stream().map(d -> {
        Set<ConstraintViolation<RegistryDeploymentCreate>> errors = validator.validate(d);
        if (!errors.isEmpty()) {
            throw new ConstraintViolationException(errors);
        }
        return d;
    }).filter(d -> !names.add(d.getName())).map(d -> d.getName()).collect(Collectors.toList());
    if (!duplicatedNames.isEmpty()) {
        throw new IllegalArgumentException("Error in static deployments config, duplicated deployments name: " + duplicatedNames.toString());
    }
    Map<String, RegistryDeploymentData> currentDeployments = storage.getAllRegistryDeployments().stream().collect(Collectors.toMap(d -> d.getName(), d -> d));
    for (RegistryDeploymentCreate dep : staticDeployments) {
        RegistryDeploymentData deploymentData = currentDeployments.get(dep.getName());
        if (deploymentData == null) {
            // deployment is new
            deploymentData = convertRegistryDeployment.convert(dep);
        } else {
            if (deploymentData.getRegistryDeploymentUrl().equals(dep.getRegistryDeploymentUrl()) && deploymentData.getTenantManagerUrl().equals(dep.getTenantManagerUrl())) {
                // no changes in the deployment
                continue;
            }
            deploymentData.setRegistryDeploymentUrl(dep.getRegistryDeploymentUrl());
            deploymentData.setTenantManagerUrl(dep.getTenantManagerUrl());
        }
        createOrUpdateRegistryDeployment(deploymentData);
    }
}
Also used : RegistryDeployment(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeployment) RegistryDeploymentNotFoundException(org.bf2.srs.fleetmanager.storage.RegistryDeploymentNotFoundException) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) KEY_DEPLOYMENT_ID(org.bf2.srs.fleetmanager.common.operation.auditing.AuditingConstants.KEY_DEPLOYMENT_ID) LoggerFactory(org.slf4j.LoggerFactory) RegistryDeploymentStorageConflictException(org.bf2.srs.fleetmanager.storage.RegistryDeploymentStorageConflictException) Audited(org.bf2.srs.fleetmanager.common.operation.auditing.Audited) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Valid(javax.validation.Valid) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) RegistryDeploymentCreate(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentCreate) ResourceStorage(org.bf2.srs.fleetmanager.storage.ResourceStorage) Map(java.util.Map) ConstraintViolation(javax.validation.ConstraintViolation) RegistryDeploymentStatusValue(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentStatusValue) ConvertRegistryDeployment(org.bf2.srs.fleetmanager.rest.service.convert.ConvertRegistryDeployment) Logger(org.slf4j.Logger) ForbiddenException(javax.ws.rs.ForbiddenException) Set(java.util.Set) Validator(javax.validation.Validator) IOException(java.io.IOException) RegistryDeploymentsConfigList(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentsConfigList) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ConstraintViolationException(javax.validation.ConstraintViolationException) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) RegistryDeploymentService(org.bf2.srs.fleetmanager.rest.service.RegistryDeploymentService) HashSet(java.util.HashSet) Set(java.util.Set) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) RegistryDeploymentsConfigList(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentsConfigList) ConstraintViolationException(javax.validation.ConstraintViolationException) RegistryDeploymentCreate(org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentCreate) HashSet(java.util.HashSet)

Example 10 with Registry

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

the class QuotaPlanIT method testQuotaPlan.

@Test
void testQuotaPlan() throws Exception {
    var alice = new AccountInfo("alice", "alice", false, 1L);
    var registry1 = new RegistryCreate();
    registry1.setName("registry-basic");
    var registry1Result = FleetManagerApi.createRegistry(registry1, alice);
    assertNotEquals(RegistryStatusValue.failed, registry1Result.getStatus());
    Awaitility.await("registry available").atMost(30, TimeUnit.SECONDS).pollInterval(5, TimeUnit.SECONDS).until(() -> {
        var reg = FleetManagerApi.getRegistry(registry1Result.getId(), alice);
        return reg.getStatus().equals(RegistryStatusValue.ready);
    });
    var bob = new AccountInfo("bob", "bob", false, 2L);
    var registry2 = new RegistryCreate();
    registry2.setName("registry-premium");
    var registry2Result = FleetManagerApi.createRegistry(registry2, bob);
    assertNotEquals(RegistryStatusValue.failed, registry2Result.getStatus());
    Awaitility.await("registry available").atMost(30, TimeUnit.SECONDS).pollInterval(5, TimeUnit.SECONDS).until(() -> {
        var reg = FleetManagerApi.getRegistry(registry2Result.getId(), bob);
        return reg.getStatus().equals(RegistryStatusValue.ready);
    });
    TenantManagerClient tenantManager = Utils.createTenantManagerClient();
    // basic
    var tenant = tenantManager.getTenant(registry1Result.getId());
    var resources = tenant.getResources();
    Long l = null;
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            l = r.getLimit();
        }
    }
    assertNotNull(l);
    assertEquals(10, l);
    // premium
    tenant = tenantManager.getTenant(registry2Result.getId());
    resources = tenant.getResources();
    l = null;
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            l = r.getLimit();
        }
    }
    assertNotNull(l);
    assertEquals(100, l);
    // Update the limit value and recheck after forced reconciliation
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            r.setLimit(-1L);
        }
    }
    var ur = new UpdateRegistryTenantRequest();
    ur.setResources(resources);
    tenantManager.updateTenant(registry2Result.getId(), ur);
    // Check updated
    tenant = tenantManager.getTenant(registry2Result.getId());
    resources = tenant.getResources();
    l = null;
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            l = r.getLimit();
        }
    }
    assertNotNull(l);
    assertEquals(-1, l);
    // Restart fleet manager(s) so the quota plan is reconciled
    TestInfraManager.getInstance().restartFleetManager();
    tenant = tenantManager.getTenant(registry2Result.getId());
    resources = tenant.getResources();
    l = null;
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            l = r.getLimit();
        }
    }
    assertNotNull(l);
    assertEquals(100, l);
    // Delete
    FleetManagerApi.deleteRegistry(registry1Result.getId(), alice);
    FleetManagerApi.deleteRegistry(registry2Result.getId(), bob);
}
Also used : TenantManagerClient(io.apicurio.multitenant.client.TenantManagerClient) RegistryCreate(org.bf2.srs.fleetmanager.rest.publicapi.beans.RegistryCreate) AccountInfo(org.bf2.srs.fleetmanager.spi.common.model.AccountInfo) UpdateRegistryTenantRequest(io.apicurio.multitenant.api.datamodel.UpdateRegistryTenantRequest) Test(org.junit.jupiter.api.Test)

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