Search in sources :

Example 1 with ManagedKafkaStatusBuilder

use of org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class ConditionUtilsTest method testFindManagedKafkaCondition.

@Test
void testFindManagedKafkaCondition() {
    ManagedKafka mk = new ManagedKafkaBuilder().withMetadata(new ObjectMetaBuilder().withNamespace("test").withName("my-managed-kafka").build()).withSpec(new ManagedKafkaSpecBuilder().withNewVersions().withKafka("2.6.0").withStrimzi("0.21.1").endVersions().build()).withStatus(new ManagedKafkaStatusBuilder().addNewCondition().withStatus(Status.True.name()).withType(ManagedKafkaCondition.Type.Ready.name()).endCondition().build()).build();
    assertNotNull(ConditionUtils.findManagedKafkaCondition(mk.getStatus().getConditions(), ManagedKafkaCondition.Type.Ready).get());
}
Also used : ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) ManagedKafkaStatusBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder) ManagedKafkaSpecBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaSpecBuilder) ManagedKafkaBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with ManagedKafkaStatusBuilder

use of org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class ControlPlaneTest method testManagedKafkaStatusComparison.

@Test
public void testManagedKafkaStatusComparison() {
    ManagedKafkaStatus status = null;
    assertFalse(ControlPlane.statusChanged(status, status));
    status = new ManagedKafkaStatus();
    assertFalse(ControlPlane.statusChanged(status, null));
    assertFalse(ControlPlane.statusChanged(status, status));
    assertTrue(ControlPlane.statusChanged(null, status));
    ManagedKafkaStatus newStatus = new ManagedKafkaStatusBuilder().withUpdatedTimestamp("2020-01-01").build();
    assertTrue(ControlPlane.statusChanged(status, newStatus));
    assertTrue(ControlPlane.statusChanged(newStatus, status));
    status.setConditions(new ArrayList<>());
    status.setUpdatedTimestamp("2021-01-01");
    assertTrue(ControlPlane.statusChanged(newStatus, status));
    newStatus.setUpdatedTimestamp("2022-01-01");
    assertTrue(ControlPlane.statusChanged(status, newStatus));
}
Also used : ManagedKafkaStatusBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder) ManagedKafkaStatus(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus) Test(org.junit.jupiter.api.Test)

Example 3 with ManagedKafkaStatusBuilder

use of org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class ManagedKafkaController method updateManagedKafkaStatus.

/**
 * Extract from the current KafkaInstance overall status (Kafka, Canary and AdminServer)
 * a corresponding list of ManagedKafkaCondition(s) to set on the ManagedKafka status
 *
 * @param managedKafka ManagedKafka instance
 */
private void updateManagedKafkaStatus(ManagedKafka managedKafka) {
    // add status if not already available on the ManagedKafka resource
    ManagedKafkaStatus status = Objects.requireNonNullElse(managedKafka.getStatus(), new ManagedKafkaStatusBuilder().build());
    status.setUpdatedTimestamp(ConditionUtils.iso8601Now());
    managedKafka.setStatus(status);
    // add conditions if not already available
    List<ManagedKafkaCondition> managedKafkaConditions = managedKafka.getStatus().getConditions();
    if (managedKafkaConditions == null) {
        managedKafkaConditions = new ArrayList<>();
        status.setConditions(managedKafkaConditions);
    }
    Optional<ManagedKafkaCondition> optReady = ConditionUtils.findManagedKafkaCondition(managedKafkaConditions, ManagedKafkaCondition.Type.Ready);
    ManagedKafkaCondition ready = null;
    if (optReady.isPresent()) {
        ready = optReady.get();
    } else {
        ready = ConditionUtils.buildCondition(ManagedKafkaCondition.Type.Ready, Status.Unknown);
        managedKafkaConditions.add(ready);
    }
    // a not valid ManagedKafka skips the handling of it, so the status will report an error condition
    OperandReadiness readiness = this.validity(managedKafka).orElse(kafkaInstance.getReadiness(managedKafka));
    ConditionUtils.updateConditionStatus(ready, readiness.getStatus(), readiness.getReason(), readiness.getMessage());
    // routes should always be set on the CR status, even if it's just an empty list
    status.setRoutes(List.of());
    int replicas = kafkaCluster.getReplicas(managedKafka);
    if (ingressControllerManagerInstance.isResolvable()) {
        IngressControllerManager ingressControllerManager = ingressControllerManagerInstance.get();
        List<ManagedKafkaRoute> routes = ingressControllerManager.getManagedKafkaRoutesFor(managedKafka);
        // expect route for each broker + 1 for bootstrap URL + 1 for Admin API server
        int expectedNumRoutes = replicas + NUM_NON_BROKER_ROUTES;
        if (routes.size() >= expectedNumRoutes && routes.stream().noneMatch(r -> "".equals(r.getRouter()))) {
            status.setRoutes(routes);
        }
    }
    if (Status.True.equals(readiness.getStatus())) {
        status.setCapacity(new ManagedKafkaCapacityBuilder(managedKafka.getSpec().getCapacity()).withMaxDataRetentionSize(kafkaInstance.getKafkaCluster().calculateRetentionSize(managedKafka)).build());
        // the versions in the status are updated incrementally copying the spec only when each stage ends
        VersionsBuilder versionsBuilder = status.getVersions() != null ? new VersionsBuilder(status.getVersions()) : new VersionsBuilder(managedKafka.getSpec().getVersions());
        if (!Reason.StrimziUpdating.equals(readiness.getReason()) && !this.strimziManager.hasStrimziChanged(managedKafka)) {
            versionsBuilder.withStrimzi(managedKafka.getSpec().getVersions().getStrimzi());
        }
        if (!Reason.KafkaUpdating.equals(readiness.getReason()) && !this.kafkaManager.hasKafkaVersionChanged(managedKafka)) {
            versionsBuilder.withKafka(managedKafka.getSpec().getVersions().getKafka());
        }
        if (!Reason.KafkaIbpUpdating.equals(readiness.getReason()) && !this.kafkaManager.hasKafkaIbpVersionChanged(managedKafka)) {
            String kafkaIbp = managedKafka.getSpec().getVersions().getKafkaIbp() != null ? managedKafka.getSpec().getVersions().getKafkaIbp() : AbstractKafkaCluster.getKafkaIbpVersion(managedKafka.getSpec().getVersions().getKafka());
            versionsBuilder.withKafkaIbp(kafkaIbp);
        }
        status.setVersions(versionsBuilder.build());
        status.setAdminServerURI(kafkaInstance.getAdminServer().uri(managedKafka));
        status.setServiceAccounts(managedKafka.getSpec().getServiceAccounts());
    }
}
Also used : DeleteControl(io.javaoperatorsdk.operator.api.DeleteControl) ManagedKafkaResourceClient(org.bf2.common.ManagedKafkaResourceClient) Context(io.javaoperatorsdk.operator.api.Context) Status(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition.Status) Timed(io.micrometer.core.annotation.Timed) StrimziVersionStatus(org.bf2.operator.resources.v1alpha1.StrimziVersionStatus) Logger(org.jboss.logging.Logger) StrimziManager(org.bf2.operator.managers.StrimziManager) ManagedKafkaRoute(org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute) ResourceEventSource(org.bf2.operator.events.ResourceEventSource) ArrayList(java.util.ArrayList) Controller(io.javaoperatorsdk.operator.api.Controller) VersionsBuilder(org.bf2.operator.resources.v1alpha1.VersionsBuilder) Inject(javax.inject.Inject) KafkaInstance(org.bf2.operator.operands.KafkaInstance) ManagedKafkaStatus(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus) UpdateControl(io.javaoperatorsdk.operator.api.UpdateControl) AbstractKafkaCluster(org.bf2.operator.operands.AbstractKafkaCluster) KafkaManager(org.bf2.operator.managers.KafkaManager) Instance(javax.enterprise.inject.Instance) NDC(org.jboss.logging.NDC) KafkaInstanceConfiguration(org.bf2.operator.operands.KafkaInstanceConfiguration) ManagedKafkaStatusBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder) IngressControllerManager(org.bf2.operator.managers.IngressControllerManager) ConditionUtils(org.bf2.common.ConditionUtils) Reason(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition.Reason) Objects(java.util.Objects) List(java.util.List) Counted(io.micrometer.core.annotation.Counted) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) OperandReadiness(org.bf2.operator.operands.OperandReadiness) ManagedKafkaCapacityBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaCapacityBuilder) Optional(java.util.Optional) ResourceController(io.javaoperatorsdk.operator.api.ResourceController) EventSourceManager(io.javaoperatorsdk.operator.processing.event.EventSourceManager) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) ManagedKafkaCapacityBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaCapacityBuilder) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) VersionsBuilder(org.bf2.operator.resources.v1alpha1.VersionsBuilder) IngressControllerManager(org.bf2.operator.managers.IngressControllerManager) ManagedKafkaStatusBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder) ManagedKafkaRoute(org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute) ManagedKafkaStatus(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus) OperandReadiness(org.bf2.operator.operands.OperandReadiness)

Example 4 with ManagedKafkaStatusBuilder

use of org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class UpdateTest method testControlPlaneUpdates.

@Test
public void testControlPlaneUpdates() {
    ManagedKafka managedKafka = PollerTest.exampleManagedKafka();
    managedKafka.getMetadata().setNamespace(managedKafka.getId());
    managedKafka.setStatus(new ManagedKafkaStatusBuilder().addNewCondition().withStatus("Installed").endCondition().build());
    managedKafkaClient.create(managedKafka);
    controlPlane.updateKafkaClusterStatus(PollerTest.exampleManagedKafka(), managedKafka);
    assertEquals("Installed", getUpdates().getValue().get(PollerTest.ID).getConditions().get(0).getStatus());
    // simulate a resync
    // for now we're just looking for equality
    Mockito.clearInvocations(controlPlaneRestClient);
    controlPlane.updateKafkaClusterStatus(managedKafka, managedKafka);
    // should not be sent
    Mockito.verifyNoInteractions(controlPlaneRestClient);
    // send everything
    controlPlane.sendResync();
    ArgumentCaptor<Map<String, ManagedKafkaStatus>> statusCaptor = getUpdates();
    assertEquals("Installed", statusCaptor.getValue().get(PollerTest.ID).getConditions().get(0).getStatus());
}
Also used : ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) ManagedKafkaStatusBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder) Map(java.util.Map) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 5 with ManagedKafkaStatusBuilder

use of org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class StrimziManagerTest method testStrimziVersionChange.

@Test
public void testStrimziVersionChange() {
    ManagedKafka mk = ManagedKafka.getDummyInstance(1);
    mk.getSpec().getVersions().setStrimzi("strimzi-cluster-operator.v1");
    Kafka kafka = this.kafkaCluster.kafkaFrom(mk, null);
    kafkaClient.create(kafka);
    // Kafka reconcile not paused and current label version as the ManagedKafka one
    assertFalse(kafka.getMetadata().getAnnotations().containsKey(StrimziManager.STRIMZI_PAUSE_RECONCILE_ANNOTATION));
    assertEquals(kafka.getMetadata().getLabels().get(this.strimziManager.getVersionLabel()), mk.getSpec().getVersions().getStrimzi());
    // ManagedKafka and Kafka updated their status information
    mk.setStatus(new ManagedKafkaStatusBuilder().withVersions(new VersionsBuilder().withStrimzi("strimzi-cluster-operator.v1").build()).build());
    kafka.setStatus(new KafkaStatusBuilder().withConditions(new ConditionBuilder().withType("Ready").withStatus("True").build()).build());
    kafkaClient.replaceStatus(kafka);
    // ask for a Strimzi version change on ManagedKafka
    mk.getSpec().getVersions().setStrimzi("strimzi-cluster-operator.v2");
    kafka = this.kafkaCluster.kafkaFrom(mk, kafka);
    // Kafka reconcile paused but label is still the current version
    assertTrue(kafka.getMetadata().getAnnotations().containsKey(StrimziManager.STRIMZI_PAUSE_RECONCILE_ANNOTATION));
    assertTrue(kafka.getMetadata().getAnnotations().containsKey(StrimziManager.STRIMZI_PAUSE_REASON_ANNOTATION));
    assertEquals(kafka.getMetadata().getLabels().get(this.strimziManager.getVersionLabel()), mk.getStatus().getVersions().getStrimzi());
    // nothing should change after an intermediate reconcile
    kafka = this.kafkaCluster.kafkaFrom(mk, kafka);
    assertTrue(kafka.getMetadata().getAnnotations().containsKey(StrimziManager.STRIMZI_PAUSE_REASON_ANNOTATION));
    // Kafka moves to be paused
    kafka.setStatus(new KafkaStatusBuilder().withConditions(new ConditionBuilder().withType("ReconciliationPaused").withStatus("True").build()).build());
    kafkaClient.replaceStatus(kafka);
    kafka = this.kafkaCluster.kafkaFrom(mk, kafka);
    // Kafka reconcile not paused and Kafka label updated to requested Strimzi version
    assertFalse(kafka.getMetadata().getAnnotations().containsKey(StrimziManager.STRIMZI_PAUSE_RECONCILE_ANNOTATION));
    // the pause reason should stay until strimzi updates to ready
    assertTrue(kafka.getMetadata().getAnnotations().containsKey(StrimziManager.STRIMZI_PAUSE_REASON_ANNOTATION));
    assertEquals(kafka.getMetadata().getLabels().get(this.strimziManager.getVersionLabel()), "strimzi-cluster-operator.v2");
}
Also used : ConditionBuilder(io.strimzi.api.kafka.model.status.ConditionBuilder) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) ManagedKafkaStatusBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder) Kafka(io.strimzi.api.kafka.model.Kafka) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) ManagedKafkaStatusBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder) KafkaStatusBuilder(io.strimzi.api.kafka.model.status.KafkaStatusBuilder) VersionsBuilder(org.bf2.operator.resources.v1alpha1.VersionsBuilder) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

ManagedKafkaStatusBuilder (org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder)6 ManagedKafka (org.bf2.operator.resources.v1alpha1.ManagedKafka)5 Test (org.junit.jupiter.api.Test)4 Counted (io.micrometer.core.annotation.Counted)2 Timed (io.micrometer.core.annotation.Timed)2 QuarkusTest (io.quarkus.test.junit.QuarkusTest)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Inject (javax.inject.Inject)2 ConditionUtils (org.bf2.common.ConditionUtils)2 ManagedKafkaResourceClient (org.bf2.common.ManagedKafkaResourceClient)2 Reason (org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition.Reason)2 Status (org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition.Status)2 VersionsBuilder (org.bf2.operator.resources.v1alpha1.VersionsBuilder)2 Logger (org.jboss.logging.Logger)2 NDC (org.jboss.logging.NDC)2 Namespace (io.fabric8.kubernetes.api.model.Namespace)1 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)1 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)1