Skip to content

feat: add ability to stream secondary resources from Context #1916

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 1 commit into from
Jun 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient;
Expand All @@ -19,6 +20,10 @@ default <R> Optional<R> getSecondaryResource(Class<R> expectedType) {

<R> Set<R> getSecondaryResources(Class<R> expectedType);

default <R> Stream<R> getSecondaryResourcesAsStream(Class<R> expectedType) {
return getSecondaryResources(expectedType).stream();
}

@Deprecated(forRemoval = true)
<R> Optional<R> getSecondaryResource(Class<R> expectedType, String eventSourceName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient;
Expand Down Expand Up @@ -35,10 +36,14 @@ public Optional<RetryInfo> getRetryInfo() {

@Override
public <T> Set<T> getSecondaryResources(Class<T> expectedType) {
return getSecondaryResourcesAsStream(expectedType).collect(Collectors.toSet());
}

@Override
public <R> Stream<R> getSecondaryResourcesAsStream(Class<R> expectedType) {
return controller.getEventSourceManager().getResourceEventSourcesFor(expectedType).stream()
.map(es -> es.getSecondaryResources(primaryResource))
.flatMap(Set::stream)
.collect(Collectors.toSet());
.flatMap(Set::stream);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ResourceIDMatcherDiscriminator(Function<P, ResourceID> mapper) {
@Override
public Optional<R> distinguish(Class<R> resource, P primary, Context<P> context) {
var resourceID = mapper.apply(primary);
return context.getSecondaryResources(resource).stream()
return context.getSecondaryResourcesAsStream(resource)
.filter(resourceID::isSameResource)
.findFirst();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public <S> ResourceEventSource<S, R> get(Class<S> dependentType, String name) {

@SuppressWarnings("rawtypes")
private String keyAsString(Class dependentType, String name) {
return name != null && name.length() > 0
return name != null && !name.isEmpty()
? "(" + dependentType.getName() + ", " + name + ")"
: dependentType.getName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.javaoperatorsdk.operator.sample.bulkdependent;

import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
Expand Down Expand Up @@ -59,15 +62,14 @@ public ConfigMap desired(BulkDependentTestCustomResource primary, String key,
@Override
public Map<String, ConfigMap> getSecondaryResources(BulkDependentTestCustomResource primary,
Context<BulkDependentTestCustomResource> context) {
var configMaps = context.getSecondaryResources(ConfigMap.class);
Map<String, ConfigMap> result = new HashMap<>(configMaps.size());
configMaps.forEach(cm -> {
String name = cm.getMetadata().getName();
if (name.startsWith(primary.getMetadata().getName())) {
String key = name.substring(name.lastIndexOf(INDEX_DELIMITER) + 1);
result.put(key, cm);
}
});
return result;
return context.getSecondaryResourcesAsStream(ConfigMap.class)
.filter(cm -> getName(cm).startsWith(primary.getMetadata().getName()))
.collect(Collectors.toMap(
cm -> getName(cm).substring(getName(cm).lastIndexOf(INDEX_DELIMITER) + 1),
Function.identity()));
}

private static String getName(ConfigMap cm) {
return cm.getMetadata().getName();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.javaoperatorsdk.operator.sample.bulkdependent.external;

import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import io.javaoperatorsdk.operator.api.reconciler.Context;
Expand Down Expand Up @@ -75,7 +78,7 @@ public Map<String, ExternalResource> desiredResources(BulkDependentTestCustomRes
public Map<String, ExternalResource> getSecondaryResources(
BulkDependentTestCustomResource primary,
Context<BulkDependentTestCustomResource> context) {
return context.getSecondaryResources(resourceType()).stream()
return context.getSecondaryResourcesAsStream(resourceType())
.filter(r -> r.getId()
.startsWith(primary.getMetadata().getName() + EXTERNAL_RESOURCE_NAME_DELIMITER +
primary.getMetadata().getNamespace() +
Expand Down