Search in sources :

Example 6 with ManagedConnector

use of org.bf2.cos.fleetshard.api.ManagedConnector in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class ConnectorStatusUpdaterTest method statusIsUpdated.

@Test
void statusIsUpdated() {
    final String clusterUrl = "/api/connector_mgmt/v1/agent/kafka_connector_clusters/" + config.cluster().id();
    final String statusUrl = clusterUrl + "/deployments/" + DEPLOYMENT_ID + "/status";
    final Condition condition = new Condition(null, uid(), null, uid(), uid(), uid());
    final Operator operator = new Operator(uid(), "operator-type", "1.2.3");
    final ManagedConnector connector = new ManagedConnectorBuilder().withMetadata(new ObjectMetaBuilder().withName(Connectors.generateConnectorId(DEPLOYMENT_ID)).withNamespace(ns).addToLabels(LABEL_CLUSTER_ID, config.cluster().id()).addToLabels(LABEL_CONNECTOR_ID, CONNECTOR_ID).addToLabels(LABEL_DEPLOYMENT_ID, DEPLOYMENT_ID).build()).withSpec(new ManagedConnectorSpecBuilder().withClusterId(config.cluster().id()).withConnectorId(CONNECTOR_ID).withDeploymentId(DEPLOYMENT_ID).withOperatorSelector(new OperatorSelectorBuilder().withId(operator.getId()).build()).build()).build();
    kubernetesClient.resources(ManagedConnector.class).inNamespace(ns).create(connector);
    connector.getStatus().setConnectorStatus(new ConnectorStatusSpecBuilder().withPhase(DESIRED_STATE_READY).withConditions(condition).withAssignedOperator(operator).build());
    kubernetesClient.resources(ManagedConnector.class).inNamespace(ns).withName(connector.getMetadata().getName()).replaceStatus(connector);
    untilAsserted(() -> {
        server.verify(putRequestedFor(urlEqualTo(statusUrl)).withHeader(ContentTypeHeader.KEY, equalTo(APPLICATION_JSON)).withRequestBody(matchingJsonPath("$.operators.assigned[?(@.version == '" + operator.getVersion() + "')]")).withRequestBody(matchingJsonPath("$.operators.assigned[?(@.type == '" + operator.getType() + "')]")).withRequestBody(matchingJsonPath("$.operators.assigned[?(@.id == '" + operator.getId() + "')]")).withRequestBody(matchingJsonPath("$[?($.phase == 'ready')]")));
    });
}
Also used : Condition(io.fabric8.kubernetes.api.model.Condition) Operator(org.bf2.cos.fleetshard.api.Operator) ManagedConnectorBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorBuilder) ManagedConnectorSpecBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorSpecBuilder) ConnectorStatusSpecBuilder(org.bf2.cos.fleetshard.api.ConnectorStatusSpecBuilder) 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 7 with ManagedConnector

use of org.bf2.cos.fleetshard.api.ManagedConnector in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class ConnectorDeletedTest method statusIsUpdated.

@Test
void statusIsUpdated() {
    final String clusterUrl = "/api/connector_mgmt/v1/agent/kafka_connector_clusters/" + config.cluster().id();
    final String statusUrl = clusterUrl + "/deployments/" + DEPLOYMENT_ID + "/status";
    final Condition condition = new Condition(null, uid(), null, uid(), uid(), uid());
    final Operator operator = new Operator(uid(), "operator-type", "1.2.3");
    final ManagedConnector connector = new ManagedConnectorBuilder().withMetadata(new ObjectMetaBuilder().withName(Connectors.generateConnectorId(DEPLOYMENT_ID)).withNamespace(ns).addToLabels(LABEL_CLUSTER_ID, config.cluster().id()).addToLabels(LABEL_CONNECTOR_ID, CONNECTOR_ID).addToLabels(LABEL_DEPLOYMENT_ID, DEPLOYMENT_ID).build()).withSpec(new ManagedConnectorSpecBuilder().withClusterId(config.cluster().id()).withConnectorId(CONNECTOR_ID).withDeploymentId(DEPLOYMENT_ID).withOperatorSelector(new OperatorSelectorBuilder().withId(operator.getId()).build()).build()).build();
    kubernetesClient.resources(ManagedConnector.class).inNamespace(ns).create(connector);
    connector.getStatus().setConnectorStatus(new ConnectorStatusSpecBuilder().withPhase(DESIRED_STATE_READY).withConditions(condition).withAssignedOperator(operator).build());
    kubernetesClient.resources(ManagedConnector.class).inNamespace(ns).withName(connector.getMetadata().getName()).replaceStatus(connector);
    untilAsserted(() -> {
        server.verify(putRequestedFor(urlEqualTo(statusUrl)).withHeader(ContentTypeHeader.KEY, equalTo(APPLICATION_JSON)).withRequestBody(matchingJsonPath("$[?($.phase == 'ready')]")));
    });
    connector.getStatus().setConnectorStatus(new ConnectorStatusSpecBuilder().withPhase(DESIRED_STATE_DELETED).withConditions(condition).withAssignedOperator(operator).build());
    kubernetesClient.resources(ManagedConnector.class).inNamespace(ns).withName(connector.getMetadata().getName()).replaceStatus(connector);
    untilAsserted(() -> {
        server.verify(putRequestedFor(urlEqualTo(statusUrl)).withHeader(ContentTypeHeader.KEY, equalTo(APPLICATION_JSON)).withRequestBody(matchingJsonPath("$[?($.phase == 'deleted')]")));
    });
    untilAsserted(() -> {
        assertThat(kubernetesClient.resources(ManagedConnector.class).inNamespace(ns).withName(connector.getMetadata().getName()).get()).isNull();
    });
}
Also used : Condition(io.fabric8.kubernetes.api.model.Condition) Operator(org.bf2.cos.fleetshard.api.Operator) ManagedConnectorBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorBuilder) ManagedConnectorSpecBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorSpecBuilder) ConnectorStatusSpecBuilder(org.bf2.cos.fleetshard.api.ConnectorStatusSpecBuilder) 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 8 with ManagedConnector

use of org.bf2.cos.fleetshard.api.ManagedConnector in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class ConnectorProvisionerWithMetaTest method connectorIsProvisioned.

@Test
void connectorIsProvisioned() {
    RestAssured.given().contentType(MediaType.TEXT_PLAIN).accept(MediaType.TEXT_PLAIN).body(0L).post("/test/provisioner/all");
    Secret secret = until(() -> fleetShardClient.getSecret(clusterId, clusterId), item -> Objects.equals("1", Resources.getLabel(item, Resources.LABEL_DEPLOYMENT_RESOURCE_VERSION)));
    ManagedConnector mc = until(() -> fleetShardClient.getConnector(clusterId, clusterId), item -> {
        return item.getSpec().getDeployment().getDeploymentResourceVersion() == 1L && item.getSpec().getDeployment().getSecret() != null;
    });
    assertThat(secret.getMetadata().getName()).isEqualTo(Secrets.generateConnectorSecretId(mc.getSpec().getDeploymentId()));
    assertThat(mc.getMetadata().getName()).startsWith(Resources.CONNECTOR_PREFIX);
    assertThat(mc.getMetadata().getLabels()).containsEntry("foo/barLabel", "baz");
    assertThat(mc.getMetadata().getAnnotations()).containsEntry("foo/barAnnotations", "baz");
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 9 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(NamespacedName id) {
    if (connectorsInformer == null) {
        throw new IllegalStateException("Informer must be started before adding handlers");
    }
    final String key = Cache.namespaceKeyFunc(id.getNamespace(), id.getName());
    final ManagedConnector val = connectorsInformer.getIndexer().getByKey(key);
    return Optional.ofNullable(val);
}
Also used : ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector)

Example 10 with ManagedConnector

use of org.bf2.cos.fleetshard.api.ManagedConnector in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class ConnectorController method handleAugmentation.

private UpdateControl<ManagedConnector> handleAugmentation(ManagedConnector connector) {
    if (connector.getSpec().getDeployment().getSecret() == null) {
        LOGGER.info("Secret for deployment not defines");
        return UpdateControl.noUpdate();
    }
    Secret secret = kubernetesClient.secrets().inNamespace(connector.getMetadata().getNamespace()).withName(connector.getSpec().getDeployment().getSecret()).get();
    if (secret == null) {
        boolean retry = hasCondition(connector, ManagedConnectorConditions.Type.Augmentation, ManagedConnectorConditions.Status.False, "SecretNotFound");
        if (!retry) {
            LOGGER.debug("Unable to find secret with name: {}", connector.getSpec().getDeployment().getSecret());
            setCondition(connector, ManagedConnectorConditions.Type.Augmentation, ManagedConnectorConditions.Status.False, "SecretNotFound", "Unable to find secret with name: " + connector.getSpec().getDeployment().getSecret());
            setCondition(connector, ManagedConnectorConditions.Type.Ready, ManagedConnectorConditions.Status.False, "AugmentationError", "AugmentationError");
            return UpdateControl.updateStatus(connector);
        } else {
            return UpdateControl.<ManagedConnector>noUpdate().rescheduleAfter(1500, TimeUnit.MILLISECONDS);
        }
    } else {
        final String connectorUow = connector.getSpec().getDeployment().getUnitOfWork();
        final String secretUow = secret.getMetadata().getLabels().get(Resources.LABEL_UOW);
        if (!Objects.equals(connectorUow, secretUow)) {
            boolean retry = hasCondition(connector, ManagedConnectorConditions.Type.Augmentation, ManagedConnectorConditions.Status.False, "SecretUoWMismatch");
            if (!retry) {
                LOGGER.debug("Secret and Connector UoW mismatch (connector: {}, secret: {})", connectorUow, secretUow);
                setCondition(connector, ManagedConnectorConditions.Type.Augmentation, ManagedConnectorConditions.Status.False, "SecretUoWMismatch", "Secret and Connector UoW mismatch (connector: " + connectorUow + ", secret: " + secretUow + ")");
                setCondition(connector, ManagedConnectorConditions.Type.Ready, ManagedConnectorConditions.Status.False, "AugmentationError", "AugmentationError");
                return UpdateControl.updateStatus(connector);
            } else {
                return UpdateControl.<ManagedConnector>noUpdate().rescheduleAfter(1500, TimeUnit.MILLISECONDS);
            }
        }
    }
    List<HasMetadata> resources;
    try {
        resources = operandController.reify(connector, secret);
    } catch (Exception e) {
        LOGGER.warn("Error reifying deployment {}", connector.getSpec().getDeploymentId(), e);
        setCondition(connector, ManagedConnectorConditions.Type.Augmentation, ManagedConnectorConditions.Status.False, "ReifyFailed", e instanceof WrappedRuntimeException ? e.getCause().getMessage() : e.getMessage());
        setCondition(connector, ManagedConnectorConditions.Type.Stopping, ManagedConnectorConditions.Status.True, "Stopping", "Stopping");
        connector.getStatus().setDeployment(connector.getSpec().getDeployment());
        connector.getStatus().setPhase(ManagedConnectorStatus.PhaseType.Stopping);
        connector.getStatus().getConnectorStatus().setPhase(STATE_FAILED);
        connector.getStatus().getConnectorStatus().setConditions(Collections.emptyList());
        return UpdateControl.updateStatus(connector);
    }
    for (var resource : resources) {
        if (resource.getMetadata().getLabels() == null) {
            resource.getMetadata().setLabels(new HashMap<>());
        }
        if (resource.getMetadata().getAnnotations() == null) {
            resource.getMetadata().setAnnotations(new HashMap<>());
        }
        ManagedConnectorSpec spec = connector.getSpec();
        final String rv = String.valueOf(spec.getDeployment().getDeploymentResourceVersion());
        final Map<String, String> labels = KubernetesResourceUtil.getOrCreateLabels(resource);
        labels.put(LABEL_CONNECTOR_OPERATOR, connector.getStatus().getConnectorStatus().getAssignedOperator().getId());
        labels.put(LABEL_CONNECTOR_ID, spec.getConnectorId());
        labels.put(LABEL_CONNECTOR_TYPE_ID, spec.getDeployment().getConnectorTypeId());
        labels.put(LABEL_DEPLOYMENT_ID, spec.getDeploymentId());
        labels.put(LABEL_CLUSTER_ID, spec.getClusterId());
        labels.put(LABEL_OPERATOR_TYPE, managedConnectorOperator.getSpec().getType());
        labels.put(LABEL_OPERATOR_OWNER, managedConnectorOperator.getMetadata().getName());
        labels.put(LABEL_DEPLOYMENT_RESOURCE_VERSION, rv);
        // Kubernetes recommended labels
        labels.put(LABEL_KUBERNETES_NAME, spec.getConnectorId());
        labels.put(LABEL_KUBERNETES_INSTANCE, spec.getDeploymentId());
        labels.put(LABEL_KUBERNETES_VERSION, rv);
        labels.put(LABEL_KUBERNETES_COMPONENT, Resources.COMPONENT_CONNECTOR);
        labels.put(LABEL_KUBERNETES_PART_OF, spec.getClusterId());
        labels.put(LABEL_KUBERNETES_MANAGED_BY, managedConnectorOperator.getMetadata().getName());
        labels.put(LABEL_KUBERNETES_CREATED_BY, managedConnectorOperator.getMetadata().getName());
        config.connectors().targetLabels().ifPresent(items -> {
            for (String item : items) {
                copyLabel(item, connector, resource);
            }
        });
        config.connectors().targetAnnotations().ifPresent(items -> {
            for (String item : items) {
                copyAnnotation(item, connector, resource);
            }
        });
        resource.getMetadata().setOwnerReferences(List.of(new OwnerReferenceBuilder().withApiVersion(connector.getApiVersion()).withKind(connector.getKind()).withName(connector.getMetadata().getName()).withUid(connector.getMetadata().getUid()).withAdditionalProperties(Map.of("namespace", connector.getMetadata().getNamespace())).withBlockOwnerDeletion(true).build()));
        var result = kubernetesClient.resource(resource).inNamespace(connector.getMetadata().getNamespace()).createOrReplace();
        LOGGER.debug("Resource {}:{}:{}@{} updated/created", result.getApiVersion(), result.getKind(), result.getMetadata().getName(), result.getMetadata().getNamespace());
    }
    connector.getStatus().setDeployment(connector.getSpec().getDeployment());
    connector.getStatus().setPhase(ManagedConnectorStatus.PhaseType.Monitor);
    connector.getStatus().getConnectorStatus().setConditions(Collections.emptyList());
    setCondition(connector, ManagedConnectorConditions.Type.Resync, false);
    setCondition(connector, ManagedConnectorConditions.Type.Monitor, true);
    setCondition(connector, ManagedConnectorConditions.Type.Ready, true);
    setCondition(connector, ManagedConnectorConditions.Type.Augmentation, true);
    return UpdateControl.updateStatus(connector);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) ManagedConnectorSpec(org.bf2.cos.fleetshard.api.ManagedConnectorSpec) WrappedRuntimeException(org.bf2.cos.fleetshard.support.exceptions.WrappedRuntimeException) WrappedRuntimeException(org.bf2.cos.fleetshard.support.exceptions.WrappedRuntimeException)

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