Search in sources :

Example 21 with ManagedConnector

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;
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ManagedConnectorClusterBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorClusterBuilder) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Connectors(org.bf2.cos.fleetshard.support.resources.Connectors) Secrets(org.bf2.cos.fleetshard.support.resources.Secrets) Secrets.toBase64(org.bf2.cos.fleetshard.support.resources.Secrets.toBase64) ConnectorDeploymentSpec(org.bf2.cos.fleet.manager.model.ConnectorDeploymentSpec) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Resources.uid(org.bf2.cos.fleetshard.support.resources.Resources.uid) Serialization(io.fabric8.kubernetes.client.utils.Serialization) Map(java.util.Map) FleetShardClient(org.bf2.cos.fleetshard.sync.client.FleetShardClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) ConnectorDeploymentAllOfMetadata(org.bf2.cos.fleet.manager.model.ConnectorDeploymentAllOfMetadata) ServiceAccount(org.bf2.cos.fleet.manager.model.ServiceAccount) ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector) Clusters(org.bf2.cos.fleetshard.support.resources.Clusters) ConnectorDesiredState(org.bf2.cos.fleet.manager.model.ConnectorDesiredState) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) SchemaRegistryConnectionSettings(org.bf2.cos.fleet.manager.model.SchemaRegistryConnectionSettings) Collection(java.util.Collection) KafkaConnectionSettings(org.bf2.cos.fleet.manager.model.KafkaConnectionSettings) UUID(java.util.UUID) FleetShardSyncConfig(org.bf2.cos.fleetshard.sync.FleetShardSyncConfig) Mockito.when(org.mockito.Mockito.when) ConnectorDeployment(org.bf2.cos.fleet.manager.model.ConnectorDeployment) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) List(java.util.List) FleetManagerClient(org.bf2.cos.fleetshard.sync.client.FleetManagerClient) Optional(java.util.Optional) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) Secret(io.fabric8.kubernetes.api.model.Secret) ManagedConnectorCluster(org.bf2.cos.fleetshard.api.ManagedConnectorCluster) ManagedConnectorClusterSpecBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorClusterSpecBuilder) Collections(java.util.Collections) Resources(org.bf2.cos.fleetshard.support.resources.Resources) Secret(io.fabric8.kubernetes.api.model.Secret) FleetShardClient(org.bf2.cos.fleetshard.sync.client.FleetShardClient) ConnectorDeployment(org.bf2.cos.fleet.manager.model.ConnectorDeployment) ManagedConnectorClusterBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorClusterBuilder) ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) ManagedConnectorClusterSpecBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorClusterSpecBuilder)

Example 22 with ManagedConnector

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");
    });
}
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 23 with ManagedConnector

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())));
}
Also used : ManagedConnectorBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorBuilder) Objects(java.util.Objects) ManagedConnectorSpecBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorSpecBuilder) ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) OperatorSelectorBuilder(org.bf2.cos.fleetshard.api.OperatorSelectorBuilder) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 24 with ManagedConnector

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);
}
Also used : ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector)

Example 25 with ManagedConnector

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);
}
Also used : Operator(org.bf2.cos.fleetshard.api.Operator) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) KameletBindingSpec(org.bf2.cos.fleetshard.operator.camel.model.KameletBindingSpec) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KameletEndpoint(org.bf2.cos.fleetshard.operator.camel.model.KameletEndpoint) Secret(io.fabric8.kubernetes.api.model.Secret) KameletBinding(org.bf2.cos.fleetshard.operator.camel.model.KameletBinding) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

ManagedConnector (org.bf2.cos.fleetshard.api.ManagedConnector)24 Secret (io.fabric8.kubernetes.api.model.Secret)15 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)8 Map (java.util.Map)6 FleetShardSyncConfig (org.bf2.cos.fleetshard.sync.FleetShardSyncConfig)6 FleetManagerClient (org.bf2.cos.fleetshard.sync.client.FleetManagerClient)6 FleetShardClient (org.bf2.cos.fleetshard.sync.client.FleetShardClient)6 Test (org.junit.jupiter.api.Test)6 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)5 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)5 List (java.util.List)5 ConnectorDeployment (org.bf2.cos.fleet.manager.model.ConnectorDeployment)5 Operator (org.bf2.cos.fleetshard.api.Operator)5 ServiceAccountSpec (org.bf2.cos.fleetshard.api.ServiceAccountSpec)5 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 ManagedConnectorBuilder (org.bf2.cos.fleetshard.api.ManagedConnectorBuilder)4 ConnectorConfiguration (org.bf2.cos.fleetshard.operator.connector.ConnectorConfiguration)4