Skip to content

fix: clean up controller default name generation #282

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
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,8 +1,6 @@
package io.javaoperatorsdk.operator;

import io.fabric8.kubernetes.client.CustomResource;
import io.javaoperatorsdk.operator.api.ResourceController;
import java.util.Locale;

public class ControllerUtils {

Expand All @@ -16,20 +14,4 @@ public static boolean hasGivenFinalizer(CustomResource resource, String finalize
return resource.getMetadata().getFinalizers() != null
&& resource.getMetadata().getFinalizers().contains(finalizer);
}

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

public static String getDefaultNameFor(Class<? extends ResourceController> controllerClass) {
return getDefaultResourceControllerName(controllerClass.getSimpleName());
}

public static String getDefaultResourceControllerName(String rcControllerClassName) {
final var lastDot = rcControllerClassName.lastIndexOf('.');
if (lastDot > 0) {
rcControllerClassName = rcControllerClassName.substring(lastDot);
}
return rcControllerClassName.toLowerCase(Locale.ROOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@

public interface ResourceController<R extends CustomResource> {

static String getDefaultNameFor(ResourceController controller) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wonder why these methods are needed to be here. Let's keep the interfaces as clean and relevant as possible!
I still believe the getName also should be pulled out from this interface, I think it's been missed in the main PR wrt quarkus extension.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't need to put these methods there, it just that it made sense to me to move them here since they pertain to ResourceControllers instead of adding them to a util class that's less discoverable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@psycho-ir why do you think the getName method should be pulled? It's needed to be able to configure controllers individually.

Copy link
Contributor

@s-soroosh s-soroosh Jan 4, 2021

Choose a reason for hiding this comment

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

It let the user set the controller name in 2 different ways, @Controller.name & overriding the getName method and that's what we should strive to avoid.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah yes, didn't think of that but I assume that someone would need to know what they're doing if they override the default getName method… since we provide a default implementation. And if they provide their own implementation, then I guess that's the one that would be used and the annotation would be ignored anyway. I don't think that's something that would be an issue in practice.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, API design is mainly about the things we let users do and the reason behind them.
The getName method has been implemented for internal use and is effectively final (no reason to be overridden by the client).

Having that in the interface as the method will lead to other questions like why finalizerName, crdName, generationAwareEventProcessing and so on don't have getter method in interface accordingly if there is a getter method with default implementation why @controller annotation is getting the name field too? and several similar questions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The name is not internal: to be able to configure a controller, you need to be able to identify it. That's the name's purpose. While there are no obvious reasons that I can think of to override the default naming scheme, some people might still want to provide their own way to name a controller independently of what we provide (because for example, they're not using annotations and don't want to incur that impact at runtime). As such, it makes sense (at least to me) that it lives on the ResourceController interface.

The other methods, on the other hand, are part of the controller's configuration, which, again, you can't retrieve without first knowing the name of the controller. These values are not part of what is essential to the controller.

Copy link
Contributor

Choose a reason for hiding this comment

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

In comment, I was talking about the getName and not name, ofc name is not an internal thing, thus the user can set it via @Controller.name.

getName for sure can be added later on (it's backward compatible change) to the interface if it's been decided to get controller essential configuration from anywhere other than the controller annotation, AFAIK it's not currently the case and annotation + its crdName property is required.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

return getDefaultNameFor(controller.getClass());
}

static String getDefaultNameFor(Class<? extends ResourceController> controllerClass) {
return getDefaultResourceControllerName(controllerClass.getCanonicalName());
}

static String getDefaultResourceControllerName(String rcControllerClassName) {
if (rcControllerClassName.indexOf('.') < 0) {
throw new IllegalArgumentException(
"Must provide a fully-qualified resource controller class name, was: "
+ rcControllerClassName);
}
return rcControllerClassName.toLowerCase(Locale.ROOT);
}

/**
* The implementation should delete the associated component(s). Note that this is method is
* called when an object is marked for deletion. After its executed the custom resource finalizer
Expand Down Expand Up @@ -52,6 +69,6 @@ default String getName() {
}

// otherwise, use the lower-cased full class name
return clazz.getCanonicalName().toLowerCase(Locale.ROOT);
return getDefaultNameFor(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private ControllerConfiguration createControllerConfiguration(
controllerAnnotation,
"name",
AnnotationValue::asString,
() -> ControllerUtils.getDefaultResourceControllerName(info.simpleName())),
() -> ResourceController.getDefaultResourceControllerName(info.name().toString())),
crdName,
valueOrDefault(
controllerAnnotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.ControllerUtils;
import io.javaoperatorsdk.operator.Operator;
import io.javaoperatorsdk.operator.api.ResourceController;
import java.util.List;
Expand Down Expand Up @@ -41,7 +40,7 @@ public void loadsRetryPropertiesProperly() {
final var retryProperties =
config
.getControllers()
.get(ControllerUtils.getDefaultNameFor(TestController.class))
.get(ResourceController.getDefaultNameFor(TestController.class))
.getRetry();
assertEquals(3, retryProperties.getMaxAttempts());
assertEquals(1000, retryProperties.getInitialInterval());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public AnnotationConfiguration(ResourceController<R> controller) {
@Override
public String getName() {
final var name = annotation.name();
return Controller.NULL.equals(name) ? ControllerUtils.getDefaultNameFor(controller) : name;
return Controller.NULL.equals(name) ? ResourceController.getDefaultNameFor(controller) : name;
}

@Override
Expand Down