Search in sources :

Example 1 with LABEL_OPERATOR_TYPE

use of org.bf2.cos.fleetshard.support.resources.Resources.LABEL_OPERATOR_TYPE 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)

Example 2 with LABEL_OPERATOR_TYPE

use of org.bf2.cos.fleetshard.support.resources.Resources.LABEL_OPERATOR_TYPE in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class ConnectorDeploymentProvisioner method createManagedConnector.

private ManagedConnector createManagedConnector(String uow, ConnectorDeployment deployment, HasMetadata owner) {
    ManagedConnector connector = fleetShard.getConnector(deployment).orElseGet(() -> {
        LOGGER.info("Connector not found (cluster_id: {}, namespace_id: {}, connector_id: {}, deployment_id: {}, resource_version: {}), creating a new one", fleetShard.getClusterId(), deployment.getSpec().getNamespaceId(), deployment.getSpec().getConnectorId(), deployment.getId(), deployment.getMetadata().getResourceVersion());
        ManagedConnector answer = new ManagedConnector();
        answer.setMetadata(new ObjectMeta());
        answer.getMetadata().setNamespace(fleetShard.generateNamespaceId(deployment.getSpec().getNamespaceId()));
        answer.getMetadata().setName(Connectors.generateConnectorId(deployment.getId()));
        Resources.setLabels(answer, LABEL_CLUSTER_ID, fleetShard.getClusterId(), LABEL_CONNECTOR_ID, deployment.getSpec().getConnectorId(), LABEL_DEPLOYMENT_ID, deployment.getId());
        answer.getSpec().setClusterId(fleetShard.getClusterId());
        answer.getSpec().setConnectorId(deployment.getSpec().getConnectorId());
        answer.getSpec().setDeploymentId(deployment.getId());
        return answer;
    });
    // TODO: change APIs to include a single operator
    // move operator one level up
    // include full operator info in ConnectorDeployment APIs
    ArrayNode operatorsMeta = deployment.getSpec().getShardMetadata().withArray("operators");
    if (operatorsMeta.size() != 1) {
        throw new IllegalArgumentException("Multiple selectors are not yet supported");
    }
    OperatorSelector operatorSelector = new OperatorSelector(deployment.getSpec().getOperatorId(), operatorsMeta.get(0).requiredAt("/type").asText(), operatorsMeta.get(0).requiredAt("/version").asText());
    if (operatorSelector.getId() == null) {
        final OperatorSelector currentSelector = connector.getSpec().getOperatorSelector();
        // don't select a new operator if previously set.
        if (currentSelector != null && currentSelector.getId() != null) {
            operatorSelector.setId(currentSelector.getId());
        } else {
            Collection<Operator> operators = fleetShard.getOperators().stream().map(mco -> new Operator(mco.getMetadata().getName(), mco.getSpec().getType(), mco.getSpec().getVersion())).collect(Collectors.toList());
            OperatorSelectorUtil.assign(operatorSelector, operators).map(Operator::getId).ifPresent(operatorSelector::setId);
        }
    }
    if (operatorSelector.getId() != null) {
        Resources.setLabel(connector, LABEL_OPERATOR_ASSIGNED, operatorSelector.getId());
    }
    if (operatorSelector.getType() != null) {
        Resources.setLabel(connector, LABEL_OPERATOR_TYPE, operatorSelector.getType());
    }
    if (config != null) {
        config.connectors().labels().forEach((k, v) -> {
            Resources.setLabel(connector, k, v);
        });
        config.connectors().annotations().forEach((k, v) -> {
            Resources.setAnnotation(connector, k, v);
        });
    }
    Resources.setOwnerReferences(connector, owner);
    // add resource version to label
    Resources.setLabel(connector, LABEL_DEPLOYMENT_RESOURCE_VERSION, "" + deployment.getMetadata().getResourceVersion());
    // add uow
    Resources.setLabel(connector, LABEL_UOW, uow);
    connector.getSpec().getDeployment().setDeploymentResourceVersion(deployment.getMetadata().getResourceVersion());
    connector.getSpec().getDeployment().setDesiredState(deployment.getSpec().getDesiredState().getValue());
    connector.getSpec().getDeployment().setConnectorTypeId(deployment.getSpec().getConnectorTypeId());
    connector.getSpec().getDeployment().setConnectorResourceVersion(deployment.getSpec().getConnectorResourceVersion());
    KafkaConnectionSettings kafkaConnectionSettings = deployment.getSpec().getKafka();
    if (kafkaConnectionSettings != null) {
        connector.getSpec().getDeployment().setKafka(new KafkaSpec(kafkaConnectionSettings.getId(), kafkaConnectionSettings.getUrl()));
    }
    SchemaRegistryConnectionSettings schemaRegistryConnectionSettings = deployment.getSpec().getSchemaRegistry();
    if (schemaRegistryConnectionSettings != null) {
        connector.getSpec().getDeployment().setSchemaRegistry(new SchemaRegistrySpec(schemaRegistryConnectionSettings.getId(), schemaRegistryConnectionSettings.getUrl()));
    }
    connector.getSpec().getDeployment().setConnectorResourceVersion(deployment.getSpec().getConnectorResourceVersion());
    connector.getSpec().getDeployment().setSecret(Secrets.generateConnectorSecretId(deployment.getId()));
    connector.getSpec().getDeployment().setUnitOfWork(uow);
    connector.getSpec().setOperatorSelector(operatorSelector);
    LOGGER.info("Provisioning connector namespace: {}, name: {}, revision: {}", connector.getMetadata().getNamespace(), connector.getMetadata().getName(), connector.getSpec().getDeployment().getDeploymentResourceVersion());
    try {
        return fleetShard.createConnector(connector);
    } catch (Exception e) {
        LOGGER.warn("", e);
        throw e;
    }
}
Also used : Operator(org.bf2.cos.fleetshard.api.Operator) Connectors(org.bf2.cos.fleetshard.support.resources.Connectors) Secrets(org.bf2.cos.fleetshard.support.resources.Secrets) LoggerFactory(org.slf4j.LoggerFactory) LABEL_DEPLOYMENT_RESOURCE_VERSION(org.bf2.cos.fleetshard.support.resources.Resources.LABEL_DEPLOYMENT_RESOURCE_VERSION) KafkaSpec(org.bf2.cos.fleetshard.api.KafkaSpec) Operator(org.bf2.cos.fleetshard.api.Operator) Conditions(org.bf2.cos.fleetshard.api.Conditions) MetricsRecorder(org.bf2.cos.fleetshard.support.metrics.MetricsRecorder) OperatorSelector(org.bf2.cos.fleetshard.api.OperatorSelector) Resources.uid(org.bf2.cos.fleetshard.support.resources.Resources.uid) FleetShardClient(org.bf2.cos.fleetshard.sync.client.FleetShardClient) LABEL_CLUSTER_ID(org.bf2.cos.fleetshard.support.resources.Resources.LABEL_CLUSTER_ID) ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector) SchemaRegistrySpec(org.bf2.cos.fleetshard.api.SchemaRegistrySpec) ConnectorDeploymentStatus(org.bf2.cos.fleet.manager.model.ConnectorDeploymentStatus) LABEL_DEPLOYMENT_ID(org.bf2.cos.fleetshard.support.resources.Resources.LABEL_DEPLOYMENT_ID) Tags(io.micrometer.core.instrument.Tags) MetaV1Condition(org.bf2.cos.fleet.manager.model.MetaV1Condition) Logger(org.slf4j.Logger) SchemaRegistryConnectionSettings(org.bf2.cos.fleet.manager.model.SchemaRegistryConnectionSettings) Collection(java.util.Collection) KafkaConnectionSettings(org.bf2.cos.fleet.manager.model.KafkaConnectionSettings) FleetShardSyncConfig(org.bf2.cos.fleetshard.sync.FleetShardSyncConfig) ConnectorDeployment(org.bf2.cos.fleet.manager.model.ConnectorDeployment) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) LABEL_OPERATOR_TYPE(org.bf2.cos.fleetshard.support.resources.Resources.LABEL_OPERATOR_TYPE) LABEL_UOW(org.bf2.cos.fleetshard.support.resources.Resources.LABEL_UOW) OperatorSelectorUtil(org.bf2.cos.fleetshard.support.OperatorSelectorUtil) FleetManagerClient(org.bf2.cos.fleetshard.sync.client.FleetManagerClient) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) LABEL_OPERATOR_ASSIGNED(org.bf2.cos.fleetshard.support.resources.Resources.LABEL_OPERATOR_ASSIGNED) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Secret(io.fabric8.kubernetes.api.model.Secret) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Resources(org.bf2.cos.fleetshard.support.resources.Resources) LABEL_CONNECTOR_ID(org.bf2.cos.fleetshard.support.resources.Resources.LABEL_CONNECTOR_ID) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector) SchemaRegistrySpec(org.bf2.cos.fleetshard.api.SchemaRegistrySpec) KafkaSpec(org.bf2.cos.fleetshard.api.KafkaSpec) SchemaRegistryConnectionSettings(org.bf2.cos.fleet.manager.model.SchemaRegistryConnectionSettings) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) OperatorSelector(org.bf2.cos.fleetshard.api.OperatorSelector) KafkaConnectionSettings(org.bf2.cos.fleet.manager.model.KafkaConnectionSettings)

Aggregations

HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 Secret (io.fabric8.kubernetes.api.model.Secret)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 OwnerReferenceBuilder (io.fabric8.kubernetes.api.model.OwnerReferenceBuilder)1 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 Tags (io.micrometer.core.instrument.Tags)1 Collection (java.util.Collection)1 Collectors (java.util.stream.Collectors)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 ConnectorDeployment (org.bf2.cos.fleet.manager.model.ConnectorDeployment)1 ConnectorDeploymentStatus (org.bf2.cos.fleet.manager.model.ConnectorDeploymentStatus)1 KafkaConnectionSettings (org.bf2.cos.fleet.manager.model.KafkaConnectionSettings)1 MetaV1Condition (org.bf2.cos.fleet.manager.model.MetaV1Condition)1 SchemaRegistryConnectionSettings (org.bf2.cos.fleet.manager.model.SchemaRegistryConnectionSettings)1 Conditions (org.bf2.cos.fleetshard.api.Conditions)1 KafkaSpec (org.bf2.cos.fleetshard.api.KafkaSpec)1 ManagedConnector (org.bf2.cos.fleetshard.api.ManagedConnector)1 ManagedConnectorSpec (org.bf2.cos.fleetshard.api.ManagedConnectorSpec)1 Operator (org.bf2.cos.fleetshard.api.Operator)1