Skip to content

discriminator improvements #2013

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 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,45 @@
package io.javaoperatorsdk.operator.api.reconciler;

import java.util.Optional;
import java.util.function.Function;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;

public class IndexDiscriminator<R extends HasMetadata, P extends HasMetadata>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Some documentation to explain what this does and why it's useful would be helpful for users.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

implements ResourceDiscriminator<R, P> {

private final String indexName;
private final String eventSourceName;
private final Function<P, String> keyMapper;

public IndexDiscriminator(String indexName, Function<P, String> keyMapper) {
this(indexName, null, keyMapper);
}

public IndexDiscriminator(String indexName, String eventSourceName,
Function<P, String> keyMapper) {
this.indexName = indexName;
this.eventSourceName = eventSourceName;
this.keyMapper = keyMapper;
}

@Override
public Optional<R> distinguish(Class<R> resource,
P primary,
Context<P> context) {

InformerEventSource<R, P> eventSource =
(InformerEventSource<R, P>) context
.eventSourceRetriever()
.getResourceEventSourceFor(resource, eventSourceName);
var resources = eventSource.byIndex(indexName, keyMapper.apply(primary));
if (resources.isEmpty()) {
return Optional.empty();
} else if (resources.size() > 1) {
throw new IllegalStateException("More than one resource found");
} else {
return Optional.of(resources.get(0));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public Map<String, EventSource> prepareEventSources(

firstDependentResourceConfigMap
.setResourceDiscriminator(
new IndexDiscriminator(CONFIG_MAP_INDEX_1, FIRST_CONFIG_MAP_SUFFIX_1));
new TestIndexDiscriminator(CONFIG_MAP_INDEX_1, FIRST_CONFIG_MAP_SUFFIX_1));
secondDependentResourceConfigMap
.setResourceDiscriminator(
new IndexDiscriminator(CONFIG_MAP_INDEX_2, FIRST_CONFIG_MAP_SUFFIX_2));
new TestIndexDiscriminator(CONFIG_MAP_INDEX_2, FIRST_CONFIG_MAP_SUFFIX_2));
return EventSourceInitializer.nameEventSources(eventSource);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.javaoperatorsdk.operator.sample.indexdiscriminator;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.javaoperatorsdk.operator.api.reconciler.IndexDiscriminator;

import static io.javaoperatorsdk.operator.sample.indexdiscriminator.IndexDiscriminatorTestReconciler.configMapKeyFromPrimary;

public class TestIndexDiscriminator
extends IndexDiscriminator<ConfigMap, IndexDiscriminatorTestCustomResource> {

public TestIndexDiscriminator(String indexName, String nameSuffix) {
super(indexName, p -> configMapKeyFromPrimary(p, nameSuffix));
}
}