Skip to content

feat: remove primary to secondary mapper (handled automatically) #1161

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 19 commits into from
Apr 20, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,41 @@
import io.javaoperatorsdk.operator.api.config.DefaultResourceConfiguration;
import io.javaoperatorsdk.operator.api.config.ResourceConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.PrimaryToSecondaryMapper;
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers;

@SuppressWarnings("rawtypes")
public interface InformerConfiguration<R extends HasMetadata, P extends HasMetadata>
public interface InformerConfiguration<R extends HasMetadata>
extends ResourceConfiguration<R> {

class DefaultInformerConfiguration<R extends HasMetadata, P extends HasMetadata> extends
DefaultResourceConfiguration<R> implements InformerConfiguration<R, P> {
class DefaultInformerConfiguration<R extends HasMetadata> extends
DefaultResourceConfiguration<R> implements InformerConfiguration<R> {

private final SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private final PrimaryToSecondaryMapper<P> primaryToSecondaryMapper;

protected DefaultInformerConfiguration(String labelSelector,
Class<R> resourceClass,
SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper,
PrimaryToSecondaryMapper<P> primaryToSecondaryMapper,
Set<String> namespaces) {
super(labelSelector, resourceClass, namespaces);
this.secondaryToPrimaryMapper =
Objects.requireNonNullElse(secondaryToPrimaryMapper,
Mappers.fromOwnerReference());
this.primaryToSecondaryMapper =
Objects.requireNonNullElseGet(primaryToSecondaryMapper, () -> ResourceID::fromResource);
}


public SecondaryToPrimaryMapper<R> getSecondaryToPrimaryMapper() {
return secondaryToPrimaryMapper;
}

public PrimaryToSecondaryMapper<P> getPrimaryToSecondaryMapper() {
return primaryToSecondaryMapper;
}

}

SecondaryToPrimaryMapper<R> getSecondaryToPrimaryMapper();

PrimaryToSecondaryMapper<P> getPrimaryToSecondaryMapper();

@SuppressWarnings("unused")
class InformerConfigurationBuilder<R extends HasMetadata, P extends HasMetadata> {

private SecondaryToPrimaryMapper<R> secondaryToPrimaryResourcesIdSet;
private PrimaryToSecondaryMapper<P> associatedWith;
private SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private Set<String> namespaces;
private String labelSelector;
private final Class<R> resourceClass;
Expand All @@ -66,17 +53,10 @@ private InformerConfigurationBuilder(Class<R> resourceClass) {

public InformerConfigurationBuilder<R, P> withSecondaryToPrimaryMapper(
SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper) {
this.secondaryToPrimaryResourcesIdSet = secondaryToPrimaryMapper;
this.secondaryToPrimaryMapper = secondaryToPrimaryMapper;
return this;
}

public InformerConfigurationBuilder<R, P> withPrimaryToSecondaryMapper(
PrimaryToSecondaryMapper<P> associatedWith) {
this.associatedWith = associatedWith;
return this;
}


public InformerConfigurationBuilder<R, P> withNamespaces(String... namespaces) {
this.namespaces = namespaces != null ? Set.of(namespaces) : Collections.emptySet();
return this;
Expand All @@ -93,9 +73,9 @@ public InformerConfigurationBuilder<R, P> withLabelSelector(String labelSelector
return this;
}

public InformerConfiguration<R, P> build() {
public InformerConfiguration<R> build() {
return new DefaultInformerConfiguration<>(labelSelector, resourceClass,
secondaryToPrimaryResourcesIdSet, associatedWith,
secondaryToPrimaryMapper,
namespaces);
}
}
Expand All @@ -111,12 +91,10 @@ static InformerConfigurationBuilder from(Class resourceClass) {
}

static <R extends HasMetadata, P extends HasMetadata> InformerConfigurationBuilder<R, P> from(
InformerConfiguration<R, P> configuration) {
InformerConfiguration<R> configuration) {
return new InformerConfigurationBuilder<R, P>(configuration.getResourceClass())
.withNamespaces(configuration.getNamespaces())
.withLabelSelector(configuration.getLabelSelector())
.withPrimaryToSecondaryMapper(
configuration.getPrimaryToSecondaryMapper())
.withSecondaryToPrimaryMapper(configuration.getSecondaryToPrimaryMapper());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.javaoperatorsdk.operator.api.reconciler;

import java.util.Optional;
import java.util.Set;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
Expand All @@ -14,6 +15,8 @@ default <T> Optional<T> getSecondaryResource(Class<T> expectedType) {
return getSecondaryResource(expectedType, null);
}

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

<T> Optional<T> getSecondaryResource(Class<T> expectedType, String eventSourceName);

ControllerConfiguration<P> getControllerConfiguration();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package io.javaoperatorsdk.operator.api.reconciler;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ManagedDependentResourceContext;
import io.javaoperatorsdk.operator.processing.Controller;
import io.javaoperatorsdk.operator.processing.MultiResourceOwner;

public class DefaultContext<P extends HasMetadata> implements Context<P> {

Expand All @@ -28,9 +33,28 @@ public Optional<RetryInfo> getRetryInfo() {
return Optional.ofNullable(retryInfo);
}

@Override
@SuppressWarnings("unchecked")
public <T> Set<T> getSecondaryResources(Class<T> expectedType) {
return controller.getEventSourceManager().getEventSourcesFor(expectedType).stream()
.map(
es -> {
if (es instanceof MultiResourceOwner) {
return ((MultiResourceOwner<T, P>) es).getSecondaryResources(primaryResource);
} else {
return es.getSecondaryResource(primaryResource)
.map(List::of)
.orElse(Collections.emptyList());
}
})
.flatMap(List::stream)
.collect(Collectors.toSet());
}

@Override
public <T> Optional<T> getSecondaryResource(Class<T> expectedType, String eventSourceName) {
return controller.getEventSourceManager()
return controller
.getEventSourceManager()
.getResourceEventSourceFor(expectedType, eventSourceName)
.getSecondaryResource(primaryResource);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.javaoperatorsdk.operator.processing;

import java.util.List;
import java.util.Optional;

import io.fabric8.kubernetes.api.model.HasMetadata;

public interface MultiResourceOwner<R, P extends HasMetadata> extends ResourceOwner<R, P> {

default Optional<R> getSecondaryResource(P primary) {
var list = getSecondaryResources(primary);
if (list.isEmpty()) {
return Optional.empty();
} else if (list.size() == 1) {
return Optional.of(list.get(0));
} else {
throw new IllegalStateException("More than 1 secondary resource related to primary");
}

}

List<R> getSecondaryResources(P primary);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public abstract class AbstractEventSourceHolderDependentResource<R, P extends HasMetadata, T extends ResourceEventSource<R, P>>
extends AbstractDependentResource<R, P>
implements EventSourceProvider<P> {

private T eventSource;
private boolean isCacheFillerEventSource;

Expand Down Expand Up @@ -48,7 +49,6 @@ protected void onUpdated(ResourceID primaryResourceId, R updated, R actual) {
}
}


@SuppressWarnings("unchecked")
private RecentOperationCacheFiller<R> recentOperationCacheFiller() {
return (RecentOperationCacheFiller<R>) eventSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.javaoperatorsdk.operator.processing.dependent.Matcher;
import io.javaoperatorsdk.operator.processing.dependent.Matcher.Result;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.PrimaryToSecondaryMapper;
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers;
Expand Down Expand Up @@ -58,16 +57,11 @@ private void configureWith(String labelSelector, Set<String> namespaces) {
final var primaryResourcesRetriever =
(this instanceof SecondaryToPrimaryMapper) ? (SecondaryToPrimaryMapper<R>) this
: Mappers.fromOwnerReference();
final PrimaryToSecondaryMapper<P> secondaryResourceIdentifier =
(this instanceof PrimaryToSecondaryMapper)
? (PrimaryToSecondaryMapper<P>) this
: ResourceID::fromResource;
InformerConfiguration<R, P> ic =
InformerConfiguration<R> ic =
InformerConfiguration.from(resourceType())
.withLabelSelector(labelSelector)
.withNamespaces(namespaces)
.withSecondaryToPrimaryMapper(primaryResourcesRetriever)
.withPrimaryToSecondaryMapper(secondaryResourceIdentifier)
.build();
configureWith(new InformerEventSource<>(ic, client));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.javaoperatorsdk.operator.processing.event;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -176,6 +177,10 @@ <S> ResourceEventSource<S, R> getResourceEventSourceFor(
return getResourceEventSourceFor(dependentType, null);
}

public <S> List<ResourceEventSource<S, R>> getEventSourcesFor(Class<S> dependentType) {
return eventSources.getEventSources(dependentType);
}

public <S> ResourceEventSource<S, R> getResourceEventSourceFor(
Class<S> dependentType, String qualifier) {
Objects.requireNonNull(dependentType, "dependentType is Mandatory");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.javaoperatorsdk.operator.processing.event;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.fabric8.kubernetes.api.model.HasMetadata;
Expand Down Expand Up @@ -131,4 +130,13 @@ private String keyAsString(Class dependentType, String name) {
? "(" + dependentType.getName() + ", " + name + ")"
: dependentType.getName();
}

@SuppressWarnings("unchecked")
public <S> List<ResourceEventSource<S, R>> getEventSources(Class<S> dependentType) {
final var sourcesForType = sources.get(keyFor(dependentType));
return sourcesForType.values().stream()
.filter(ResourceEventSource.class::isInstance)
.map(es -> (ResourceEventSource<S, R>) es)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.javaoperatorsdk.operator.processing.event;

import java.util.Optional;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.dependent.RecentOperationCacheFiller;
import io.javaoperatorsdk.operator.processing.event.source.CachingEventSource;
Expand Down Expand Up @@ -50,4 +52,9 @@ public synchronized void handleRecentResourceUpdate(ResourceID resourceID, R res
}
});
}

@Override
public Optional<R> getSecondaryResource(P primary) {
return cache.get(ResourceID.fromResource(primary));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,4 @@ public Optional<R> getCachedValue(ResourceID resourceID) {
return cache.get(resourceID);
}

@Override
public Optional<R> getSecondaryResource(P primary) {
return cache.get(ResourceID.fromResource(primary));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
import io.javaoperatorsdk.operator.processing.event.ResourceID;

@FunctionalInterface
public interface SecondaryToPrimaryMapper<T> {
Set<ResourceID> toPrimaryResourceIDs(T dependentResource);
public interface SecondaryToPrimaryMapper<R> {
Set<ResourceID> toPrimaryResourceIDs(R dependentResource);
}
Loading