Skip to content

Commit

Permalink
name in ES configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Mészáros <[email protected]>
  • Loading branch information
csviri committed May 21, 2024
1 parent 4bbe20c commit ae405d6
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ public interface InformerConfiguration<R extends HasMetadata>
class DefaultInformerConfiguration<R extends HasMetadata> extends
DefaultResourceConfiguration<R> implements InformerConfiguration<R> {

private final String name;
private final PrimaryToSecondaryMapper<?> primaryToSecondaryMapper;
private final SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private final boolean followControllerNamespaceChanges;
private final OnDeleteFilter<? super R> onDeleteFilter;
private final GroupVersionKind groupVersionKind;

protected DefaultInformerConfiguration(String labelSelector,
protected DefaultInformerConfiguration(
String name,
String labelSelector,
Class<R> resourceClass,
GroupVersionKind groupVersionKind,
PrimaryToSecondaryMapper<?> primaryToSecondaryMapper,
Expand All @@ -49,6 +52,7 @@ protected DefaultInformerConfiguration(String labelSelector,
ItemStore<R> itemStore, Long informerListLimit) {
super(resourceClass, namespaces, labelSelector, onAddFilter, onUpdateFilter, genericFilter,
itemStore, informerListLimit);
this.name = name;
this.followControllerNamespaceChanges = followControllerNamespaceChanges;
this.groupVersionKind = groupVersionKind;
this.primaryToSecondaryMapper = primaryToSecondaryMapper;
Expand Down Expand Up @@ -77,10 +81,16 @@ public <P extends HasMetadata> PrimaryToSecondaryMapper<P> getPrimaryToSecondary
return (PrimaryToSecondaryMapper<P>) primaryToSecondaryMapper;
}

@Override
public Optional<GroupVersionKind> getGroupVersionKind() {
return Optional.ofNullable(groupVersionKind);
}

@Override
public String name() {
return name;
}

public boolean inheritsNamespacesFromController() {
return InformerConfiguration.inheritsNamespacesFromController(getNamespaces());
}
Expand Down Expand Up @@ -132,6 +142,8 @@ public Set<String> getEffectiveNamespaces(ControllerConfiguration<?> controllerC

Optional<GroupVersionKind> getGroupVersionKind();

String name();

static boolean inheritsNamespacesFromController(Set<String> namespaces) {
return SAME_AS_CONTROLLER_NAMESPACES_SET.equals(namespaces);
}
Expand All @@ -142,6 +154,7 @@ class InformerConfigurationBuilder<R extends HasMetadata> {
private final Class<R> resourceClass;
private final GroupVersionKind groupVersionKind;
private final Class<? extends HasMetadata> primaryResourceClass;
private String name;
private PrimaryToSecondaryMapper<?> primaryToSecondaryMapper;
private SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private Set<String> namespaces = SAME_AS_CONTROLLER_NAMESPACES_SET;
Expand Down Expand Up @@ -173,6 +186,11 @@ private InformerConfigurationBuilder(Class<R> resourceClass,
this.primaryResourceClass = primaryResourceClass;
}

public InformerConfigurationBuilder<R> withName(String name) {
this.name = name;
return this;
}

public <P extends HasMetadata> InformerConfigurationBuilder<R> withPrimaryToSecondaryMapper(
PrimaryToSecondaryMapper<P> primaryToSecondaryMapper) {
this.primaryToSecondaryMapper = primaryToSecondaryMapper;
Expand Down Expand Up @@ -288,7 +306,8 @@ public InformerConfigurationBuilder<R> withInformerListLimit(Long informerListLi
public InformerConfiguration<R> build() {
Objects.requireNonNull(secondaryToPrimaryMapper, "SecondaryToPrimaryMapper must not be null");

return new DefaultInformerConfiguration<>(labelSelector, resourceClass, groupVersionKind,
return new DefaultInformerConfiguration<>(name, labelSelector, resourceClass,
groupVersionKind,
primaryToSecondaryMapper,
Objects.requireNonNullElse(secondaryToPrimaryMapper,
Mappers.fromOwnerReferences(HasMetadata.getApiVersion(primaryResourceClass),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public PerResourcePollingDependentResource(Class<R> resourceType, Duration polli
protected ExternalResourceCachingEventSource<R, P> createEventSource(
EventSourceContext<P> context) {

return new PerResourcePollingEventSource<>(name(), resourceType(), context,
return new PerResourcePollingEventSource<>(resourceType(), context,
new PerResourcePollingConfigurationBuilder<>(
this, getPollingPeriod())
.withCacheKeyMapper(this)
.withName(name())
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public PollingDependentResource(Class<R> resourceType, Duration pollingPeriod,
@Override
protected ExternalResourceCachingEventSource<R, P> createEventSource(
EventSourceContext<P> context) {
return new PollingEventSource<>(name(), resourceType(),
new PollingConfiguration<>(this, getPollingPeriod(), cacheKeyMapper));
return new PollingEventSource<>(resourceType(),
new PollingConfiguration<>(name(), this, getPollingPeriod(), cacheKeyMapper));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi

}

@SuppressWarnings("unchecked")
private InformerConfiguration<R> createInformerConfiguration(KubernetesDependent configAnnotation,
ControllerConfiguration<?> controllerConfig,
Class<KubernetesDependentResource<R, P>> originatingClass) {
Expand All @@ -61,6 +62,12 @@ private InformerConfiguration<R> createInformerConfiguration(KubernetesDependent

if (configAnnotation != null) {

if (Constants.NO_VALUE_SET.equals(configAnnotation.informerConfig().name())) {
informerConfig.withName(dependentInstance.name());
} else {
informerConfig.withName(configAnnotation.informerConfig().name());
}

var namespaces = Set.of(configAnnotation.informerConfig().namespaces());
informerConfig.withNamespaces(namespaces);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ protected InformerEventSource<R, P> createEventSource(EventSourceContext<P> cont
.withSecondaryToPrimaryMapper(getSecondaryToPrimaryMapper().orElseThrow())
.build();
}
var es = new InformerEventSource<>(name(), config, context);
var es = new InformerEventSource<>(config, context);
setEventSource(es);
return eventSource().orElseThrow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,21 @@ public class InformerEventSource<R extends HasMetadata, P extends HasMetadata>
private final PrimaryToSecondaryMapper<P> primaryToSecondaryMapper;
private final String id = UUID.randomUUID().toString();

public InformerEventSource(String name,
InformerConfiguration<R> configuration, EventSourceContext<P> context) {
this(name, configuration, context.getClient(),
context.getControllerConfiguration().getConfigurationService()
.parseResourceVersionsForEventFilteringAndCaching());
}

public InformerEventSource(
InformerConfiguration<R> configuration, EventSourceContext<P> context) {
this(null, configuration, context.getClient(),
this(configuration, context.getClient(),
context.getControllerConfiguration().getConfigurationService()
.parseResourceVersionsForEventFilteringAndCaching());
}

public InformerEventSource(InformerConfiguration<R> configuration, KubernetesClient client) {
this(null, configuration, client, false);
this(configuration, client, false);
}

public InformerEventSource(String name, InformerConfiguration<R> configuration,
public InformerEventSource(InformerConfiguration<R> configuration,
KubernetesClient client,
boolean parseResourceVersions) {
super(name,
super(configuration.name(),
configuration.getGroupVersionKind()
.map(gvk -> client.genericKubernetesResources(gvk.apiVersion(), gvk.getKind()))
.orElseGet(() -> (MixedOperation) client.resources(configuration.getResourceClass())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.processing.event.source.CacheKeyMapper;

public record PerResourcePollingConfiguration<R, P extends HasMetadata>(ScheduledExecutorService executorService, CacheKeyMapper<R> cacheKeyMapper,
public record PerResourcePollingConfiguration<R, P extends HasMetadata>(String name,ScheduledExecutorService executorService, CacheKeyMapper<R> cacheKeyMapper,
PerResourcePollingEventSource.ResourceFetcher<R, P> resourceFetcher,
Predicate<P> registerPredicate, Duration defaultPollingPeriod) {

public static final int DEFAULT_EXECUTOR_THREAD_NUMBER = 1;

public PerResourcePollingConfiguration(ScheduledExecutorService executorService,
public PerResourcePollingConfiguration(
String name,
ScheduledExecutorService executorService,
CacheKeyMapper<R> cacheKeyMapper,
PerResourcePollingEventSource.ResourceFetcher<R, P> resourceFetcher,
Predicate<P> registerPredicate,
Duration defaultPollingPeriod) {
this.name = name;
this.executorService = executorService == null ? new ScheduledThreadPoolExecutor(DEFAULT_EXECUTOR_THREAD_NUMBER)
: executorService;
this.cacheKeyMapper = cacheKeyMapper == null ? CacheKeyMapper.singleResourceCacheKeyMapper() : cacheKeyMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class PerResourcePollingConfigurationBuilder<R, P extends HasMetada
private final Duration defaultPollingPeriod;
private final PerResourcePollingEventSource.ResourceFetcher<R, P> resourceFetcher;

private String name;
private Predicate<P> registerPredicate;
private ScheduledExecutorService executorService;
private CacheKeyMapper<R> cacheKeyMapper;
Expand Down Expand Up @@ -42,8 +43,13 @@ public PerResourcePollingConfigurationBuilder<R, P> withCacheKeyMapper(
return this;
}

public PerResourcePollingConfigurationBuilder<R, P> withName(String name) {
this.name = name;
return this;
}

public PerResourcePollingConfiguration<R, P> build() {
return new PerResourcePollingConfiguration<>(executorService, cacheKeyMapper,
return new PerResourcePollingConfiguration<>(name, executorService, cacheKeyMapper,
resourceFetcher, registerPredicate, defaultPollingPeriod);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,12 @@ public class PerResourcePollingEventSource<R, P extends HasMetadata>
private final Predicate<P> registerPredicate;
private final Duration period;

public PerResourcePollingEventSource(Class<R> resourceClass, EventSourceContext<P> context,
PerResourcePollingConfiguration<R, P> config) {
this(null, resourceClass, context, config);
}

public PerResourcePollingEventSource(String name, Class<R> resourceClass,

public PerResourcePollingEventSource(Class<R> resourceClass,
EventSourceContext<P> context,
PerResourcePollingConfiguration<R, P> config) {
super(name, resourceClass, config.cacheKeyMapper());
super(config.name(), resourceClass, config.cacheKeyMapper());
this.primaryResourceCache = context.getPrimaryCache();
this.resourceFetcher = config.resourceFetcher();
this.registerPredicate = config.registerPredicate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import io.javaoperatorsdk.operator.processing.event.source.CacheKeyMapper;

public record PollingConfiguration<R>(PollingEventSource.GenericResourceFetcher<R> genericResourceFetcher,
public record PollingConfiguration<R>(String name,PollingEventSource.GenericResourceFetcher<R> genericResourceFetcher,
Duration period, CacheKeyMapper<R> cacheKeyMapper) {

public PollingConfiguration(PollingEventSource.GenericResourceFetcher<R> genericResourceFetcher, Duration period,
public PollingConfiguration(String name,PollingEventSource.GenericResourceFetcher<R> genericResourceFetcher, Duration period,
CacheKeyMapper<R> cacheKeyMapper) {
this.name = name;
this.genericResourceFetcher = Objects.requireNonNull(genericResourceFetcher);
this.period = period;
this.cacheKeyMapper =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public final class PollingConfigurationBuilder<R> {
private final Duration period;
private final PollingEventSource.GenericResourceFetcher<R> genericResourceFetcher;
private CacheKeyMapper<R> cacheKeyMapper;
private String name;

public PollingConfigurationBuilder(PollingEventSource.GenericResourceFetcher<R> fetcher,
Duration period) {
Expand All @@ -20,7 +21,12 @@ public PollingConfigurationBuilder<R> withCacheKeyMapper(CacheKeyMapper<R> cache
return this;
}

public PollingConfigurationBuilder<R> withName(String name) {
this.name = name;
return this;
}

public PollingConfiguration<R> build() {
return new PollingConfiguration<>(genericResourceFetcher, period, cacheKeyMapper);
return new PollingConfiguration<>(name, genericResourceFetcher, period, cacheKeyMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ public class PollingEventSource<R, P extends HasMetadata>
private final Duration period;
private final AtomicBoolean healthy = new AtomicBoolean(true);

public PollingEventSource(Class<R> resourceClass, PollingConfiguration<R> config) {
this(null, resourceClass, config);
}

public PollingEventSource(String name, Class<R> resourceClass, PollingConfiguration<R> config) {
super(name, resourceClass, config.cacheKeyMapper());

public PollingEventSource(Class<R> resourceClass, PollingConfiguration<R> config) {
super(config.name(), resourceClass, config.cacheKeyMapper());
this.genericResourceFetcher = config.genericResourceFetcher();
this.period = config.period();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PollingEventSourceTest
mock(PollingEventSource.GenericResourceFetcher.class);
private final PollingEventSource<SampleExternalResource, HasMetadata> pollingEventSource =
new PollingEventSource<>(SampleExternalResource.class,
new PollingConfiguration<>(resourceFetcher, POLL_PERIOD,
new PollingConfiguration<>(null, resourceFetcher, POLL_PERIOD,
(SampleExternalResource er) -> er.getName() + "#" + er.getValue()));

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ public UpdateControl<ComplexDependentCustomResource> reconcile(
public List<EventSource> prepareEventSources(
EventSourceContext<ComplexDependentCustomResource> context) {
InformerEventSource<Service, ComplexDependentCustomResource> serviceEventSource =
new InformerEventSource<>(SERVICE_EVENT_SOURCE_NAME,
InformerConfiguration.from(Service.class, ComplexDependentCustomResource.class).build(),
new InformerEventSource<>(
InformerConfiguration.from(Service.class, ComplexDependentCustomResource.class)
.withName(SERVICE_EVENT_SOURCE_NAME)
.build(),
context);
InformerEventSource<StatefulSet, ComplexDependentCustomResource> statefulSetEventSource =
new InformerEventSource<>(STATEFUL_SET_EVENT_SOURCE_NAME,
new InformerEventSource<>(
InformerConfiguration.from(StatefulSet.class, ComplexDependentCustomResource.class)
.withName(STATEFUL_SET_EVENT_SOURCE_NAME)
.build(),
context);
return List.of(serviceEventSource, statefulSetEventSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ private InformerEventSource<GenericKubernetesResource, DynamicGenericEventSource
Class<? extends HasMetadata> clazz,
Context<DynamicGenericEventSourceRegistrationCustomResource> context) {

return new InformerEventSource<>(clazz.getSimpleName(),
return new InformerEventSource<>(
InformerConfiguration
.from(gvkFor(clazz), DynamicGenericEventSourceRegistrationCustomResource.class).build(),
.from(gvkFor(clazz), DynamicGenericEventSourceRegistrationCustomResource.class)
.withName(clazz.getSimpleName())
.build(),
context.eventSourceRetriever().eventSourceContextForDynamicRegistration());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public int getNumberOfExecutions() {
public List<EventSource> prepareEventSources(
EventSourceContext<MultipleManagedDependentNoDiscriminatorCustomResource> context) {
InformerEventSource<ConfigMap, MultipleManagedDependentNoDiscriminatorCustomResource> ies =
new InformerEventSource<>(CONFIG_MAP_EVENT_SOURCE,
new InformerEventSource<>(
InformerConfiguration.from(ConfigMap.class,
MultipleManagedDependentNoDiscriminatorCustomResource.class)
.withName(CONFIG_MAP_EVENT_SOURCE)
.build(),
context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public int getNumberOfExecutions() {
public List<EventSource> prepareEventSources(
EventSourceContext<MultipleManagedDependentResourceCustomResource> context) {
InformerEventSource<ConfigMap, MultipleManagedDependentResourceCustomResource> ies =
new InformerEventSource<>(CONFIG_MAP_EVENT_SOURCE,
new InformerEventSource<>(
InformerConfiguration
.from(ConfigMap.class, MultipleManagedDependentResourceCustomResource.class)
.withName(CONFIG_MAP_EVENT_SOURCE)
.build(),
context);
return List.of(ies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ public List<EventSource> prepareEventSources(
};

PollingEventSource<ExternalResource, MultipleManagedExternalDependentResourceCustomResource> pollingEventSource =
new PollingEventSource<>(EVENT_SOURCE_NAME, ExternalResource.class,
new PollingEventSource<>(ExternalResource.class,
new PollingConfigurationBuilder<>(fetcher, Duration.ofMillis(1000L))
.withName(EVENT_SOURCE_NAME)
.withCacheKeyMapper(ExternalResource::getId)
.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public List<EventSource> prepareEventSources(
context.getPrimaryCache().addIndexer(CONFIG_MAP_INDEX, (primary -> List
.of(indexKey(primary.getSpec().getConfigMapName(), primary.getMetadata().getNamespace()))));

var es = new InformerEventSource<>(CONFIG_MAP_EVENT_SOURCE, InformerConfiguration
var es = new InformerEventSource<>(InformerConfiguration
.from(ConfigMap.class, PrimaryToSecondaryDependentCustomResource.class)
.withName(CONFIG_MAP_EVENT_SOURCE)
// if there is a many-to-many relationship (thus no direct owner reference)
// PrimaryToSecondaryMapper needs to be added
.withPrimaryToSecondaryMapper(
Expand Down

0 comments on commit ae405d6

Please sign in to comment.