Search in sources :

Example 1 with ConnectorNamespace

use of org.bf2.cos.fleet.manager.model.ConnectorNamespace in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class ConnectorNamespaceProvisioner method provisionNamespaces.

private void provisionNamespaces(Collection<ConnectorNamespace> namespaces, boolean sync) {
    for (ConnectorNamespace namespace : namespaces) {
        this.recorder.record(() -> provision(namespace), Tags.of(TAG_NAMESPACE_ID, namespace.getId()), e -> {
            LOGGER.error("Failure while trying to provision connector namespace: id={}, revision={}", namespace.getId(), namespace.getResourceVersion(), e);
            try {
                MetaV1Condition condition = new MetaV1Condition();
                condition.setType(Conditions.TYPE_READY);
                condition.setStatus(Conditions.STATUS_FALSE);
                condition.setReason(Conditions.FAILED_TO_CREATE_OR_UPDATE_RESOURCE_REASON);
                condition.setMessage(e.getMessage());
                ConnectorNamespaceStatus status = new ConnectorNamespaceStatus().id(namespace.getId()).version("" + namespace.getResourceVersion()).phase(ConnectorNamespaceState.DISCONNECTED).conditions(List.of(condition));
                fleetManager.updateNamespaceStatus(fleetShard.getClusterId(), namespace.getId(), status);
            } catch (Exception ex) {
                LOGGER.warn("Error wile reporting failure to the control plane", e);
            }
            fleetShard.getConnectorCluster().ifPresent(cc -> {
                fleetShard.broadcast("Warning", "FailedToCreateOrUpdateResource", String.format("Unable to create or update namespace %s, revision: %s, reason: %s", namespace.getId(), namespace.getResourceVersion(), e.getMessage()), cc);
            });
        });
    }
    if (sync) {
        Set<String> knownIds = namespaces.stream().map(ConnectorNamespace::getId).collect(Collectors.toSet());
        for (Namespace namespace : fleetShard.getNamespaces()) {
            String nsId = Resources.getLabel(namespace, Resources.LABEL_NAMESPACE_ID);
            if (nsId == null || knownIds.contains(nsId)) {
                continue;
            }
            try {
                Resources.setLabels(namespace, Resources.LABEL_NAMESPACE_STATE, Namespaces.PHASE_DELETED);
                Resources.setLabels(namespace, Resources.LABEL_NAMESPACE_STATE_FORCED, "true");
                fleetShard.getKubernetesClient().namespaces().withName(namespace.getMetadata().getName()).replace(namespace);
            } catch (Exception e) {
                LOGGER.warn("Error marking na {} for deletion (sync)", namespace.getMetadata().getName(), e);
            }
        }
    }
}
Also used : ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) MetaV1Condition(org.bf2.cos.fleet.manager.model.MetaV1Condition) ConnectorNamespaceStatus(org.bf2.cos.fleet.manager.model.ConnectorNamespaceStatus) ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) Namespace(io.fabric8.kubernetes.api.model.Namespace)

Example 2 with ConnectorNamespace

use of org.bf2.cos.fleet.manager.model.ConnectorNamespace in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class ConnectorNamespaceProvisioner method provision.

public void provision(ConnectorNamespace connectorNamespace) {
    LOGGER.info("Got cluster_id: {}, namespace_d: {}, state: {}, connectors_deployed: {}", fleetShard.getClusterId(), connectorNamespace.getId(), connectorNamespace.getStatus().getState(), connectorNamespace.getStatus().getConnectorsDeployed());
    String uow = uid();
    String state = Namespaces.PHASE_READY;
    switch(connectorNamespace.getStatus().getState()) {
        case DELETED:
        case DELETING:
            if (connectorNamespace.getStatus().getConnectorsDeployed() == 0) {
                if (fleetShard.getNamespace(connectorNamespace.getId()).isEmpty()) {
                    LOGGER.info("Namespace {} is being deleted and does not exists, skip provisioning", connectorNamespace.getId());
                    return;
                }
                state = Namespaces.PHASE_DELETED;
            }
            break;
        default:
            state = Namespaces.PHASE_READY;
            break;
    }
    boolean quota = hasQuota(connectorNamespace);
    Namespace ns = new Namespace();
    KubernetesResourceUtil.getOrCreateMetadata(ns).setName(fleetShard.generateNamespaceId(connectorNamespace.getId()));
    Resources.setLabels(ns, Resources.LABEL_UOW, uow, Resources.LABEL_CLUSTER_ID, fleetShard.getClusterId(), Resources.LABEL_NAMESPACE_ID, connectorNamespace.getId(), Resources.LABEL_NAMESPACE_STATE, state, Resources.LABEL_KUBERNETES_NAME, KubernetesResourceUtil.sanitizeName(connectorNamespace.getName()), Resources.LABEL_KUBERNETES_MANAGED_BY, fleetShard.getClusterId(), Resources.LABEL_KUBERNETES_CREATED_BY, fleetShard.getClusterId(), Resources.LABEL_KUBERNETES_PART_OF, fleetShard.getClusterId(), Resources.LABEL_KUBERNETES_COMPONENT, Resources.COMPONENT_NAMESPACE, Resources.LABEL_KUBERNETES_INSTANCE, connectorNamespace.getId(), Resources.LABEL_KUBERNETES_VERSION, "" + connectorNamespace.getResourceVersion(), Resources.LABEL_NAMESPACE_TENANT_KIND, connectorNamespace.getTenant().getKind().getValue(), Resources.LABEL_NAMESPACE_TENANT_ID, KubernetesResourceUtil.sanitizeName(connectorNamespace.getTenant().getId()));
    Resources.setAnnotations(ns, Resources.ANNOTATION_NAMESPACE_EXPIRATION, connectorNamespace.getExpiration(), Resources.ANNOTATION_NAMESPACE_QUOTA, Boolean.toString(quota));
    fleetShard.createNamespace(ns);
    if (quota) {
        LOGGER.debug("Creating LimitRange for namespace: {}", ns.getMetadata().getName());
        createResourceLimit(uow, connectorNamespace);
        LOGGER.debug("Creating ResourceQuota for namespace: {}", ns.getMetadata().getName());
        createResourceQuota(uow, connectorNamespace);
    }
    copyAddonPullSecret(uow, ns);
}
Also used : ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) Namespace(io.fabric8.kubernetes.api.model.Namespace)

Example 3 with ConnectorNamespace

use of org.bf2.cos.fleet.manager.model.ConnectorNamespace in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class NamespaceProvisionerTest method nameIsSanitized.

@Test
void nameIsSanitized() {
    // 
    // Given that no resources associated to the provided deployment exist
    // 
    final ConnectorNamespace namespace = new ConnectorNamespace();
    namespace.id(uid());
    namespace.name("--eval");
    ConnectorNamespaceTenant tenant = new ConnectorNamespaceTenant().id(uid()).kind(ConnectorNamespaceTenantKind.ORGANISATION);
    namespace.setStatus(new ConnectorNamespaceStatus1().state(ConnectorNamespaceState.READY).connectorsDeployed(0));
    namespace.setTenant(tenant);
    namespace.setExpiration(new Date().toString());
    final List<ManagedConnector> connectors = List.of();
    final List<Secret> secrets = List.of();
    final FleetShardClient fleetShard = ConnectorTestSupport.fleetShard(CLUSTER_ID, connectors, secrets);
    final FleetManagerClient fleetManager = ConnectorTestSupport.fleetManagerClient();
    final FleetShardSyncConfig config = ConnectorTestSupport.config();
    final MeterRegistry registry = Mockito.mock(MeterRegistry.class);
    final ConnectorNamespaceProvisioner provisioner = new ConnectorNamespaceProvisioner(config, fleetShard, fleetManager, registry);
    final ArgumentCaptor<Namespace> nc = ArgumentCaptor.forClass(Namespace.class);
    // 
    // When deployment is applied
    // 
    provisioner.provision(namespace);
    verify(fleetShard).createNamespace(nc.capture());
    // 
    // Then resources must be created according to the deployment
    // 
    assertThat(nc.getValue()).satisfies(val -> {
        assertThat(val.getMetadata().getLabels()).containsEntry(LABEL_KUBERNETES_NAME, "a--eval");
    });
}
Also used : FleetShardClient(org.bf2.cos.fleetshard.sync.client.FleetShardClient) ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) ConnectorNamespaceStatus1(org.bf2.cos.fleet.manager.model.ConnectorNamespaceStatus1) ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector) ConnectorNamespaceProvisioner(org.bf2.cos.fleetshard.sync.resources.ConnectorNamespaceProvisioner) Date(java.util.Date) ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) Namespace(io.fabric8.kubernetes.api.model.Namespace) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Secret(io.fabric8.kubernetes.api.model.Secret) FleetManagerClient(org.bf2.cos.fleetshard.sync.client.FleetManagerClient) FleetShardSyncConfig(org.bf2.cos.fleetshard.sync.FleetShardSyncConfig) ConnectorNamespaceTenant(org.bf2.cos.fleet.manager.model.ConnectorNamespaceTenant) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectorNamespace

use of org.bf2.cos.fleet.manager.model.ConnectorNamespace in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class SyncTestSupport method namespace.

public static ConnectorNamespace namespace(String id, String name) {
    ConnectorNamespace answer = new ConnectorNamespace().id(id).name(name);
    ConnectorNamespaceTenant tenant = new ConnectorNamespaceTenant().id(uid()).kind(ConnectorNamespaceTenantKind.ORGANISATION);
    answer.setStatus(new ConnectorNamespaceStatus1().state(ConnectorNamespaceState.READY).connectorsDeployed(0));
    answer.setTenant(tenant);
    answer.setExpiration(new Date().toString());
    return answer;
}
Also used : ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) ConnectorNamespaceStatus1(org.bf2.cos.fleet.manager.model.ConnectorNamespaceStatus1) ConnectorNamespaceTenant(org.bf2.cos.fleet.manager.model.ConnectorNamespaceTenant) Date(java.util.Date)

Example 5 with ConnectorNamespace

use of org.bf2.cos.fleet.manager.model.ConnectorNamespace in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class SyncTestSupport method namespaceList.

public static ObjectNode namespaceList(ConnectorNamespace... namespaces) {
    var items = new ConnectorNamespaceList();
    items.page(1);
    items.size(namespaces.length);
    items.total(namespaces.length);
    for (ConnectorNamespace namespace : namespaces) {
        items.addItemsItem(namespace);
    }
    return Serialization.jsonMapper().convertValue(items, ObjectNode.class);
}
Also used : ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) ConnectorNamespaceList(org.bf2.cos.fleet.manager.model.ConnectorNamespaceList)

Aggregations

ConnectorNamespace (org.bf2.cos.fleet.manager.model.ConnectorNamespace)7 Namespace (io.fabric8.kubernetes.api.model.Namespace)3 ConnectorNamespaceStatus1 (org.bf2.cos.fleet.manager.model.ConnectorNamespaceStatus1)3 Date (java.util.Date)2 ConnectorNamespaceList (org.bf2.cos.fleet.manager.model.ConnectorNamespaceList)2 ConnectorNamespaceTenant (org.bf2.cos.fleet.manager.model.ConnectorNamespaceTenant)2 Secret (io.fabric8.kubernetes.api.model.Secret)1 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ConnectorNamespaceStatus (org.bf2.cos.fleet.manager.model.ConnectorNamespaceStatus)1 MetaV1Condition (org.bf2.cos.fleet.manager.model.MetaV1Condition)1 ManagedConnector (org.bf2.cos.fleetshard.api.ManagedConnector)1 FleetShardSyncConfig (org.bf2.cos.fleetshard.sync.FleetShardSyncConfig)1 FleetManagerClient (org.bf2.cos.fleetshard.sync.client.FleetManagerClient)1 FleetShardClient (org.bf2.cos.fleetshard.sync.client.FleetShardClient)1 ConnectorNamespaceProvisioner (org.bf2.cos.fleetshard.sync.resources.ConnectorNamespaceProvisioner)1 Test (org.junit.jupiter.api.Test)1