Search in sources :

Example 1 with ManagedKafkaCondition

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

the class ConditionUtilsTest method testBuildUpdateCondition.

@ParameterizedTest
@ValueSource(strings = { "Ready" })
void testBuildUpdateCondition(String type) {
    ManagedKafkaCondition mkcondition = ConditionUtils.buildCondition(ManagedKafkaCondition.Type.valueOf(type), Status.True);
    assertEquals("True", mkcondition.getStatus());
    assertEquals(type, mkcondition.getType());
    ConditionUtils.updateConditionStatus(mkcondition, Status.False, null, null);
    assertEquals("False", mkcondition.getStatus());
    assertEquals(type, mkcondition.getType());
    var mockCondition = Mockito.mock(ManagedKafkaCondition.class, Mockito.CALLS_REAL_METHODS);
    ConditionUtils.updateConditionStatus(mockCondition, Status.False, Reason.Deleted, null);
    Mockito.verify(mockCondition, Mockito.times(1)).setLastTransitionTime(Mockito.anyString());
    // only update if different
    ConditionUtils.updateConditionStatus(mockCondition, Status.False, Reason.Deleted, null);
    Mockito.verify(mockCondition, Mockito.times(1)).setLastTransitionTime(Mockito.anyString());
}
Also used : ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with ManagedKafkaCondition

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

the class ManagedKafkaControllerTest method shouldCreateStatus.

@Test
void shouldCreateStatus() throws InterruptedException {
    ManagedKafka mk = ManagedKafka.getDummyInstance(1);
    mk.getMetadata().setUid(UUID.randomUUID().toString());
    mk.getMetadata().setGeneration(1l);
    mk.getMetadata().setResourceVersion("1");
    // create
    Context<ManagedKafka> context = Mockito.mock(Context.class);
    Mockito.when(context.getEvents()).thenReturn(new EventList(Arrays.asList(new CustomResourceEvent(Action.ADDED, mk, null))));
    StrimziManager strimziManager = Mockito.mock(StrimziManager.class);
    Mockito.when(strimziManager.getStrimziVersion("strimzi-cluster-operator.v0.23.0")).thenReturn(new StrimziVersionStatusBuilder().withVersion(mk.getSpec().getVersions().getStrimzi()).withKafkaVersions(mk.getSpec().getVersions().getKafka()).build());
    Mockito.when(strimziManager.getVersionLabel()).thenReturn("managedkafka.bf2.org/strimziVersion");
    QuarkusMock.installMockForType(strimziManager, StrimziManager.class);
    mkController.createOrUpdateResource(mk, context);
    ManagedKafkaCondition condition = mk.getStatus().getConditions().get(0);
    assertEquals(ManagedKafkaCondition.Reason.Installing.name(), condition.getReason());
    mk.getSpec().setDeleted(true);
    // this simulates, but not exactly an issue seen with older logic
    // essentially there "last event" of the delete is something other than a deployment or a kafka
    // it should still trigger the update of the status
    Mockito.when(context.getEvents()).thenReturn(new EventList(Arrays.asList(new ResourceEvent<>(new ServiceBuilder().withNewMetadata().withOwnerReferences(new OwnerReferenceBuilder().withUid(mk.getMetadata().getUid()).build()).endMetadata().build(), null, Watcher.Action.DELETED))));
    mkController.createOrUpdateResource(mk, context);
    // should now be deleted
    condition = mk.getStatus().getConditions().get(0);
    assertEquals(ManagedKafkaCondition.Reason.Deleted.name(), condition.getReason());
}
Also used : ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) StrimziVersionStatusBuilder(org.bf2.operator.resources.v1alpha1.StrimziVersionStatusBuilder) EventList(io.javaoperatorsdk.operator.processing.event.EventList) StrimziManager(org.bf2.operator.managers.StrimziManager) CustomResourceEvent(io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 3 with ManagedKafkaCondition

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

the class ManagedKafkaControllerTest method testWrongVersions.

@Test
void testWrongVersions() throws InterruptedException {
    ManagedKafka mk = ManagedKafka.getDummyInstance(1);
    mk.getMetadata().setUid(UUID.randomUUID().toString());
    mk.getMetadata().setGeneration(1l);
    mk.getMetadata().setResourceVersion("1");
    // create
    Context<ManagedKafka> context = Mockito.mock(Context.class);
    Mockito.when(context.getEvents()).thenReturn(new EventList(Arrays.asList(new CustomResourceEvent(Action.ADDED, mk, null))));
    StrimziManager strimziManager = Mockito.mock(StrimziManager.class);
    Mockito.when(strimziManager.getVersionLabel()).thenReturn("managedkafka.bf2.org/strimziVersion");
    QuarkusMock.installMockForType(strimziManager, StrimziManager.class);
    mkController.createOrUpdateResource(mk, context);
    ManagedKafkaCondition condition = mk.getStatus().getConditions().get(0);
    assertEquals(ManagedKafkaCondition.Reason.Error.name(), condition.getReason());
    assertEquals("The requested Strimzi version strimzi-cluster-operator.v0.23.0 is not supported", condition.getMessage());
    Mockito.when(strimziManager.getStrimziVersion("strimzi-cluster-operator.v0.23.0")).thenReturn(new StrimziVersionStatusBuilder().withVersion(mk.getSpec().getVersions().getStrimzi()).withKafkaVersions("3.0.0").build());
    mkController.createOrUpdateResource(mk, context);
    condition = mk.getStatus().getConditions().get(0);
    assertEquals(ManagedKafkaCondition.Reason.Error.name(), condition.getReason());
    assertEquals("The requested Kafka version 2.7.0 is not supported by the Strimzi version strimzi-cluster-operator.v0.23.0", condition.getMessage());
}
Also used : ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) StrimziVersionStatusBuilder(org.bf2.operator.resources.v1alpha1.StrimziVersionStatusBuilder) EventList(io.javaoperatorsdk.operator.processing.event.EventList) StrimziManager(org.bf2.operator.managers.StrimziManager) CustomResourceEvent(io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 4 with ManagedKafkaCondition

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

the class StrimziBundleManager method updateStatus.

/**
 * Update status of ManagedKafkaAgent resource about approval of Strimzi bundle installation
 * NOTE: it creates a condition if Strimzi bundle installation was not approved. The condition is taken out if approved.
 *
 * @param approval the status of the approval
 */
private void updateStatus(Approval approval) {
    ManagedKafkaAgent resource = this.agentClient.getByName(this.agentClient.getNamespace(), ManagedKafkaAgentResourceClient.RESOURCE_NAME);
    if (resource != null && resource.getStatus() != null) {
        List<ManagedKafkaCondition> conditions = resource.getStatus().getConditions();
        ManagedKafkaCondition bundleReadyCondition = ConditionUtils.findManagedKafkaCondition(conditions, ManagedKafkaCondition.Type.StrimziBundleReady).orElse(null);
        ManagedKafkaCondition.Reason reason = ManagedKafkaCondition.Reason.OrphanedKafkas;
        ManagedKafkaCondition.Status status = ManagedKafkaCondition.Status.False;
        if (approval != Approval.ORPHANED) {
            if (bundleReadyCondition == null) {
                return;
            }
            conditions.remove(bundleReadyCondition);
        } else {
            if (bundleReadyCondition == null) {
                bundleReadyCondition = ConditionUtils.buildCondition(ManagedKafkaCondition.Type.StrimziBundleReady, status);
                bundleReadyCondition.reason(reason);
                conditions.add(bundleReadyCondition);
            } else {
                ConditionUtils.updateConditionStatus(bundleReadyCondition, status, reason, null);
            }
        }
        this.agentClient.replaceStatus(resource);
    }
}
Also used : ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) ManagedKafkaAgent(org.bf2.operator.resources.v1alpha1.ManagedKafkaAgent)

Example 5 with ManagedKafkaCondition

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

the class ManagedKafkaResourceType method readiness.

@Override
public Predicate<ManagedKafka> readiness(KubeClient client) {
    AtomicInteger count = new AtomicInteger();
    Set<String> messages = Collections.synchronizedSet(new LinkedHashSet<>());
    return mk -> {
        if (mk == null) {
            throw new IllegalStateException("ManagedKafka is null");
        }
        ManagedKafkaCondition mkc = getCondition(mk.getStatus(), ManagedKafkaCondition.Type.Ready).orElse(null);
        if (mkc == null) {
            return false;
        }
        if (ManagedKafkaCondition.Status.True.name().equals(mkc.getStatus())) {
            return true;
        }
        if (ManagedKafkaCondition.Reason.Error.name().equals(mkc.getReason())) {
            if (messages.add(mkc.getMessage())) {
                LOGGER.warn("ManagedKafka {} in error state {}", mk.getMetadata().getName(), mkc.getMessage());
            }
        // throw new IllegalStateException(String.format("ManagedKafka %s in error state %s", mk.getMetadata().getName(), mkc.getMessage()));
        }
        if (count.getAndIncrement() % 15 == 0) {
            ListOptions opts = new ListOptionsBuilder().withFieldSelector("status.phase=Pending").build();
            client.client().pods().inNamespace(mk.getMetadata().getNamespace()).withLabel("strimzi.io/cluster").list(opts).getItems().forEach(ManagedKafkaResourceType::checkUnschedulablePod);
        }
        return false;
    };
}
Also used : KubernetesResourceList(io.fabric8.kubernetes.api.model.KubernetesResourceList) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) ListOptions(io.fabric8.kubernetes.api.model.ListOptions) Resource(io.fabric8.kubernetes.client.dsl.Resource) KubeClient(org.bf2.test.k8s.KubeClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedKafkaStatus(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus) LinkedHashSet(java.util.LinkedHashSet) KeycloakInstance(org.bf2.systemtest.framework.KeycloakInstance) SecurityUtils(org.bf2.systemtest.framework.SecurityUtils) Predicate(java.util.function.Predicate) TestUtils(org.bf2.test.TestUtils) Pod(io.fabric8.kubernetes.api.model.Pod) SystemTestEnvironment(org.bf2.systemtest.framework.SystemTestEnvironment) Set(java.util.Set) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) Optional(java.util.Optional) Kafka(io.strimzi.api.kafka.model.Kafka) LogManager(org.apache.logging.log4j.LogManager) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListOptions(io.fabric8.kubernetes.api.model.ListOptions) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition)

Aggregations

ManagedKafkaCondition (org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition)7 ManagedKafka (org.bf2.operator.resources.v1alpha1.ManagedKafka)4 StrimziManager (org.bf2.operator.managers.StrimziManager)3 EventList (io.javaoperatorsdk.operator.processing.event.EventList)2 CustomResourceEvent (io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent)2 QuarkusTest (io.quarkus.test.junit.QuarkusTest)2 List (java.util.List)2 Optional (java.util.Optional)2 Status (org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition.Status)2 ManagedKafkaStatus (org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus)2 StrimziVersionStatus (org.bf2.operator.resources.v1alpha1.StrimziVersionStatus)2 StrimziVersionStatusBuilder (org.bf2.operator.resources.v1alpha1.StrimziVersionStatusBuilder)2 Test (org.junit.jupiter.api.Test)2 KubernetesResourceList (io.fabric8.kubernetes.api.model.KubernetesResourceList)1 ListOptions (io.fabric8.kubernetes.api.model.ListOptions)1 ListOptionsBuilder (io.fabric8.kubernetes.api.model.ListOptionsBuilder)1 OwnerReferenceBuilder (io.fabric8.kubernetes.api.model.OwnerReferenceBuilder)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)1 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)1