use of org.bf2.cos.fleetshard.api.ManagedConnector in project cos-fleetshard by bf2fc6cc711aee1a0c2a.
the class ConnectorTestSupport method fleetShard.
public static FleetShardClient fleetShard(String clusterId, Collection<ManagedConnector> connectors, Collection<Secret> secrets) {
Map<String, ManagedConnector> allConnectors = connectors.stream().collect(Collectors.toMap(e -> e.getMetadata().getName(), Function.identity()));
Map<String, Secret> allSecrets = secrets.stream().collect(Collectors.toMap(e -> e.getMetadata().getName(), Function.identity()));
FleetShardClient answer = Mockito.mock(FleetShardClient.class);
when(answer.getClusterId()).thenAnswer(invocation -> clusterId);
when(answer.getConnector(any(ConnectorDeployment.class))).thenAnswer(invocation -> {
return lookupConnector(allConnectors.values(), clusterId, invocation.getArgument(0));
});
when(answer.getSecret(any(ConnectorDeployment.class))).thenAnswer(invocation -> {
return lookupSecret(allSecrets.values(), clusterId, invocation.getArgument(0));
});
when(answer.createConnector(any(ManagedConnector.class))).thenAnswer(invocation -> {
var arg = invocation.getArgument(0, ManagedConnector.class);
allConnectors.put(arg.getMetadata().getName(), arg);
return arg;
});
when(answer.createSecret(any(Secret.class))).thenAnswer(invocation -> {
var arg = invocation.getArgument(0, Secret.class);
allSecrets.put(arg.getMetadata().getName(), arg);
return arg;
});
when(answer.getOrCreateManagedConnectorCluster()).thenAnswer(invocation -> {
return new ManagedConnectorClusterBuilder().withMetadata(new ObjectMetaBuilder().withName(Clusters.CONNECTOR_CLUSTER_PREFIX + "-" + clusterId).addToLabels(Resources.LABEL_CLUSTER_ID, clusterId).build()).withSpec(new ManagedConnectorClusterSpecBuilder().withClusterId(clusterId).build()).build();
});
return answer;
}
use of org.bf2.cos.fleetshard.api.ManagedConnector 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.fleetshard.api.ManagedConnector in project cos-fleetshard by bf2fc6cc711aee1a0c2a.
the class NamespaceReaperWithLeftoversTest method namespaceIsProvisioned.
@Test
void namespaceIsProvisioned() {
final String deploymentId = ConfigProvider.getConfig().getValue("test.deployment.id", String.class);
final String statusUrl = "/api/connector_mgmt/v1/agent/kafka_connector_clusters/" + config.cluster().id() + "/status";
final String namespaceName = fleetShardClient.generateNamespaceId(deploymentId);
given().contentType(MediaType.TEXT_PLAIN).body(0L).post("/test/provisioner/namespaces");
until(() -> fleetShardClient.getNamespace(deploymentId), Objects::nonNull);
server.until(putRequestedFor(urlEqualTo(statusUrl)).withHeader(ContentTypeHeader.KEY, equalTo(APPLICATION_JSON)).withRequestBody(jp("$.namespaces.size()", "1")).withRequestBody(jp("$.namespaces[0].phase", Namespaces.PHASE_READY)).withRequestBody(jp("$.namespaces[0].version", "0")).withRequestBody(jp("$.namespaces[0].connectors_deployed", "0")));
final ManagedConnector connector = new ManagedConnectorBuilder().withMetadata(new ObjectMetaBuilder().withName(Connectors.generateConnectorId(deploymentId)).withNamespace(namespaceName).addToLabels(LABEL_CLUSTER_ID, config.cluster().id()).addToLabels(LABEL_CONNECTOR_ID, deploymentId).addToLabels(LABEL_DEPLOYMENT_ID, deploymentId).build()).withSpec(new ManagedConnectorSpecBuilder().withClusterId(config.cluster().id()).withConnectorId(deploymentId).withDeploymentId(deploymentId).withOperatorSelector(new OperatorSelectorBuilder().withId(deploymentId).build()).build()).build();
kubernetesClient.resources(ManagedConnector.class).inNamespace(connector.getMetadata().getNamespace()).create(connector);
untilAsserted(() -> {
assertThat(fleetShardClient.getAllConnectors()).isNotEmpty();
assertThat(fleetShardClient.getConnectors(connector.getMetadata().getNamespace())).isNotEmpty();
});
given().contentType(MediaType.TEXT_PLAIN).body(1L).post("/test/provisioner/namespaces");
server.until(putRequestedFor(urlEqualTo(statusUrl)).withHeader(ContentTypeHeader.KEY, equalTo(APPLICATION_JSON)).withRequestBody(matchingJsonPath("$.namespaces", WireMock.absent())));
}
use of org.bf2.cos.fleetshard.api.ManagedConnector in project cos-fleetshard by bf2fc6cc711aee1a0c2a.
the class FleetShardClient method getConnector.
public Optional<ManagedConnector> getConnector(String namespaceId, String deploymentId) {
if (connectorsInformer == null) {
throw new IllegalStateException("Informer must be started before adding handlers");
}
final String key = Cache.namespaceKeyFunc(generateNamespaceId(namespaceId), generateConnectorId(deploymentId));
final ManagedConnector val = connectorsInformer.getIndexer().getByKey(key);
return Optional.ofNullable(val);
}
use of org.bf2.cos.fleetshard.api.ManagedConnector in project cos-fleetshard by bf2fc6cc711aee1a0c2a.
the class CamelOperandController method doReify.
@SuppressFBWarnings("HARD_CODE_PASSWORD")
@Override
protected List<HasMetadata> doReify(ManagedConnector connector, CamelShardMetadata shardMetadata, ConnectorConfiguration<ObjectNode, ObjectNode> connectorConfiguration, ServiceAccountSpec serviceAccountSpec) {
final Map<String, String> properties = createSecretsData(connector, connectorConfiguration, serviceAccountSpec, configuration);
final ObjectNode errorHandler = createErrorHandler(shardMetadata, connector, connectorConfiguration.getErrorHandlerSpec());
final List<KameletEndpoint> stepDefinitions;
final KameletEndpoint source;
final KameletEndpoint sink;
//
switch(shardMetadata.getConnectorType()) {
case CONNECTOR_TYPE_SOURCE:
source = KameletEndpoint.kamelet(shardMetadata.getKamelets().getAdapter().getName());
source.getProperties().put("id", connector.getSpec().getDeploymentId() + "-source");
configureKameletProperties(source.getProperties(), connectorConfiguration.getConnectorSpec(), shardMetadata.getKamelets().getAdapter());
sink = KameletEndpoint.kamelet(shardMetadata.getKamelets().getKafka().getName());
sink.getProperties().put("id", connector.getSpec().getDeploymentId() + "-sink");
sink.getProperties().put("bootstrapServers", connector.getSpec().getDeployment().getKafka().getUrl());
sink.getProperties().put("user", SA_CLIENT_ID_PLACEHOLDER);
sink.getProperties().put("password", SA_CLIENT_SECRET_PLACEHOLDER);
configureKameletProperties(sink.getProperties(), connectorConfiguration.getConnectorSpec(), shardMetadata.getKamelets().getKafka());
if (hasSchemaRegistry(connector)) {
sink.getProperties().put("registryUrl", connector.getSpec().getDeployment().getSchemaRegistry().getUrl());
}
stepDefinitions = createSteps(connector, connectorConfiguration, shardMetadata, sink);
break;
case CONNECTOR_TYPE_SINK:
source = KameletEndpoint.kamelet(shardMetadata.getKamelets().getKafka().getName());
source.getProperties().put("id", connector.getSpec().getDeploymentId() + "-source");
source.getProperties().put("consumerGroup", connector.getSpec().getDeploymentId());
source.getProperties().put("bootstrapServers", connector.getSpec().getDeployment().getKafka().getUrl());
source.getProperties().put("user", SA_CLIENT_ID_PLACEHOLDER);
source.getProperties().put("password", SA_CLIENT_SECRET_PLACEHOLDER);
configureKameletProperties(source.getProperties(), connectorConfiguration.getConnectorSpec(), shardMetadata.getKamelets().getKafka());
if (hasSchemaRegistry(connector)) {
source.getProperties().put("registryUrl", connector.getSpec().getDeployment().getSchemaRegistry().getUrl());
}
sink = KameletEndpoint.kamelet(shardMetadata.getKamelets().getAdapter().getName());
sink.getProperties().put("id", connector.getSpec().getDeploymentId() + "-sink");
configureKameletProperties(sink.getProperties(), connectorConfiguration.getConnectorSpec(), shardMetadata.getKamelets().getAdapter());
stepDefinitions = createSteps(connector, connectorConfiguration, shardMetadata, source);
break;
default:
throw new IllegalArgumentException("Unknown connector type: " + shardMetadata.getConnectorType());
}
final Secret secret = new Secret();
secret.setMetadata(new ObjectMeta());
secret.getMetadata().setName(connector.getMetadata().getName() + Resources.CONNECTOR_SECRET_SUFFIX);
secret.setData(Map.of(APPLICATION_PROPERTIES, asBytesBase64(properties)));
final ObjectNode integration = createIntegrationSpec(secret.getMetadata().getName(), configuration, Map.of("CONNECTOR_SECRET_NAME", secret.getMetadata().getName(), "CONNECTOR_SECRET_CHECKSUM", Secrets.computeChecksum(secret), "CONNECTOR_ID", connector.getSpec().getConnectorId(), "CONNECTOR_DEPLOYMENT_ID", connector.getSpec().getDeploymentId()));
final KameletBinding binding = new KameletBinding();
binding.setMetadata(new ObjectMeta());
binding.getMetadata().setName(connector.getMetadata().getName());
binding.getMetadata().setAnnotations(new TreeMap<>());
binding.setSpec(new KameletBindingSpec());
binding.getSpec().setSource(source);
binding.getSpec().setSink(sink);
binding.getSpec().setErrorHandler(errorHandler);
binding.getSpec().setSteps(stepDefinitions);
binding.getSpec().setIntegration(integration);
Map<String, String> annotations = binding.getMetadata().getAnnotations();
if (shardMetadata.getAnnotations() != null) {
annotations.putAll(shardMetadata.getAnnotations());
}
if (configuration.labelSelection().enabled()) {
Operator assigned = connector.getStatus().getConnectorStatus().getAssignedOperator();
if (assigned != null && assigned.getId() != null) {
annotations.putIfAbsent(KAMEL_OPERATOR_ID, assigned.getId());
}
}
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_CONTAINER_IMAGE, shardMetadata.getConnectorImage());
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_KAMELETS_ENABLED, "false");
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_JVM_ENABLED, "false");
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_LOGGING_JSON, "false");
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_OWNER_TARGET_LABELS, LABELS_TO_TRANSFER);
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_PROMETHEUS_ENABLED, "true");
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_PROMETHEUS_POD_MONITOR, "false");
// health check annotations
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_ENABLED, "true");
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_READINESS_PROBE_ENABLED, "true");
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_LIVENESS_PROBE_ENABLED, "true");
CamelOperandConfiguration.Health health = configuration.health();
if (health != null) {
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_READINESS_SUCCESS_THRESHOLD, health.readinessSuccessThreshold());
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_READINESS_FAILURE_THRESHOLD, health.readinessFailureThreshold());
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_READINESS_PERIOD, health.readinessPeriodSeconds());
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_READINESS_TIMEOUT, health.readinessTimeoutSeconds());
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_LIVENESS_SUCCESS_THRESHOLD, health.livenessSuccessThreshold());
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_LIVENESS_FAILURE_THRESHOLD, health.livenessFailureThreshold());
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_LIVENESS_PERIOD, health.livenessPeriodSeconds());
annotations.putIfAbsent(TRAIT_CAMEL_APACHE_ORG_HEALTH_LIVENESS_TIMEOUT, health.livenessTimeoutSeconds());
}
if (configuration.connectors() != null) {
if (configuration.connectors().traits() != null) {
annotations.putAll(configuration.connectors().traits());
}
if (configuration.connectors().types() != null) {
final String typeId = connector.getSpec().getDeployment().getConnectorTypeId();
final CamelOperandConfiguration.ConnectorConfiguration typeConfig = configuration.connectors().types().get(typeId);
if (typeConfig != null && typeConfig.traits() != null) {
annotations.putAll(typeConfig.traits());
}
}
}
return List.of(secret, binding);
}
Aggregations