Skip to content

Nicer config override #944

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 6 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 @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -14,6 +15,7 @@
import io.fabric8.kubernetes.client.Version;
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider;
import io.javaoperatorsdk.operator.api.config.ExecutorServiceManager;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.processing.Controller;
Expand Down Expand Up @@ -118,7 +120,7 @@ public <R extends HasMetadata> void register(Reconciler<R> reconciler)
*
* @param reconciler part of the reconciler to register
* @param configuration the configuration with which we want to register the reconciler
* @param <R> the {@code CustomResource} type associated with the reconciler
* @param <R> the {@code HasMetadata} type associated with the reconciler
* @throws OperatorException if a problem occurred during the registration process
*/
public <R extends HasMetadata> void register(Reconciler<R> reconciler,
Expand Down Expand Up @@ -147,6 +149,21 @@ public <R extends HasMetadata> void register(Reconciler<R> reconciler,
watchedNS);
}

/**
* Method to register operator and facilitate configuration override.
*
* @param reconciler part of the reconciler to register
* @param configOverrider consumer to use to change config values
* @param <R> the {@code HasMetadata} type associated with the reconciler
*/
public <R extends HasMetadata> void register(Reconciler<R> reconciler,
Consumer<ControllerConfigurationOverrider> configOverrider) {
final var controllerConfiguration = configurationService.getConfigurationFor(reconciler);
var configToOverride = ControllerConfigurationOverrider.override(controllerConfiguration);
configOverrider.accept(configToOverride);
register(reconciler, configToOverride.build());
}

static class ControllerManager implements LifecycleAware {
private final Map<String, Controller> controllers = new HashMap<>();
private boolean started = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,19 @@ 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) {
public void replaceDependentResourceConfig(
Class<? extends DependentResource<?, R, ?>> dependentResourceClass,
Object dependentResourceConfig) {

var currentConfig =
findConfigForDependentResourceClass(dependentResourceSpec.getDependentResourceClass());
findConfigForDependentResourceClass(dependentResourceClass);
if (currentConfig.isEmpty()) {
throw new IllegalStateException("Cannot find DependentResource config for class: "
+ dependentResourceSpec.getDependentResourceClass());
+ dependentResourceClass);
}
dependentResourceSpecs.remove(currentConfig.get());
dependentResourceSpecs.add(dependentResourceSpec);
dependentResourceSpecs
.add(new DependentResourceSpec(dependentResourceClass, dependentResourceConfig));
}

public void addNewDependentResourceConfig(DependentResourceSpec dependentResourceSpec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public List<EventSource> prepareEventSources(EventSourceContext<P> context) {
.map(
drc -> {
final var dependentResource =
from(drc, context.getClient());
createAndConfigureFrom(drc, context.getClient());
dependentResource
.eventSource(context)
.ifPresent(es -> sources.add((EventSource) es));
Expand Down Expand Up @@ -81,7 +81,7 @@ private void initContextIfNeeded(P resource, Context context) {
}
}

private DependentResource from(DependentResourceSpec dependentResourceSpec,
private DependentResource createAndConfigureFrom(DependentResourceSpec dependentResourceSpec,
KubernetesClient client) {
try {
DependentResource dependentResource =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void shouldRegisterReconcilerToController() {
@Test
@DisplayName("should throw `OperationException` when Configuration is null")
public void shouldThrowOperatorExceptionWhenConfigurationIsNull() {
Assertions.assertThrows(OperatorException.class, () -> operator.register(fooReconciler, null));
Assertions.assertThrows(OperatorException.class, () -> operator.register(fooReconciler));
}

private static class FooCustomResource extends CustomResource<FooSpec, FooStatus> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.Operator;
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceOverrider;
import io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider;
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
import io.javaoperatorsdk.operator.config.runtime.AnnotationControllerConfiguration;
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
import io.javaoperatorsdk.operator.monitoring.micrometer.MicrometerMetrics;
import io.micrometer.core.instrument.logging.LoggingMeterRegistry;
Expand All @@ -37,15 +34,11 @@ public static void main(String[] args) throws IOException {
.build());

MySQLSchemaReconciler schemaReconciler = new MySQLSchemaReconciler();
ControllerConfigurationOverrider<MySQLSchema> configOverrider =
ControllerConfigurationOverrider
.override(new AnnotationControllerConfiguration(schemaReconciler));

configOverrider.replaceDependentResourceConfig(
new DependentResourceSpec(SchemaDependentResource.class,
operator.register(schemaReconciler,
configOverrider -> configOverrider.replaceDependentResourceConfig(
SchemaDependentResource.class,
new ResourcePollerConfig(500, MySQLDbConfig.loadFromEnvironmentVars())));

operator.register(schemaReconciler, configOverrider.build());
operator.installShutdownHook();
operator.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SchemaDependentResource extends
AbstractDependentResource<Schema, MySQLSchema, ResourcePollerConfig> {

private MySQLDbConfig dbConfig;
private int pollPeriod;
private int pollPeriod = 500;

@Override
public void configureWith(ResourcePollerConfig config) {
Expand All @@ -29,6 +29,9 @@ public void configureWith(ResourcePollerConfig config) {

@Override
public Optional<EventSource> eventSource(EventSourceContext<MySQLSchema> context) {
if (dbConfig == null) {
dbConfig = MySQLDbConfig.loadFromEnvironmentVars();
}
return Optional.of(new PerResourcePollingEventSource<>(
new SchemaPollingResourceSupplier(dbConfig), context.getPrimaryCache(), pollPeriod,
Schema.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.LocalPortForward;
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
import io.javaoperatorsdk.operator.junit.AbstractOperatorExtension;
import io.javaoperatorsdk.operator.junit.E2EOperatorExtension;
Expand Down Expand Up @@ -67,10 +66,9 @@ boolean isLocal() {
new MySQLSchemaReconciler(),
c -> {
c.replaceDependentResourceConfig(
new DependentResourceSpec(
SchemaDependentResource.class,
new ResourcePollerConfig(
700, new MySQLDbConfig("127.0.0.1", "3306", "root", "password"))));
SchemaDependentResource.class,
new ResourcePollerConfig(
700, new MySQLDbConfig("127.0.0.1", "3306", "root", "password")));
})
.withInfrastructure(infrastructure)
.build()
Expand Down