Skip to content

fix: condition for bulk resources #1688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
import io.javaoperatorsdk.operator.processing.dependent.BulkDependentResource;

@SuppressWarnings("rawtypes")
public abstract class AbstractWorkflowExecutor<P extends HasMetadata> {
Expand Down Expand Up @@ -101,9 +102,15 @@ protected synchronized void handleNodeExecutionFinish(

protected <R> boolean isConditionMet(Optional<Condition<R, P>> condition,
DependentResource<R, P> dependentResource) {
if (condition.isEmpty()) {
return true;
}
var resources = dependentResource instanceof BulkDependentResource
? ((BulkDependentResource) dependentResource).getSecondaryResources(primary, context)
: dependentResource.getSecondaryResource(primary, context).orElse(null);

return condition.map(c -> c.isMet(primary,
dependentResource.getSecondaryResource(primary, context).orElse(null),
context))
(R) resources, context))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't work because R can never be a Map in a DependentResource declaration so this should most likely cause a ClassCastException.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That thing does not get into the bytecode, but also there is a test for this in the PR. Will try to make it more nice though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked but this is still the simplest way to do locally, this will change probably within this issue:
#1689

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will improve this sample so it shows more the issue with the Bulk resources.

.orElse(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.javaoperatorsdk.operator.bulkdependent;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
import io.javaoperatorsdk.operator.sample.bulkdependent.ManagedBulkDependentWithPreconditionReconciler;

import static io.javaoperatorsdk.operator.bulkdependent.BulkDependentTestBase.INITIAL_NUMBER_OF_CONFIG_MAPS;
import static io.javaoperatorsdk.operator.bulkdependent.BulkDependentTestBase.testResource;
import static io.javaoperatorsdk.operator.sample.bulkdependent.ConfigMapDeleterBulkDependentResource.LABEL_KEY;
import static io.javaoperatorsdk.operator.sample.bulkdependent.ConfigMapDeleterBulkDependentResource.LABEL_VALUE;
import static org.assertj.core.api.Assertions.*;
import static org.awaitility.Awaitility.await;

class BulkDependentWithPreconditionIT {

@RegisterExtension
LocallyRunOperatorExtension extension =
LocallyRunOperatorExtension.builder()
.withReconciler(new ManagedBulkDependentWithPreconditionReconciler())
.build();

@Test
void handlesBulkDependentWithPrecondition() {
var resource = testResource();
extension.create(resource);

await().untilAsserted(() -> {
var cms = extension.getKubernetesClient().configMaps().inNamespace(extension.getNamespace())
.withLabel(LABEL_KEY, LABEL_VALUE)
.list().getItems();
assertThat(cms).hasSize(INITIAL_NUMBER_OF_CONFIG_MAPS);
});
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.javaoperatorsdk.operator.sample.bulkdependent;

import java.util.concurrent.atomic.AtomicInteger;

import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent;

@ControllerConfiguration(dependents = @Dependent(reconcilePrecondition = SamplePrecondition.class,
type = CRUDConfigMapBulkDependentResource.class))
public class ManagedBulkDependentWithPreconditionReconciler
implements Reconciler<BulkDependentTestCustomResource> {

private final AtomicInteger numberOfExecutions = new AtomicInteger(0);

@Override
public UpdateControl<BulkDependentTestCustomResource> reconcile(
BulkDependentTestCustomResource resource,
Context<BulkDependentTestCustomResource> context) throws Exception {
numberOfExecutions.incrementAndGet();
return UpdateControl.noUpdate();
}

public int getNumberOfExecutions() {
return numberOfExecutions.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.javaoperatorsdk.operator.sample.bulkdependent;

import java.util.Map;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;

public class SamplePrecondition
implements Condition<Map<String, ConfigMap>, BulkDependentTestCustomResource> {

public static final String SKIP_RESOURCE_DATA = "skipThis";

@Override
public boolean isMet(BulkDependentTestCustomResource primary, Map<String, ConfigMap> secondary,
Context<BulkDependentTestCustomResource> context) {
return !SKIP_RESOURCE_DATA.equals(primary.getSpec().getAdditionalData());
}
}