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);
}
}
}
}
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);
}
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");
});
}
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;
}
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);
}
Aggregations