Skip to content

Configuration polish #926

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 11 commits into from
Feb 16, 2022
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 @@ -7,7 +7,7 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.ReconcilerUtils;
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceConfiguration;
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
import io.javaoperatorsdk.operator.api.reconciler.Constants;
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEventFilter;
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEventFilters;
Expand Down Expand Up @@ -50,7 +50,7 @@ default ResourceEventFilter<R> getEventFilter() {
return ResourceEventFilters.passthrough();
}

default List<DependentResourceConfiguration> getDependentResources() {
default List<DependentResourceSpec> getDependentResources() {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEventFilter;

public class ControllerConfigurationOverrider<R extends HasMetadata> {
Expand All @@ -18,6 +21,7 @@ public class ControllerConfigurationOverrider<R extends HasMetadata> {
private ResourceEventFilter<R> customResourcePredicate;
private final ControllerConfiguration<R> original;
private Duration reconciliationMaxInterval;
private List<DependentResourceSpec> dependentResourceSpecs;

private ControllerConfigurationOverrider(ControllerConfiguration<R> original) {
finalizer = original.getFinalizer();
Expand All @@ -27,8 +31,8 @@ private ControllerConfigurationOverrider(ControllerConfiguration<R> original) {
labelSelector = original.getLabelSelector();
customResourcePredicate = original.getEventFilter();
reconciliationMaxInterval = original.reconciliationMaxInterval().orElse(null);
dependentResourceSpecs = original.getDependentResources();
this.original = original;

}

public ControllerConfigurationOverrider<R> withFinalizer(String finalizer) {
Expand Down Expand Up @@ -84,6 +88,41 @@ public ControllerConfigurationOverrider<R> withReconciliationMaxInterval(
return this;
}

/**
* If a {@link DependentResourceSpec} already exists with the same dependentResourceClass it will
* be replaced. Otherwise, an exception is thrown;
*
* @param dependentResourceSpec to add or replace
*/
public void replaceDependentResourceConfig(DependentResourceSpec dependentResourceSpec) {
var currentConfig =
findConfigForDependentResourceClass(dependentResourceSpec.getDependentResourceClass());
if (currentConfig.isEmpty()) {
throw new IllegalStateException("Cannot find DependentResource config for class: "
+ dependentResourceSpec.getDependentResourceClass());
}
dependentResourceSpecs.remove(currentConfig.get());
dependentResourceSpecs.add(dependentResourceSpec);
}

public void addNewDependentResourceConfig(DependentResourceSpec dependentResourceSpec) {
var currentConfig =
findConfigForDependentResourceClass(dependentResourceSpec.getDependentResourceClass());
if (currentConfig.isPresent()) {
throw new IllegalStateException(
"Config already present for class: "
+ dependentResourceSpec.getDependentResourceClass());
}
dependentResourceSpecs.add(dependentResourceSpec);
}

private Optional<DependentResourceSpec> findConfigForDependentResourceClass(
Class<? extends DependentResource> dependentResourceClass) {
return dependentResourceSpecs.stream()
.filter(dc -> dc.getDependentResourceClass().equals(dependentResourceClass))
.findFirst();
}

public ControllerConfiguration<R> build() {
return new DefaultControllerConfiguration<>(
original.getAssociatedReconcilerClassName(),
Expand All @@ -98,7 +137,7 @@ public ControllerConfiguration<R> build() {
original.getResourceClass(),
reconciliationMaxInterval,
original.getConfigurationService(),
original.getDependentResources());
dependentResourceSpecs);
}

public static <R extends HasMetadata> ControllerConfigurationOverrider<R> override(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Set;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceConfiguration;
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEventFilter;

public class DefaultControllerConfiguration<R extends HasMetadata>
Expand All @@ -21,7 +21,7 @@ public class DefaultControllerConfiguration<R extends HasMetadata>
private final boolean generationAware;
private final RetryConfiguration retryConfiguration;
private final ResourceEventFilter<R> resourceEventFilter;
private final List<DependentResourceConfiguration> dependents;
private final List<DependentResourceSpec> dependents;
private final Duration reconciliationMaxInterval;

// NOSONAR constructor is meant to provide all information
Expand All @@ -38,7 +38,7 @@ public DefaultControllerConfiguration(
Class<R> resourceClass,
Duration reconciliationMaxInterval,
ConfigurationService service,
List<DependentResourceConfiguration> dependents) {
List<DependentResourceSpec> dependents) {
super(labelSelector, resourceClass, namespaces);
this.associatedControllerClassName = associatedControllerClassName;
this.name = name;
Expand Down Expand Up @@ -102,7 +102,7 @@ public ResourceEventFilter<R> getEventFilter() {
}

@Override
public List<DependentResourceConfiguration> getDependentResources() {
public List<DependentResourceSpec> getDependentResources() {
return dependents;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.Properties;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -69,8 +71,17 @@ public static boolean debugThreadPool() {
return Boolean.getBoolean(System.getProperty(DEBUG_THREAD_POOL_ENV_KEY, "false"));
}

public static Class<?> getFirstTypeArgumentFromExtendedClass(Class<?> clazz) {
Type type = clazz.getGenericSuperclass();
return (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0];
}

public static Class<?> getFirstTypeArgumentFromInterface(Class<?> clazz) {
ParameterizedType type = (ParameterizedType) clazz.getGenericInterfaces()[0];
return (Class<?>) type.getActualTypeArguments()[0];
}

public static <C, T> T valueOrDefault(C annotation, Function<C, T> mapper, T defaultValue) {
return annotation == null ? defaultValue : mapper.apply(annotation);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.javaoperatorsdk.operator.api.config.dependent;

import java.util.Optional;

import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;

public class DependentResourceSpec<T extends DependentResource<?, ?, C>, C> {

private final Class<T> dependentResourceClass;

private final C dependentResourceConfig;

public DependentResourceSpec(Class<T> dependentResourceClass) {
this(dependentResourceClass, null);
}

public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig) {
this.dependentResourceClass = dependentResourceClass;
this.dependentResourceConfig = dependentResourceConfig;
}

public Class<T> getDependentResourceClass() {
return dependentResourceClass;
}

public Optional<C> getDependentResourceConfiguration() {
return Optional.ofNullable(dependentResourceConfig);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ class DefaultInformerConfiguration<R extends HasMetadata, P extends HasMetadata>

private final PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet;
private final AssociatedSecondaryResourceIdentifier<P> associatedWith;
private final boolean skipUpdateEventPropagationIfNoChange;

protected DefaultInformerConfiguration(ConfigurationService service, String labelSelector,
Class<R> resourceClass,
PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet,
AssociatedSecondaryResourceIdentifier<P> associatedWith,
boolean skipUpdateEventPropagationIfNoChange, Set<String> namespaces) {
Set<String> namespaces) {
super(labelSelector, resourceClass, namespaces);
setConfigurationService(service);
this.secondaryToPrimaryResourcesIdSet =
Objects.requireNonNullElse(secondaryToPrimaryResourcesIdSet,
Mappers.fromOwnerReference());
this.associatedWith =
Objects.requireNonNullElseGet(associatedWith, () -> ResourceID::fromResource);
this.skipUpdateEventPropagationIfNoChange = skipUpdateEventPropagationIfNoChange;
}

public PrimaryResourcesRetriever<R> getPrimaryResourcesRetriever() {
Expand All @@ -47,22 +45,16 @@ public AssociatedSecondaryResourceIdentifier<P> getAssociatedResourceIdentifier(
return associatedWith;
}

public boolean isSkipUpdateEventPropagationIfNoChange() {
return skipUpdateEventPropagationIfNoChange;
}
}

PrimaryResourcesRetriever<R> getPrimaryResourcesRetriever();

AssociatedSecondaryResourceIdentifier<P> getAssociatedResourceIdentifier();

boolean isSkipUpdateEventPropagationIfNoChange();

class InformerConfigurationBuilder<R extends HasMetadata, P extends HasMetadata> {

private PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet;
private AssociatedSecondaryResourceIdentifier<P> associatedWith;
private boolean skipUpdateEventPropagationIfNoChange = true;
private Set<String> namespaces;
private String labelSelector;
private final Class<R> resourceClass;
Expand All @@ -86,16 +78,6 @@ public InformerConfigurationBuilder<R, P> withAssociatedSecondaryResourceIdentif
return this;
}

public InformerConfigurationBuilder<R, P> withoutSkippingEventPropagationIfUnchanged() {
this.skipUpdateEventPropagationIfNoChange = false;
return this;
}

public InformerConfigurationBuilder<R, P> skippingEventPropagationIfUnchanged(
boolean skipIfUnchanged) {
this.skipUpdateEventPropagationIfNoChange = skipIfUnchanged;
return this;
}

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

public InformerConfiguration<R, P> build() {
return new DefaultInformerConfiguration<>(configurationService, labelSelector, resourceClass,
secondaryToPrimaryResourcesIdSet, associatedWith, skipUpdateEventPropagationIfNoChange,
secondaryToPrimaryResourcesIdSet, associatedWith,
namespaces);
}
}
Expand All @@ -136,8 +118,6 @@ static <R extends HasMetadata, P extends HasMetadata> InformerConfigurationBuild
configuration.getConfigurationService())
.withNamespaces(configuration.getNamespaces())
.withLabelSelector(configuration.getLabelSelector())
.skippingEventPropagationIfUnchanged(
configuration.isSkipUpdateEventPropagationIfNoChange())
.withAssociatedSecondaryResourceIdentifier(
configuration.getAssociatedResourceIdentifier())
.withPrimaryResourcesRetriever(configuration.getPrimaryResourcesRetriever());
Expand Down
Loading