Skip to content

refactor: more controller -> reconciler renaming #750

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 1 commit into from
Dec 13, 2021
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 @@ -107,11 +107,11 @@ public void close() {
}

/**
* Add a registration requests for the specified controller with this operator. The effective
* registration of the controller is delayed till the operator is started.
* Add a registration requests for the specified reconciler with this operator. The effective
* registration of the reconciler is delayed till the operator is started.
*
* @param reconciler the controller to register
* @param <R> the {@code CustomResource} type associated with the controller
* @param reconciler the reconciler to register
* @param <R> the {@code CustomResource} 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 All @@ -121,15 +121,15 @@ public <R extends HasMetadata> void register(Reconciler<R> reconciler)
}

/**
* Add a registration requests for the specified controller with this operator, overriding its
* Add a registration requests for the specified reconciler with this operator, overriding its
* default configuration by the specified one (usually created via
* {@link io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider#override(ControllerConfiguration)},
* passing it the controller's original configuration. The effective registration of the
* controller is delayed till the operator is started.
* passing it the reconciler's original configuration. The effective registration of the
* reconciler is delayed till the operator is started.
*
* @param reconciler part of the controller to register
* @param configuration the configuration with which we want to register the controller
* @param <R> the {@code CustomResource} type associated with the controller
* @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
* @throws OperatorException if a problem occurred during the registration process
*/
public <R extends HasMetadata> void register(Reconciler<R> reconciler,
Expand All @@ -138,10 +138,10 @@ public <R extends HasMetadata> void register(Reconciler<R> reconciler,

if (configuration == null) {
throw new OperatorException(
"Cannot register controller with name " + reconciler.getClass().getCanonicalName() +
" controller named " + ControllerUtils.getNameFor(reconciler)
"Cannot register reconciler with name " + reconciler.getClass().getCanonicalName() +
" reconciler named " + ReconcilerUtils.getNameFor(reconciler)
+ " because its configuration cannot be found.\n" +
" Known controllers are: " + configurationService.getKnownControllerNames());
" Known reconcilers are: " + configurationService.getKnownReconcilerNames());
}

final var controller = new Controller<>(reconciler, configuration, kubernetesClient);
Expand All @@ -152,7 +152,7 @@ public <R extends HasMetadata> void register(Reconciler<R> reconciler,
: configuration.getEffectiveNamespaces();

log.info(
"Registered Controller: '{}' for CRD: '{}' for namespace(s): {}",
"Registered reconciler: '{}' for resource: '{}' for namespace(s): {}",
configuration.getName(),
configuration.getResourceClass(),
watchedNS);
Expand Down Expand Up @@ -191,14 +191,14 @@ public synchronized void stop() {

public synchronized void add(Controller controller) {
final var configuration = controller.getConfiguration();
final var crdName = configuration.getResourceTypeName();
final var existing = controllers.get(crdName);
final var resourceTypeName = configuration.getResourceTypeName();
final var existing = controllers.get(resourceTypeName);
if (existing != null) {
throw new OperatorException("Cannot register controller '" + configuration.getName()
+ "': another controller named '" + existing.getConfiguration().getName()
+ "' is already registered for CRD '" + crdName + "'");
+ "' is already registered for resource '" + resourceTypeName + "'");
}
this.controllers.put(crdName, controller);
this.controllers.put(resourceTypeName, controller);
if (started) {
controller.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;

@SuppressWarnings("rawtypes")
public class ControllerUtils {
public class ReconcilerUtils {

private static final String FINALIZER_NAME_SUFFIX = "/finalizer";

public static String getDefaultFinalizerName(String crdName) {
return crdName + FINALIZER_NAME_SUFFIX;
}

public static String getNameFor(Class<? extends Reconciler> controllerClass) {
// if the controller annotation has a name attribute, use it
final var annotation = controllerClass.getAnnotation(ControllerConfiguration.class);
public static String getNameFor(Class<? extends Reconciler> reconcilerClass) {
// if the reconciler annotation has a name attribute, use it
final var annotation = reconcilerClass.getAnnotation(ControllerConfiguration.class);
if (annotation != null) {
final var name = annotation.name();
if (!ControllerConfiguration.EMPTY_STRING.equals(name)) {
return name;
}
}
// otherwise, use the lower-cased full class name
return getDefaultNameFor(controllerClass);
return getDefaultNameFor(reconcilerClass);
}

public static String getNameFor(Reconciler controller) {
return getNameFor(controller.getClass());
public static String getNameFor(Reconciler reconciler) {
return getNameFor(reconciler.getClass());
}

public static String getDefaultNameFor(Reconciler controller) {
return getDefaultNameFor(controller.getClass());
public static String getDefaultNameFor(Reconciler reconciler) {
return getDefaultNameFor(reconciler.getClass());
}

public static String getDefaultNameFor(Class<? extends Reconciler> reconcilerClass) {
return getDefaultReconcilerName(reconcilerClass.getSimpleName());
}

public static String getDefaultReconcilerName(String rcControllerClassName) {
public static String getDefaultReconcilerName(String reconcilerClassName) {
// if the name is fully qualified, extract the simple class name
final var lastDot = rcControllerClassName.lastIndexOf('.');
final var lastDot = reconcilerClassName.lastIndexOf('.');
if (lastDot > 0) {
rcControllerClassName = rcControllerClassName.substring(lastDot + 1);
reconcilerClassName = reconcilerClassName.substring(lastDot + 1);
}
return rcControllerClassName.toLowerCase(Locale.ROOT);
return reconcilerClassName.toLowerCase(Locale.ROOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.stream.Stream;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.ControllerUtils;
import io.javaoperatorsdk.operator.ReconcilerUtils;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -40,53 +40,52 @@ private <R extends HasMetadata> void put(
}

protected <R extends HasMetadata> void throwExceptionOnNameCollision(
String newControllerClassName, ControllerConfiguration<R> existing) {
String newReconcilerClassName, ControllerConfiguration<R> existing) {
throw new IllegalArgumentException(
"Controller name '"
"Reconciler name '"
+ existing.getName()
+ "' is used by both "
+ existing.getAssociatedReconcilerClassName()
+ " and "
+ newControllerClassName);
+ newReconcilerClassName);
}

@Override
public <R extends HasMetadata> ControllerConfiguration<R> getConfigurationFor(
Reconciler<R> controller) {
final var key = keyFor(controller);
Reconciler<R> reconciler) {
final var key = keyFor(reconciler);
final var configuration = configurations.get(key);
if (configuration == null) {
logMissingControllerWarning(key, getControllersNameMessage());
logMissingReconcilerWarning(key, getReconcilersNameMessage());
}
return configuration;
}

protected void logMissingControllerWarning(String controllerKey,
String controllersNameMessage) {
protected void logMissingReconcilerWarning(String reconcilerKey, String reconcilersNameMessage) {
System.out
.println("Cannot find controller named '" + controllerKey + "'. " + controllersNameMessage);
.println("Cannot find reconciler named '" + reconcilerKey + "'. " + reconcilersNameMessage);
}

private String getControllersNameMessage() {
return "Known controllers: "
+ getKnownControllerNames().stream().reduce((s, s2) -> s + ", " + s2).orElse("None")
private String getReconcilersNameMessage() {
return "Known reconcilers: "
+ getKnownReconcilerNames().stream().reduce((s, s2) -> s + ", " + s2).orElse("None")
+ ".";
}

protected <R extends HasMetadata> String keyFor(Reconciler<R> controller) {
return ControllerUtils.getNameFor(controller);
protected <R extends HasMetadata> String keyFor(Reconciler<R> reconciler) {
return ReconcilerUtils.getNameFor(reconciler);
}

protected ControllerConfiguration getFor(String controllerName) {
return configurations.get(controllerName);
protected ControllerConfiguration getFor(String reconcilerName) {
return configurations.get(reconcilerName);
}

protected Stream<ControllerConfiguration> controllerConfigurations() {
return configurations.values().stream();
}

@Override
public Set<String> getKnownControllerNames() {
public Set<String> getKnownReconcilerNames() {
return configurations.keySet();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public BaseConfigurationService(Version version) {
}

@Override
protected void logMissingControllerWarning(String controllerKey, String controllersNameMessage) {
logger.warn("Configuration for controller '{}' was not found. {}", controllerKey,
controllersNameMessage);
protected void logMissingReconcilerWarning(String reconcilerKey, String reconcilersNameMessage) {
logger.warn("Configuration for reconciler '{}' was not found. {}", reconcilerKey,
reconcilersNameMessage);
}

public String getLoggerName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ public HasMetadata clone(HasMetadata object) {
};

/**
* Retrieves the configuration associated with the specified controller
* Retrieves the configuration associated with the specified reconciler
*
* @param controller the controller we want the configuration of
* @param <R> the {@code CustomResource} type associated with the specified controller
* @return the {@link ControllerConfiguration} associated with the specified controller or {@code
* null} if no configuration exists for the controller
* @param reconciler the reconciler we want the configuration of
* @param <R> the {@code CustomResource} type associated with the specified reconciler
* @return the {@link ControllerConfiguration} associated with the specified reconciler or {@code
* null} if no configuration exists for the reconciler
*/
<R extends HasMetadata> ControllerConfiguration<R> getConfigurationFor(
Reconciler<R> controller);
<R extends HasMetadata> ControllerConfiguration<R> getConfigurationFor(Reconciler<R> reconciler);

/**
* Retrieves the Kubernetes client configuration
Expand All @@ -51,11 +50,11 @@ default Config getClientConfiguration() {
}

/**
* Retrieves the set of the names of controllers for which a configuration exists
* Retrieves the set of the names of reconcilers for which a configuration exists
*
* @return the set of known controller names
* @return the set of known reconciler names
*/
Set<String> getKnownControllerNames();
Set<String> getKnownReconcilerNames();

/**
* Retrieves the {@link Version} information associated with this particular instance of the SDK
Expand All @@ -67,7 +66,7 @@ default Config getClientConfiguration() {
/**
* Whether the operator should query the CRD to make sure it's deployed and validate
* {@link CustomResource} implementations before attempting to register the associated
* controllers.
* reconcilers.
*
* <p>
* Note that this might require elevating the privileges associated with the operator to gain read
Expand All @@ -83,7 +82,7 @@ default boolean checkCRDAndValidateLocalModel() {

/**
* Retrieves the maximum number of threads the operator can spin out to dispatch reconciliation
* requests to controllers
* requests to reconcilers
*
* @return the maximum number of concurrent reconciliation threads
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public ConfigurationService build() {
return new ConfigurationService() {
@Override
public <R extends HasMetadata> ControllerConfiguration<R> getConfigurationFor(
Reconciler<R> controller) {
return original.getConfigurationFor(controller);
Reconciler<R> reconciler) {
return original.getConfigurationFor(reconciler);
}

@Override
public Set<String> getKnownControllerNames() {
return original.getKnownControllerNames();
public Set<String> getKnownReconcilerNames() {
return original.getKnownReconcilerNames();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.CustomResource;
import io.javaoperatorsdk.operator.ControllerUtils;
import io.javaoperatorsdk.operator.ReconcilerUtils;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventFilter;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventFilters;

public interface ControllerConfiguration<R extends HasMetadata> {

default String getName() {
return ControllerUtils.getDefaultReconcilerName(getAssociatedReconcilerClassName());
return ReconcilerUtils.getDefaultReconcilerName(getAssociatedReconcilerClassName());
}

default String getResourceTypeName() {
return CustomResource.getCRDName(getResourceClass());
}

default String getFinalizer() {
return ControllerUtils.getDefaultFinalizerName(getResourceTypeName());
return ReconcilerUtils.getDefaultFinalizerName(getResourceTypeName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ public interface Metrics {

default void receivedEvent(Event event) {}

default void reconcileCustomResource(ResourceID resourceID,
RetryInfo retryInfo) {}
default void reconcileCustomResource(ResourceID resourceID, RetryInfo retryInfo) {}

default void failedReconciliation(ResourceID resourceID,
RuntimeException exception) {}
default void failedReconciliation(ResourceID resourceID, RuntimeException exception) {}

default void cleanupDoneFor(ResourceID customResourceUid) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

class ControllerUtilsTest {
class ReconcilerUtilsTest {

@Test
void getDefaultResourceControllerName() {
assertEquals(
"testcustomreconciler",
ControllerUtils.getDefaultReconcilerName(
ReconcilerUtils.getDefaultReconcilerName(
TestCustomReconciler.class.getCanonicalName()));
}
}
Loading