Skip to content

refactor: clean up configurations #876

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 2 commits into from
Jan 28, 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 @@ -8,21 +8,17 @@
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEventFilter;

public class DefaultControllerConfiguration<R extends HasMetadata>
extends DefaultResourceConfiguration<R>
implements ControllerConfiguration<R> {

private final String associatedControllerClassName;
private final String name;
private final String crdName;
private final String finalizer;
private final boolean generationAware;
private final Set<String> namespaces;
private final boolean watchAllNamespaces;
private final RetryConfiguration retryConfiguration;
private final String labelSelector;
private final ResourceEventFilter<R> resourceEventFilter;
private final Class<R> resourceClass;
private final List<DependentResource> dependents;
private ConfigurationService service;

// NOSONAR constructor is meant to provide all information
public DefaultControllerConfiguration(
Expand All @@ -38,23 +34,18 @@ public DefaultControllerConfiguration(
Class<R> resourceClass,
ConfigurationService service,
List<DependentResource> dependents) {
super(labelSelector, resourceClass, namespaces);
this.associatedControllerClassName = associatedControllerClassName;
this.name = name;
this.crdName = crdName;
this.finalizer = finalizer;
this.generationAware = generationAware;
this.namespaces =
namespaces != null ? Collections.unmodifiableSet(namespaces) : Collections.emptySet();
this.watchAllNamespaces = this.namespaces.isEmpty();
this.retryConfiguration =
retryConfiguration == null
? ControllerConfiguration.super.getRetryConfiguration()
: retryConfiguration;
this.labelSelector = labelSelector;
this.resourceEventFilter = resourceEventFilter;
this.resourceClass =
resourceClass == null ? ControllerConfiguration.super.getResourceClass()
: resourceClass;

setConfigurationService(service);
this.dependents = dependents != null ? dependents : Collections.emptyList();
}
Expand Down Expand Up @@ -84,43 +75,19 @@ public String getAssociatedReconcilerClassName() {
return associatedControllerClassName;
}

@Override
public Set<String> getNamespaces() {
return namespaces;
}

@Override
public boolean watchAllNamespaces() {
return watchAllNamespaces;
}

@Override
public RetryConfiguration getRetryConfiguration() {
return retryConfiguration;
}

@Override
public ConfigurationService getConfigurationService() {
return service;
}

@Override
public void setConfigurationService(ConfigurationService service) {
if (this.service != null) {
if (getConfigurationService() != null) {
throw new IllegalStateException("A ConfigurationService is already associated with '" + name
+ "' ControllerConfiguration. Cannot change it once set!");
}
this.service = service;
}

@Override
public String getLabelSelector() {
return labelSelector;
}

@Override
public Class<R> getResourceClass() {
return resourceClass;
super.setConfigurationService(service);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public DefaultResourceConfiguration(String labelSelector, Class<R> resourceClass
public DefaultResourceConfiguration(String labelSelector, Class<R> resourceClass,
Set<String> namespaces) {
this.labelSelector = labelSelector;
this.resourceClass = resourceClass;
this.resourceClass = resourceClass == null ? ResourceConfiguration.super.getResourceClass()
: resourceClass;
this.namespaces = namespaces != null ? namespaces : Collections.emptySet();
this.watchAllNamespaces = this.namespaces.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.javaoperatorsdk.operator.api.reconciler.dependent;

import java.util.Set;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
import io.javaoperatorsdk.operator.processing.event.source.AssociatedSecondaryResourceIdentifier;
import io.javaoperatorsdk.operator.processing.event.source.PrimaryResourcesRetriever;
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerConfiguration;

public class KubernetesDependentResourceConfiguration<R extends HasMetadata, P extends HasMetadata>
extends InformerConfiguration<R, P> {

private final boolean owned;

protected KubernetesDependentResourceConfiguration(
ConfigurationService service,
String labelSelector, Class<R> resourceClass,
PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet,
AssociatedSecondaryResourceIdentifier<P> associatedWith,
boolean skipUpdateEventPropagationIfNoChange, Set<String> namespaces, boolean owned) {
super(service, labelSelector, resourceClass, secondaryToPrimaryResourcesIdSet, associatedWith,
skipUpdateEventPropagationIfNoChange, namespaces);
this.owned = owned;
}

public static <R extends HasMetadata, P extends HasMetadata> KubernetesDependentResourceConfiguration<R, P> from(
InformerConfiguration<R, P> cfg, boolean owned) {
return new KubernetesDependentResourceConfiguration<>(cfg.getConfigurationService(),
cfg.getLabelSelector(), cfg.getResourceClass(), cfg.getPrimaryResourcesRetriever(),
cfg.getAssociatedResourceIdentifier(), cfg.isSkipUpdateEventPropagationIfNoChange(),
cfg.getNamespaces(), owned);
}

public boolean isOwned() {
return owned;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@

public class KubernetesDependentResourceController<R extends HasMetadata, P extends HasMetadata>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, don't see why we need this controller per resource model. Why cannot just the dependent resource manager handle this? Like having it aggregate the controllers and the configuration. But this is probably something to discuss if we decide we got his way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it needs to be passed around at the moment. This change is about putting together the informer part and the ownership configuration into a single coherent configuration. Cleans up things by showing what belongs together. Helps things with the Quarkus extension as well.
We might indeed revisit, clean things further later but this change makes sense within the current scope.

extends DependentResourceController<R, P> {
private final InformerConfiguration<R, P> configuration;
private final boolean owned;
private final KubernetesDependentResourceConfiguration<R, P> configuration;
private KubernetesClient client;
private InformerEventSource<R, P> informer;


public KubernetesDependentResourceController(DependentResource<R, P> delegate,
InformerConfiguration<R, P> configuration, boolean owned) {
KubernetesDependentResourceConfiguration<R, P> configuration) {
super(delegate);
// todo: check if we can validate that types actually match properly
final var associatedPrimaries =
Expand All @@ -32,11 +31,12 @@ public KubernetesDependentResourceController(DependentResource<R, P> delegate,
? (AssociatedSecondaryResourceIdentifier<P>) delegate
: configuration.getAssociatedResourceIdentifier();

this.configuration = InformerConfiguration.from(configuration)
final var augmented = InformerConfiguration.from(configuration)
.withPrimaryResourcesRetriever(associatedPrimaries)
.withAssociatedSecondaryResourceIdentifier(associatedSecondary)
.build();
this.owned = owned;
this.configuration =
KubernetesDependentResourceConfiguration.from(augmented, configuration.isOwned());
}

@Override
Expand Down Expand Up @@ -69,6 +69,6 @@ public R getFor(P primary, Context context) {
}

public boolean owned() {
return owned;
return configuration.isOwned();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class InformerConfiguration<R extends HasMetadata, P extends HasMetadata>
private final AssociatedSecondaryResourceIdentifier<P> associatedWith;
private final boolean skipUpdateEventPropagationIfNoChange;

private InformerConfiguration(ConfigurationService service, String labelSelector,
protected InformerConfiguration(ConfigurationService service, String labelSelector,
Class<R> resourceClass,
PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet,
AssociatedSecondaryResourceIdentifier<P> associatedWith,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.javaoperatorsdk.operator.api.config.KubernetesDependent;
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.dependent.KubernetesDependentResourceConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.dependent.KubernetesDependentResourceController;
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEventFilter;
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEventFilters;
Expand Down Expand Up @@ -153,7 +154,8 @@ public List<DependentResource> getDependentResources() {
.skippingEventPropagationIfUnchanged(skipIfUnchanged)
.withNamespaces(namespaces)
.build();
dependent = new KubernetesDependentResourceController(dependent, configuration, owned);
dependent = new KubernetesDependentResourceController(dependent,
KubernetesDependentResourceConfiguration.from(configuration, owned));
}

dependents.add(dependent);
Expand Down