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 all 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
2 changes: 2 additions & 0 deletions docs/documentation/dependent-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ interface.
Various examples are provided as [integration tests](https://github.com/java-operator-sdk/java-operator-sdk/tree/f5ffcfb6f546d79b4bab04ea503c8bad9d6acce6/operator-framework/src/test/java/io/javaoperatorsdk/operator/bulkdependent)
.

To see how to add conditions on a bulk dependent resource [see integration test](https://github.com/java-operator-sdk/java-operator-sdk/blob/ad8759856cc59380ea4b0973478daa1140ec93e7/operator-framework/src/test/java/io/javaoperatorsdk/operator/bulkdependent/BulkDependentWithConditionIT.java).

## Dependent Resources with Explicit State

For cases when an external (non-Kubernetes) resource generates an ID during creation and from that point
Expand Down
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,46 @@
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.BulkDependentTestCustomResource;
import io.javaoperatorsdk.operator.sample.bulkdependent.ManagedBulkDependentWithReadyConditionReconciler;

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 BulkDependentWithConditionIT {

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

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

await().untilAsserted(() -> {
var res = extension.get(BulkDependentTestCustomResource.class,
testResource().getMetadata().getName());
assertThat(res.getStatus()).isNotNull();
assertThat(res.getStatus().getReady()).isTrue();

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
Expand Up @@ -10,6 +10,6 @@
@Version("v1")
@ShortNames("sbd")
public class BulkDependentTestCustomResource
extends CustomResource<BulkDependentTestSpec, Void>
extends CustomResource<BulkDependentTestSpec, BulkDependentTestStatus>
implements Namespaced {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.javaoperatorsdk.operator.sample.bulkdependent;

public class BulkDependentTestStatus {

private Boolean ready;

public Boolean getReady() {
return ready;
}

public BulkDependentTestStatus setReady(boolean ready) {
this.ready = ready;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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(readyPostcondition = SampleBulkCondition.class,
type = CRUDConfigMapBulkDependentResource.class))
public class ManagedBulkDependentWithReadyConditionReconciler
implements Reconciler<BulkDependentTestCustomResource> {

private final AtomicInteger numberOfExecutions = new AtomicInteger(0);

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

var ready = context.managedDependentResourceContext().getWorkflowReconcileResult()
.map(res -> res.allDependentResourcesReady()).orElseThrow();

resource.setStatus(new BulkDependentTestStatus());
resource.getStatus().setReady(ready);

return UpdateControl.patchStatus(resource);
}

public int getNumberOfExecutions() {
return numberOfExecutions.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 SampleBulkCondition
implements Condition<Map<String, ConfigMap>, BulkDependentTestCustomResource> {

// We use ConfigMaps here just to show how to check some properties of resources managed by a
// BulkDependentResource. In real life example this would be rather based on some status of those
// resources, like Pods.

@Override
public boolean isMet(BulkDependentTestCustomResource primary, Map<String, ConfigMap> secondary,
Context<BulkDependentTestCustomResource> context) {

return secondary.values().stream().allMatch(cm -> !cm.getData().isEmpty());

}
}